Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Executor.h @ 1349

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