Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/Executor.h @ 1247

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

several changes in TclThreadManager but couldn't yet fix the bug

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