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