Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Executor.h @ 939

Last change on this file since 939 was 939, checked in by landauf, 16 years ago
  • 3 times the (almost) same implementation + macro
File size: 14.1 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: Executor by Benjamin Grauer
27 */
28
29#ifndef _Executor_H__
30#define _Executor_H__
31
32#include "CorePrereqs.h"
33#include "Functor.h"
34#include "Debug.h"
35#include "util/SubString.h"
36#include "util/String.h"
37
38
39#define EXECUTOR_PARSE_FUNCTORCALL(mode) EXECUTOR_PARSE_FUNCTORCALL##mode
40#define EXECUTOR_PARSE_FUNCTORCALLnormal (*this->functor_)
41#define EXECUTOR_PARSE_FUNCTORCALLobject (*((FunctorMember<T>*)this->functor_))
42
43#define EXECUTOR_PARSE_OBJECT(mode, comma) EXECUTOR_PARSE_OBJECT##mode##comma
44#define EXECUTOR_PARSE_OBJECTnormal0
45#define EXECUTOR_PARSE_OBJECTnormal1
46#define EXECUTOR_PARSE_OBJECTobject0 object
47#define EXECUTOR_PARSE_OBJECTobject1 object,
48
49#define EXECUTOR_PARSE(mode) \
50    unsigned int paramCount = this->functor_->getParamCount(); \
51    \
52    if (paramCount == 0) \
53    { \
54        COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl; \
55        EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 0)); \
56    } \
57    else if (paramCount == 1) \
58    { \
59        std::string temp = getStripped(params); \
60        if ((temp != "") && (temp.size() != 0)) \
61        { \
62            COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl; \
63            EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) MultiTypeMath(params)); \
64        } \
65        else if (this->bAddedDefaultValue_[0]) \
66        { \
67            COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl; \
68            EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) this->defaultValue_[0]); \
69        } \
70        else \
71        { \
72            COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << temp << ")." << std::endl; \
73            return false; \
74        } \
75    } \
76    else \
77    { \
78        SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); \
79    \
80        for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) \
81        { \
82            if (!this->bAddedDefaultValue_[i]) \
83            { \
84                COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input:" << params << ")." << std::endl; \
85                return false; \
86            } \
87        } \
88    \
89        MultiTypeMath param[paramCount]; \
90        COUT(5) << "Calling Executor " << this->name_ << " through parser with " << paramCount << " parameters, using " << tokens.size() << " tokens ("; \
91        for (unsigned int i = 0; i < tokens.size(); i++) \
92        { \
93            param[i] = tokens[i]; \
94            if (i != 0) \
95            { \
96                COUT(5) << ", "; \
97            } \
98            COUT(5) << tokens[i]; \
99        } \
100        COUT(5) << ") and " << (paramCount - tokens.size()) << " default values ("; \
101        for (unsigned int i = tokens.size(); i < paramCount; i++) \
102        { \
103            param[i] = this->defaultValue_[i]; \
104            if (i != 0) \
105            { \
106                COUT(5) << ", "; \
107            } \
108            COUT(5) << this->defaultValue_[i]; \
109        } \
110        COUT(5) << ")." << std::endl; \
111    \
112        switch(paramCount) \
113        { \
114            case 2: \
115                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1]); \
116                break; \
117            case 3: \
118                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1], param[2]); \
119                break; \
120            case 4: \
121                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1], param[2], param[3]); \
122                break; \
123            case 5: \
124                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1], param[2], param[3], param[4]); \
125                break; \
126        } \
127    } \
128    \
129    return true
130
131
132namespace orxonox
133{
134    class _CoreExport Executor
135    {
136        public:
137            Executor(Functor* functor, const std::string& name = "");
138            virtual ~Executor();
139
140            inline void operator()() const
141                { (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
142            inline void operator()(const MultiTypeMath& param1) const
143                { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
144            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2) const
145                { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
146            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
147                { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
148            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
149                { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
150            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
151                { (*this->functor_)(param1, param2, param3, param4, param5); }
152
153            bool parse(const std::string& params, const std::string& delimiter = " ") const;
154
155            void setName(const std::string name);
156            const std::string& getName() const;
157
158            void setDescription(const std::string& description);
159            const std::string& getDescription() const;
160
161            void setDescriptionParam(int param, const std::string& description);
162            const std::string& getDescriptionParam(int param) const;
163
164            void setDescriptionReturnvalue(const std::string& description);
165            const std::string& getDescriptionReturnvalue(int param) const;
166
167            inline int getParamCount() const
168                { return this->functor_->getParamCount(); }
169            inline bool hasReturnvalue() const
170                { return this->functor_->hasReturnvalue(); }
171            inline FunctionType getType() const
172                { return this->functor_->getType(); }
173            inline MultiTypeMath getReturnvalue() const
174                { return this->functor_->getReturnvalue(); }
175            inline std::string getTypenameParam(int param) const
176                { return this->functor_->getTypenameParam(param); }
177            inline std::string getTypenameReturnvalue() const
178                { return this->functor_->getTypenameReturnvalue(); }
179
180            void setDefaultValues(const MultiTypeMath& param1);
181            void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2);
182            void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3);
183            void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4);
184            void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5);
185            void setDefaultValue(unsigned int index, const MultiTypeMath& param);
186
187            bool allDefaultValuesSet() const;
188
189        protected:
190            Functor* functor_;
191            std::string name_;
192            MultiTypeMath defaultValue_[MAX_FUNCTOR_ARGUMENTS];
193            bool bAddedDefaultValue_[MAX_FUNCTOR_ARGUMENTS];
194
195        private:
196            LanguageEntryLabel description_;
197            LanguageEntryLabel descriptionReturnvalue_;
198            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
199
200            bool bAddedDescription_;
201            bool bAddedDescriptionReturnvalue_;
202            bool bAddedDescriptionParam_[MAX_FUNCTOR_ARGUMENTS];
203    };
204
205    class _CoreExport ExecutorStatic : public Executor
206    {
207        public:
208            ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {}
209            virtual ~ExecutorStatic() {}
210    };
211
212    template <class T>
213    class ExecutorMember : public Executor
214    {
215        public:
216            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {}
217            virtual ~ExecutorMember() {}
218
219            inline void operator()(T* object) const
220                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
221            inline void operator()(T* object, const MultiTypeMath& param1) const
222                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
223            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const
224                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
225            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
226                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
227            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
228                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
229            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
230                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
231
232
233            inline void operator()(const T* object) const
234                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
235            inline void operator()(const T* object, const MultiTypeMath& param1) const
236                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
237            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const
238                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
239            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
240                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
241            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
242                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
243            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
244                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
245
246            inline void setObject(T* object) const
247                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
248            inline void setObject(const T* object) const
249                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
250
251            bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const
252            {
253                EXECUTOR_PARSE(object);
254            }
255
256            bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const
257            {
258                EXECUTOR_PARSE(object);
259            }
260    };
261
262    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
263    {
264        return new Executor(functor, name);
265    }
266
267    template <class T>
268    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "")
269    {
270        return new ExecutorMember<T>(functor, name);
271    }
272
273    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "")
274    {
275        return new ExecutorStatic(functor, name);
276    }
277}
278
279#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.