slotarg.h

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 
00011 #ifndef SLOTARG_H
00012 #define SLOTARG_H
00013 
00014 #include <sigc++/functors/slot.h>
00015 
00016 namespace cwidget
00017 {
00018   namespace util
00019   {
00020     template<typename T>
00021     class slotarg
00022     {
00023       bool hasslot;
00024       T theslot;
00025     public:
00026       slotarg(const T *slot)
00027       {
00028         if(slot)
00029           {
00030             theslot=*slot;
00031             hasslot=true;
00032           }
00033         else
00034           hasslot=false;
00035       }
00036       slotarg(const T &slot)
00037         :hasslot(true), theslot(slot)
00038       {
00039       }
00040 
00041       template <typename S>
00042       operator slotarg<S>() const
00043       {
00044         if(hasslot)
00045           return slotarg<S>(theslot);
00046         else
00047           return slotarg<S>(NULL);
00048       }
00049 
00050       operator bool() const {return hasslot;}
00051       const T & operator*() const {return theslot;}
00052       T & operator*() {return theslot;}
00053     };
00054 
00055     typedef slotarg<sigc::slot0<void> > slot0arg;
00056 
00057     template<typename T>
00058     slotarg<T> arg(const T &slot)
00059     {
00060       return slotarg<T>(slot);
00061     }
00062   }
00063 }
00064 
00065 #endif

Generated on Mon Feb 16 01:16:31 2009 for cwidget by  doxygen 1.4.6