[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" |
---|
[931] | 34 | #include "util/SubString.h" |
---|
[847] | 35 | |
---|
[931] | 36 | |
---|
[847] | 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | class _CoreExport Executor |
---|
| 40 | { |
---|
| 41 | public: |
---|
| 42 | Executor(Functor* functor, const std::string& name = ""); |
---|
| 43 | virtual ~Executor(); |
---|
| 44 | |
---|
[931] | 45 | inline void operator()() const |
---|
| 46 | { (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 47 | inline void operator()(const MultiTypeMath& param1) const |
---|
| 48 | { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 49 | inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2) const |
---|
| 50 | { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 51 | inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const |
---|
| 52 | { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 53 | inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const |
---|
| 54 | { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
| 55 | inline void operator()(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const |
---|
[847] | 56 | { (*this->functor_)(param1, param2, param3, param4, param5); } |
---|
| 57 | |
---|
[931] | 58 | bool parse(const std::string& params, const std::string& delimiter = " ") const; |
---|
| 59 | |
---|
[847] | 60 | void setName(const std::string name); |
---|
| 61 | const std::string& getName() const; |
---|
| 62 | |
---|
| 63 | void description(const std::string& description); |
---|
| 64 | const std::string& getDescription() const; |
---|
| 65 | |
---|
| 66 | void descriptionParam(int param, const std::string& description); |
---|
| 67 | const std::string& getDescriptionParam(int param) const; |
---|
| 68 | |
---|
| 69 | void descriptionReturnvalue(const std::string& description); |
---|
| 70 | const std::string& getDescriptionReturnvalue(int param) const; |
---|
| 71 | |
---|
| 72 | inline int getParamCount() const |
---|
| 73 | { return this->functor_->getParamCount(); } |
---|
| 74 | inline bool hasReturnvalue() const |
---|
| 75 | { return this->functor_->hasReturnvalue(); } |
---|
| 76 | inline FunctionType getType() const |
---|
| 77 | { return this->functor_->getType(); } |
---|
| 78 | inline MultiTypeMath getReturnvalue() const |
---|
| 79 | { return this->functor_->getReturnvalue(); } |
---|
| 80 | inline std::string getTypenameParam(int param) const |
---|
| 81 | { return this->functor_->getTypenameParam(param); } |
---|
| 82 | inline std::string getTypenameReturnvalue() const |
---|
| 83 | { return this->functor_->getTypenameReturnvalue(); } |
---|
| 84 | |
---|
[931] | 85 | void setDefaultValues(const MultiTypeMath& param1); |
---|
| 86 | void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2); |
---|
| 87 | void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3); |
---|
| 88 | void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4); |
---|
| 89 | void setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5); |
---|
| 90 | void setDefaultValue(int index, const MultiTypeMath& param); |
---|
| 91 | |
---|
[847] | 92 | protected: |
---|
| 93 | Functor* functor_; |
---|
| 94 | |
---|
| 95 | private: |
---|
| 96 | std::string name_; |
---|
| 97 | |
---|
| 98 | LanguageEntryLabel description_; |
---|
| 99 | LanguageEntryLabel descriptionReturnvalue_; |
---|
[931] | 100 | LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS]; |
---|
[847] | 101 | |
---|
| 102 | bool bAddedDescription_; |
---|
| 103 | bool bAddedDescriptionReturnvalue_; |
---|
[931] | 104 | bool bAddedDescriptionParam_[MAX_FUNCTOR_ARGUMENTS]; |
---|
| 105 | |
---|
| 106 | MultiTypeMath defaultValue_[MAX_FUNCTOR_ARGUMENTS]; |
---|
| 107 | bool bAddedDefaultValue_[MAX_FUNCTOR_ARGUMENTS]; |
---|
[847] | 108 | }; |
---|
| 109 | |
---|
| 110 | class _CoreExport ExecutorStatic : public Executor |
---|
| 111 | { |
---|
| 112 | public: |
---|
| 113 | ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {} |
---|
| 114 | virtual ~ExecutorStatic() {} |
---|
| 115 | }; |
---|
| 116 | |
---|
| 117 | template <class T> |
---|
| 118 | class ExecutorMember : public Executor |
---|
| 119 | { |
---|
| 120 | public: |
---|
| 121 | ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {} |
---|
| 122 | virtual ~ExecutorMember() {} |
---|
| 123 | |
---|
[931] | 124 | inline void operator()(T* object) const |
---|
| 125 | { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 126 | inline void operator()(T* object, const MultiTypeMath& param1) const |
---|
| 127 | { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 128 | inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const |
---|
| 129 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 130 | inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const |
---|
| 131 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 132 | inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const |
---|
| 133 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
| 134 | inline void operator()(T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const |
---|
| 135 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); } |
---|
[847] | 136 | |
---|
[931] | 137 | |
---|
| 138 | inline void operator()(const T* object) const |
---|
| 139 | { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 140 | inline void operator()(const T* object, const MultiTypeMath& param1) const |
---|
| 141 | { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 142 | inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2) const |
---|
| 143 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 144 | inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) const |
---|
| 145 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 146 | inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) const |
---|
| 147 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
| 148 | inline void operator()(const T* object, const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) const |
---|
| 149 | { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); } |
---|
| 150 | |
---|
| 151 | inline void setObject(T* object) const |
---|
| 152 | { ((FunctorMember<T>*)this->functor_)->setObject(object); } |
---|
| 153 | inline void setObject(const T* object) const |
---|
| 154 | { ((FunctorMember<T>*)this->functor_)->setObject(object); } |
---|
| 155 | |
---|
| 156 | bool parse(T* object, const std::string& params, const std::string& delimiter) const |
---|
| 157 | { |
---|
| 158 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
| 159 | |
---|
| 160 | if (paramCount == 0) |
---|
| 161 | { |
---|
| 162 | (*((FunctorMember<T>*)this->functor_))(object); |
---|
| 163 | } |
---|
| 164 | else if (paramCount == 1) |
---|
| 165 | { |
---|
| 166 | (*((FunctorMember<T>*)this->functor_))(object, MultiTypeMath(params)); |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); |
---|
| 171 | |
---|
| 172 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
| 173 | if (!this->bAddedDefaultValue_[i]) |
---|
| 174 | return false; |
---|
| 175 | |
---|
| 176 | MultiTypeMath param[paramCount]; |
---|
| 177 | for (unsigned int i = 0; i < tokens.size(); i++) |
---|
| 178 | param[i] = tokens[i]; |
---|
| 179 | for (unsigned int i = tokens.size(); i < paramCount; i++) |
---|
| 180 | param[i] = this->defaultValue_[i]; |
---|
| 181 | |
---|
| 182 | switch(paramCount) |
---|
| 183 | { |
---|
| 184 | case 2: |
---|
| 185 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1]); |
---|
| 186 | break; |
---|
| 187 | case 3: |
---|
| 188 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1], param[2]); |
---|
| 189 | break; |
---|
| 190 | case 4: |
---|
| 191 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1], param[2], param[3]); |
---|
| 192 | break; |
---|
| 193 | case 5: |
---|
| 194 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1], param[2], param[3], param[4]); |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | return true; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | bool parse(const T* object, const std::string& params, const std::string& delimiter) const |
---|
| 203 | { |
---|
| 204 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
| 205 | |
---|
| 206 | if (paramCount == 0) |
---|
| 207 | { |
---|
| 208 | (*((FunctorMember<T>*)this->functor_))(object); |
---|
| 209 | } |
---|
| 210 | else if (paramCount == 1) |
---|
| 211 | { |
---|
| 212 | (*((FunctorMember<T>*)this->functor_))(object, MultiTypeMath(params)); |
---|
| 213 | } |
---|
| 214 | else |
---|
| 215 | { |
---|
| 216 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); |
---|
| 217 | |
---|
| 218 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
| 219 | if (!this->bAddedDefaultValue_[i]) |
---|
| 220 | return false; |
---|
| 221 | |
---|
| 222 | MultiTypeMath param[paramCount]; |
---|
| 223 | for (unsigned int i = 0; i < tokens.size(); i++) |
---|
| 224 | param[i] = tokens[i]; |
---|
| 225 | for (unsigned int i = tokens.size(); i < paramCount; i++) |
---|
| 226 | param[i] = this->defaultValue_[i]; |
---|
| 227 | |
---|
| 228 | switch(paramCount) |
---|
| 229 | { |
---|
| 230 | case 2: |
---|
| 231 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1]); |
---|
| 232 | break; |
---|
| 233 | case 3: |
---|
| 234 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1], param[2]); |
---|
| 235 | break; |
---|
| 236 | case 4: |
---|
| 237 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1], param[2], param[3]); |
---|
| 238 | break; |
---|
| 239 | case 5: |
---|
| 240 | (*((FunctorMember<T>*)this->functor_))(object, param[0], param[1], param[2], param[3], param[4]); |
---|
| 241 | break; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | return true; |
---|
| 246 | } |
---|
[847] | 247 | }; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | #endif /* _Executor_H__ */ |
---|