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 | |
---|
39 | namespace 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 | Executor& setDescription(const std::string& description); |
---|
65 | const std::string& getDescription() const; |
---|
66 | |
---|
67 | Executor& setDescriptionParam(unsigned int param, const std::string& description); |
---|
68 | const std::string& getDescriptionParam(unsigned int param) const; |
---|
69 | |
---|
70 | Executor& setDescriptionReturnvalue(const std::string& description); |
---|
71 | const std::string& getDescriptionReturnvalue(int param) const; |
---|
72 | |
---|
73 | inline Functor* getFunctor() const |
---|
74 | { return this->functor_; } |
---|
75 | inline unsigned int getParamCount() const |
---|
76 | { return this->functor_->getParamCount(); } |
---|
77 | inline bool hasReturnvalue() const |
---|
78 | { return this->functor_->hasReturnvalue(); } |
---|
79 | inline FunctionType::Value getType() const |
---|
80 | { return this->functor_->getType(); } |
---|
81 | inline const MultiType& getReturnvalue() const |
---|
82 | { return this->functor_->getReturnvalue(); } |
---|
83 | inline const std::string& getTypenameParam(unsigned int param) const |
---|
84 | { return this->functor_->getTypenameParam(param); } |
---|
85 | inline const std::string& getTypenameReturnvalue() const |
---|
86 | { return this->functor_->getTypenameReturnvalue(); } |
---|
87 | |
---|
88 | inline void setName(const std::string& name) |
---|
89 | { this->name_ = name; } |
---|
90 | inline const std::string& getName() const |
---|
91 | { return this->name_; } |
---|
92 | |
---|
93 | Executor& setDefaultValues(const MultiType& param1); |
---|
94 | Executor& setDefaultValues(const MultiType& param1, const MultiType& param2); |
---|
95 | Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3); |
---|
96 | Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4); |
---|
97 | Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5); |
---|
98 | Executor& setDefaultValue(unsigned int index, const MultiType& param); |
---|
99 | |
---|
100 | inline MultiType getDefaultValue(unsigned int index) const |
---|
101 | { |
---|
102 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
103 | return this->defaultValue_[index]; |
---|
104 | |
---|
105 | return MT_Type::Null; |
---|
106 | } |
---|
107 | |
---|
108 | bool allDefaultValuesSet() const; |
---|
109 | inline bool defaultValueSet(unsigned int index) const |
---|
110 | { |
---|
111 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
112 | return this->bAddedDefaultValue_[index]; |
---|
113 | |
---|
114 | return false; |
---|
115 | } |
---|
116 | |
---|
117 | protected: |
---|
118 | Functor* functor_; |
---|
119 | std::string name_; |
---|
120 | MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS]; |
---|
121 | bool bAddedDefaultValue_[MAX_FUNCTOR_ARGUMENTS]; |
---|
122 | |
---|
123 | private: |
---|
124 | LanguageEntryLabel description_; |
---|
125 | LanguageEntryLabel descriptionReturnvalue_; |
---|
126 | LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS]; |
---|
127 | |
---|
128 | bool bAddedDescription_; |
---|
129 | bool bAddedDescriptionReturnvalue_; |
---|
130 | bool bAddedDescriptionParam_[MAX_FUNCTOR_ARGUMENTS]; |
---|
131 | }; |
---|
132 | |
---|
133 | class _CoreExport ExecutorStatic : public Executor |
---|
134 | { |
---|
135 | public: |
---|
136 | ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {} |
---|
137 | virtual ~ExecutorStatic() {} |
---|
138 | }; |
---|
139 | |
---|
140 | template <class T> |
---|
141 | class ExecutorMember : public Executor |
---|
142 | { |
---|
143 | public: |
---|
144 | ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {} |
---|
145 | virtual ~ExecutorMember() {} |
---|
146 | |
---|
147 | using Executor::operator(); |
---|
148 | |
---|
149 | inline void operator()(T* object) const |
---|
150 | { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
151 | inline void operator()(T* object, const MultiType& param1) const |
---|
152 | { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
153 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2) const |
---|
154 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
155 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
156 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
157 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
158 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
159 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
160 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); } |
---|
161 | |
---|
162 | |
---|
163 | inline void operator()(const T* object) const |
---|
164 | { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
165 | inline void operator()(const T* object, const MultiType& param1) const |
---|
166 | { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
167 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2) const |
---|
168 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
169 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
170 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
171 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
172 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
173 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
174 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); } |
---|
175 | |
---|
176 | inline void setObject(T* object) const |
---|
177 | { ((FunctorMember<T>*)this->functor_)->setObject(object); } |
---|
178 | inline void setObject(const T* object) const |
---|
179 | { ((FunctorMember<T>*)this->functor_)->setObject(object); } |
---|
180 | |
---|
181 | using Executor::parse; |
---|
182 | |
---|
183 | bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const |
---|
184 | { |
---|
185 | static_cast<FunctorMember<T>*>(this->functor_)->setObject(object); |
---|
186 | if (Executor::parse(params, delimiter)) |
---|
187 | return true; |
---|
188 | else |
---|
189 | { |
---|
190 | static_cast<FunctorMember<T>*>(this->functor_)->setObject((T*)NULL); |
---|
191 | return false; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const |
---|
196 | { |
---|
197 | static_cast<FunctorMember<T>*>(this->functor_)->setObject(object); |
---|
198 | if (Executor::parse(params, delimiter)) |
---|
199 | return true; |
---|
200 | else |
---|
201 | { |
---|
202 | static_cast<FunctorMember<T>*>(this->functor_)->setObject((T*)NULL); |
---|
203 | return false; |
---|
204 | } |
---|
205 | } |
---|
206 | }; |
---|
207 | |
---|
208 | inline Executor* createExecutor(Functor* functor, const std::string& name = "") |
---|
209 | { |
---|
210 | return new Executor(functor, name); |
---|
211 | } |
---|
212 | |
---|
213 | template <class T> |
---|
214 | inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "") |
---|
215 | { |
---|
216 | return new ExecutorMember<T>(functor, name); |
---|
217 | } |
---|
218 | |
---|
219 | inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "") |
---|
220 | { |
---|
221 | return new ExecutorStatic(functor, name); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | #endif /* _Executor_H__ */ |
---|