Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands2/src/libraries/core/Functor.h @ 6644

Last change on this file since 6644 was 6452, checked in by landauf, 15 years ago

merged current state of the new cc system to the updated branch

  • Property svn:eol-style set to native
File size: 27.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 *   Inspiration: Functor by Benjamin Grauer
28 */
29
30#ifndef _Functor_H__
31#define _Functor_H__
32
33#include <typeinfo>
34
35#include "CorePrereqs.h"
36
37#include "util/Convert.h"
38#include "util/Debug.h"
39#include "util/MultiType.h"
40#include "BaseObject.h"
41
42namespace orxonox
43{
44    const unsigned int MAX_FUNCTOR_ARGUMENTS = 5;
45
46    namespace FunctionType
47    {
48        enum Value
49        {
50            Member,
51            ConstMember,
52            Static
53        };
54    }
55
56
57    template <class T>
58    inline std::string typeToString() { return "unknown"; }
59
60#define CreateTypeToStringTemplate(type) \
61    template <> \
62    inline std::string typeToString<type>() { return #type; } \
63    template <> \
64    inline std::string typeToString<type&>() { return #type; } \
65    template <> \
66    inline std::string typeToString<const type>() { return #type; } \
67    template <> \
68    inline std::string typeToString<const type&>() { return #type; }
69
70    CreateTypeToStringTemplate(int);
71    CreateTypeToStringTemplate(unsigned int);
72    CreateTypeToStringTemplate(char);
73    CreateTypeToStringTemplate(unsigned char);
74    CreateTypeToStringTemplate(short);
75    CreateTypeToStringTemplate(unsigned short);
76    CreateTypeToStringTemplate(long);
77    CreateTypeToStringTemplate(unsigned long);
78    CreateTypeToStringTemplate(long long);
79    CreateTypeToStringTemplate(unsigned long long);
80    CreateTypeToStringTemplate(float);
81    CreateTypeToStringTemplate(double);
82    CreateTypeToStringTemplate(long double);
83    CreateTypeToStringTemplate(bool);
84    CreateTypeToStringTemplate(Vector2);
85    CreateTypeToStringTemplate(Vector3);
86    CreateTypeToStringTemplate(Quaternion);
87    CreateTypeToStringTemplate(ColourValue);
88    CreateTypeToStringTemplate(Radian);
89    CreateTypeToStringTemplate(Degree);
90
91    template <>
92    inline std::string typeToString<std::string>() { return "string"; }
93    template <>
94    inline std::string typeToString<std::string&>() { return "string"; }
95    template <>
96    inline std::string typeToString<const std::string>() { return "string"; }
97    template <>
98    inline std::string typeToString<const std::string&>() { return "string"; }
99
100    class _CoreExport Functor
101    {
102        public:
103            Functor() {}
104            virtual ~Functor() {}
105
106            virtual void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0;
107
108            inline unsigned int getParamCount() const { return this->numParams_; }
109            inline bool hasReturnvalue() const { return this->hasReturnValue_; }
110            inline FunctionType::Value getType() const { return this->type_; }
111            inline const MultiType& getReturnvalue() const { return this->returnedValue_; }
112
113            const std::string& getTypenameParam(unsigned int param) const { return (param < 5) ? this->typeParam_[param] : BLANKSTRING; }
114            const std::string& getTypenameReturnvalue() const { return this->typeReturnvalue_; }
115
116            virtual void evaluateParam(unsigned int index, MultiType& param) const = 0;
117
118            virtual void setBaseObject(BaseObject* object) {}
119            virtual void setBaseObject(const BaseObject* object) {}
120            virtual BaseObject* getBaseObject() const { return 0; }
121
122            virtual void setRawObjectPointer(void* object) {}
123            virtual void* getRawObjectPointer() const { return 0; }
124
125            virtual const std::type_info& getHeaderIdentifier() const = 0;
126
127        protected:
128            unsigned int numParams_;
129            bool hasReturnValue_;
130            FunctionType::Value type_;
131            MultiType returnedValue_;
132
133            std::string typeReturnvalue_;
134            std::string typeParam_[MAX_FUNCTOR_ARGUMENTS];
135    };
136
137    class _CoreExport FunctorStatic : public Functor
138    {
139        public:
140            virtual ~FunctorStatic() {}
141            virtual void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0;
142    };
143
144    template <class T>
145    class FunctorMember : public Functor
146    {
147        public:
148            FunctorMember()
149            {
150                constObject_ = 0;
151                object_ = 0;
152                bConstObject_ = false;
153            }
154            virtual ~FunctorMember() {}
155
156            virtual void operator()(T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0;
157            virtual void operator()(const T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0;
158
159            virtual void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null)
160            {
161                if (this->bConstObject_)
162                {
163                    if (this->constObject_)
164                        (*this)(this->constObject_, param1, param2, param3, param4, param5);
165                    else
166                    {
167                        COUT(1) << "An error occurred in Functor.h:" << std::endl;
168                        COUT(1) << "Error: No const object set." << std::endl;
169                    }
170                }
171                else
172                {
173                    if (this->object_)
174                        (*this)(this->object_, param1, param2, param3, param4, param5);
175                    else
176                    {
177                        COUT(1) << "An error occurred in Functor.h:" << std::endl;
178                        COUT(1) << "Error: No object set." << std::endl;
179                    }
180                }
181            }
182
183            FunctorMember<T>* setObject(T* object)
184            {
185                this->bConstObject_ = false;
186                this->object_ = object;
187                return this;
188            }
189
190            FunctorMember<T>* setObject(const T* object)
191            {
192                this->bConstObject_ = true;
193                this->constObject_ = object;
194                return this;
195            }
196
197            void setBaseObject(BaseObject* object)
198            {
199                this->bConstObject_ = false;
200                this->object_ = dynamic_cast<T*>(object);
201            }
202
203            void setBaseObject(const BaseObject* object)
204            {
205                this->bConstObject_ = true;
206                this->constObject_ = dynamic_cast<const T*>(object);
207            }
208
209            BaseObject* getBaseObject() const
210            {
211                if (this->bConstObject_)
212                    return const_cast<BaseObject*>(upcast<const BaseObject*>(this->constObject_));
213                else
214                    return upcast<BaseObject*>(this->object_);
215            }
216
217            void setRawObjectPointer(void* object)
218            {
219                this->bConstObject_ = false;
220                this->object_ = (T*)object;
221            }
222
223            void* getRawObjectPointer() const
224            {
225                if (this->bConstObject_)
226                    return (void*)this->constObject_;
227                else
228                    return (void*)this->object_;
229            }
230
231        private:
232            const T* constObject_;
233            T* object_;
234            bool bConstObject_;
235    };
236
237
238
239    template <int r, class R, class P1, class P2, class P3, class P4, class P5>
240    struct FunctorHeaderIdentifier {};
241
242
243
244    inline Functor* createFunctor(Functor* functor)
245    {
246        return functor;
247    }
248
249
250
251#define FUNCTOR_TEMPLATE(ismember, returnvalue, numparams, additionalobject) FUNCTOR_TEMPLATE##ismember##returnvalue##numparams(additionalobject)
252#define FUNCTOR_TEMPLATE000(additionalobject)
253#define FUNCTOR_TEMPLATE001(additionalobject) template <class P1>
254#define FUNCTOR_TEMPLATE002(additionalobject) template <class P1, class P2>
255#define FUNCTOR_TEMPLATE003(additionalobject) template <class P1, class P2, class P3>
256#define FUNCTOR_TEMPLATE004(additionalobject) template <class P1, class P2, class P3, class P4>
257#define FUNCTOR_TEMPLATE005(additionalobject) template <class P1, class P2, class P3, class P4, class P5>
258#define FUNCTOR_TEMPLATE010(additionalobject) template <class R>
259#define FUNCTOR_TEMPLATE011(additionalobject) template <class R, class P1>
260#define FUNCTOR_TEMPLATE012(additionalobject) template <class R, class P1, class P2>
261#define FUNCTOR_TEMPLATE013(additionalobject) template <class R, class P1, class P2, class P3>
262#define FUNCTOR_TEMPLATE014(additionalobject) template <class R, class P1, class P2, class P3, class P4>
263#define FUNCTOR_TEMPLATE015(additionalobject) template <class R, class P1, class P2, class P3, class P4, class P5>
264#define FUNCTOR_TEMPLATE100(additionalobject) template <class T FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
265#define FUNCTOR_TEMPLATE101(additionalobject) template <class T, class P1 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
266#define FUNCTOR_TEMPLATE102(additionalobject) template <class T, class P1, class P2 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
267#define FUNCTOR_TEMPLATE103(additionalobject) template <class T, class P1, class P2, class P3 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
268#define FUNCTOR_TEMPLATE104(additionalobject) template <class T, class P1, class P2, class P3, class P4 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
269#define FUNCTOR_TEMPLATE105(additionalobject) template <class T, class P1, class P2, class P3, class P4, class P5 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
270#define FUNCTOR_TEMPLATE110(additionalobject) template <class T, class R FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
271#define FUNCTOR_TEMPLATE111(additionalobject) template <class T, class R, class P1 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
272#define FUNCTOR_TEMPLATE112(additionalobject) template <class T, class R, class P1, class P2 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
273#define FUNCTOR_TEMPLATE113(additionalobject) template <class T, class R, class P1, class P2, class P3 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
274#define FUNCTOR_TEMPLATE114(additionalobject) template <class T, class R, class P1, class P2, class P3, class P4 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
275#define FUNCTOR_TEMPLATE115(additionalobject) template <class T, class R, class P1, class P2, class P3, class P4, class P5 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
276
277
278
279#define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT##additionalobject
280#define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT0
281#define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT1 , class O
282
283
284
285#define FUNCTOR_TEMPLATE_CLASSES(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE_CLASSES##ismember##returnvalue##numparams
286#define FUNCTOR_TEMPLATE_CLASSES000
287#define FUNCTOR_TEMPLATE_CLASSES001 <P1>
288#define FUNCTOR_TEMPLATE_CLASSES002 <P1, P2>
289#define FUNCTOR_TEMPLATE_CLASSES003 <P1, P2, P3>
290#define FUNCTOR_TEMPLATE_CLASSES004 <P1, P2, P3, P4>
291#define FUNCTOR_TEMPLATE_CLASSES005 <P1, P2, P3, P4, P5>
292#define FUNCTOR_TEMPLATE_CLASSES010 <R>
293#define FUNCTOR_TEMPLATE_CLASSES011 <R, P1>
294#define FUNCTOR_TEMPLATE_CLASSES012 <R, P1, P2>
295#define FUNCTOR_TEMPLATE_CLASSES013 <R, P1, P2, P3>
296#define FUNCTOR_TEMPLATE_CLASSES014 <R, P1, P2, P3, P4>
297#define FUNCTOR_TEMPLATE_CLASSES015 <R, P1, P2, P3, P4, P5>
298#define FUNCTOR_TEMPLATE_CLASSES100 <T>
299#define FUNCTOR_TEMPLATE_CLASSES101 <T, P1>
300#define FUNCTOR_TEMPLATE_CLASSES102 <T, P1, P2>
301#define FUNCTOR_TEMPLATE_CLASSES103 <T, P1, P2, P3>
302#define FUNCTOR_TEMPLATE_CLASSES104 <T, P1, P2, P3, P4>
303#define FUNCTOR_TEMPLATE_CLASSES105 <T, P1, P2, P3, P4, P5>
304#define FUNCTOR_TEMPLATE_CLASSES110 <T, R>
305#define FUNCTOR_TEMPLATE_CLASSES111 <T, R, P1>
306#define FUNCTOR_TEMPLATE_CLASSES112 <T, R, P1, P2>
307#define FUNCTOR_TEMPLATE_CLASSES113 <T, R, P1, P2, P3>
308#define FUNCTOR_TEMPLATE_CLASSES114 <T, R, P1, P2, P3, P4>
309#define FUNCTOR_TEMPLATE_CLASSES115 <T, R, P1, P2, P3, P4, P5>
310
311
312
313#define FUNCTOR_TYPENAME_PARAMS(numparams) FUNCTOR_TYPENAME_PARAMS##numparams
314#define FUNCTOR_TYPENAME_PARAMS0
315#define FUNCTOR_TYPENAME_PARAMS1 this->typeParam_[0] = typeToString<P1>();
316#define FUNCTOR_TYPENAME_PARAMS2 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>();
317#define FUNCTOR_TYPENAME_PARAMS3 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>();
318#define FUNCTOR_TYPENAME_PARAMS4 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>(); this->typeParam_[3] = typeToString<P4>();
319#define FUNCTOR_TYPENAME_PARAMS5 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>(); this->typeParam_[3] = typeToString<P4>(); this->typeParam_[4] = typeToString<P5>();
320
321#define FUNCTOR_TYPENAME_RETURN(returnvalue) FUNCTOR_TYPENAME_RETURN##returnvalue
322#define FUNCTOR_TYPENAME_RETURN0
323#define FUNCTOR_TYPENAME_RETURN1 this->typeReturnvalue_ = typeToString<R>();
324
325
326
327#define FUNCTOR_FUNCTION_PARAMS(numparams) FUNCTOR_FUNCTION_PARAMS##numparams
328#define FUNCTOR_FUNCTION_PARAMS0
329#define FUNCTOR_FUNCTION_PARAMS1 P1 param1
330#define FUNCTOR_FUNCTION_PARAMS2 P1 param1, P2 param2
331#define FUNCTOR_FUNCTION_PARAMS3 P1 param1, P2 param2, P3 param3
332#define FUNCTOR_FUNCTION_PARAMS4 P1 param1, P2 param2, P3 param3, P4 param4
333#define FUNCTOR_FUNCTION_PARAMS5 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5
334
335#define FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) FUNCTOR_FUNCTION_RETURNVALUE##returnvalue
336#define FUNCTOR_FUNCTION_RETURNVALUE0 void
337#define FUNCTOR_FUNCTION_RETURNVALUE1 R
338
339
340
341#define FUNCTOR_FUNCTION_CALL(numparams) FUNCTOR_FUNCTION_CALL##numparams
342#define FUNCTOR_FUNCTION_CALL0
343#define FUNCTOR_FUNCTION_CALL1 param1
344#define FUNCTOR_FUNCTION_CALL2 param1, param2
345#define FUNCTOR_FUNCTION_CALL3 param1, param2, param3
346#define FUNCTOR_FUNCTION_CALL4 param1, param2, param3, param4
347#define FUNCTOR_FUNCTION_CALL5 param1, param2, param3, param4, param5
348
349#define FUNCTOR_STORE_RETURNVALUE(returnvalue, functioncall) FUNCTOR_STORE_RETURNVALUE##returnvalue(functioncall)
350#define FUNCTOR_STORE_RETURNVALUE0(functioncall) functioncall
351#define FUNCTOR_STORE_RETURNVALUE1(functioncall) this->returnedValue_ = functioncall
352
353
354
355#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES##numparams(returnvalue)
356#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>
357#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>
358#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>
359#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>
360#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>
361#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>
362
363#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue
364#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE0 void
365#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE1 R
366
367
368
369#define FUNCTOR_EVALUATE_PARAM(numparams) FUNCTOR_EVALUATE_PARAM##numparams
370#define FUNCTOR_EVALUATE_PARAM0
371#define FUNCTOR_EVALUATE_PARAM1 \
372    if (index == 0) { param.convert<P1>(); }
373#define FUNCTOR_EVALUATE_PARAM2 \
374    if (index == 0) { param.convert<P1>(); } \
375    else if (index == 1) { param.convert<P2>(); }
376#define FUNCTOR_EVALUATE_PARAM3 \
377    if (index == 0) { param.convert<P1>(); } \
378    else if (index == 1) { param.convert<P2>(); } \
379    else if (index == 2) { param.convert<P3>(); }
380#define FUNCTOR_EVALUATE_PARAM4 \
381    if (index == 0) { param.convert<P1>(); } \
382    else if (index == 1) { param.convert<P2>(); } \
383    else if (index == 2) { param.convert<P3>(); } \
384    else if (index == 3) { param.convert<P4>(); }
385#define FUNCTOR_EVALUATE_PARAM5 \
386    if (index == 0) { param.convert<P1>(); } \
387    else if (index == 1) { param.convert<P2>(); } \
388    else if (index == 2) { param.convert<P3>(); } \
389    else if (index == 3) { param.convert<P4>(); } \
390    else if (index == 4) { param.convert<P5>(); }
391
392
393
394
395#define CREATE_STATIC_FUNCTOR(returnvalue, numparams) \
396    FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \
397    class FunctorStatic##returnvalue##numparams : public FunctorStatic \
398    { \
399        public: \
400            FunctorStatic##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
401            { \
402                this->numParams_ = numparams; \
403                this->hasReturnValue_ = returnvalue; \
404                this->type_ = FunctionType::Static; \
405                this->functionPointer_ = functionPointer; \
406                \
407                FUNCTOR_TYPENAME_PARAMS(numparams); \
408                FUNCTOR_TYPENAME_RETURN(returnvalue); \
409            } \
410    \
411            void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \
412            { \
413                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
414            } \
415    \
416            void evaluateParam(unsigned int index, MultiType& param) const \
417            { \
418                FUNCTOR_EVALUATE_PARAM(numparams); \
419            } \
420    \
421            const std::type_info& getHeaderIdentifier() const \
422            { \
423                return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \
424            } \
425    \
426        private: \
427            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
428    }; \
429    \
430    \
431    FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \
432    inline FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
433    { \
434        return new FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams) (functionPointer); \
435    }
436
437
438
439
440
441#define CREATE_MEMBER_FUNCTOR(returnvalue, numparams) \
442    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
443    class FunctorMember##returnvalue##numparams : public FunctorMember<T> \
444    { \
445        public: \
446            FunctorMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
447            { \
448                this->numParams_ = numparams; \
449                this->hasReturnValue_ = returnvalue; \
450                this->type_ = FunctionType::Member; \
451                this->functionPointer_ = functionPointer; \
452            } \
453    \
454            void operator()(T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \
455            { \
456                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
457            } \
458    \
459            void operator()(const T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \
460            { \
461                COUT(1) << "An error occurred in Functor.h:" << std::endl; \
462                COUT(1) << "Error: Function is not const." << std::endl; \
463            } \
464    \
465            void evaluateParam(unsigned int index, MultiType& param) const \
466            { \
467                FUNCTOR_EVALUATE_PARAM(numparams); \
468            } \
469    \
470            const std::type_info& getHeaderIdentifier() const \
471            { \
472                return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \
473            } \
474    \
475        private: \
476            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
477    }; \
478    \
479    \
480    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
481    class FunctorConstMember##returnvalue##numparams : public FunctorMember<T> \
482    { \
483        public: \
484            FunctorConstMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
485            { \
486                this->numParams_ = numparams; \
487                this->hasReturnValue_ = returnvalue; \
488                this->type_ = FunctionType::ConstMember; \
489                this->functionPointer_ = functionPointer; \
490            } \
491    \
492            void operator()(T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \
493            { \
494                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
495            } \
496    \
497            void operator()(const T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \
498            { \
499                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
500            } \
501    \
502            void evaluateParam(unsigned int index, MultiType& param) const \
503            { \
504                FUNCTOR_EVALUATE_PARAM(numparams); \
505            } \
506    \
507            const std::type_info& getHeaderIdentifier() const \
508            { \
509                return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \
510            } \
511    \
512        private: \
513            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)) const; \
514    }; \
515    \
516    \
517    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
518    inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
519    { \
520        return new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
521    } \
522    \
523    \
524    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
525    inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
526    { \
527        return new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
528    } \
529    \
530    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 1) \
531    inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)), O* object) \
532    { \
533        FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* functor = new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
534        functor->setObject(object); \
535        return functor; \
536    } \
537    \
538    \
539    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 1) \
540    inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const, O* object) \
541    { \
542        FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* functor = new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
543        functor->setObject(object); \
544        return functor; \
545    }
546
547
548
549// disable annoying warning about forcing value to boolean
550#ifdef ORXONOX_COMPILER_MSVC
551#pragma warning(push)
552#pragma warning(disable:4100 4800)
553#endif
554
555#define CREATE_ALL_STATIC_FUNCTORS() \
556    CREATE_STATIC_FUNCTOR(0, 0); \
557    CREATE_STATIC_FUNCTOR(0, 1); \
558    CREATE_STATIC_FUNCTOR(0, 2); \
559    CREATE_STATIC_FUNCTOR(0, 3); \
560    CREATE_STATIC_FUNCTOR(0, 4); \
561    CREATE_STATIC_FUNCTOR(0, 5); \
562    CREATE_STATIC_FUNCTOR(1, 0); \
563    CREATE_STATIC_FUNCTOR(1, 1); \
564    CREATE_STATIC_FUNCTOR(1, 2); \
565    CREATE_STATIC_FUNCTOR(1, 3); \
566    CREATE_STATIC_FUNCTOR(1, 4); \
567    CREATE_STATIC_FUNCTOR(1, 5)
568
569
570#define CREATE_ALL_MEMBER_FUNCTORS() \
571    CREATE_MEMBER_FUNCTOR(0, 0); \
572    CREATE_MEMBER_FUNCTOR(0, 1); \
573    CREATE_MEMBER_FUNCTOR(0, 2); \
574    CREATE_MEMBER_FUNCTOR(0, 3); \
575    CREATE_MEMBER_FUNCTOR(0, 4); \
576    CREATE_MEMBER_FUNCTOR(0, 5); \
577    CREATE_MEMBER_FUNCTOR(1, 0); \
578    CREATE_MEMBER_FUNCTOR(1, 1); \
579    CREATE_MEMBER_FUNCTOR(1, 2); \
580    CREATE_MEMBER_FUNCTOR(1, 3); \
581    CREATE_MEMBER_FUNCTOR(1, 4); \
582    CREATE_MEMBER_FUNCTOR(1, 5)
583
584
585    CREATE_ALL_STATIC_FUNCTORS();
586    CREATE_ALL_MEMBER_FUNCTORS();
587}
588
589#ifdef ORXONOX_COMPILER_MSVC
590#pragma warning(pop)
591#endif
592
593#endif /* _Functor_H__ */
Note: See TracBrowser for help on using the repository browser.