library of assembled shared sources

http://lass.cocamware.com

callback_0.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 /** @class lass::util::Callback0
00044  *  @ingroup Callback
00045  *  @brief callback with 0 parameter(s) and without returnvalue.
00046  *  @date 2002
00047  *  @author Bram de Greve [Bramz] (contact: bramz@users.sourceforge.net)
00048  *
00049  *  It's a single object that can hold a reference to a free function or an
00050  *  object/(const) method pair.  Once the callback is constructed, it works
00051  *  completely transparent.  All it shows to the client is a function that
00052  *  takes one parameter but gives no returnvalue.
00053  */
00054 
00055 
00056 
00057 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_0_H
00058 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_0_H
00059 
00060 
00061 
00062 // --- OLD INTERFACES ----------------------------------------------------------
00063 
00064 #include "util_common.h"
00065 #include "callback_common.h"
00066 #include "shared_ptr.h"
00067 #include "impl/dispatcher_0.h"
00068 #include "../meta/is_derived.h"
00069 #include "../meta/wrap.h"
00070 
00071 
00072 
00073 // --- NEW INTERFACES ----------------------------------------------------------
00074 
00075 namespace lass
00076 {
00077 namespace util
00078 {
00079 
00080 // THE CALLBACK:
00081 // This is the actuall class you use to hold callbacks:
00082 
00083 class Callback0
00084 {
00085 public:
00086 
00087     typedef Callback0 TSelf;
00088     typedef SharedPtr<impl::Dispatcher0> TDispatcherPtr;
00089 
00090     // STRUCTORS
00091 
00092     /** Default constructor, construct empty callback.
00093      */
00094     Callback0()
00095     {
00096     }
00097 
00098     /** callback to function or other callable entity.
00099      */
00100     template <typename Function>
00101     Callback0(Function iFunction):
00102         dispatcher_(make(iFunction, meta::Wrap<typename meta::IsDerived<Function, impl::Dispatcher0>::Type>()))
00103     {
00104     }
00105 
00106     /** callback to method of object
00107      */
00108     template <typename ObjectPtr, typename Method>
00109     Callback0(ObjectPtr iObject, Method iMethod):
00110         dispatcher_(new impl::Dispatcher0Method<ObjectPtr, Method>(iObject, iMethod))
00111     {
00112     }
00113 
00114     /** copy constructor
00115      */
00116     Callback0(const Callback0& iOther):
00117         dispatcher_(iOther.dispatcher_)
00118     {
00119     }
00120 
00121 
00122 
00123     // OPERATORS
00124 
00125     /** assignment operator (also usable for conversion)
00126      */
00127     template <typename Other>
00128     TSelf& operator=(const Other& iOther)
00129     {
00130         TSelf temp(iOther);
00131         swap(temp);
00132         return *this;
00133     }
00134 
00135 
00136 
00137     /** THE operator.  Executes the callback.
00138      */
00139     void operator()() const
00140     {
00141         if (!isEmpty())
00142         {
00143             dispatcher_->call();
00144         }
00145     }
00146 
00147 
00148     // METHODS
00149 
00150     /** Reset to empty callback.
00151      */
00152     void reset()
00153     {
00154         dispatcher_.reset();
00155     }
00156 
00157     /** Returns true if no callback dispatcher is assigned to this object.
00158      */
00159     bool isEmpty() const
00160     {
00161         return dispatcher_.isEmpty();
00162     }
00163 
00164     /** return this->isEmpty()
00165      */
00166     bool operator!() const
00167     {
00168         return dispatcher_.isEmpty(); 
00169     }
00170 
00171     /** return !this->isEmpty())
00172      */
00173     operator num::SafeBool() const
00174     {
00175         return dispatcher_.isEmpty() ? num::safeFalse : num::safeTrue;
00176     }
00177 
00178     /** Swaps the dispatcher of this callback with the dispatcher of another.
00179      */
00180     void swap(TSelf& iOther)
00181     {
00182         dispatcher_.swap(iOther.dispatcher_);
00183     }
00184 
00185     /** return true if two callbacks call the same function/method,
00186      *  NEEDS RTTI!
00187      */
00188     bool operator==(const TSelf& iOther) const
00189     {
00190         return dispatcher_->isEquivalent(iOther.dispatcher_.get());
00191     }
00192 
00193 private:
00194 
00195     template <typename Function>
00196     static TDispatcherPtr make(Function iFunction, meta::Wrap<meta::False>)
00197     {
00198         return TDispatcherPtr(new impl::Dispatcher0Function<Function>(iFunction));
00199     }
00200 
00201     template <typename Dispatcher>
00202     static TDispatcherPtr make(Dispatcher iDispatcher, meta::Wrap<meta::True>)
00203     {
00204         return TDispatcherPtr(new Dispatcher(iDispatcher));
00205     }
00206 
00207     TDispatcherPtr dispatcher_;
00208 };
00209 
00210 
00211 
00212 /** return true if two callbacks are different
00213  *  @relates Callback0
00214  */
00215 inline bool operator!=(const Callback0& iA, const Callback0& iB)
00216 {
00217     return !(iA == iB);
00218 }
00219 
00220 
00221 
00222 /** make a Callback0 from a function
00223  *  @relates Callback0
00224  */
00225 inline Callback0 makeCallback(void (*iFunction)())
00226 {
00227     return Callback0(iFunction);
00228 }
00229 
00230 
00231 
00232 /** convencie function, make callback from callback
00233  */
00234 inline const Callback0& makeCallback(const Callback0& iCallback)
00235 {
00236     return iCallback;
00237 }
00238 
00239 
00240 /** make a Callback0 from a object and method
00241  *  @relates Callback0
00242  */
00243 template <typename ObjectPtr, typename Object> inline
00244 Callback0 makeCallback(ObjectPtr iObject, void (Object::*iMethod)())
00245 {
00246     return Callback0(iObject, iMethod);
00247 }
00248 
00249 
00250 
00251 /** make a Callback0 from a object and const method
00252  *  @relates Callback0
00253  */
00254 template <typename ObjectPtr, typename Object> inline
00255 Callback0 makeCallback(ObjectPtr iObject, void (Object::*iMethod)() const)
00256 {
00257     return Callback0(iObject, iMethod);
00258 }
00259 
00260 
00261 
00262 }
00263 
00264 }
00265 
00266 #define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_CALLBACK_0
00267 #ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_PYTHON_H
00268 #   include "callback_python.h"
00269 #endif
00270 
00271 #endif
00272 
00273 // EOF

Generated on Mon Nov 10 14:20:00 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo