Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 955 was 955, checked in by landauf, 16 years ago
  • added input buffer: this class captures key-input (at the moment it's using OIS directly, later it will use the InputHandler) and writes it into a string - other classes can listen to changes and can read and modify the string.
  • fixed some bugs in CommandExecutor
  • fixed a small bug (or changed a questionable feature) in Functor
File size: 18.9 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    template <> \
56    inline std::string typeToString<type&>() { return #type; } \
57    template <> \
58    inline std::string typeToString<const type>() { return #type; } \
59    template <> \
60    inline std::string typeToString<const type&>() { return #type; }
61
62    CreateTypeToStringTemplate(int);
63    CreateTypeToStringTemplate(unsigned int);
64    CreateTypeToStringTemplate(char);
65    CreateTypeToStringTemplate(unsigned char);
66    CreateTypeToStringTemplate(short);
67    CreateTypeToStringTemplate(unsigned short);
68    CreateTypeToStringTemplate(long);
69    CreateTypeToStringTemplate(unsigned long);
70    CreateTypeToStringTemplate(float);
71    CreateTypeToStringTemplate(double);
72    CreateTypeToStringTemplate(long double);
73    CreateTypeToStringTemplate(bool);
74    CreateTypeToStringTemplate(Vector2);
75    CreateTypeToStringTemplate(Vector3);
76    CreateTypeToStringTemplate(Quaternion);
77    CreateTypeToStringTemplate(ColourValue);
78    CreateTypeToStringTemplate(Radian);
79    CreateTypeToStringTemplate(Degree);
80
81    template <> \
82    inline std::string typeToString<std::string>() { return "string"; } \
83    template <> \
84    inline std::string typeToString<std::string&>() { return "string"; } \
85    template <> \
86    inline std::string typeToString<const std::string>() { return "string"; } \
87    template <> \
88    inline std::string typeToString<const std::string&>() { return "string"; }
89
90    class _CoreExport Functor
91    {
92        public:
93            Functor() {}
94            virtual ~Functor() {}
95
96            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;
97
98            inline unsigned int getParamCount() const { return this->numParams_; }
99            inline bool hasReturnvalue() const { return this->hasReturnValue_; }
100            inline FunctionType getType() const { return this->type_; }
101            inline MultiTypeMath getReturnvalue() const { return this->returnedValue_; }
102
103            std::string getTypenameParam(unsigned int param) const { return (param >= 0 && param < 5) ? this->typeParam_[param] : ""; }
104            std::string getTypenameReturnvalue() const { return this->typeReturnvalue_; }
105
106        protected:
107            unsigned int numParams_;
108            bool hasReturnValue_;
109            FunctionType type_;
110            MultiTypeMath returnedValue_;
111
112            std::string typeReturnvalue_;
113            std::string typeParam_[MAX_FUNCTOR_ARGUMENTS];
114    };
115
116    class _CoreExport FunctorStatic : public Functor
117    {
118        public:
119            virtual ~FunctorStatic() {}
120            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;
121    };
122
123    template <class T>
124    class FunctorMember : public Functor
125    {
126        public:
127            FunctorMember()
128            {
129                constObject_ = 0;
130                object_ = 0;
131                bConstObject_ = false;
132            }
133            virtual ~FunctorMember() {}
134
135            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;
136            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;
137
138            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)
139            {
140                if (this->bConstObject_)
141                {
142                    if (this->constObject_)
143                        (*this)(this->constObject_, param1, param2, param3, param4, param5);
144                    else
145                    {
146                        COUT(1) << "An error occurred in Functor.h:" << std::endl;
147                        COUT(1) << "Error: No const object set." << std::endl;
148                    }
149                }
150                else
151                {
152                    if (this->object_)
153                        (*this)(this->object_, param1, param2, param3, param4, param5);
154                    else
155                    {
156                        COUT(1) << "An error occurred in Functor.h:" << std::endl;
157                        COUT(1) << "Error: No object set." << std::endl;
158                    }
159                }
160            }
161
162            void setObject(T* object)
163            {
164                this->bConstObject_ = false;
165                this->object_ = object;
166            }
167
168            void setObject(const T* object)
169            {
170                this->bConstObject_ = true;
171                this->constObject_ = object;
172            }
173
174        private:
175            const T* constObject_;
176            T* object_;
177            bool bConstObject_;
178    };
179
180
181
182#define FUNCTOR_TEMPLATE(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE##ismember##returnvalue##numparams
183#define FUNCTOR_TEMPLATE000
184#define FUNCTOR_TEMPLATE001 template <class P1>
185#define FUNCTOR_TEMPLATE002 template <class P1, class P2>
186#define FUNCTOR_TEMPLATE003 template <class P1, class P2, class P3>
187#define FUNCTOR_TEMPLATE004 template <class P1, class P2, class P3, class P4>
188#define FUNCTOR_TEMPLATE005 template <class P1, class P2, class P3, class P4, class P5>
189#define FUNCTOR_TEMPLATE010 template <class R>
190#define FUNCTOR_TEMPLATE011 template <class R, class P1>
191#define FUNCTOR_TEMPLATE012 template <class R, class P1, class P2>
192#define FUNCTOR_TEMPLATE013 template <class R, class P1, class P2, class P3>
193#define FUNCTOR_TEMPLATE014 template <class R, class P1, class P2, class P3, class P4>
194#define FUNCTOR_TEMPLATE015 template <class R, class P1, class P2, class P3, class P4, class P5>
195#define FUNCTOR_TEMPLATE100 template <class T>
196#define FUNCTOR_TEMPLATE101 template <class T, class P1>
197#define FUNCTOR_TEMPLATE102 template <class T, class P1, class P2>
198#define FUNCTOR_TEMPLATE103 template <class T, class P1, class P2, class P3>
199#define FUNCTOR_TEMPLATE104 template <class T, class P1, class P2, class P3, class P4>
200#define FUNCTOR_TEMPLATE105 template <class T, class P1, class P2, class P3, class P4, class P5>
201#define FUNCTOR_TEMPLATE110 template <class T, class R>
202#define FUNCTOR_TEMPLATE111 template <class T, class R, class P1>
203#define FUNCTOR_TEMPLATE112 template <class T, class R, class P1, class P2>
204#define FUNCTOR_TEMPLATE113 template <class T, class R, class P1, class P2, class P3>
205#define FUNCTOR_TEMPLATE114 template <class T, class R, class P1, class P2, class P3, class P4>
206#define FUNCTOR_TEMPLATE115 template <class T, class R, class P1, class P2, class P3, class P4, class P5>
207
208
209
210#define FUNCTOR_TEMPLATE_CLASSES(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE_CLASSES##ismember##returnvalue##numparams
211#define FUNCTOR_TEMPLATE_CLASSES000
212#define FUNCTOR_TEMPLATE_CLASSES001 <P1>
213#define FUNCTOR_TEMPLATE_CLASSES002 <P1, P2>
214#define FUNCTOR_TEMPLATE_CLASSES003 <P1, P2, P3>
215#define FUNCTOR_TEMPLATE_CLASSES004 <P1, P2, P3, P4>
216#define FUNCTOR_TEMPLATE_CLASSES005 <P1, P2, P3, P4, P5>
217#define FUNCTOR_TEMPLATE_CLASSES010 <R>
218#define FUNCTOR_TEMPLATE_CLASSES011 <R, P1>
219#define FUNCTOR_TEMPLATE_CLASSES012 <R, P1, P2>
220#define FUNCTOR_TEMPLATE_CLASSES013 <R, P1, P2, P3>
221#define FUNCTOR_TEMPLATE_CLASSES014 <R, P1, P2, P3, P4>
222#define FUNCTOR_TEMPLATE_CLASSES015 <R, P1, P2, P3, P4, P5>
223#define FUNCTOR_TEMPLATE_CLASSES100 <T>
224#define FUNCTOR_TEMPLATE_CLASSES101 <T, P1>
225#define FUNCTOR_TEMPLATE_CLASSES102 <T, P1, P2>
226#define FUNCTOR_TEMPLATE_CLASSES103 <T, P1, P2, P3>
227#define FUNCTOR_TEMPLATE_CLASSES104 <T, P1, P2, P3, P4>
228#define FUNCTOR_TEMPLATE_CLASSES105 <T, P1, P2, P3, P4, P5>
229#define FUNCTOR_TEMPLATE_CLASSES110 <T, R>
230#define FUNCTOR_TEMPLATE_CLASSES111 <T, R, P1>
231#define FUNCTOR_TEMPLATE_CLASSES112 <T, R, P1, P2>
232#define FUNCTOR_TEMPLATE_CLASSES113 <T, R, P1, P2, P3>
233#define FUNCTOR_TEMPLATE_CLASSES114 <T, R, P1, P2, P3, P4>
234#define FUNCTOR_TEMPLATE_CLASSES115 <T, R, P1, P2, P3, P4, P5>
235
236
237
238#define FUNCTOR_TYPENAME_PARAMS(numparams) FUNCTOR_TYPENAME_PARAMS##numparams
239#define FUNCTOR_TYPENAME_PARAMS0
240#define FUNCTOR_TYPENAME_PARAMS1 this->typeParam_[0] = typeToString<P1>();
241#define FUNCTOR_TYPENAME_PARAMS2 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>();
242#define FUNCTOR_TYPENAME_PARAMS3 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>();
243#define FUNCTOR_TYPENAME_PARAMS4 this->typeParam_[0] = typeToString<P1>(); this->typeParam_[1] = typeToString<P2>(); this->typeParam_[2] = typeToString<P3>(); this->typeParam_[3] = typeToString<P4>();
244#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>();
245
246#define FUNCTOR_TYPENAME_RETURN(returnvalue) FUNCTOR_TYPENAME_RETURN##returnvalue
247#define FUNCTOR_TYPENAME_RETURN0
248#define FUNCTOR_TYPENAME_RETURN1 this->typeReturnvalue_ = typeToString<R>();
249
250
251
252#define FUNCTOR_FUNCTION_PARAMS(numparams) FUNCTOR_FUNCTION_PARAMS##numparams
253#define FUNCTOR_FUNCTION_PARAMS0
254#define FUNCTOR_FUNCTION_PARAMS1 P1 param1
255#define FUNCTOR_FUNCTION_PARAMS2 P1 param1, P2 param2
256#define FUNCTOR_FUNCTION_PARAMS3 P1 param1, P2 param2, P3 param3
257#define FUNCTOR_FUNCTION_PARAMS4 P1 param1, P2 param2, P3 param3, P4 param4
258#define FUNCTOR_FUNCTION_PARAMS5 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5
259
260#define FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) FUNCTOR_FUNCTION_RETURNVALUE##returnvalue
261#define FUNCTOR_FUNCTION_RETURNVALUE0 void
262#define FUNCTOR_FUNCTION_RETURNVALUE1 R
263
264
265
266#define FUNCTOR_FUNCTION_CALL(numparams) FUNCTOR_FUNCTION_CALL##numparams
267#define FUNCTOR_FUNCTION_CALL0
268#define FUNCTOR_FUNCTION_CALL1 param1
269#define FUNCTOR_FUNCTION_CALL2 param1, param2
270#define FUNCTOR_FUNCTION_CALL3 param1, param2, param3
271#define FUNCTOR_FUNCTION_CALL4 param1, param2, param3, param4
272#define FUNCTOR_FUNCTION_CALL5 param1, param2, param3, param4, param5
273
274#define FUNCTOR_STORE_RETURNVALUE(returnvalue, functioncall) FUNCTOR_STORE_RETURNVALUE##returnvalue(functioncall)
275#define FUNCTOR_STORE_RETURNVALUE0(functioncall) functioncall
276#define FUNCTOR_STORE_RETURNVALUE1(functioncall) this->returnedValue_ = functioncall
277
278
279
280
281
282#define CREATE_STATIC_FUNCTOR(returnvalue, numparams) \
283    FUNCTOR_TEMPLATE(0, returnvalue, numparams) \
284    class FunctorStatic##returnvalue##numparams : public FunctorStatic \
285    { \
286        public: \
287            FunctorStatic##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
288            { \
289                this->numParams_ = numparams; \
290                this->hasReturnValue_ = returnvalue; \
291                this->type_ = FT_STATIC; \
292                this->functionPointer_ = functionPointer; \
293                \
294                FUNCTOR_TYPENAME_PARAMS(numparams); \
295                FUNCTOR_TYPENAME_RETURN(returnvalue); \
296            } \
297    \
298            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) \
299            { \
300                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
301            } \
302    \
303        private: \
304            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
305    }; \
306    \
307    \
308    FUNCTOR_TEMPLATE(0, returnvalue, numparams) \
309    inline FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
310    { \
311        return new FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams) (functionPointer); \
312    }
313
314
315
316
317
318#define CREATE_MEMBER_FUNCTOR(returnvalue, numparams) \
319    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
320    class FunctorMember##returnvalue##numparams : public FunctorMember<T> \
321    { \
322        public: \
323            FunctorMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
324            { \
325                this->numParams_ = numparams; \
326                this->hasReturnValue_ = returnvalue; \
327                this->type_ = FT_MEMBER; \
328                this->functionPointer_ = functionPointer; \
329            } \
330    \
331            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) \
332            { \
333                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
334            } \
335    \
336            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) \
337            { \
338                COUT(1) << "An error occurred in Functor.h:" << std::endl; \
339                COUT(1) << "Error: Function is not const." << std::endl; \
340            } \
341    \
342        private: \
343            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \
344    }; \
345    \
346    \
347    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
348    class FunctorConstMember##returnvalue##numparams : public FunctorMember<T> \
349    { \
350        public: \
351            FunctorConstMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
352            { \
353                this->numParams_ = numparams; \
354                this->hasReturnValue_ = returnvalue; \
355                this->type_ = FT_CONSTMEMBER; \
356                this->functionPointer_ = functionPointer; \
357            } \
358    \
359            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) \
360            { \
361                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
362            } \
363    \
364            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) \
365            { \
366                FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \
367            } \
368    \
369        private: \
370            FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)) const; \
371    }; \
372    \
373    \
374    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
375    inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \
376    { \
377        return new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
378    } \
379    \
380    \
381    FUNCTOR_TEMPLATE(1, returnvalue, numparams) \
382    inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \
383    { \
384        return new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \
385    }
386
387
388
389
390#define CREATE_ALL_STATIC_FUNCTORS() \
391    CREATE_STATIC_FUNCTOR(0, 0); \
392    CREATE_STATIC_FUNCTOR(0, 1); \
393    CREATE_STATIC_FUNCTOR(0, 2); \
394    CREATE_STATIC_FUNCTOR(0, 3); \
395    CREATE_STATIC_FUNCTOR(0, 4); \
396    CREATE_STATIC_FUNCTOR(0, 5); \
397    CREATE_STATIC_FUNCTOR(1, 0); \
398    CREATE_STATIC_FUNCTOR(1, 1); \
399    CREATE_STATIC_FUNCTOR(1, 2); \
400    CREATE_STATIC_FUNCTOR(1, 3); \
401    CREATE_STATIC_FUNCTOR(1, 4); \
402    CREATE_STATIC_FUNCTOR(1, 5)
403
404
405#define CREATE_ALL_MEMBER_FUNCTORS() \
406    CREATE_MEMBER_FUNCTOR(0, 0); \
407    CREATE_MEMBER_FUNCTOR(0, 1); \
408    CREATE_MEMBER_FUNCTOR(0, 2); \
409    CREATE_MEMBER_FUNCTOR(0, 3); \
410    CREATE_MEMBER_FUNCTOR(0, 4); \
411    CREATE_MEMBER_FUNCTOR(0, 5); \
412    CREATE_MEMBER_FUNCTOR(1, 0); \
413    CREATE_MEMBER_FUNCTOR(1, 1); \
414    CREATE_MEMBER_FUNCTOR(1, 2); \
415    CREATE_MEMBER_FUNCTOR(1, 3); \
416    CREATE_MEMBER_FUNCTOR(1, 4); \
417    CREATE_MEMBER_FUNCTOR(1, 5)
418
419
420    CREATE_ALL_STATIC_FUNCTORS();
421    CREATE_ALL_MEMBER_FUNCTORS();
422}
423
424#endif /* _Functor_H__ */
Note: See TracBrowser for help on using the repository browser.