Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added debug output to executor

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