Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

don't panic, no codechanges!
added a link to www.orxonox.net

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