Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/Executor.h @ 7186

Last change on this file since 7186 was 7186, checked in by landauf, 14 years ago

Moved ability to possess descriptions from Executor to ConsoleCommand, since no other executors use this feature. Also simplified this code a little by introducing a new shortcut in Language.h. XMLPort has to use a temporary solution for descriptions without Language support atm.

  • Property svn:eol-style set to native
File size: 10.6 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 <string>
36#include "util/MultiType.h"
37#include "Functor.h"
38
39namespace orxonox
40{
41    class _CoreExport Executor
42    {
43        public:
44            Executor(Functor* functor, const std::string& name = "");
45            virtual ~Executor();
46
47            inline void operator()() const
48                { (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
49            inline void operator()(const MultiType& param1) const
50                { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
51            inline void operator()(const MultiType& param1, const MultiType& param2) const
52                { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
53            inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const
54                { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
55            inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
56                { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
57            inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
58                { (*this->functor_)(param1, param2, param3, param4, param5); }
59
60            bool parse(const std::string& params, const std::string& delimiter = " ") const;
61
62            bool evaluate(const std::string& params, MultiType param[5], const std::string& delimiter = " ") const;
63
64            inline Functor* getFunctor() const
65                { return this->functor_; }
66            inline unsigned int getParamCount() const
67                { return this->functor_->getParamCount(); }
68            inline bool hasReturnvalue() const
69                { return this->functor_->hasReturnvalue(); }
70            inline FunctionType::Value getType() const
71                { return this->functor_->getType(); }
72            inline const MultiType& getReturnvalue() const
73                { return this->functor_->getReturnvalue(); }
74            inline const std::string& getTypenameParam(unsigned int param) const
75                { return this->functor_->getTypenameParam(param); }
76            inline const std::string& getTypenameReturnvalue() const
77                { return this->functor_->getTypenameReturnvalue(); }
78
79            inline void setName(const std::string& name)
80                { this->name_ = name; }
81            inline const std::string& getName() const
82                { return this->name_; }
83
84            Executor& setDefaultValues(const MultiType& param1);
85            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2);
86            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
87            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
88            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
89            Executor& setDefaultValue(unsigned int index, const MultiType& param);
90
91            inline MultiType getDefaultValue(unsigned int index) const
92            {
93                if (index < MAX_FUNCTOR_ARGUMENTS)
94                    return this->defaultValue_[index];
95
96                return MT_Type::Null;
97            }
98
99            bool allDefaultValuesSet() const;
100            inline bool defaultValueSet(unsigned int index) const
101            {
102                if (index < MAX_FUNCTOR_ARGUMENTS)
103                    return this->bAddedDefaultValue_[index];
104
105                return false;
106            }
107
108        protected:
109            Functor* functor_;
110            std::string name_;
111            MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS];
112            bool bAddedDefaultValue_[MAX_FUNCTOR_ARGUMENTS];
113    };
114
115    class _CoreExport ExecutorStatic : public Executor
116    {
117        public:
118            ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {}
119            virtual ~ExecutorStatic() {}
120    };
121
122    template <class T>
123    class ExecutorMember : public Executor
124    {
125        public:
126            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {}
127            virtual ~ExecutorMember() {}
128
129            using Executor::operator();
130
131            inline void operator()(T* object) const
132                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
133            inline void operator()(T* object, const MultiType& param1) const
134                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
135            inline void operator()(T* object, const MultiType& param1, const MultiType& param2) const
136                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
137            inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
138                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
139            inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
140                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
141            inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
142                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
143
144
145            inline void operator()(const T* object) const
146                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
147            inline void operator()(const T* object, const MultiType& param1) const
148                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
149            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2) const
150                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
151            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
152                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
153            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
154                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
155            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
156                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
157
158            inline void setObject(T* object) const
159                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
160            inline void setObject(const T* object) const
161                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
162
163            using Executor::parse;
164
165            bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const
166            {
167                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
168
169                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
170
171                functorMember->setObject(object);
172                bool result = Executor::parse(params, delimiter);
173                functorMember->setObjects(objects);
174
175                return result;
176            }
177
178            bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const
179            {
180                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
181
182                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
183
184                functorMember->setObject(object);
185                bool result = Executor::parse(params, delimiter);
186                functorMember->setObjects(objects);
187
188                return result;
189            }
190    };
191
192    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
193    {
194        return new Executor(functor, name);
195    }
196
197    template <class T>
198    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "")
199    {
200        return new ExecutorMember<T>(functor, name);
201    }
202
203    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "")
204    {
205        return new ExecutorStatic(functor, name);
206    }
207}
208
209#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.