cwidget 0.5.16

slotarg.h

Go to the documentation of this file.
00001 // slotarg.h                    -*-c++-*-
00002 //
00003 //  Copyright 2000 Daniel Burrows
00004 //
00005 //  Provides a mechanism for nicely passing in optional slots to a function.
00006 // (you can pass either a reference to one or a pointer (which can be NULL))
00007 //
00008 //  Eg: some_slot_function(slotarg, slotarg, slotarg) can be called as:
00009 // some_slot_function(arg(slota), NULL, arg(slotb)) to omit the second slot.
00010 
00017 #ifndef SLOTARG_H
00018 #define SLOTARG_H
00019 
00020 #include <sigc++/functors/slot.h>
00021 
00022 namespace cwidget
00023 {
00027   namespace util
00028   {
00035     template<typename T>
00036     class slotarg
00037     {
00038       bool hasslot;
00039       T theslot;
00040     public:
00045       slotarg(const T *slot)
00046       {
00047         if(slot)
00048           {
00049             theslot=*slot;
00050             hasslot=true;
00051           }
00052         else
00053           hasslot=false;
00054       }
00055 
00057       slotarg(const T &slot)
00058         :hasslot(true), theslot(slot)
00059       {
00060       }
00061 
00063       template <typename S>
00064       operator slotarg<S>() const
00065       {
00066         if(hasslot)
00067           return slotarg<S>(theslot);
00068         else
00069           return slotarg<S>(NULL);
00070       }
00071 
00073       operator bool() const {return hasslot;}
00075       const T & operator*() const {return theslot;}
00077       T & operator*() {return theslot;}
00078     };
00079 
00083     typedef slotarg<sigc::slot0<void> > slot0arg;
00084 
00097     template<typename T>
00098     slotarg<T> arg(const T &slot)
00099     {
00100       return slotarg<T>(slot);
00101     }
00102   }
00103 }
00104 
00105 #endif