00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __UT_Functor_C__
00023 #define __UT_Functor_C__
00024
00025 #include "UT_Functor.h"
00026
00027
00028
00029
00030 template <>
00031 inline void
00032 UT_Functor<void>::operator()() const
00033 {
00034 if (myFunctorImpl.myFunctorImpl)
00035 (*myFunctorImpl.myFunctorImpl)();
00036 }
00037
00038
00039
00040
00041
00042
00043 template <typename PointerToObj, typename PointerToMemFn>
00044 class UT_MemFunHandler<UT_Functor<void>, PointerToObj, PointerToMemFn>
00045 : public UT_FunctorImpl<void>
00046 {
00047 public:
00048 typedef void ReturnType;
00049
00050 UT_MemFunHandler(const PointerToObj &pointer_to_obj,
00051 const PointerToMemFn pointer_to_mem_fn)
00052 : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
00053 {}
00054
00055 virtual UT_FunctorImpl<void> *clone() const
00056 { return new UT_MemFunHandler<UT_Functor<void>, PointerToObj,
00057 PointerToMemFn>(*this); }
00058
00059
00060 virtual void operator()() const
00061 { ((*myPointerToObj).*myPointerToMemFn)(); }
00062
00063 private:
00064 PointerToObj myPointerToObj;
00065 PointerToMemFn myPointerToMemFn;
00066 };
00067
00068
00069
00070 template <typename P1, typename PointerToObj, typename PointerToMemFn>
00071 class UT_MemFunHandler1<UT_Functor1<void, P1>, PointerToObj, PointerToMemFn>
00072 : public UT_FunctorImpl1<void, P1>
00073 {
00074 public:
00075 typedef void ReturnType;
00076 typedef P1 Parm1Type;
00077
00078 UT_MemFunHandler1(const PointerToObj &pointer_to_obj,
00079 const PointerToMemFn pointer_to_mem_fn)
00080 : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
00081 {}
00082
00083 virtual UT_FunctorImpl1<void, P1> *clone() const
00084 { return new UT_MemFunHandler1<UT_Functor1<void, P1>, PointerToObj,
00085 PointerToMemFn>(*this); }
00086
00087
00088 virtual void operator()(Parm1Type parm1) const
00089 { ((*myPointerToObj).*myPointerToMemFn)(parm1); }
00090
00091 private:
00092 PointerToObj myPointerToObj;
00093 PointerToMemFn myPointerToMemFn;
00094 };
00095
00096
00097
00098 template <typename P1, typename P2,
00099 typename PointerToObj, typename PointerToMemFn>
00100 class UT_MemFunHandler2<UT_Functor2<void, P1, P2>, PointerToObj, PointerToMemFn>
00101 : public UT_FunctorImpl2<void, P1, P2>
00102 {
00103 public:
00104 typedef void ReturnType;
00105 typedef P1 Parm1Type;
00106 typedef P2 Parm2Type;
00107
00108 UT_MemFunHandler2(const PointerToObj &pointer_to_obj,
00109 const PointerToMemFn pointer_to_mem_fn)
00110 : myPointerToObj(pointer_to_obj), myPointerToMemFn(pointer_to_mem_fn)
00111 {}
00112
00113 virtual UT_FunctorImpl2<void, Parm1Type, Parm2Type> *clone() const
00114 { return new UT_MemFunHandler2<UT_Functor2<void, P1, P2>, PointerToObj,
00115 PointerToMemFn>(*this); }
00116
00117
00118 virtual void operator()(Parm1Type parm1, Parm2Type parm2) const
00119 { ((*myPointerToObj).*myPointerToMemFn)(parm1, parm2); }
00120
00121 private:
00122 PointerToObj myPointerToObj;
00123 PointerToMemFn myPointerToMemFn;
00124 };
00125
00126 template <typename P1>
00127 class UT_Functor1<void, P1>
00128 {
00129 public:
00130 typedef void ReturnType;
00131 typedef P1 Parm1Type;
00132
00133 UT_Functor1() {}
00134
00135 explicit UT_Functor1(
00136 UT_FunctorImpl1<void, Parm1Type> *functor_implementation)
00137 : myFunctorImpl(functor_implementation)
00138 {}
00139
00140 template <typename FunctorType>
00141 UT_Functor1(FunctorType functor)
00142 : myFunctorImpl(new UT_FunctorHandler1<UT_Functor1, FunctorType>(functor))
00143 {}
00144
00145 template <typename PointerToObj, typename PointerToMemFn>
00146 UT_Functor1(const PointerToObj &pointer, PointerToMemFn method)
00147 : myFunctorImpl(new UT_MemFunHandler1<UT_Functor1, PointerToObj,
00148 PointerToMemFn>(pointer, method))
00149 {}
00150
00151 ~UT_Functor1()
00152 {
00153 delete myFunctorImpl.myFunctorImpl;
00154 myFunctorImpl.myFunctorImpl = 0;
00155 }
00156
00157 UT_Functor1 &operator=(const UT_Functor1 &functor)
00158 {
00159 if (&functor != this)
00160 {
00161 delete myFunctorImpl.myFunctorImpl;
00162 myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
00163 ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
00164 }
00165 return *this;
00166 }
00167
00168
00169 void operator()(Parm1Type parm1) const
00170 {
00171 UT_ASSERT(myFunctorImpl.myFunctorImpl);
00172 (*myFunctorImpl.myFunctorImpl)(parm1);
00173 }
00174
00175 bool isSet() const
00176 { return myFunctorImpl.myFunctorImpl != 0; }
00177
00178 private:
00179 class Helper
00180 {
00181 public:
00182 Helper() : myFunctorImpl(0) {}
00183
00184
00185 Helper(const Helper &helper)
00186 { myFunctorImpl = helper.myFunctorImpl
00187 ? helper.myFunctorImpl->clone() : 0; }
00188
00189 explicit Helper(
00190 UT_FunctorImpl1<void, Parm1Type> *functor_implementation)
00191 : myFunctorImpl(functor_implementation) {}
00192
00193 template<typename U>
00194 explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
00195
00196 UT_FunctorImpl1<void, Parm1Type> *myFunctorImpl;
00197 };
00198
00199 Helper myFunctorImpl;
00200 };
00201
00202
00203
00204 template <typename P1, typename P2>
00205 class UT_Functor2<void, P1, P2>
00206 {
00207 public:
00208 typedef void ReturnType;
00209 typedef P1 Parm1Type;
00210 typedef P2 Parm2Type;
00211
00212 UT_Functor2() {}
00213
00214 explicit UT_Functor2(
00215 UT_FunctorImpl2<void, Parm1Type, Parm2Type> *functor_impl)
00216 : myFunctorImpl(functor_impl)
00217 {}
00218
00219 template <typename FunctorType>
00220 UT_Functor2(FunctorType functor)
00221 : myFunctorImpl(new UT_FunctorHandler2<UT_Functor2, FunctorType>(functor))
00222 {}
00223
00224 template <typename PointerToObj, typename PointerToMemFn>
00225 UT_Functor2(const PointerToObj &pointer, PointerToMemFn method)
00226 : myFunctorImpl(new UT_MemFunHandler2<UT_Functor2, PointerToObj,
00227 PointerToMemFn>(pointer, method))
00228 {}
00229
00230 ~UT_Functor2()
00231 {
00232 delete myFunctorImpl.myFunctorImpl;
00233 myFunctorImpl.myFunctorImpl = 0;
00234 }
00235
00236 UT_Functor2 &operator=(const UT_Functor2 &functor)
00237 {
00238 if (&functor != this)
00239 {
00240 delete myFunctorImpl.myFunctorImpl;
00241 myFunctorImpl.myFunctorImpl = functor.myFunctorImpl.myFunctorImpl
00242 ? functor.myFunctorImpl.myFunctorImpl->clone() : 0;
00243 }
00244 return *this;
00245 }
00246
00247
00248 void operator()(Parm1Type parm1, Parm2Type parm2) const
00249 {
00250 UT_ASSERT(myFunctorImpl.myFunctorImpl);
00251 (*myFunctorImpl.myFunctorImpl)(parm1, parm2);
00252 }
00253
00254 bool isSet() const
00255 { return myFunctorImpl.myFunctorImpl != 0; }
00256
00257 private:
00258 class Helper
00259 {
00260 public:
00261 Helper() : myFunctorImpl(0) {}
00262
00263
00264 Helper(const Helper &helper)
00265 { myFunctorImpl = helper.myFunctorImpl
00266 ? helper.myFunctorImpl->clone() : 0; }
00267
00268 explicit Helper(
00269 UT_FunctorImpl2<void, Parm1Type, Parm2Type> *functor_impl)
00270 : myFunctorImpl(functor_impl) {}
00271
00272 template<typename U>
00273 explicit Helper(U *ptr) : myFunctorImpl(ptr) {}
00274
00275 UT_FunctorImpl2<void, Parm1Type, Parm2Type> *myFunctorImpl;
00276 };
00277
00278 Helper myFunctorImpl;
00279 };
00280
00281 #endif