Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 957 was 957, checked in by landauf, 16 years ago
  • added set and tset functions to the ConfigValueContainer to (temporary) set a config-value to a new value
  • ConfigValueContainer uses now the functions of MultiTypeMath to convert and assign values
  • added some errorhandling to the CommandExecutor in case there are not enough parameters when executing the command
  • added updateConfigValues function to Identifier
  • added addTime and removeTime functions to the Timer
  • some changes in Executor to allow adding description and default-values when using the ConsoleCommand macro
File size: 15.5 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
131namespace AccessLevel
132{
133    enum Level
134    {
135        None,
136        User,
137        Admin,
138        Offline,
139        Debug,
140        Disabled
141    };
142}
143
144namespace orxonox
145{
146    class _CoreExport Executor
147    {
148        public:
149            Executor(Functor* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None);
150            virtual ~Executor();
151
152            inline void operator()() const
153                { (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
154            inline void operator()(const MultiTypeMath& param1) const
155                { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
156            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2) const
157                { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
158            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
159                { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
160            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
161                { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
162            inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
163                { (*this->functor_)(param1, param2, param3, param4, param5); }
164
165            bool parse(const std::string& params, const std::string& delimiter = " ") const;
166
167            Executor& setDescription(const std::string& description);
168            const std::string& getDescription() const;
169
170            Executor& setDescriptionParam(int param, const std::string& description);
171            const std::string& getDescriptionParam(int param) const;
172
173            Executor& setDescriptionReturnvalue(const std::string& description);
174            const std::string& getDescriptionReturnvalue(int param) const;
175
176            inline unsigned int getParamCount() const
177                { return this->functor_->getParamCount(); }
178            inline bool hasReturnvalue() const
179                { return this->functor_->hasReturnvalue(); }
180            inline FunctionType getType() const
181                { return this->functor_->getType(); }
182            inline MultiTypeMath getReturnvalue() const
183                { return this->functor_->getReturnvalue(); }
184            inline std::string getTypenameParam(unsigned int param) const
185                { return this->functor_->getTypenameParam(param); }
186            inline std::string getTypenameReturnvalue() const
187                { return this->functor_->getTypenameReturnvalue(); }
188
189            inline void setName(const std::string name)
190                { this->name_ = name; }
191            inline const std::string& getName() const
192                { return this->name_; }
193
194            inline void setAccessLevel(AccessLevel::Level level)
195                { this->accessLevel_ = level; }
196            inline AccessLevel::Level getAccessLevel() const
197                { return this->accessLevel_; }
198
199            Executor& setDefaultValues(const MultiTypeMath& param1);
200            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2);
201            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3);
202            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4);
203            Executor& setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5);
204            Executor& setDefaultValue(unsigned int index, const MultiTypeMath& param);
205
206            inline MultiTypeMath getDefaultValue(unsigned int index) const
207            {
208                if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
209                    return this->defaultValue_[index];
210
211                return MT_null;
212            }
213
214            bool allDefaultValuesSet() const;
215            inline bool defaultValueSet(unsigned int index) const
216            {
217                if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
218                    return this->bAddedDefaultValue_[index];
219
220                return false;
221            }
222
223        protected:
224            Functor* functor_;
225            std::string name_;
226            MultiTypeMath defaultValue_[MAX_FUNCTOR_ARGUMENTS];
227            bool bAddedDefaultValue_[MAX_FUNCTOR_ARGUMENTS];
228
229        private:
230            LanguageEntryLabel description_;
231            LanguageEntryLabel descriptionReturnvalue_;
232            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
233
234            bool bAddedDescription_;
235            bool bAddedDescriptionReturnvalue_;
236            bool bAddedDescriptionParam_[MAX_FUNCTOR_ARGUMENTS];
237
238            AccessLevel::Level accessLevel_;
239    };
240
241    class _CoreExport ExecutorStatic : public Executor
242    {
243        public:
244            ExecutorStatic(FunctorStatic* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None) : Executor(functor, name, level) {}
245            virtual ~ExecutorStatic() {}
246    };
247
248    template <class T>
249    class ExecutorMember : public Executor
250    {
251        public:
252            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None) : Executor(functor, name, level) {}
253            virtual ~ExecutorMember() {}
254
255            inline void operator()(T* object) const
256                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
257            inline void operator()(T* object, const MultiTypeMath& param1) const
258                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
259            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const
260                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
261            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
262                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
263            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
264                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
265            inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
266                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
267
268
269            inline void operator()(const T* object) const
270                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
271            inline void operator()(const T* object, const MultiTypeMath& param1) const
272                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
273            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const
274                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
275            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const
276                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
277            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const
278                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
279            inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const
280                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
281
282            inline void setObject(T* object) const
283                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
284            inline void setObject(const T* object) const
285                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
286
287            bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const
288            {
289                EXECUTOR_PARSE(object);
290            }
291
292            bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const
293            {
294                EXECUTOR_PARSE(object);
295            }
296    };
297
298    inline Executor* createExecutor(Functor* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None)
299    {
300        return new Executor(functor, name, level);
301    }
302
303    template <class T>
304    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None)
305    {
306        return new ExecutorMember<T>(functor, name, level);
307    }
308
309    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "", AccessLevel::Level level = AccessLevel::None)
310    {
311        return new ExecutorStatic(functor, name, level);
312    }
313}
314
315#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.