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