[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 | |
---|
[7203] | 33 | #include "core/CorePrereqs.h" |
---|
[1505] | 34 | |
---|
[3280] | 35 | #include <string> |
---|
[7163] | 36 | #include "util/MultiType.h" |
---|
[1505] | 37 | #include "Functor.h" |
---|
[7196] | 38 | #include "ExecutorPtr.h" |
---|
[1505] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | class _CoreExport Executor |
---|
| 43 | { |
---|
| 44 | public: |
---|
[7198] | 45 | Executor(const FunctorPtr& functor, const std::string& name = ""); |
---|
[1505] | 46 | virtual ~Executor(); |
---|
| 47 | |
---|
[7189] | 48 | inline MultiType operator()() const |
---|
| 49 | { return (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 50 | inline MultiType operator()(const MultiType& param1) const |
---|
| 51 | { return (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 52 | inline MultiType operator()(const MultiType& param1, const MultiType& param2) const |
---|
| 53 | { return (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 54 | inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
| 55 | { return (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
| 56 | inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
| 57 | { return (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
| 58 | inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
| 59 | { return (*this->functor_)(param1, param2, param3, param4, param5); } |
---|
[1505] | 60 | |
---|
[7230] | 61 | MultiType parse(const std::string& arguments, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const; |
---|
| 62 | MultiType parse(const SubString& arguments, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const; |
---|
[1505] | 63 | |
---|
[7230] | 64 | int evaluateParams(const SubString& arguments, MultiType param[MAX_FUNCTOR_ARGUMENTS], int* error = 0, const std::string& delimiter = " ") const; |
---|
[1505] | 65 | |
---|
[7214] | 66 | inline void setFunctor(const FunctorPtr& functor) |
---|
| 67 | { this->functor_ = functor; } |
---|
[7198] | 68 | inline const FunctorPtr& getFunctor() const |
---|
[1505] | 69 | { return this->functor_; } |
---|
[7214] | 70 | |
---|
| 71 | inline void setName(const std::string& name) |
---|
| 72 | { this->name_ = name; } |
---|
| 73 | inline const std::string& getName() const |
---|
| 74 | { return this->name_; } |
---|
| 75 | |
---|
[1505] | 76 | inline unsigned int getParamCount() const |
---|
| 77 | { return this->functor_->getParamCount(); } |
---|
| 78 | inline bool hasReturnvalue() const |
---|
| 79 | { return this->functor_->hasReturnvalue(); } |
---|
[7188] | 80 | inline Functor::Type::Enum getType() const |
---|
[1505] | 81 | { return this->functor_->getType(); } |
---|
[7188] | 82 | inline std::string getTypenameParam(unsigned int param) const |
---|
[1505] | 83 | { return this->functor_->getTypenameParam(param); } |
---|
[7188] | 84 | inline std::string getTypenameReturnvalue() const |
---|
[1505] | 85 | { return this->functor_->getTypenameReturnvalue(); } |
---|
| 86 | |
---|
[7200] | 87 | void setDefaultValues(const MultiType& param1); |
---|
| 88 | void setDefaultValues(const MultiType& param1, const MultiType& param2); |
---|
| 89 | void setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3); |
---|
| 90 | void setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4); |
---|
| 91 | void setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5); |
---|
| 92 | void setDefaultValue(unsigned int index, const MultiType& param); |
---|
[1505] | 93 | |
---|
[1747] | 94 | inline MultiType getDefaultValue(unsigned int index) const |
---|
[1505] | 95 | { |
---|
[1879] | 96 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 97 | return this->defaultValue_[index]; |
---|
| 98 | |
---|
[3280] | 99 | return MT_Type::Null; |
---|
[1505] | 100 | } |
---|
| 101 | |
---|
| 102 | bool allDefaultValuesSet() const; |
---|
| 103 | inline bool defaultValueSet(unsigned int index) const |
---|
| 104 | { |
---|
[1879] | 105 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[7187] | 106 | return !this->defaultValue_[index].null(); |
---|
[1505] | 107 | |
---|
| 108 | return false; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | protected: |
---|
[7198] | 112 | FunctorPtr functor_; |
---|
[1505] | 113 | std::string name_; |
---|
[1747] | 114 | MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS]; |
---|
[1505] | 115 | }; |
---|
| 116 | |
---|
| 117 | class _CoreExport ExecutorStatic : public Executor |
---|
| 118 | { |
---|
| 119 | public: |
---|
[7198] | 120 | ExecutorStatic(const FunctorStaticPtr& functor, const std::string& name = "") : Executor(functor, name) {} |
---|
[1505] | 121 | virtual ~ExecutorStatic() {} |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | template <class T> |
---|
| 125 | class ExecutorMember : public Executor |
---|
| 126 | { |
---|
| 127 | public: |
---|
[7198] | 128 | ExecutorMember(const FunctorMemberPtr<T>& functor, const std::string& name = "") : Executor(functor, name), functorMember_(functor) {} |
---|
[1505] | 129 | virtual ~ExecutorMember() {} |
---|
| 130 | |
---|
[3304] | 131 | using Executor::operator(); |
---|
| 132 | |
---|
[7189] | 133 | inline MultiType operator()(T* object) const |
---|
[7198] | 134 | { return (*this->functorMember_)(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 135 | inline MultiType operator()(T* object, const MultiType& param1) const |
---|
[7198] | 136 | { return (*this->functorMember_)(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 137 | inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2) const |
---|
[7198] | 138 | { return (*this->functorMember_)(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 139 | inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
[7198] | 140 | { return (*this->functorMember_)(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 141 | inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
[7198] | 142 | { return (*this->functorMember_)(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
[7189] | 143 | inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
[7198] | 144 | { return (*this->functorMember_)(object, param1, param2, param3, param4, param5); } |
---|
[1505] | 145 | |
---|
| 146 | |
---|
[7189] | 147 | inline MultiType operator()(const T* object) const |
---|
[7198] | 148 | { return (*this->functorMember_)(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 149 | inline MultiType operator()(const T* object, const MultiType& param1) const |
---|
[7198] | 150 | { return (*this->functorMember_)(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 151 | inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2) const |
---|
[7198] | 152 | { return (*this->functorMember_)(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 153 | inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const |
---|
[7198] | 154 | { return (*this->functorMember_)(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); } |
---|
[7189] | 155 | inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const |
---|
[7198] | 156 | { return (*this->functorMember_)(object, param1, param2, param3, param4, this->defaultValue_[4]); } |
---|
[7189] | 157 | inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const |
---|
[7198] | 158 | { return (*this->functorMember_)(object, param1, param2, param3, param4, param5); } |
---|
[1505] | 159 | |
---|
| 160 | inline void setObject(T* object) const |
---|
[7198] | 161 | { this->functorMember_->setObject(object); } |
---|
[1505] | 162 | inline void setObject(const T* object) const |
---|
[7198] | 163 | { this->functorMember_->setObject(object); } |
---|
[1505] | 164 | |
---|
[3304] | 165 | using Executor::parse; |
---|
| 166 | |
---|
[7228] | 167 | MultiType parse(T* object, const std::string& params, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const |
---|
[1505] | 168 | { |
---|
[7212] | 169 | T* oldobject = this->functorMember_->getObject(); |
---|
[7177] | 170 | |
---|
[7198] | 171 | this->functorMember_->setObject(object); |
---|
[7228] | 172 | const MultiType& result = this->Executor::parse(params, error, delimiter, bPrintError); |
---|
[7212] | 173 | this->functorMember_->setObject(oldobject); |
---|
[7177] | 174 | |
---|
| 175 | return result; |
---|
[1505] | 176 | } |
---|
| 177 | |
---|
[7228] | 178 | MultiType parse(const T* object, const std::string& params, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const |
---|
[1505] | 179 | { |
---|
[7212] | 180 | T* oldobject = this->functorMember_->getObject(); |
---|
[7177] | 181 | |
---|
[7198] | 182 | this->functorMember_->setObject(object); |
---|
[7228] | 183 | const MultiType& result = this->Executor::parse(params, error, delimiter, bPrintError); |
---|
[7212] | 184 | this->functorMember_->setObjects(oldobject); |
---|
[7177] | 185 | |
---|
| 186 | return result; |
---|
[1505] | 187 | } |
---|
[7198] | 188 | |
---|
| 189 | protected: |
---|
| 190 | FunctorMemberPtr<T> functorMember_; |
---|
[1505] | 191 | }; |
---|
| 192 | |
---|
[7200] | 193 | inline ExecutorPtr createExecutor(const FunctorPtr& functor, const std::string& name = "") |
---|
[1505] | 194 | { |
---|
| 195 | return new Executor(functor, name); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | template <class T> |
---|
[7200] | 199 | inline ExecutorMemberPtr<T> createExecutor(const FunctorMemberPtr<T>& functor, const std::string& name = "") |
---|
[1505] | 200 | { |
---|
| 201 | return new ExecutorMember<T>(functor, name); |
---|
| 202 | } |
---|
| 203 | |
---|
[7200] | 204 | inline ExecutorStaticPtr createExecutor(const FunctorStaticPtr& functor, const std::string& name = "") |
---|
[1505] | 205 | { |
---|
| 206 | return new ExecutorStatic(functor, name); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | #endif /* _Executor_H__ */ |
---|