Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1055 was 1052, checked in by landauf, 16 years ago

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

File size: 15.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: 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, '\\', true, '"', true, '(', ')', true, '\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[MAX_FUNCTOR_ARGUMENTS]; \
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 < MAX_FUNCTOR_ARGUMENTS; 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        if ((tokens.size() > paramCount) && (this->functor_->getTypenameParam(paramCount - 1) == "string")) \
113            param[paramCount - 1] = tokens.subSet(paramCount - 1).join(); \
114        \
115        switch(paramCount) \
116        { \
117            case 2: \
118                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1]); \
119                break; \
120            case 3: \
121                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1], param[2]); \
122                break; \
123            case 4: \
124                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1], param[2], param[3]); \
125                break; \
126            case 5: \
127                EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) param[0], param[1], param[2], param[3], param[4]); \
128                break; \
129        } \
130    } \
131    \
132    return true
133
134namespace AccessLevel
135{
136    enum Level
137    {
138        None,
139        User,
140        Admin,
141        Offline,
142        Debug,
143        Disabled
144    };
145}
146
147namespace orxonox
148{
149    class _CoreExport Executor
150    {
151        public:
152            Executor(Functor* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None);
153            virtual ~Executor();
154
155            inline void operator()() const
156                { (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
157            inline void operator()(const MultiTypeMath& param1) const
158                { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
159            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2) const
160                { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
161            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
162                { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
163            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
164                { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
165            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
166                { (*this->functor_)(param1, param2, param3, param4, param5); }
167
168            bool parse(const std::string& params, const std::string& delimiter = " ") const;
169
170            bool evaluate(const std::string& params, MultiTypeMath param[5], const std::string& delimiter = " ") const;
171
172            Executor& setDescription(const std::string& description);
173            const std::string& getDescription() const;
174
175            Executor& setDescriptionParam(int param, const std::string& description);
176            const std::string& getDescriptionParam(int param) const;
177
178            Executor& setDescriptionReturnvalue(const std::string& description);
179            const std::string& getDescriptionReturnvalue(int param) const;
180
181            inline unsigned int getParamCount() const
182                { return this->functor_->getParamCount(); }
183            inline bool hasReturnvalue() const
184                { return this->functor_->hasReturnvalue(); }
185            inline FunctionType getType() const
186                { return this->functor_->getType(); }
187            inline MultiTypeMath getReturnvalue() const
188                { return this->functor_->getReturnvalue(); }
189            inline std::string getTypenameParam(unsigned int param) const
190                { return this->functor_->getTypenameParam(param); }
191            inline std::string getTypenameReturnvalue() const
192                { return this->functor_->getTypenameReturnvalue(); }
193
194            inline void setName(const std::string name)
195                { this->name_ = name; }
196            inline const std::string& getName() const
197                { return this->name_; }
198
199            inline void setAccessLevel(AccessLevel::Level level)
200                { this->accessLevel_ = level; }
201            inline AccessLevel::Level getAccessLevel() const
202                { return this->accessLevel_; }
203
204            Executor& setDefaultValues(const MultiTypeMath& param1);
205            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2);
206            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3);
207            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4);
208            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5);
209            Executor& setDefaultValue(unsigned int index, const MultiTypeMath& param);
210
211            inline MultiTypeMath getDefaultValue(unsigned int index) const
212            {
213                if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
214                    return this->defaultValue_[index];
215
216                return MT_null;
217            }
218
219            bool allDefaultValuesSet() const;
220            inline bool defaultValueSet(unsigned int index) const
221            {
222                if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
223                    return this->bAddedDefaultValue_[index];
224
225                return false;
226            }
227
228        protected:
229            Functor* functor_;
230            std::string name_;
231            MultiTypeMath defaultValue_[MAX_FUNCTOR_ARGUMENTS];
232            bool bAddedDefaultValue_[MAX_FUNCTOR_ARGUMENTS];
233
234        private:
235            LanguageEntryLabel description_;
236            LanguageEntryLabel descriptionReturnvalue_;
237            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
238
239            bool bAddedDescription_;
240            bool bAddedDescriptionReturnvalue_;
241            bool bAddedDescriptionParam_[MAX_FUNCTOR_ARGUMENTS];
242
243            AccessLevel::Level accessLevel_;
244    };
245
246    class _CoreExport ExecutorStatic : public Executor
247    {
248        public:
249            ExecutorStatic(FunctorStatic* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None) : Executor(functor, name, level) {}
250            virtual ~ExecutorStatic() {}
251    };
252
253    template <class T>
254    class ExecutorMember : public Executor
255    {
256        public:
257            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None) : Executor(functor, name, level) {}
258            virtual ~ExecutorMember() {}
259
260            inline void operator()(T* object) const
261                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
262            inline void operator()(T* object, const MultiTypeMath& param1) const
263                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
264            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const
265                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
266            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
267                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
268            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
269                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
270            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
271                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
272
273
274            inline void operator()(const T* object) const
275                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
276            inline void operator()(const T* object, const MultiTypeMath& param1) const
277                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
278            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const
279                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
280            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
281                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
282            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
283                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
284            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
285                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
286
287            inline void setObject(T* object) const
288                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
289            inline void setObject(const T* object) const
290                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
291
292            bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const
293            {
294                EXECUTOR_PARSE(object);
295            }
296
297            bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const
298            {
299                EXECUTOR_PARSE(object);
300            }
301    };
302
303    inline Executor* createExecutor(Functor* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None)
304    {
305        return new Executor(functor, name, level);
306    }
307
308    template <class T>
309    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None)
310    {
311        return new ExecutorMember<T>(functor, name, level);
312    }
313
314    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None)
315    {
316        return new ExecutorStatic(functor, name, level);
317    }
318}
319
320#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.