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