Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Functor.h @ 947

Last change on this file since 947 was 947, checked in by landauf, 16 years ago
  • added CommandExecutor
  • added ConsoleCommand macros
  • added getTypename to all MultiTypes
  • added 2 static maps to Identifier that contain all existing Identifiers with their names and lowercase names respectively.
  • added 2 maps to each Identifier that contain all console commands of the Identifier with their names and lowercase names respectively
  • using tolower(.) and toupper(.) instead of selfmade hacks in String.h
  • added AccessLevel enum
  • added some test-console-commands to OutputHandler, Ambient and SpaceShip
File size: 18.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 *   Inspiration: Functor by Benjamin Grauer
27 */
28
29#ifndef _Functor_H__
30#define _Functor_H__
31
32#include "util/MultiTypeMath.h"
33#include "Debug.h"
34
35#include "CorePrereqs.h"
36
37#define MAX_FUNCTOR_ARGUMENTS 5
38
39namespace orxonox
40{
41    enum FunctionType
42    {
43        FT_MEMBER,
44        FT_CONSTMEMBER,
45        FT_STATIC
46    };
47
48
49    template <class T>
50    inline std::string typeToString() { return "unknown"; }
51
52#define CreateTypeToStringTemplate(type) \
53    template <> \
54    inline std::string typeToString<type>() { return #type; }
55
56    CreateTypeToStringTemplate(int);
57    CreateTypeToStringTemplate(unsigned int);
58    CreateTypeToStringTemplate(char);
59    CreateTypeToStringTemplate(unsigned char);
60    CreateTypeToStringTemplate(short);
61    CreateTypeToStringTemplate(unsigned short);
62    CreateTypeToStringTemplate(long);
63    CreateTypeToStringTemplate(unsigned long);
64    CreateTypeToStringTemplate(float);
65    CreateTypeToStringTemplate(double);
66    CreateTypeToStringTemplate(long double);
67    CreateTypeToStringTemplate(bool);
68    CreateTypeToStringTemplate(std::string);
69    CreateTypeToStringTemplate(orxonox::Vector2);
70    CreateTypeToStringTemplate(orxonox::Vector3);
71    CreateTypeToStringTemplate(orxonox::Quaternion);
72    CreateTypeToStringTemplate(orxonox::ColourValue);
73    CreateTypeToStringTemplate(orxonox::Radian);
74    CreateTypeToStringTemplate(orxonox::Degree);
75
76
77    class _CoreExport Functor
78    {
79        public:
80            Functor() {}
81            virtual ~Functor() {}
82
83            virtual void operator()(const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) = 0;
84
85            inline unsigned int getParamCount() const { return this->numParams_; }
86            inline bool hasReturnvalue() const { return this->hasReturnValue_; }
87            inline FunctionType getType() const { return this->type_; }
88            inline MultiTypeMath getReturnvalue() const { return this->returnedValue_; }
89
90            std::string getTypenameParam(unsigned int param) const { return (param > 0 && param <= 5) ? this->typeParam_[param-1] : ""; }
91            std::string getTypenameReturnvalue() const { return this->typeReturnvalue_; }
92
93        protected:
94            unsigned int numParams_;
95            bool hasReturnValue_;
96            FunctionType type_;
97            MultiTypeMath returnedValue_;
98
99            std::string typeReturnvalue_;
100            std::string typeParam_[MAX_FUNCTOR_ARGUMENTS];
101    };
102
103    class _CoreExport FunctorStatic : public Functor
104    {
105        public:
106            virtual ~FunctorStatic() {}
107            virtual void operator()(const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) = 0;
108    };
109
110    template <class T>
111    class FunctorMember : public Functor
112    {
113        public:
114            FunctorMember()
115            {
116                constObject_ = 0;
117                object_ = 0;
118                bConstObject_ = false;
119            }
120            virtual ~FunctorMember() {}
121
122            virtual void operator()(T* object, const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) = 0;
123            virtual void operator()(const T* object, const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) = 0;
124
125            virtual void operator()(const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null)
126            {
127                if (this->bConstObject_)
128                {
129                    if (this->constObject_)
130                        (*this)(this->constObject_, param1, param2, param3, param4, param5);
131                    else
132                    {
133                        COUT(1) << "An error occurred in Functor.h:" << std::endl;
134                        COUT(1) << "Error: No const object set." << std::endl;
135                    }
136                }
137                else
138                {
139                    if (this->object_)
140                        (*this)(this->object_, param1, param2, param3, param4, param5);
141                    else
142                    {
143                        COUT(1) << "An error occurred in Functor.h:" << std::endl;
144                        COUT(1) << "Error: No object set." << std::endl;
145                    }
146                }
147            }
148
149            void setObject(T* object)
150            {
151                this->bConstObject_ = false;
152                this->object_ = object;
153            }
154
155            void setObject(const T* object)
156            {
157                this->bConstObject_ = true;
158                this->constObject_ = object;
159            }
160
161        private:
162            const T* constObject_;
163            T* object_;
164            bool bConstObject_;
165    };
166
167
168
169#define FUNCTOR_TEMPLATE(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE##ismember##returnvalue##numparams
170#define FUNCTOR_TEMPLATE000
171#define FUNCTOR_TEMPLATE001 template <class P1>
172#define FUNCTOR_TEMPLATE002 template <class P1, class P2>
173#define FUNCTOR_TEMPLATE003 template <class P1, class P2, class P3>
174#define FUNCTOR_TEMPLATE004 template <class P1, class P2, class P3, class P4>
175#define FUNCTOR_TEMPLATE005 template <class P1, class P2, class P3, class P4, class P5>
176#define FUNCTOR_TEMPLATE010 template <class R>
177#define FUNCTOR_TEMPLATE011 template <class R, class P1>
178#define FUNCTOR_TEMPLATE012 template <class R, class P1, class P2>
179#define FUNCTOR_TEMPLATE013 template <class R, class P1, class P2, class P3>
180#define FUNCTOR_TEMPLATE014 template <class R, class P1, class P2, class P3, class P4>
181#define FUNCTOR_TEMPLATE015 template <class R, class P1, class P2, class P3, class P4, class P5>
182#define FUNCTOR_TEMPLATE100 template <class T>
183#define FUNCTOR_TEMPLATE101 template <class T, class P1>
184#define FUNCTOR_TEMPLATE102 template <class T, class P1, class P2>
185#define FUNCTOR_TEMPLATE103 template <class T, class P1, class P2, class P3>
186#define FUNCTOR_TEMPLATE104 template <class T, class P1, class P2, class P3, class P4>
187#define FUNCTOR_TEMPLATE105 template <class T, class P1, class P2, class P3, class P4, class P5>
188#define FUNCTOR_TEMPLATE110 template <class T, class R>
189#define FUNCTOR_TEMPLATE111 template <class T, class R, class P1>
190#define FUNCTOR_TEMPLATE112 template <class T, class R, class P1, class P2>
191#define FUNCTOR_TEMPLATE113 template <class T, class R, class P1, class P2, class P3>
192#define FUNCTOR_TEMPLATE114 template <class T, class R, class P1, class P2, class P3, class P4>
193#define FUNCTOR_TEMPLATE115 template <class T, class R, class P1, class P2, class P3, class P4, class P5>
194
195
196
197#define FUNCTOR_TEMPLATE_CLASSES(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE_CLASSES##ismember##returnvalue##numparams
198#define FUNCTOR_TEMPLATE_CLASSES000
199#define FUNCTOR_TEMPLATE_CLASSES001 <P1>
200#define FUNCTOR_TEMPLATE_CLASSES002 <P1, P2>
201#define FUNCTOR_TEMPLATE_CLASSES003 <P1, P2, P3>
202#define FUNCTOR_TEMPLATE_CLASSES004 <P1, P2, P3, P4>
203#define FUNCTOR_TEMPLATE_CLASSES005 <P1, P2, P3, P4, P5>
204#define FUNCTOR_TEMPLATE_CLASSES010 <R>
205#define FUNCTOR_TEMPLATE_CLASSES011 <R, P1>
206#define FUNCTOR_TEMPLATE_CLASSES012 <R, P1, P2>
207#define FUNCTOR_TEMPLATE_CLASSES013 <R, P1, P2, P3>
208#define FUNCTOR_TEMPLATE_CLASSES014 <R, P1, P2, P3, P4>
209#define FUNCTOR_TEMPLATE_CLASSES015 <R, P1, P2, P3, P4, P5>
210#define FUNCTOR_TEMPLATE_CLASSES100 <T>
211#define FUNCTOR_TEMPLATE_CLASSES101 <T, P1>
212#define FUNCTOR_TEMPLATE_CLASSES102 <T, P1, P2>
213#define FUNCTOR_TEMPLATE_CLASSES103 <T, P1, P2, P3>
214#define FUNCTOR_TEMPLATE_CLASSES104 <T, P1, P2, P3, P4>
215#define FUNCTOR_TEMPLATE_CLASSES105 <T, P1, P2, P3, P4, P5>
216#define FUNCTOR_TEMPLATE_CLASSES110 <T, R>
217#define FUNCTOR_TEMPLATE_CLASSES111 <T, R, P1>
218#define FUNCTOR_TEMPLATE_CLASSES112 <T, R, P1, P2>
219#define FUNCTOR_TEMPLATE_CLASSES113 <T, R, P1, P2, P3>
220#define FUNCTOR_TEMPLATE_CLASSES114 <T, R, P1, P2, P3, P4>
221#define FUNCTOR_TEMPLATE_CLASSES115 <T, R, P1, P2, P3, P4, P5>
222
223
224
225#define FUNCTOR_TYPENAME_PARAMS(numparams) FUNCTOR_TYPENAME_PARAMS##numparams
226#define FUNCTOR_TYPENAME_PARAMS0
227#define FUNCTOR_TYPENAME_PARAMS1 this->typeParam_[0] = typeToString<P1>();
228#define FUNCTOR_TYPENAME_PARAMS2 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>();
229#define FUNCTOR_TYPENAME_PARAMS3 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>();
230#define FUNCTOR_TYPENAME_PARAMS4 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>(); this->typeParam_[3] = typeToString<P4>();
231#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>();
232
233#define FUNCTOR_TYPENAME_RETURN(returnvalue) FUNCTOR_TYPENAME_RETURN##returnvalue
234#define FUNCTOR_TYPENAME_RETURN0
235#define FUNCTOR_TYPENAME_RETURN1 this->typeReturnvalue_ = typeToString<R>();
236
237
238
239#define FUNCTOR_FUNCTION_PARAMS(numparams) FUNCTOR_FUNCTION_PARAMS##numparams
240#define FUNCTOR_FUNCTION_PARAMS0
241#define FUNCTOR_FUNCTION_PARAMS1 P1 param1
242#define FUNCTOR_FUNCTION_PARAMS2 P1 param1, P2 param2
243#define FUNCTOR_FUNCTION_PARAMS3 P1 param1, P2 param2, P3 param3
244#define FUNCTOR_FUNCTION_PARAMS4 P1 param1, P2 param2, P3 param3, P4 param4
245#define FUNCTOR_FUNCTION_PARAMS5 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5
246
247#define FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) FUNCTOR_FUNCTION_RETURNVALUE##returnvalue
248#define FUNCTOR_FUNCTION_RETURNVALUE0 void
249#define FUNCTOR_FUNCTION_RETURNVALUE1 R
250
251
252
253#define FUNCTOR_FUNCTION_CALL(numparams) FUNCTOR_FUNCTION_CALL##numparams
254#define FUNCTOR_FUNCTION_CALL0
255#define FUNCTOR_FUNCTION_CALL1 param1
256#define FUNCTOR_FUNCTION_CALL2 param1, param2
257#define FUNCTOR_FUNCTION_CALL3 param1, param2, param3
258#define FUNCTOR_FUNCTION_CALL4 param1, param2, param3, param4
259#define FUNCTOR_FUNCTION_CALL5 param1, param2, param3, param4, param5
260
261#define FUNCTOR_STORE_RETURNVALUE(returnvalue, functioncall) FUNCTOR_STORE_RETURNVALUE##returnvalue(functioncall)
262#define FUNCTOR_STORE_RETURNVALUE0(functioncall) functioncall
263#define FUNCTOR_STORE_RETURNVALUE1(functioncall) this->returnedValue_ = functioncall
264
265
266
267
268
269#define CREATE_STATIC_FUNCTOR(returnvalue, numparams) \
270    FUNCTOR_TEMPLATE(0, returnvalue, numparams) \
271    class FunctorStatic##returnvalue##numparams : public FunctorStatic \
272    { \
273        public: \
274            FunctorStatic##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
275            { \
276                this->numParams_ = numparams; \
277                this->hasReturnValue_ = returnvalue; \
278                this->type_ = FT_STATIC; \
279                this->functionPointer_ = functionPointer; \
280                \
281                FUNCTOR_TYPENAME_PARAMS(numparams); \
282                FUNCTOR_TYPENAME_RETURN(returnvalue); \
283            } \
284    \
285            void operator()(const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) \
286            { \
287                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
288            } \
289    \
290        private: \
291            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
292    }; \
293    \
294    \
295    FUNCTOR_TEMPLATE(0, returnvalue, numparams) \
296    inline FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
297    { \
298        return new FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams) (functionPointer); \
299    }
300
301
302
303
304
305#define CREATE_MEMBER_FUNCTOR(returnvalue, numparams) \
306    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
307    class FunctorMember##returnvalue##numparams : public FunctorMember<T> \
308    { \
309        public: \
310            FunctorMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
311            { \
312                this->numParams_ = numparams; \
313                this->hasReturnValue_ = returnvalue; \
314                this->type_ = FT_MEMBER; \
315                this->functionPointer_ = functionPointer; \
316            } \
317    \
318            void operator()(T* object, const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) \
319            { \
320                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
321            } \
322    \
323            void operator()(const T* object, const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) \
324            { \
325                COUT(1) << "An error occurred in Functor.h:" << std::endl; \
326                COUT(1) << "Error: Function is not const." << std::endl; \
327            } \
328    \
329        private: \
330            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
331    }; \
332    \
333    \
334    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
335    class FunctorConstMember##returnvalue##numparams : public FunctorMember<T> \
336    { \
337        public: \
338            FunctorConstMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
339            { \
340                this->numParams_ = numparams; \
341                this->hasReturnValue_ = returnvalue; \
342                this->type_ = FT_CONSTMEMBER; \
343                this->functionPointer_ = functionPointer; \
344            } \
345    \
346            void operator()(T* object, const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) \
347            { \
348                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
349            } \
350    \
351            void operator()(const T* object, const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) \
352            { \
353                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
354            } \
355    \
356        private: \
357            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)) const; \
358    }; \
359    \
360    \
361    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
362    inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
363    { \
364        return new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
365    } \
366    \
367    \
368    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
369    inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
370    { \
371        return new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
372    }
373
374
375
376
377#define CREATE_ALL_STATIC_FUNCTORS() \
378    CREATE_STATIC_FUNCTOR(0, 0); \
379    CREATE_STATIC_FUNCTOR(0, 1); \
380    CREATE_STATIC_FUNCTOR(0, 2); \
381    CREATE_STATIC_FUNCTOR(0, 3); \
382    CREATE_STATIC_FUNCTOR(0, 4); \
383    CREATE_STATIC_FUNCTOR(0, 5); \
384    CREATE_STATIC_FUNCTOR(1, 0); \
385    CREATE_STATIC_FUNCTOR(1, 1); \
386    CREATE_STATIC_FUNCTOR(1, 2); \
387    CREATE_STATIC_FUNCTOR(1, 3); \
388    CREATE_STATIC_FUNCTOR(1, 4); \
389    CREATE_STATIC_FUNCTOR(1, 5)
390
391
392#define CREATE_ALL_MEMBER_FUNCTORS() \
393    CREATE_MEMBER_FUNCTOR(0, 0); \
394    CREATE_MEMBER_FUNCTOR(0, 1); \
395    CREATE_MEMBER_FUNCTOR(0, 2); \
396    CREATE_MEMBER_FUNCTOR(0, 3); \
397    CREATE_MEMBER_FUNCTOR(0, 4); \
398    CREATE_MEMBER_FUNCTOR(0, 5); \
399    CREATE_MEMBER_FUNCTOR(1, 0); \
400    CREATE_MEMBER_FUNCTOR(1, 1); \
401    CREATE_MEMBER_FUNCTOR(1, 2); \
402    CREATE_MEMBER_FUNCTOR(1, 3); \
403    CREATE_MEMBER_FUNCTOR(1, 4); \
404    CREATE_MEMBER_FUNCTOR(1, 5)
405
406
407    CREATE_ALL_STATIC_FUNCTORS();
408    CREATE_ALL_MEMBER_FUNCTORS();
409}
410
411#endif /* _Functor_H__ */
Note: See TracBrowser for help on using the repository browser.