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