| Home · All Classes · All Namespaces · Modules · Functions · Files

callbacks.h

    00001
    00023 #ifndef _TelepathyQt_callbacks_h_HEADER_GUARD_
    00024 #define _TelepathyQt_callbacks_h_HEADER_GUARD_
    00025
    00026 #ifndef IN_TP_QT_HEADER
    00027 #error IN_TP_QT_HEADER
    00028 #endif
    00029
    00030 #include <TelepathyQt/Functors>
    00031 #include <TelepathyQt/Global>
    00032
    00033 namespace Tp
    00034 {
    00035
    00036 struct TP_QT_EXPORT AbstractFunctorCaller
    00037 {
    00038     typedef void *(*HookType)(void*);
    00039
    00040     AbstractFunctorCaller(HookType invokeMethodHook) : invokeMethodHook(invokeMethodHook) {}
    00041     virtual ~AbstractFunctorCaller() {}
    00042
    00043     virtual AbstractFunctorCaller *clone() const = 0;
    00044
    00045     HookType invokeMethodHook;
    00046
    00047 private:
    00048     AbstractFunctorCaller(const AbstractFunctorCaller &other);
    00049     AbstractFunctorCaller &operator=(const AbstractFunctorCaller &other);
    00050 };
    00051
    00052 template <class T, class Functor>
    00053 struct BaseFunctorCaller : public AbstractFunctorCaller
    00054 {
    00055     BaseFunctorCaller(const Functor &functor, AbstractFunctorCaller::HookType invokeMethodHook)
    00056         : AbstractFunctorCaller(invokeMethodHook),
    00057           functor(functor) {}
    00058     virtual ~BaseFunctorCaller() {}
    00059
    00060     virtual AbstractFunctorCaller *clone() const { return new T(functor); }
    00061
    00062     Functor functor;
    00063 };
    00064
    00065 struct TP_QT_EXPORT BaseCallback
    00066 {
    00067     BaseCallback() : caller(0) {}
    00068     /* takes ownership of caller */
    00069     BaseCallback(AbstractFunctorCaller *caller) : caller(caller) {}
    00070     BaseCallback(const BaseCallback &other) : caller(other.caller->clone()) {}
    00071     virtual ~BaseCallback() { delete caller; }
    00072
    00073     bool isValid() const { return caller != 0; }
    00074
    00075     BaseCallback &operator=(const BaseCallback &other)
    00076     {
    00077         if (caller == other.caller) return *this;
    00078         delete caller;
    00079         caller = other.caller->clone();
    00080         return *this;
    00081     }
    00082
    00083 protected:
    00084     /* may be null */
    00085     AbstractFunctorCaller *caller;
    00086 };
    00087
    00088 template <class Functor, class R >
    00089 struct FunctorCaller0 : public BaseFunctorCaller<FunctorCaller0<Functor, R >, Functor>
    00090 {
    00091     typedef R ResultType;
    00092     typedef R (*InvokeType)(AbstractFunctorCaller* );
    00093
    00094     explicit FunctorCaller0(const Functor &functor) : BaseFunctorCaller<FunctorCaller0, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller0<Functor, R >::invoke)) {}
    00095
    00096     static ResultType invoke(AbstractFunctorCaller *call )
    00097     {
    00098         typedef FunctorCaller0<Functor, R > Type;
    00099         Type *typed = static_cast<Type*>(call);
    00100         return (typed->functor)();
    00101     }
    00102 };
    00103
    00104 template <class R >
    00105 struct Callback0 : public BaseCallback
    00106 {
    00107     typedef R (*FunctionType)();
    00108     typedef R ResultType;
    00109
    00110     Callback0() {}
    00111     template <class Functor>
    00112     Callback0(const Functor &functor) : BaseCallback(new FunctorCaller0<Functor, R >(functor)) {}
    00113
    00114     ResultType operator()() const
    00115     {
    00116         if (caller) {
    00117             typedef R (*InvokeType)(AbstractFunctorCaller* );
    00118             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00119             return invokeMethod(caller );
    00120         }
    00121         return ResultType();
    00122     }
    00123 };
    00124
    00125 template <class Functor, class R , class Arg1>
    00126 struct FunctorCaller1 : public BaseFunctorCaller<FunctorCaller1<Functor, R , Arg1>, Functor>
    00127 {
    00128     typedef R ResultType;
    00129     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1);
    00130
    00131     explicit FunctorCaller1(const Functor &functor) : BaseFunctorCaller<FunctorCaller1, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller1<Functor, R , Arg1>::invoke)) {}
    00132
    00133     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1)
    00134     {
    00135         typedef FunctorCaller1<Functor, R , Arg1> Type;
    00136         Type *typed = static_cast<Type*>(call);
    00137         return (typed->functor)(a1);
    00138     }
    00139 };
    00140
    00141 template <class R , class Arg1>
    00142 struct Callback1 : public BaseCallback
    00143 {
    00144     typedef R (*FunctionType)(Arg1);
    00145     typedef R ResultType;
    00146
    00147     Callback1() {}
    00148     template <class Functor>
    00149     Callback1(const Functor &functor) : BaseCallback(new FunctorCaller1<Functor, R , Arg1>(functor)) {}
    00150
    00151     ResultType operator()(Arg1 a1) const
    00152     {
    00153         if (caller) {
    00154             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1);
    00155             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00156             return invokeMethod(caller , a1);
    00157         }
    00158         return ResultType();
    00159     }
    00160 };
    00161
    00162 template <class Functor, class R , class Arg1, class Arg2>
    00163 struct FunctorCaller2 : public BaseFunctorCaller<FunctorCaller2<Functor, R , Arg1, Arg2>, Functor>
    00164 {
    00165     typedef R ResultType;
    00166     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2);
    00167
    00168     explicit FunctorCaller2(const Functor &functor) : BaseFunctorCaller<FunctorCaller2, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller2<Functor, R , Arg1, Arg2>::invoke)) {}
    00169
    00170     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1, Arg2 a2)
    00171     {
    00172         typedef FunctorCaller2<Functor, R , Arg1, Arg2> Type;
    00173         Type *typed = static_cast<Type*>(call);
    00174         return (typed->functor)(a1, a2);
    00175     }
    00176 };
    00177
    00178 template <class R , class Arg1, class Arg2>
    00179 struct Callback2 : public BaseCallback
    00180 {
    00181     typedef R (*FunctionType)(Arg1, Arg2);
    00182     typedef R ResultType;
    00183
    00184     Callback2() {}
    00185     template <class Functor>
    00186     Callback2(const Functor &functor) : BaseCallback(new FunctorCaller2<Functor, R , Arg1, Arg2>(functor)) {}
    00187
    00188     ResultType operator()(Arg1 a1, Arg2 a2) const
    00189     {
    00190         if (caller) {
    00191             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2);
    00192             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00193             return invokeMethod(caller , a1, a2);
    00194         }
    00195         return ResultType();
    00196     }
    00197 };
    00198
    00199 template <class Functor, class R , class Arg1, class Arg2, class Arg3>
    00200 struct FunctorCaller3 : public BaseFunctorCaller<FunctorCaller3<Functor, R , Arg1, Arg2, Arg3>, Functor>
    00201 {
    00202     typedef R ResultType;
    00203     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3);
    00204
    00205     explicit FunctorCaller3(const Functor &functor) : BaseFunctorCaller<FunctorCaller3, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller3<Functor, R , Arg1, Arg2, Arg3>::invoke)) {}
    00206
    00207     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1, Arg2 a2, Arg3 a3)
    00208     {
    00209         typedef FunctorCaller3<Functor, R , Arg1, Arg2, Arg3> Type;
    00210         Type *typed = static_cast<Type*>(call);
    00211         return (typed->functor)(a1, a2, a3);
    00212     }
    00213 };
    00214
    00215 template <class R , class Arg1, class Arg2, class Arg3>
    00216 struct Callback3 : public BaseCallback
    00217 {
    00218     typedef R (*FunctionType)(Arg1, Arg2, Arg3);
    00219     typedef R ResultType;
    00220
    00221     Callback3() {}
    00222     template <class Functor>
    00223     Callback3(const Functor &functor) : BaseCallback(new FunctorCaller3<Functor, R , Arg1, Arg2, Arg3>(functor)) {}
    00224
    00225     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3) const
    00226     {
    00227         if (caller) {
    00228             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3);
    00229             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00230             return invokeMethod(caller , a1, a2, a3);
    00231         }
    00232         return ResultType();
    00233     }
    00234 };
    00235
    00236 template <class Functor, class R , class Arg1, class Arg2, class Arg3, class Arg4>
    00237 struct FunctorCaller4 : public BaseFunctorCaller<FunctorCaller4<Functor, R , Arg1, Arg2, Arg3, Arg4>, Functor>
    00238 {
    00239     typedef R ResultType;
    00240     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4);
    00241
    00242     explicit FunctorCaller4(const Functor &functor) : BaseFunctorCaller<FunctorCaller4, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller4<Functor, R , Arg1, Arg2, Arg3, Arg4>::invoke)) {}
    00243
    00244     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4)
    00245     {
    00246         typedef FunctorCaller4<Functor, R , Arg1, Arg2, Arg3, Arg4> Type;
    00247         Type *typed = static_cast<Type*>(call);
    00248         return (typed->functor)(a1, a2, a3, a4);
    00249     }
    00250 };
    00251
    00252 template <class R , class Arg1, class Arg2, class Arg3, class Arg4>
    00253 struct Callback4 : public BaseCallback
    00254 {
    00255     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4);
    00256     typedef R ResultType;
    00257
    00258     Callback4() {}
    00259     template <class Functor>
    00260     Callback4(const Functor &functor) : BaseCallback(new FunctorCaller4<Functor, R , Arg1, Arg2, Arg3, Arg4>(functor)) {}
    00261
    00262     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4) const
    00263     {
    00264         if (caller) {
    00265             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4);
    00266             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00267             return invokeMethod(caller , a1, a2, a3, a4);
    00268         }
    00269         return ResultType();
    00270     }
    00271 };
    00272
    00273 template <class Functor, class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
    00274 struct FunctorCaller5 : public BaseFunctorCaller<FunctorCaller5<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5>, Functor>
    00275 {
    00276     typedef R ResultType;
    00277     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4, Arg5);
    00278
    00279     explicit FunctorCaller5(const Functor &functor) : BaseFunctorCaller<FunctorCaller5, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller5<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5>::invoke)) {}
    00280
    00281     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5)
    00282     {
    00283         typedef FunctorCaller5<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5> Type;
    00284         Type *typed = static_cast<Type*>(call);
    00285         return (typed->functor)(a1, a2, a3, a4, a5);
    00286     }
    00287 };
    00288
    00289 template <class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
    00290 struct Callback5 : public BaseCallback
    00291 {
    00292     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5);
    00293     typedef R ResultType;
    00294
    00295     Callback5() {}
    00296     template <class Functor>
    00297     Callback5(const Functor &functor) : BaseCallback(new FunctorCaller5<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5>(functor)) {}
    00298
    00299     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5) const
    00300     {
    00301         if (caller) {
    00302             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4, Arg5);
    00303             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00304             return invokeMethod(caller , a1, a2, a3, a4, a5);
    00305         }
    00306         return ResultType();
    00307     }
    00308 };
    00309
    00310 template <class Functor, class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
    00311 struct FunctorCaller6 : public BaseFunctorCaller<FunctorCaller6<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>, Functor>
    00312 {
    00313     typedef R ResultType;
    00314     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
    00315
    00316     explicit FunctorCaller6(const Functor &functor) : BaseFunctorCaller<FunctorCaller6, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller6<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>::invoke)) {}
    00317
    00318     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6)
    00319     {
    00320         typedef FunctorCaller6<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6> Type;
    00321         Type *typed = static_cast<Type*>(call);
    00322         return (typed->functor)(a1, a2, a3, a4, a5, a6);
    00323     }
    00324 };
    00325
    00326 template <class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
    00327 struct Callback6 : public BaseCallback
    00328 {
    00329     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
    00330     typedef R ResultType;
    00331
    00332     Callback6() {}
    00333     template <class Functor>
    00334     Callback6(const Functor &functor) : BaseCallback(new FunctorCaller6<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(functor)) {}
    00335
    00336     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6) const
    00337     {
    00338         if (caller) {
    00339             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
    00340             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00341             return invokeMethod(caller , a1, a2, a3, a4, a5, a6);
    00342         }
    00343         return ResultType();
    00344     }
    00345 };
    00346
    00347 template <class Functor, class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
    00348 struct FunctorCaller7 : public BaseFunctorCaller<FunctorCaller7<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>, Functor>
    00349 {
    00350     typedef R ResultType;
    00351     typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
    00352
    00353     explicit FunctorCaller7(const Functor &functor) : BaseFunctorCaller<FunctorCaller7, Functor>(functor, reinterpret_cast<AbstractFunctorCaller::HookType>(&FunctorCaller7<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>::invoke)) {}
    00354
    00355     static ResultType invoke(AbstractFunctorCaller *call , Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6, Arg7 a7)
    00356     {
    00357         typedef FunctorCaller7<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7> Type;
    00358         Type *typed = static_cast<Type*>(call);
    00359         return (typed->functor)(a1, a2, a3, a4, a5, a6, a7);
    00360     }
    00361 };
    00362
    00363 template <class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
    00364 struct Callback7 : public BaseCallback
    00365 {
    00366     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
    00367     typedef R ResultType;
    00368
    00369     Callback7() {}
    00370     template <class Functor>
    00371     Callback7(const Functor &functor) : BaseCallback(new FunctorCaller7<Functor, R , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(functor)) {}
    00372
    00373     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6, Arg7 a7) const
    00374     {
    00375         if (caller) {
    00376             typedef R (*InvokeType)(AbstractFunctorCaller* , Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
    00377             InvokeType invokeMethod = reinterpret_cast<InvokeType>(caller->invokeMethodHook);
    00378             return invokeMethod(caller , a1, a2, a3, a4, a5, a6, a7);
    00379         }
    00380         return ResultType();
    00381     }
    00382 };
    00383
    00384 }
    00385
    00386 #endif