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

functors.h

    00001
    00023 #ifndef _TelepathyQt_functors_h_HEADER_GUARD_
    00024 #define _TelepathyQt_functors_h_HEADER_GUARD_
    00025
    00026 #ifndef IN_TP_QT_HEADER
    00027 #error IN_TP_QT_HEADER
    00028 #endif
    00029
    00030 #include <TelepathyQt/Global>
    00031
    00032 namespace Tp
    00033 {
    00034
    00035 struct TP_QT_EXPORT BaseFunctor
    00036 {
    00037 };
    00038
    00039 template <class R >
    00040 struct PtrFunctor0 : public BaseFunctor
    00041 {
    00042     typedef R (*FunctionType)();
    00043     typedef R ResultType;
    00044
    00045     PtrFunctor0(FunctionType fn) : fn(fn) {}
    00046
    00047     ResultType operator()() const { return fn(); }
    00048
    00049     FunctionType fn;
    00050 };
    00051
    00052 template <class R, class T >
    00053 struct MemberFunctor0 : public BaseFunctor
    00054 {
    00055     typedef R (T::*FunctionType)();
    00056     typedef R ResultType;
    00057
    00058     MemberFunctor0(T *object, FunctionType fn) : object(object), fn(fn) {}
    00059
    00060     ResultType operator()() const { return (object->*(fn))(); }
    00061
    00062     T *object;
    00063     FunctionType fn;
    00064 };
    00065
    00066 template <class R , class Arg1>
    00067 struct PtrFunctor1 : public BaseFunctor
    00068 {
    00069     typedef R (*FunctionType)(Arg1);
    00070     typedef R ResultType;
    00071
    00072     PtrFunctor1(FunctionType fn) : fn(fn) {}
    00073
    00074     ResultType operator()(Arg1 a1) const { return fn(a1); }
    00075
    00076     FunctionType fn;
    00077 };
    00078
    00079 template <class R, class T , class Arg1>
    00080 struct MemberFunctor1 : public BaseFunctor
    00081 {
    00082     typedef R (T::*FunctionType)(Arg1);
    00083     typedef R ResultType;
    00084
    00085     MemberFunctor1(T *object, FunctionType fn) : object(object), fn(fn) {}
    00086
    00087     ResultType operator()(Arg1 a1) const { return (object->*(fn))(a1); }
    00088
    00089     T *object;
    00090     FunctionType fn;
    00091 };
    00092
    00093 template <class R , class Arg1, class Arg2>
    00094 struct PtrFunctor2 : public BaseFunctor
    00095 {
    00096     typedef R (*FunctionType)(Arg1, Arg2);
    00097     typedef R ResultType;
    00098
    00099     PtrFunctor2(FunctionType fn) : fn(fn) {}
    00100
    00101     ResultType operator()(Arg1 a1, Arg2 a2) const { return fn(a1, a2); }
    00102
    00103     FunctionType fn;
    00104 };
    00105
    00106 template <class R, class T , class Arg1, class Arg2>
    00107 struct MemberFunctor2 : public BaseFunctor
    00108 {
    00109     typedef R (T::*FunctionType)(Arg1, Arg2);
    00110     typedef R ResultType;
    00111
    00112     MemberFunctor2(T *object, FunctionType fn) : object(object), fn(fn) {}
    00113
    00114     ResultType operator()(Arg1 a1, Arg2 a2) const { return (object->*(fn))(a1, a2); }
    00115
    00116     T *object;
    00117     FunctionType fn;
    00118 };
    00119
    00120 template <class R , class Arg1, class Arg2, class Arg3>
    00121 struct PtrFunctor3 : public BaseFunctor
    00122 {
    00123     typedef R (*FunctionType)(Arg1, Arg2, Arg3);
    00124     typedef R ResultType;
    00125
    00126     PtrFunctor3(FunctionType fn) : fn(fn) {}
    00127
    00128     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3) const { return fn(a1, a2, a3); }
    00129
    00130     FunctionType fn;
    00131 };
    00132
    00133 template <class R, class T , class Arg1, class Arg2, class Arg3>
    00134 struct MemberFunctor3 : public BaseFunctor
    00135 {
    00136     typedef R (T::*FunctionType)(Arg1, Arg2, Arg3);
    00137     typedef R ResultType;
    00138
    00139     MemberFunctor3(T *object, FunctionType fn) : object(object), fn(fn) {}
    00140
    00141     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3) const { return (object->*(fn))(a1, a2, a3); }
    00142
    00143     T *object;
    00144     FunctionType fn;
    00145 };
    00146
    00147 template <class R , class Arg1, class Arg2, class Arg3, class Arg4>
    00148 struct PtrFunctor4 : public BaseFunctor
    00149 {
    00150     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4);
    00151     typedef R ResultType;
    00152
    00153     PtrFunctor4(FunctionType fn) : fn(fn) {}
    00154
    00155     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4) const { return fn(a1, a2, a3, a4); }
    00156
    00157     FunctionType fn;
    00158 };
    00159
    00160 template <class R, class T , class Arg1, class Arg2, class Arg3, class Arg4>
    00161 struct MemberFunctor4 : public BaseFunctor
    00162 {
    00163     typedef R (T::*FunctionType)(Arg1, Arg2, Arg3, Arg4);
    00164     typedef R ResultType;
    00165
    00166     MemberFunctor4(T *object, FunctionType fn) : object(object), fn(fn) {}
    00167
    00168     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4) const { return (object->*(fn))(a1, a2, a3, a4); }
    00169
    00170     T *object;
    00171     FunctionType fn;
    00172 };
    00173
    00174 template <class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
    00175 struct PtrFunctor5 : public BaseFunctor
    00176 {
    00177     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5);
    00178     typedef R ResultType;
    00179
    00180     PtrFunctor5(FunctionType fn) : fn(fn) {}
    00181
    00182     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5) const { return fn(a1, a2, a3, a4, a5); }
    00183
    00184     FunctionType fn;
    00185 };
    00186
    00187 template <class R, class T , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
    00188 struct MemberFunctor5 : public BaseFunctor
    00189 {
    00190     typedef R (T::*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5);
    00191     typedef R ResultType;
    00192
    00193     MemberFunctor5(T *object, FunctionType fn) : object(object), fn(fn) {}
    00194
    00195     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5) const { return (object->*(fn))(a1, a2, a3, a4, a5); }
    00196
    00197     T *object;
    00198     FunctionType fn;
    00199 };
    00200
    00201 template <class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
    00202 struct PtrFunctor6 : public BaseFunctor
    00203 {
    00204     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
    00205     typedef R ResultType;
    00206
    00207     PtrFunctor6(FunctionType fn) : fn(fn) {}
    00208
    00209     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6) const { return fn(a1, a2, a3, a4, a5, a6); }
    00210
    00211     FunctionType fn;
    00212 };
    00213
    00214 template <class R, class T , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
    00215 struct MemberFunctor6 : public BaseFunctor
    00216 {
    00217     typedef R (T::*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
    00218     typedef R ResultType;
    00219
    00220     MemberFunctor6(T *object, FunctionType fn) : object(object), fn(fn) {}
    00221
    00222     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6) const { return (object->*(fn))(a1, a2, a3, a4, a5, a6); }
    00223
    00224     T *object;
    00225     FunctionType fn;
    00226 };
    00227
    00228 template <class R , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
    00229 struct PtrFunctor7 : public BaseFunctor
    00230 {
    00231     typedef R (*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
    00232     typedef R ResultType;
    00233
    00234     PtrFunctor7(FunctionType fn) : fn(fn) {}
    00235
    00236     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6, Arg7 a7) const { return fn(a1, a2, a3, a4, a5, a6, a7); }
    00237
    00238     FunctionType fn;
    00239 };
    00240
    00241 template <class R, class T , class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
    00242 struct MemberFunctor7 : public BaseFunctor
    00243 {
    00244     typedef R (T::*FunctionType)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
    00245     typedef R ResultType;
    00246
    00247     MemberFunctor7(T *object, FunctionType fn) : object(object), fn(fn) {}
    00248
    00249     ResultType operator()(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5, Arg6 a6, Arg7 a7) const { return (object->*(fn))(a1, a2, a3, a4, a5, a6, a7); }
    00250
    00251     T *object;
    00252     FunctionType fn;
    00253 };
    00254
    00255 // convenience methods
    00256
    00257 // ptrFun
    00258
    00259 template <class R>
    00260 inline PtrFunctor0<R>
    00261 ptrFun(R (*fn)() )
    00262 { return PtrFunctor0<R>(fn); }
    00263
    00264 template <class R, class Arg1>
    00265 inline PtrFunctor1<R, Arg1>
    00266 ptrFun(R (*fn)(Arg1) )
    00267 { return PtrFunctor1<R, Arg1>(fn); }
    00268
    00269 template <class R, class Arg1, class Arg2>
    00270 inline PtrFunctor2<R, Arg1, Arg2>
    00271 ptrFun(R (*fn)(Arg1, Arg2) )
    00272 { return PtrFunctor2<R, Arg1, Arg2>(fn); }
    00273
    00274 template <class R, class Arg1, class Arg2, class Arg3>
    00275 inline PtrFunctor3<R, Arg1, Arg2, Arg3>
    00276 ptrFun(R (*fn)(Arg1, Arg2, Arg3) )
    00277 { return PtrFunctor3<R, Arg1, Arg2, Arg3>(fn); }
    00278
    00279 template <class R, class Arg1, class Arg2, class Arg3, class Arg4>
    00280 inline PtrFunctor4<R, Arg1, Arg2, Arg3, Arg4>
    00281 ptrFun(R (*fn)(Arg1, Arg2, Arg3, Arg4) )
    00282 { return PtrFunctor4<R, Arg1, Arg2, Arg3, Arg4>(fn); }
    00283
    00284 template <class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
    00285 inline PtrFunctor5<R, Arg1, Arg2, Arg3, Arg4, Arg5>
    00286 ptrFun(R (*fn)(Arg1, Arg2, Arg3, Arg4, Arg5) )
    00287 { return PtrFunctor5<R, Arg1, Arg2, Arg3, Arg4, Arg5>(fn); }
    00288
    00289 template <class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
    00290 inline PtrFunctor6<R, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>
    00291 ptrFun(R (*fn)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) )
    00292 { return PtrFunctor6<R, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(fn); }
    00293
    00294 template <class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
    00295 inline PtrFunctor7<R, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>
    00296 ptrFun(R (*fn)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) )
    00297 { return PtrFunctor7<R, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(fn); }
    00298
    00299 // memFun
    00300
    00301 template <class R, class T>
    00302 inline MemberFunctor0<R, T>
    00303 memFun( T *obj, R (T::*fn)() )
    00304 { return MemberFunctor0<R, T>(obj, fn); }
    00305
    00306 template <class R, class T, class Arg1>
    00307 inline MemberFunctor1<R, T, Arg1>
    00308 memFun( T *obj, R (T::*fn)(Arg1) )
    00309 { return MemberFunctor1<R, T, Arg1>(obj, fn); }
    00310
    00311 template <class R, class T, class Arg1, class Arg2>
    00312 inline MemberFunctor2<R, T, Arg1, Arg2>
    00313 memFun( T *obj, R (T::*fn)(Arg1, Arg2) )
    00314 { return MemberFunctor2<R, T, Arg1, Arg2>(obj, fn); }
    00315
    00316 template <class R, class T, class Arg1, class Arg2, class Arg3>
    00317 inline MemberFunctor3<R, T, Arg1, Arg2, Arg3>
    00318 memFun( T *obj, R (T::*fn)(Arg1, Arg2, Arg3) )
    00319 { return MemberFunctor3<R, T, Arg1, Arg2, Arg3>(obj, fn); }
    00320
    00321 template <class R, class T, class Arg1, class Arg2, class Arg3, class Arg4>
    00322 inline MemberFunctor4<R, T, Arg1, Arg2, Arg3, Arg4>
    00323 memFun( T *obj, R (T::*fn)(Arg1, Arg2, Arg3, Arg4) )
    00324 { return MemberFunctor4<R, T, Arg1, Arg2, Arg3, Arg4>(obj, fn); }
    00325
    00326 template <class R, class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
    00327 inline MemberFunctor5<R, T, Arg1, Arg2, Arg3, Arg4, Arg5>
    00328 memFun( T *obj, R (T::*fn)(Arg1, Arg2, Arg3, Arg4, Arg5) )
    00329 { return MemberFunctor5<R, T, Arg1, Arg2, Arg3, Arg4, Arg5>(obj, fn); }
    00330
    00331 template <class R, class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
    00332 inline MemberFunctor6<R, T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>
    00333 memFun( T *obj, R (T::*fn)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6) )
    00334 { return MemberFunctor6<R, T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(obj, fn); }
    00335
    00336 template <class R, class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
    00337 inline MemberFunctor7<R, T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>
    00338 memFun( T *obj, R (T::*fn)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7) )
    00339 { return MemberFunctor7<R, T, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(obj, fn); }
    00340
    00341 }
    00342
    00343 #endif