library of assembled shared sources

http://lass.cocamware.com

callback_r_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 
00044 
00045 /** @class lass::util::CallbackR0
00046  *  @ingroup Callback
00047  *  @brief callback with 0 parameters but with returnvalue.
00048  *  @date 2002
00049  *  @author Bram de Greve [Bramz] (contact: bramz@users.sourceforge.net)
00050  *
00051  *  It's a single object that can hold a reference to a free function or an
00052  *  object/(const) method pair.  Once the callback is constructed, it works
00053  *  completely transparent.  All it shows to the client is a function that
00054  *  takes 0 parameter(s) and gives a returnvalue.
00055  */
00056 
00057 
00058 
00059 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_R_0_H
00060 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_R_0_H
00061 
00062 
00063 
00064 // --- OLD INTERFACES ----------------------------------------------------------
00065 
00066 #include "util_common.h"
00067 #include "shared_ptr.h"
00068 #include "callback_common.h"
00069 #include "impl/dispatcher_r_0.h"
00070 
00071 
00072 
00073 
00074 // --- NEW INTERFACES ----------------------------------------------------------
00075 
00076 namespace lass
00077 {
00078 namespace util
00079 {
00080 
00081 
00082 // THE CALLBACK:
00083 // This is the actuall class you use to hold callbacks:
00084 
00085 template
00086 <
00087     typename R
00088 >
00089 class CallbackR0
00090 {
00091 public:
00092 
00093     typedef CallbackR0<R> TSelf;
00094     typedef SharedPtr< impl::DispatcherR0<R> > TDispatcherPtr;
00095 
00096     // STRUCTORS
00097 
00098     /** Default constructor, construct empty callback.
00099      */
00100     CallbackR0()
00101     {
00102     }
00103 
00104     /** Construct function callback
00105      */
00106     template <typename Function>
00107     CallbackR0(Function iFunction):
00108         dispatcher_(make(iFunction, meta::Wrap<typename meta::IsDerived<Function, impl::DispatcherR0<R> >::Type>()))
00109     {
00110     }
00111 
00112     /** Construct object/method callback.
00113      */
00114     template <typename ObjectPtr, typename Method>
00115     CallbackR0(ObjectPtr iObject, Method iMethod):
00116         dispatcher_(new impl::DispatcherR0Method<R, ObjectPtr, Method>(iObject, iMethod))
00117     {
00118     }
00119 
00120     /** copy constructor
00121      */
00122     CallbackR0(const TSelf& iOther):
00123         dispatcher_(iOther.dispatcher_)
00124     {
00125     }
00126 
00127 
00128 
00129     // OPERATORS
00130 
00131     /** assignment operator
00132      */
00133     template <typename Other>
00134     TSelf& operator=(const Other& iOther)
00135     {
00136         TSelf temp(iOther);
00137         swap(temp);
00138         return *this;
00139     }
00140 
00141     /** THE operator.  Executes the callback.
00142      */
00143     R operator()() const
00144     {
00145         if (isEmpty())
00146         {
00147             LASS_THROW_EX(EmptyCallback, "You've tried to call an empty CallbackR0.  Can't return a value.");
00148         }
00149         return dispatcher_->call();
00150     }
00151 
00152 
00153     // METHODS
00154 
00155     /** Reset to empty callback.
00156      */
00157     void reset()
00158     {
00159         dispatcher_.reset();
00160     }
00161 
00162     /** Returns true if no callback dispatcher is assigned to this object.
00163      */
00164     bool isEmpty() const
00165     {
00166         return dispatcher_.isEmpty();
00167     }
00168 
00169     /** return this->isEmpty()
00170      */
00171     bool operator!() const
00172     {
00173         return dispatcher_.isEmpty(); 
00174     }
00175 
00176     /** return !this->isEmpty())
00177      */
00178     operator num::SafeBool() const
00179     {
00180         return dispatcher_.isEmpty() ? num::safeFalse : num::safeTrue;
00181     }
00182 
00183     /** Swaps the dispatcher of this callback with the dispatcher of another.
00184      */
00185     void swap(TSelf& iOther)
00186     {
00187         dispatcher_.swap(iOther.dispatcher_);
00188     }
00189 
00190 private:
00191 
00192     template <typename Function>
00193     static TDispatcherPtr make(Function iFunction, meta::Wrap<meta::False>)
00194     {
00195         return TDispatcherPtr(new impl::DispatcherR0Function<R, Function>(iFunction));
00196     }
00197 
00198     template <typename Dispatcher>
00199     static TDispatcherPtr make(Dispatcher iDispatcher, meta::Wrap<meta::True>)
00200     {
00201         return TDispatcherPtr(new Dispatcher(iDispatcher));
00202     }
00203 
00204     TDispatcherPtr dispatcher_;
00205 };
00206 
00207 
00208 
00209 /** return true if two callbacks are different
00210  *  @relates CallbackR0
00211  */
00212 template <typename R>
00213 bool operator!=(const CallbackR0<R>& iA, const CallbackR0<R>& iB)
00214 {
00215     return !(iA == iB);
00216 }
00217 
00218 
00219 
00220 /** make a CallbackR0 from a function
00221  *  @relates CallbackR0
00222  */
00223 template <typename R> inline
00224 CallbackR0<R> makeCallback(R (*iFunction)())
00225 {
00226     return Callback0(iFunction);
00227 }
00228 
00229 
00230 
00231 /** make a CallbackR0 from a callback
00232  *  @relates CallbackR0
00233  */
00234 template <typename R> inline
00235 const CallbackR0<R>& makeCallback(const CallbackR0<R>& iCallback)
00236 {
00237     return iCallback;
00238 }
00239 
00240 
00241 
00242 /** make a CallbackR0 from a object and method
00243  *  @relates CallbackR0
00244  */
00245 template <typename ObjectPtr, typename Object, typename R> inline
00246 CallbackR0<R> makeCallback(ObjectPtr iObject, R (Object::*iMethod)())
00247 {
00248     return Callback0(iObject, iMethod);
00249 }
00250 
00251 
00252 
00253 /** make a CallbackR0 from a object and const method
00254  *  @relates CallbackR0
00255  */
00256 template <typename ObjectPtr, typename Object, typename R> inline
00257 CallbackR0<R> makeCallback(ObjectPtr iObject, R (Object::*iMethod)() const)
00258 {
00259     return Callback0(iObject, iMethod);
00260 }
00261 
00262 
00263 
00264 }
00265 
00266 }
00267 
00268 #define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_CALLBACK_R0
00269 #ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_PYTHON_H
00270 #   include "callback_python.h"
00271 #endif
00272 
00273 #endif // Guardian of Inclusion
00274 
00275 // 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