Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Functor.h

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020     
00021     $Id:  $
00022 */
00023 
00024 
00025 #ifndef TEDDY__SIGNALS__FUNCTOR__H
00026 #define TEDDY__SIGNALS__FUNCTOR__H
00027 
00028 
00029 #include "Teddy/SysSupport/StdString.h"
00030 
00031 namespace Teddy   {
00032 namespace Signals {
00033 
00034 
00036 template <typename R                        > class Functor0 {public:virtual R    operator()() = 0;};
00037 template <typename R,typename A1            > class Functor1 {public:virtual R    operator()( A1 a1 ) = 0;};
00038 template <typename R,typename A1,typename A2> class Functor2 {public:virtual R    operator()( A1 a1, A2 a2 ) = 0;};
00039 #if 0
00040                                               class FunctorV0{public:virtual void operator()() = 0;};
00041 template <typename A1                       > class FunctorV1{public:virtual void operator()( A1 a1 ) = 0;};
00042 template <typename A1,typename A2           > class FunctorV2{public:virtual void operator()( A1 a1, A2 a2 ) = 0;};
00043 #endif
00044 
00045 
00047 template <typename R> class Holder0 {
00048 public:
00049     Holder0(){ functor = NULL; }
00050     ~Holder0(){ if( functor != NULL ){ delete functor; } }
00051     R operator()(){ return (*functor)(); }
00052     Functor0<R> *functor;
00053 };
00054 template <typename R,typename A1> class Holder1 {
00055 public:
00056     Holder1(){ functor = NULL; }
00057     ~Holder1(){ if( functor != NULL ){ delete functor; } }
00058     R operator()( A1 a1 ){ return (*functor)( a1 ); }
00059     Functor1<R,A1> *functor;
00060 };
00061 template <typename R,typename A1,typename A2> class Holder2 {
00062 public:
00063     Holder2(){ functor = NULL; }
00064     ~Holder2(){ if( functor != NULL ){ delete functor; } }
00065     R operator()( A1 a1, A2 a2 ){ return (*functor)( a1, a2 ); }
00066     Functor2<R,A1,A2> *functor;
00067 };
00068 #if 0
00069 class HolderV0 {
00070 public:
00071     HolderV0(){ functor = NULL; }
00072     ~HolderV0(){ if( functor != NULL ){ delete functor; } }
00073     void operator()(){ (*functor)(); }
00074     FunctorV0 *functor;
00075 };
00076 template <typename A1> class HolderV1 {
00077 public:
00078     HolderV1(){ functor = NULL; }
00079     ~HolderV1(){ if( functor != NULL ){ delete functor; } }
00080     void operator()( A1 a1 ){ (*functor)( a1 ); }
00081     FunctorV1<A1> *functor;
00082 };
00083 template <typename A1,typename A2> class HolderV2 {
00084 public:
00085     HolderV2(){ functor = NULL; }
00086     ~HolderV2(){ if( functor != NULL ){ delete functor; } }
00087     void operator()( A1 a1, A2 a2 ){ (*functor)( a1, a2 ); }
00088     FunctorV2<A1,A2> *functor;
00089 };
00090 #endif
00091 
00092 
00093 
00095 template <typename T,typename R> class TFunctor0 : public Functor0<R> {
00096 public:
00097     TFunctor0( T *object, R(T::*operation)()):object(object),operation(operation){}
00098     virtual R operator()(){ return (*object.*operation)(); }
00099 protected:
00100     T *object;
00101     R (T::*operation)();
00102 };
00103 template <typename T,typename R,typename A1> class TFunctor1 : public Functor1<R,A1> {
00104 public:
00105     TFunctor1( T *object, R(T::*operation)( A1 )):object(object),operation(operation){}
00106     virtual R operator()( A1 a1 ){ return (*object.*operation)( a1 ); }
00107 protected:
00108     T *object;
00109     R (T::*operation)( A1 );
00110 };
00111 template <typename T,typename R,typename A1,typename A2> class TFunctor2 : public Functor2<R,A1,A2> {
00112 public:
00113     TFunctor2( T *object, R(T::*operation)( A1, A2 )):object(object),operation(operation){}
00114     virtual R operator()( A1 a1, A2 a2 ){ return (*object.*operation)( a1, a2 ); }
00115 protected:
00116     T *object;
00117     R (T::*operation)( A1, A2 );
00118 };
00119 #if 0
00120 template <typename T> class TFunctorV0 : public FunctorV0 {
00121 public:
00122     TFunctorV0( T *object, void(T::*operation)()):object(object),operation(operation){}
00123     virtual void operator()(){ (*object.*operation)(); }
00124 protected:
00125     T    *object;
00126     void (T::*operation)();
00127 };
00128 template <typename T,typename A1> class TFunctorV1 : public FunctorV1<A1> {
00129 public:
00130     TFunctorV1( T *object, void(T::*operation)( A1 )):object(object),operation(operation){}
00131     virtual void operator()( A1 a1 ){ (*object.*operation)( a1 ); }
00132 protected:
00133     T    *object;
00134     void (T::*operation)( A1 );
00135 };
00136 template <typename T,typename A1,typename A2> class TFunctorV2 : public FunctorV2<A1,A2> {
00137 public:
00138     TFunctorV2( T *object, void(T::*operation)( A1, A2 )):object(object),operation(operation){}
00139     virtual void operator()( A1 a1, A2 a2 ){ (*object.*operation)( a1, a2 ); }
00140 protected:
00141     T    *object;
00142     void (T::*operation)( A1, A2 );
00143 };
00144 #endif
00145 
00146 
00147 //  Anonymous constructors
00148 
00149 template <typename T,typename R> Functor0<R> *functor( T *object, R(T::*operation)()){
00150     return new TFunctor0<T,R>( object, operation );
00151 }
00152 template <typename T,typename R,typename A1> Functor1<R,A1> *functor( T *object, R(T::*operation)( A1 )){
00153     return new TFunctor1<T,R,A1>( object, operation );
00154 }
00155 template <typename T,typename R,typename A1,typename A2> Functor2<R,A1,A2> *functor( T *object, R(T::*operation)( A1, A2 )){
00156     return new TFunctor2<T,R,A1,A2>( object, operation );
00157 }
00158 #if 0
00159 template <typename T> FunctorV0 *functor( T *object, void(T::*operation)()){
00160     return new TFunctorV0<T>( object, operation );
00161 }
00162 template <typename T,typename A1> FunctorV1<A1> *functor( T *object, void(T::*operation)( A1 )){
00163     return new TFunctorV1<T,A1>( object, operation );
00164 }
00165 template <typename T,typename A1,typename A2> FunctorV2<A1,A2> *functor( T *object, void(T::*operation)( A1, A2 )){
00166     return new TFunctorV2<T,A1,A2>( object, operation );
00167 }
00168 #endif
00169 
00170 };  //  namespace Signals
00171 };  //  namespace Teddy
00172 
00173 
00174 #endif  //  TEDDY__SIGNALS__FUNCTOR__H
00175