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