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