Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/Functor.h @ 7196

Last change on this file since 7196 was 7196, checked in by landauf, 14 years ago

Changed implementation of SharedPtr, it's now non-intrusive and uses an allocated counter instead. Hence it's possible to create an instance of SharedPtr<T> even if T is only known from a forward declaration. Also changed some parts of the code to reflect the inheritance of the underlying object pointers without using inheritance in the SharedPtr itself.

  • 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 "FunctorPtr.h"
41
42namespace orxonox
43{
44    const unsigned int MAX_FUNCTOR_ARGUMENTS = 5;
45
46    template <class T>
47    inline std::string typeToString() { return "unknown"; }
48
49#define CreateTypeToStringTemplate(type) \
50    template <> \
51    inline std::string typeToString<type>() { return #type; } \
52    template <> \
53    inline std::string typeToString<type&>() { return #type; } \
54    template <> \
55    inline std::string typeToString<const type>() { return #type; } \
56    template <> \
57    inline std::string typeToString<const type&>() { return #type; }
58
59    CreateTypeToStringTemplate(int);
60    CreateTypeToStringTemplate(unsigned int);
61    CreateTypeToStringTemplate(char);
62    CreateTypeToStringTemplate(unsigned char);
63    CreateTypeToStringTemplate(short);
64    CreateTypeToStringTemplate(unsigned short);
65    CreateTypeToStringTemplate(long);
66    CreateTypeToStringTemplate(unsigned long);
67    CreateTypeToStringTemplate(long long);
68    CreateTypeToStringTemplate(unsigned long long);
69    CreateTypeToStringTemplate(float);
70    CreateTypeToStringTemplate(double);
71    CreateTypeToStringTemplate(long double);
72    CreateTypeToStringTemplate(bool);
73    CreateTypeToStringTemplate(Vector2);
74    CreateTypeToStringTemplate(Vector3);
75    CreateTypeToStringTemplate(Quaternion);
76    CreateTypeToStringTemplate(ColourValue);
77    CreateTypeToStringTemplate(Radian);
78    CreateTypeToStringTemplate(Degree);
79
80    template <>
81    inline std::string typeToString<std::string>() { return "string"; }
82    template <>
83    inline std::string typeToString<std::string&>() { return "string"; }
84    template <>
85    inline std::string typeToString<const std::string>() { return "string"; }
86    template <>
87    inline std::string typeToString<const std::string&>() { return "string"; }
88
89    class _CoreExport Functor
90    {
91        friend class SharedPtr<Functor>;
92
93        public:
94            struct Type
95            {
96                enum Enum
97                {
98                    Member,
99                    ConstMember,
100                    Static,
101                    Lua
102                };
103            };
104
105        public:
106            Functor() { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }
107            virtual ~Functor() { --instances_s; COUT(0) << "functor --: " << instances_s << std::endl; }
108
109            virtual MultiType 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;
110
111            virtual Type::Enum getType() const = 0;
112            virtual unsigned int getParamCount() const = 0;
113            virtual bool hasReturnvalue() const = 0;
114
115            virtual std::string getTypenameParam(unsigned int param) const = 0;
116            virtual std::string getTypenameReturnvalue() const = 0;
117
118            virtual void evaluateParam(unsigned int index, MultiType& param) const = 0;
119
120            virtual void setRawObjectPointer(void* object) {}
121            virtual void* getRawObjectPointer() const { return 0; }
122
123            virtual const std::type_info& getHeaderIdentifier() const = 0;
124
125        private:
126            static int instances_s;
127    };
128
129    class _CoreExport FunctorStatic : public Functor
130    {
131        public:
132            virtual ~FunctorStatic() {}
133            virtual MultiType 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;
134    };
135
136    template <class T>
137    class FunctorMember : public Functor
138    {
139        public:
140            FunctorMember()
141            {
142                this->object_ = 0;
143                this->constObject_ = 0;
144            }
145            virtual ~FunctorMember() {}
146
147            virtual MultiType 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;
148            virtual MultiType 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;
149
150            virtual MultiType 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)
151            {
152                if (this->object_)
153                    return (*this)(this->object_, param1, param2, param3, param4, param5);
154                else if (this->constObject_)
155                    return (*this)(this->constObject_, param1, param2, param3, param4, param5);
156                else
157                {
158                    COUT(1) << "An error occurred in Functor.h:" << std::endl;
159                    COUT(1) << "Error: No object set." << std::endl;
160                    return MT_Type::Null;
161                }
162            }
163
164            inline FunctorMember<T>* setObject(T* object)
165            {
166                this->object_ = object;
167                this->constObject_ = 0;
168                return this;
169            }
170
171            inline FunctorMember<T>* setObject(const T* object)
172            {
173                this->object_ = 0;
174                this->constObject_ = object;
175                return this;
176            }
177
178            void setRawObjectPointer(void* object)
179            {
180                this->object_ = (T*)object;
181                this->constObject_ = 0;
182            }
183
184            void* getRawObjectPointer() const
185            {
186                if (this->object_)
187                    return (void*)this->object_;
188                else
189                    return (void*)this->constObject_;
190            }
191
192            typedef std::pair<T*, const T*> Objects;
193
194            inline Objects getObjects() const
195            {
196                return Objects(this->object_, this->constObject_);
197            }
198
199            inline void setObjects(const Objects& objects)
200            {
201                this->object_ = objects.first;
202                this->constObject_ = objects.second;
203            }
204
205        private:
206            T* object_;
207            const T* constObject_;
208    };
209
210
211
212    template <class R, class P1, class P2, class P3, class P4, class P5>
213    struct FunctorHeaderIdentifier {};
214
215
216
217    inline Functor* createFunctor(Functor* functor)
218    {
219        return functor;
220    }
221
222
223
224#define FUNCTOR_TEMPLATE(ismember, returnvalue, numparams, additionalobject) FUNCTOR_TEMPLATE##ismember##returnvalue##numparams(additionalobject)
225#define FUNCTOR_TEMPLATE000(additionalobject)
226#define FUNCTOR_TEMPLATE001(additionalobject) template <class P1>
227#define FUNCTOR_TEMPLATE002(additionalobject) template <class P1, class P2>
228#define FUNCTOR_TEMPLATE003(additionalobject) template <class P1, class P2, class P3>
229#define FUNCTOR_TEMPLATE004(additionalobject) template <class P1, class P2, class P3, class P4>
230#define FUNCTOR_TEMPLATE005(additionalobject) template <class P1, class P2, class P3, class P4, class P5>
231#define FUNCTOR_TEMPLATE010(additionalobject) template <class R>
232#define FUNCTOR_TEMPLATE011(additionalobject) template <class R, class P1>
233#define FUNCTOR_TEMPLATE012(additionalobject) template <class R, class P1, class P2>
234#define FUNCTOR_TEMPLATE013(additionalobject) template <class R, class P1, class P2, class P3>
235#define FUNCTOR_TEMPLATE014(additionalobject) template <class R, class P1, class P2, class P3, class P4>
236#define FUNCTOR_TEMPLATE015(additionalobject) template <class R, class P1, class P2, class P3, class P4, class P5>
237#define FUNCTOR_TEMPLATE100(additionalobject) template <class T FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
238#define FUNCTOR_TEMPLATE101(additionalobject) template <class T, class P1 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
239#define FUNCTOR_TEMPLATE102(additionalobject) template <class T, class P1, class P2 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
240#define FUNCTOR_TEMPLATE103(additionalobject) template <class T, class P1, class P2, class P3 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
241#define FUNCTOR_TEMPLATE104(additionalobject) template <class T, class P1, class P2, class P3, class P4 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
242#define FUNCTOR_TEMPLATE105(additionalobject) template <class T, class P1, class P2, class P3, class P4, class P5 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
243#define FUNCTOR_TEMPLATE110(additionalobject) template <class T, class R FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
244#define FUNCTOR_TEMPLATE111(additionalobject) template <class T, class R, class P1 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
245#define FUNCTOR_TEMPLATE112(additionalobject) template <class T, class R, class P1, class P2 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
246#define FUNCTOR_TEMPLATE113(additionalobject) template <class T, class R, class P1, class P2, class P3 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
247#define FUNCTOR_TEMPLATE114(additionalobject) template <class T, class R, class P1, class P2, class P3, class P4 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
248#define FUNCTOR_TEMPLATE115(additionalobject) template <class T, class R, class P1, class P2, class P3, class P4, class P5 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) >
249
250
251
252#define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT##additionalobject
253#define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT0
254#define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT1 , class O
255
256
257
258#define FUNCTOR_TEMPLATE_CLASSES(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE_CLASSES##ismember##returnvalue##numparams
259#define FUNCTOR_TEMPLATE_CLASSES000
260#define FUNCTOR_TEMPLATE_CLASSES001 <P1>
261#define FUNCTOR_TEMPLATE_CLASSES002 <P1, P2>
262#define FUNCTOR_TEMPLATE_CLASSES003 <P1, P2, P3>
263#define FUNCTOR_TEMPLATE_CLASSES004 <P1, P2, P3, P4>
264#define FUNCTOR_TEMPLATE_CLASSES005 <P1, P2, P3, P4, P5>
265#define FUNCTOR_TEMPLATE_CLASSES010 <R>
266#define FUNCTOR_TEMPLATE_CLASSES011 <R, P1>
267#define FUNCTOR_TEMPLATE_CLASSES012 <R, P1, P2>
268#define FUNCTOR_TEMPLATE_CLASSES013 <R, P1, P2, P3>
269#define FUNCTOR_TEMPLATE_CLASSES014 <R, P1, P2, P3, P4>
270#define FUNCTOR_TEMPLATE_CLASSES015 <R, P1, P2, P3, P4, P5>
271#define FUNCTOR_TEMPLATE_CLASSES100 <T>
272#define FUNCTOR_TEMPLATE_CLASSES101 <T, P1>
273#define FUNCTOR_TEMPLATE_CLASSES102 <T, P1, P2>
274#define FUNCTOR_TEMPLATE_CLASSES103 <T, P1, P2, P3>
275#define FUNCTOR_TEMPLATE_CLASSES104 <T, P1, P2, P3, P4>
276#define FUNCTOR_TEMPLATE_CLASSES105 <T, P1, P2, P3, P4, P5>
277#define FUNCTOR_TEMPLATE_CLASSES110 <T, R>
278#define FUNCTOR_TEMPLATE_CLASSES111 <T, R, P1>
279#define FUNCTOR_TEMPLATE_CLASSES112 <T, R, P1, P2>
280#define FUNCTOR_TEMPLATE_CLASSES113 <T, R, P1, P2, P3>
281#define FUNCTOR_TEMPLATE_CLASSES114 <T, R, P1, P2, P3, P4>
282#define FUNCTOR_TEMPLATE_CLASSES115 <T, R, P1, P2, P3, P4, P5>
283
284
285
286#define FUNCTOR_TYPENAME_PARAM(numparams) FUNCTOR_TYPENAME_PARAM##numparams
287#define FUNCTOR_TYPENAME_PARAM0 \
288    return BLANKSTRING
289#define FUNCTOR_TYPENAME_PARAM1 \
290    if (param == 0) { return typeToString<P1>(); } \
291    else { return BLANKSTRING; }
292#define FUNCTOR_TYPENAME_PARAM2 \
293    if (param == 0) { return typeToString<P1>(); } \
294    else if (param == 1) { return typeToString<P2>(); } \
295    else { return BLANKSTRING; }
296#define FUNCTOR_TYPENAME_PARAM3 \
297    if (param == 0) { return typeToString<P1>(); } \
298    else if (param == 1) { return typeToString<P2>(); } \
299    else if (param == 2) { return typeToString<P3>(); } \
300    else { return BLANKSTRING; }
301#define FUNCTOR_TYPENAME_PARAM4 \
302    if (param == 0) { return typeToString<P1>(); } \
303    else if (param == 1) { return typeToString<P2>(); } \
304    else if (param == 2) { return typeToString<P3>(); } \
305    else if (param == 3) { return typeToString<P4>(); } \
306    else { return BLANKSTRING; }
307#define FUNCTOR_TYPENAME_PARAM5 \
308    if (param == 0) { return typeToString<P1>(); } \
309    else if (param == 1) { return typeToString<P2>(); } \
310    else if (param == 2) { return typeToString<P3>(); } \
311    else if (param == 3) { return typeToString<P4>(); } \
312    else if (param == 4) { return typeToString<P5>(); } \
313    else { return BLANKSTRING; }
314
315#define FUNCTOR_TYPENAME_RETURN(returnvalue) FUNCTOR_TYPENAME_RETURN##returnvalue
316#define FUNCTOR_TYPENAME_RETURN0 BLANKSTRING
317#define FUNCTOR_TYPENAME_RETURN1 typeToString<R>()
318
319
320
321#define FUNCTOR_FUNCTION_PARAMS(numparams) FUNCTOR_FUNCTION_PARAMS##numparams
322#define FUNCTOR_FUNCTION_PARAMS0
323#define FUNCTOR_FUNCTION_PARAMS1 P1 param1
324#define FUNCTOR_FUNCTION_PARAMS2 P1 param1, P2 param2
325#define FUNCTOR_FUNCTION_PARAMS3 P1 param1, P2 param2, P3 param3
326#define FUNCTOR_FUNCTION_PARAMS4 P1 param1, P2 param2, P3 param3, P4 param4
327#define FUNCTOR_FUNCTION_PARAMS5 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5
328
329#define FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) FUNCTOR_FUNCTION_RETURNVALUE##returnvalue
330#define FUNCTOR_FUNCTION_RETURNVALUE0 void
331#define FUNCTOR_FUNCTION_RETURNVALUE1 R
332
333
334
335#define FUNCTOR_FUNCTION_CALL(numparams) FUNCTOR_FUNCTION_CALL##numparams
336#define FUNCTOR_FUNCTION_CALL0
337#define FUNCTOR_FUNCTION_CALL1 param1
338#define FUNCTOR_FUNCTION_CALL2 param1, param2
339#define FUNCTOR_FUNCTION_CALL3 param1, param2, param3
340#define FUNCTOR_FUNCTION_CALL4 param1, param2, param3, param4
341#define FUNCTOR_FUNCTION_CALL5 param1, param2, param3, param4, param5
342
343#define FUNCTOR_STORE_RETURNVALUE(returnvalue, functioncall) FUNCTOR_STORE_RETURNVALUE##returnvalue(functioncall)
344#define FUNCTOR_STORE_RETURNVALUE0(functioncall) functioncall; return MT_Type::Null
345#define FUNCTOR_STORE_RETURNVALUE1(functioncall) return functioncall
346
347
348
349#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES##numparams(returnvalue)
350#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>
351#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>
352#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>
353#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>
354#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>
355#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>
356
357#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue
358#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE0 void
359#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE1 R
360
361
362
363#define FUNCTOR_EVALUATE_PARAM(numparams) FUNCTOR_EVALUATE_PARAM##numparams
364#define FUNCTOR_EVALUATE_PARAM0
365#define FUNCTOR_EVALUATE_PARAM1 \
366    if (index == 0) { param.convert<P1>(); }
367#define FUNCTOR_EVALUATE_PARAM2 \
368    if (index == 0) { param.convert<P1>(); } \
369    else if (index == 1) { param.convert<P2>(); }
370#define FUNCTOR_EVALUATE_PARAM3 \
371    if (index == 0) { param.convert<P1>(); } \
372    else if (index == 1) { param.convert<P2>(); } \
373    else if (index == 2) { param.convert<P3>(); }
374#define FUNCTOR_EVALUATE_PARAM4 \
375    if (index == 0) { param.convert<P1>(); } \
376    else if (index == 1) { param.convert<P2>(); } \
377    else if (index == 2) { param.convert<P3>(); } \
378    else if (index == 3) { param.convert<P4>(); }
379#define FUNCTOR_EVALUATE_PARAM5 \
380    if (index == 0) { param.convert<P1>(); } \
381    else if (index == 1) { param.convert<P2>(); } \
382    else if (index == 2) { param.convert<P3>(); } \
383    else if (index == 3) { param.convert<P4>(); } \
384    else if (index == 4) { param.convert<P5>(); }
385
386
387
388
389#define CREATE_STATIC_FUNCTOR(returnvalue, numparams) \
390    FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \
391    class FunctorStatic##returnvalue##numparams : public FunctorStatic \
392    { \
393        public: \
394            FunctorStatic##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
395            { \
396                this->functionPointer_ = functionPointer; \
397            } \
398    \
399            MultiType 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) \
400            { \
401                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
402            } \
403    \
404            void evaluateParam(unsigned int index, MultiType& param) const \
405            { \
406                FUNCTOR_EVALUATE_PARAM(numparams); \
407            } \
408    \
409            Functor::Type::Enum getType() const { return Functor::Type::Static; } \
410            unsigned int getParamCount() const { return numparams; } \
411            bool hasReturnvalue() const { return returnvalue; } \
412            std::string getTypenameParam(unsigned int param) const { FUNCTOR_TYPENAME_PARAM(numparams); } \
413            std::string getTypenameReturnvalue() const { return FUNCTOR_TYPENAME_RETURN(returnvalue); } \
414    \
415            const std::type_info& getHeaderIdentifier() const \
416            { \
417                return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \
418            } \
419    \
420        private: \
421            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
422    }; \
423    \
424    \
425    FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \
426    inline FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
427    { \
428        return new FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams) (functionPointer); \
429    }
430
431
432
433
434
435#define CREATE_MEMBER_FUNCTOR(returnvalue, numparams) \
436    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
437    class FunctorMember##returnvalue##numparams : public FunctorMember<T> \
438    { \
439        public: \
440            FunctorMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
441            { \
442                this->functionPointer_ = functionPointer; \
443            } \
444    \
445            MultiType 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) \
446            { \
447                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
448            } \
449    \
450            MultiType 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) \
451            { \
452                COUT(1) << "An error occurred in Functor.h:" << std::endl; \
453                COUT(1) << "Error: Function is not const." << std::endl; \
454                return MT_Type::Null; \
455            } \
456    \
457            void evaluateParam(unsigned int index, MultiType& param) const \
458            { \
459                FUNCTOR_EVALUATE_PARAM(numparams); \
460            } \
461    \
462            Functor::Type::Enum getType() const { return Functor::Type::Member; } \
463            unsigned int getParamCount() const { return numparams; } \
464            bool hasReturnvalue() const { return returnvalue; } \
465            std::string getTypenameParam(unsigned int param) const { FUNCTOR_TYPENAME_PARAM(numparams); } \
466            std::string getTypenameReturnvalue() const { return FUNCTOR_TYPENAME_RETURN(returnvalue); } \
467    \
468            const std::type_info& getHeaderIdentifier() const \
469            { \
470                return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \
471            } \
472    \
473        private: \
474            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
475    }; \
476    \
477    \
478    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
479    class FunctorConstMember##returnvalue##numparams : public FunctorMember<T> \
480    { \
481        public: \
482            FunctorConstMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
483            { \
484                this->functionPointer_ = functionPointer; \
485            } \
486    \
487            MultiType 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) \
488            { \
489                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
490            } \
491    \
492            MultiType 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) \
493            { \
494                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
495            } \
496    \
497            void evaluateParam(unsigned int index, MultiType& param) const \
498            { \
499                FUNCTOR_EVALUATE_PARAM(numparams); \
500            } \
501    \
502            Functor::Type::Enum getType() const { return Functor::Type::ConstMember; } \
503            unsigned int getParamCount() const { return numparams; } \
504            bool hasReturnvalue() const { return returnvalue; } \
505            std::string getTypenameParam(unsigned int param) const { FUNCTOR_TYPENAME_PARAM(numparams); } \
506            std::string getTypenameReturnvalue() const { return FUNCTOR_TYPENAME_RETURN(returnvalue); } \
507    \
508            const std::type_info& getHeaderIdentifier() const \
509            { \
510                return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \
511            } \
512    \
513        private: \
514            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)) const; \
515    }; \
516    \
517    \
518    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
519    inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
520    { \
521        return new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
522    } \
523    \
524    \
525    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \
526    inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
527    { \
528        return new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
529    } \
530    \
531    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 1) \
532    inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)), O* object) \
533    { \
534        FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* functor = new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
535        functor->setObject(object); \
536        return functor; \
537    } \
538    \
539    \
540    FUNCTOR_TEMPLATE(1, returnvalue, numparams, 1) \
541    inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const, O* object) \
542    { \
543        FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* functor = new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
544        functor->setObject(object); \
545        return functor; \
546    }
547
548
549
550// disable annoying warning about forcing value to boolean
551#ifdef ORXONOX_COMPILER_MSVC
552#pragma warning(push)
553#pragma warning(disable:4100 4800)
554#endif
555
556#define CREATE_ALL_STATIC_FUNCTORS() \
557    CREATE_STATIC_FUNCTOR(0, 0); \
558    CREATE_STATIC_FUNCTOR(0, 1); \
559    CREATE_STATIC_FUNCTOR(0, 2); \
560    CREATE_STATIC_FUNCTOR(0, 3); \
561    CREATE_STATIC_FUNCTOR(0, 4); \
562    CREATE_STATIC_FUNCTOR(0, 5); \
563    CREATE_STATIC_FUNCTOR(1, 0); \
564    CREATE_STATIC_FUNCTOR(1, 1); \
565    CREATE_STATIC_FUNCTOR(1, 2); \
566    CREATE_STATIC_FUNCTOR(1, 3); \
567    CREATE_STATIC_FUNCTOR(1, 4); \
568    CREATE_STATIC_FUNCTOR(1, 5)
569
570
571#define CREATE_ALL_MEMBER_FUNCTORS() \
572    CREATE_MEMBER_FUNCTOR(0, 0); \
573    CREATE_MEMBER_FUNCTOR(0, 1); \
574    CREATE_MEMBER_FUNCTOR(0, 2); \
575    CREATE_MEMBER_FUNCTOR(0, 3); \
576    CREATE_MEMBER_FUNCTOR(0, 4); \
577    CREATE_MEMBER_FUNCTOR(0, 5); \
578    CREATE_MEMBER_FUNCTOR(1, 0); \
579    CREATE_MEMBER_FUNCTOR(1, 1); \
580    CREATE_MEMBER_FUNCTOR(1, 2); \
581    CREATE_MEMBER_FUNCTOR(1, 3); \
582    CREATE_MEMBER_FUNCTOR(1, 4); \
583    CREATE_MEMBER_FUNCTOR(1, 5)
584
585
586    CREATE_ALL_STATIC_FUNCTORS();
587    CREATE_ALL_MEMBER_FUNCTORS();
588}
589
590#ifdef ORXONOX_COMPILER_MSVC
591#pragma warning(pop)
592#endif
593
594#endif /* _Functor_H__ */
Note: See TracBrowser for help on using the repository browser.