[1505] | 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 | |
---|
[3280] | 35 | #include <algorithm> |
---|
| 36 | #include <string> |
---|
| 37 | |
---|
[3196] | 38 | #include "util/Debug.h" |
---|
| 39 | #include "util/Math.h" |
---|
[3280] | 40 | #include "util/StringUtils.h" |
---|
[1505] | 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; \ |
---|
[1747] | 69 | EXECUTOR_PARSE_FUNCTORCALL(mode)(EXECUTOR_PARSE_OBJECT(mode, 1) MultiType(params)); \ |
---|
[1505] | 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 | \ |
---|
[1747] | 95 | MultiType param[MAX_FUNCTOR_ARGUMENTS]; \ |
---|
[1505] | 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 | } \ |
---|
[3301] | 106 | COUT(5) << ") and " << std::max(static_cast<int>(paramCount) - static_cast<int>(tokens.size()), 0) << " default values ("; \ |
---|
[1505] | 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]); } |
---|
[1747] | 150 | inline void operator()(const MultiType& param1) const |
---|
[1505] | 151 | { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 152 | inline void operator()(const MultiType& param1, const MultiType& param2) const |
---|
[1505] | 153 | { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 154 | inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
[1505] | 155 | { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 156 | inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
[1505] | 157 | { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
[1747] | 158 | inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
[1505] | 159 | { (*this->functor_)(param1, param2, param3, param4, param5); } |
---|
| 160 | |
---|
| 161 | bool parse(const std::string& params, const std::string& delimiter = " ") const; |
---|
| 162 | |
---|
[1747] | 163 | bool evaluate(const std::string& params, MultiType param[5], const std::string& delimiter = " ") const; |
---|
[1505] | 164 | |
---|
| 165 | Executor& setDescription(const std::string& description); |
---|
| 166 | const std::string& getDescription() const; |
---|
| 167 | |
---|
[1786] | 168 | Executor& setDescriptionParam(unsigned int param, const std::string& description); |
---|
| 169 | const std::string& getDescriptionParam(unsigned int param) const; |
---|
[1505] | 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(); } |
---|
[3280] | 180 | inline FunctionType::Value getType() const |
---|
[1505] | 181 | { return this->functor_->getType(); } |
---|
[1889] | 182 | inline const MultiType& getReturnvalue() const |
---|
[1505] | 183 | { return this->functor_->getReturnvalue(); } |
---|
[1889] | 184 | inline const std::string& getTypenameParam(unsigned int param) const |
---|
[1505] | 185 | { return this->functor_->getTypenameParam(param); } |
---|
[1889] | 186 | inline const std::string& getTypenameReturnvalue() const |
---|
[1505] | 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 | |
---|
[1747] | 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); |
---|
[1505] | 200 | |
---|
[1747] | 201 | inline MultiType getDefaultValue(unsigned int index) const |
---|
[1505] | 202 | { |
---|
[1879] | 203 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 204 | return this->defaultValue_[index]; |
---|
| 205 | |
---|
[3280] | 206 | return MT_Type::Null; |
---|
[1505] | 207 | } |
---|
| 208 | |
---|
| 209 | bool allDefaultValuesSet() const; |
---|
| 210 | inline bool defaultValueSet(unsigned int index) const |
---|
| 211 | { |
---|
[1879] | 212 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 213 | return this->bAddedDefaultValue_[index]; |
---|
| 214 | |
---|
| 215 | return false; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | protected: |
---|
| 219 | Functor* functor_; |
---|
| 220 | std::string name_; |
---|
[1747] | 221 | MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS]; |
---|
[1505] | 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 | |
---|
[3304] | 248 | using Executor::operator(); |
---|
| 249 | |
---|
[1505] | 250 | inline void operator()(T* object) const |
---|
| 251 | { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 252 | inline void operator()(T* object, const MultiType& param1) const |
---|
[1505] | 253 | { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 254 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2) const |
---|
[1505] | 255 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 256 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
[1505] | 257 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 258 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
[1505] | 259 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
[1747] | 260 | inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
[1505] | 261 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | inline void operator()(const T* object) const |
---|
| 265 | { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 266 | inline void operator()(const T* object, const MultiType& param1) const |
---|
[1505] | 267 | { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 268 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2) const |
---|
[1505] | 269 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 270 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
[1505] | 271 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[1747] | 272 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
[1505] | 273 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
[1747] | 274 | inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
[1505] | 275 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); } |
---|
| 276 | |
---|
| 277 | inline void setObject(T* object) const |
---|
| 278 | { ((FunctorMember<T>*)this->functor_)->setObject(object); } |
---|
| 279 | inline void setObject(const T* object) const |
---|
| 280 | { ((FunctorMember<T>*)this->functor_)->setObject(object); } |
---|
| 281 | |
---|
[3304] | 282 | using Executor::parse; |
---|
| 283 | |
---|
[1505] | 284 | bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const |
---|
| 285 | { |
---|
| 286 | EXECUTOR_PARSE(object); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const |
---|
| 290 | { |
---|
| 291 | EXECUTOR_PARSE(object); |
---|
| 292 | } |
---|
| 293 | }; |
---|
| 294 | |
---|
| 295 | inline Executor* createExecutor(Functor* functor, const std::string& name = "") |
---|
| 296 | { |
---|
| 297 | return new Executor(functor, name); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | template <class T> |
---|
| 301 | inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "") |
---|
| 302 | { |
---|
| 303 | return new ExecutorMember<T>(functor, name); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "") |
---|
| 307 | { |
---|
| 308 | return new ExecutorStatic(functor, name); |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | #endif /* _Executor_H__ */ |
---|