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