| 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: Functor by Benjamin Grauer |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #ifndef _Functor_H__ |
|---|
| 31 | #define _Functor_H__ |
|---|
| 32 | |
|---|
| 33 | #include <typeinfo> |
|---|
| 34 | |
|---|
| 35 | #include "CorePrereqs.h" |
|---|
| 36 | |
|---|
| 37 | #include "util/Convert.h" |
|---|
| 38 | #include "util/Debug.h" |
|---|
| 39 | #include "util/MultiType.h" |
|---|
| 40 | #include "SharedPtr.h" |
|---|
| 41 | |
|---|
| 42 | namespace orxonox |
|---|
| 43 | { |
|---|
| 44 | const unsigned int MAX_FUNCTOR_ARGUMENTS = 5; |
|---|
| 45 | |
|---|
| 46 | template <class T> |
|---|
| 47 | inline std::string typeToString() { return "unknown"; } |
|---|
| 48 | |
|---|
| 49 | #define CreateTypeToStringTemplate(type) \ |
|---|
| 50 | template <> \ |
|---|
| 51 | inline std::string typeToString<type>() { return #type; } \ |
|---|
| 52 | template <> \ |
|---|
| 53 | inline std::string typeToString<type&>() { return #type; } \ |
|---|
| 54 | template <> \ |
|---|
| 55 | inline std::string typeToString<const type>() { return #type; } \ |
|---|
| 56 | template <> \ |
|---|
| 57 | inline std::string typeToString<const type&>() { return #type; } |
|---|
| 58 | |
|---|
| 59 | CreateTypeToStringTemplate(int); |
|---|
| 60 | CreateTypeToStringTemplate(unsigned int); |
|---|
| 61 | CreateTypeToStringTemplate(char); |
|---|
| 62 | CreateTypeToStringTemplate(unsigned char); |
|---|
| 63 | CreateTypeToStringTemplate(short); |
|---|
| 64 | CreateTypeToStringTemplate(unsigned short); |
|---|
| 65 | CreateTypeToStringTemplate(long); |
|---|
| 66 | CreateTypeToStringTemplate(unsigned long); |
|---|
| 67 | CreateTypeToStringTemplate(long long); |
|---|
| 68 | CreateTypeToStringTemplate(unsigned long long); |
|---|
| 69 | CreateTypeToStringTemplate(float); |
|---|
| 70 | CreateTypeToStringTemplate(double); |
|---|
| 71 | CreateTypeToStringTemplate(long double); |
|---|
| 72 | CreateTypeToStringTemplate(bool); |
|---|
| 73 | CreateTypeToStringTemplate(Vector2); |
|---|
| 74 | CreateTypeToStringTemplate(Vector3); |
|---|
| 75 | CreateTypeToStringTemplate(Quaternion); |
|---|
| 76 | CreateTypeToStringTemplate(ColourValue); |
|---|
| 77 | CreateTypeToStringTemplate(Radian); |
|---|
| 78 | CreateTypeToStringTemplate(Degree); |
|---|
| 79 | |
|---|
| 80 | template <> |
|---|
| 81 | inline std::string typeToString<std::string>() { return "string"; } |
|---|
| 82 | template <> |
|---|
| 83 | inline std::string typeToString<std::string&>() { return "string"; } |
|---|
| 84 | template <> |
|---|
| 85 | inline std::string typeToString<const std::string>() { return "string"; } |
|---|
| 86 | template <> |
|---|
| 87 | inline std::string typeToString<const std::string&>() { return "string"; } |
|---|
| 88 | |
|---|
| 89 | class _CoreExport Functor |
|---|
| 90 | { |
|---|
| 91 | friend class SharedPtr<Functor>; |
|---|
| 92 | |
|---|
| 93 | public: |
|---|
| 94 | struct Type |
|---|
| 95 | { |
|---|
| 96 | enum Enum |
|---|
| 97 | { |
|---|
| 98 | Member, |
|---|
| 99 | ConstMember, |
|---|
| 100 | Static, |
|---|
| 101 | Lua |
|---|
| 102 | }; |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | public: |
|---|
| 106 | Functor() : references_(0) { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; } |
|---|
| 107 | virtual ~Functor() { --instances_s; COUT(0) << "functor --: " << instances_s << std::endl; } |
|---|
| 108 | |
|---|
| 109 | virtual MultiType operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0; |
|---|
| 110 | |
|---|
| 111 | virtual Type::Enum getType() const = 0; |
|---|
| 112 | virtual unsigned int getParamCount() const = 0; |
|---|
| 113 | virtual bool hasReturnvalue() const = 0; |
|---|
| 114 | |
|---|
| 115 | virtual std::string getTypenameParam(unsigned int param) const = 0; |
|---|
| 116 | virtual std::string getTypenameReturnvalue() const = 0; |
|---|
| 117 | |
|---|
| 118 | virtual void evaluateParam(unsigned int index, MultiType& param) const = 0; |
|---|
| 119 | |
|---|
| 120 | virtual void setRawObjectPointer(void* object) {} |
|---|
| 121 | virtual void* getRawObjectPointer() const { return 0; } |
|---|
| 122 | |
|---|
| 123 | virtual const std::type_info& getHeaderIdentifier() const = 0; |
|---|
| 124 | |
|---|
| 125 | private: |
|---|
| 126 | inline void incrementReferenceCount() |
|---|
| 127 | { ++this->references_; } |
|---|
| 128 | inline void decrementReferenceCount() |
|---|
| 129 | { --this->references_; if (this->references_ == 0) delete this; } |
|---|
| 130 | |
|---|
| 131 | int references_; |
|---|
| 132 | static int instances_s; |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | class _CoreExport FunctorStatic : public Functor |
|---|
| 136 | { |
|---|
| 137 | public: |
|---|
| 138 | virtual ~FunctorStatic() {} |
|---|
| 139 | virtual MultiType operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0; |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | template <class T> |
|---|
| 143 | class FunctorMember : public Functor |
|---|
| 144 | { |
|---|
| 145 | public: |
|---|
| 146 | FunctorMember() |
|---|
| 147 | { |
|---|
| 148 | this->object_ = 0; |
|---|
| 149 | this->constObject_ = 0; |
|---|
| 150 | } |
|---|
| 151 | virtual ~FunctorMember() {} |
|---|
| 152 | |
|---|
| 153 | virtual MultiType operator()(T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0; |
|---|
| 154 | virtual MultiType operator()(const T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0; |
|---|
| 155 | |
|---|
| 156 | virtual MultiType operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) |
|---|
| 157 | { |
|---|
| 158 | if (this->object_) |
|---|
| 159 | return (*this)(this->object_, param1, param2, param3, param4, param5); |
|---|
| 160 | else if (this->constObject_) |
|---|
| 161 | return (*this)(this->constObject_, param1, param2, param3, param4, param5); |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | COUT(1) << "An error occurred in Functor.h:" << std::endl; |
|---|
| 165 | COUT(1) << "Error: No object set." << std::endl; |
|---|
| 166 | return MT_Type::Null; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | inline FunctorMember<T>* setObject(T* object) |
|---|
| 171 | { |
|---|
| 172 | this->object_ = object; |
|---|
| 173 | this->constObject_ = 0; |
|---|
| 174 | return this; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | inline FunctorMember<T>* setObject(const T* object) |
|---|
| 178 | { |
|---|
| 179 | this->object_ = 0; |
|---|
| 180 | this->constObject_ = object; |
|---|
| 181 | return this; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | void setRawObjectPointer(void* object) |
|---|
| 185 | { |
|---|
| 186 | this->object_ = (T*)object; |
|---|
| 187 | this->constObject_ = 0; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void* getRawObjectPointer() const |
|---|
| 191 | { |
|---|
| 192 | if (this->object_) |
|---|
| 193 | return (void*)this->object_; |
|---|
| 194 | else |
|---|
| 195 | return (void*)this->constObject_; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | typedef std::pair<T*, const T*> Objects; |
|---|
| 199 | |
|---|
| 200 | inline Objects getObjects() const |
|---|
| 201 | { |
|---|
| 202 | return Objects(this->object_, this->constObject_); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | inline void setObjects(const Objects& objects) |
|---|
| 206 | { |
|---|
| 207 | this->object_ = objects.first; |
|---|
| 208 | this->constObject_ = objects.second; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | private: |
|---|
| 212 | T* object_; |
|---|
| 213 | const T* constObject_; |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | typedef SharedPtr<Functor> FunctorPtr; |
|---|
| 219 | |
|---|
| 220 | typedef SharedChildPtr<FunctorStatic, Functor> FunctorStaticPtr; |
|---|
| 221 | |
|---|
| 222 | template <class T> |
|---|
| 223 | class FunctorMemberPtr : public SharedChildPtr<FunctorMember<T>, Functor> |
|---|
| 224 | { |
|---|
| 225 | public: |
|---|
| 226 | inline FunctorMemberPtr() : SharedChildPtr<FunctorMember<T>, Functor>() {} |
|---|
| 227 | inline FunctorMemberPtr(FunctorMember<T>* pointer) : SharedChildPtr<FunctorMember<T>, Functor>(pointer) {} |
|---|
| 228 | // inline FunctorMemberPtr(const FunctorMemberPtr& other) : SharedChildPtr<FunctorMember<T>, Functor>(other) {} |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | template <class R, class P1, class P2, class P3, class P4, class P5> |
|---|
| 234 | struct FunctorHeaderIdentifier {}; |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | inline Functor* createFunctor(Functor* functor) |
|---|
| 239 | { |
|---|
| 240 | return functor; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | #define FUNCTOR_TEMPLATE(ismember, returnvalue, numparams, additionalobject) FUNCTOR_TEMPLATE##ismember##returnvalue##numparams(additionalobject) |
|---|
| 246 | #define FUNCTOR_TEMPLATE000(additionalobject) |
|---|
| 247 | #define FUNCTOR_TEMPLATE001(additionalobject) template <class P1> |
|---|
| 248 | #define FUNCTOR_TEMPLATE002(additionalobject) template <class P1, class P2> |
|---|
| 249 | #define FUNCTOR_TEMPLATE003(additionalobject) template <class P1, class P2, class P3> |
|---|
| 250 | #define FUNCTOR_TEMPLATE004(additionalobject) template <class P1, class P2, class P3, class P4> |
|---|
| 251 | #define FUNCTOR_TEMPLATE005(additionalobject) template <class P1, class P2, class P3, class P4, class P5> |
|---|
| 252 | #define FUNCTOR_TEMPLATE010(additionalobject) template <class R> |
|---|
| 253 | #define FUNCTOR_TEMPLATE011(additionalobject) template <class R, class P1> |
|---|
| 254 | #define FUNCTOR_TEMPLATE012(additionalobject) template <class R, class P1, class P2> |
|---|
| 255 | #define FUNCTOR_TEMPLATE013(additionalobject) template <class R, class P1, class P2, class P3> |
|---|
| 256 | #define FUNCTOR_TEMPLATE014(additionalobject) template <class R, class P1, class P2, class P3, class P4> |
|---|
| 257 | #define FUNCTOR_TEMPLATE015(additionalobject) template <class R, class P1, class P2, class P3, class P4, class P5> |
|---|
| 258 | #define FUNCTOR_TEMPLATE100(additionalobject) template <class T FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 259 | #define FUNCTOR_TEMPLATE101(additionalobject) template <class T, class P1 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 260 | #define FUNCTOR_TEMPLATE102(additionalobject) template <class T, class P1, class P2 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 261 | #define FUNCTOR_TEMPLATE103(additionalobject) template <class T, class P1, class P2, class P3 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 262 | #define FUNCTOR_TEMPLATE104(additionalobject) template <class T, class P1, class P2, class P3, class P4 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 263 | #define FUNCTOR_TEMPLATE105(additionalobject) template <class T, class P1, class P2, class P3, class P4, class P5 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 264 | #define FUNCTOR_TEMPLATE110(additionalobject) template <class T, class R FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 265 | #define FUNCTOR_TEMPLATE111(additionalobject) template <class T, class R, class P1 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 266 | #define FUNCTOR_TEMPLATE112(additionalobject) template <class T, class R, class P1, class P2 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 267 | #define FUNCTOR_TEMPLATE113(additionalobject) template <class T, class R, class P1, class P2, class P3 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 268 | #define FUNCTOR_TEMPLATE114(additionalobject) template <class T, class R, class P1, class P2, class P3, class P4 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 269 | #define FUNCTOR_TEMPLATE115(additionalobject) template <class T, class R, class P1, class P2, class P3, class P4, class P5 FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) > |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | #define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT(additionalobject) FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT##additionalobject |
|---|
| 274 | #define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT0 |
|---|
| 275 | #define FUNCTOR_TEMPLATE_ADDITIONAL_OBJECT1 , class O |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | #define FUNCTOR_TEMPLATE_CLASSES(ismember, returnvalue, numparams) FUNCTOR_TEMPLATE_CLASSES##ismember##returnvalue##numparams |
|---|
| 280 | #define FUNCTOR_TEMPLATE_CLASSES000 |
|---|
| 281 | #define FUNCTOR_TEMPLATE_CLASSES001 <P1> |
|---|
| 282 | #define FUNCTOR_TEMPLATE_CLASSES002 <P1, P2> |
|---|
| 283 | #define FUNCTOR_TEMPLATE_CLASSES003 <P1, P2, P3> |
|---|
| 284 | #define FUNCTOR_TEMPLATE_CLASSES004 <P1, P2, P3, P4> |
|---|
| 285 | #define FUNCTOR_TEMPLATE_CLASSES005 <P1, P2, P3, P4, P5> |
|---|
| 286 | #define FUNCTOR_TEMPLATE_CLASSES010 <R> |
|---|
| 287 | #define FUNCTOR_TEMPLATE_CLASSES011 <R, P1> |
|---|
| 288 | #define FUNCTOR_TEMPLATE_CLASSES012 <R, P1, P2> |
|---|
| 289 | #define FUNCTOR_TEMPLATE_CLASSES013 <R, P1, P2, P3> |
|---|
| 290 | #define FUNCTOR_TEMPLATE_CLASSES014 <R, P1, P2, P3, P4> |
|---|
| 291 | #define FUNCTOR_TEMPLATE_CLASSES015 <R, P1, P2, P3, P4, P5> |
|---|
| 292 | #define FUNCTOR_TEMPLATE_CLASSES100 <T> |
|---|
| 293 | #define FUNCTOR_TEMPLATE_CLASSES101 <T, P1> |
|---|
| 294 | #define FUNCTOR_TEMPLATE_CLASSES102 <T, P1, P2> |
|---|
| 295 | #define FUNCTOR_TEMPLATE_CLASSES103 <T, P1, P2, P3> |
|---|
| 296 | #define FUNCTOR_TEMPLATE_CLASSES104 <T, P1, P2, P3, P4> |
|---|
| 297 | #define FUNCTOR_TEMPLATE_CLASSES105 <T, P1, P2, P3, P4, P5> |
|---|
| 298 | #define FUNCTOR_TEMPLATE_CLASSES110 <T, R> |
|---|
| 299 | #define FUNCTOR_TEMPLATE_CLASSES111 <T, R, P1> |
|---|
| 300 | #define FUNCTOR_TEMPLATE_CLASSES112 <T, R, P1, P2> |
|---|
| 301 | #define FUNCTOR_TEMPLATE_CLASSES113 <T, R, P1, P2, P3> |
|---|
| 302 | #define FUNCTOR_TEMPLATE_CLASSES114 <T, R, P1, P2, P3, P4> |
|---|
| 303 | #define FUNCTOR_TEMPLATE_CLASSES115 <T, R, P1, P2, P3, P4, P5> |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | #define FUNCTOR_TYPENAME_PARAM(numparams) FUNCTOR_TYPENAME_PARAM##numparams |
|---|
| 308 | #define FUNCTOR_TYPENAME_PARAM0 \ |
|---|
| 309 | return BLANKSTRING |
|---|
| 310 | #define FUNCTOR_TYPENAME_PARAM1 \ |
|---|
| 311 | if (param == 0) { return typeToString<P1>(); } \ |
|---|
| 312 | else { return BLANKSTRING; } |
|---|
| 313 | #define FUNCTOR_TYPENAME_PARAM2 \ |
|---|
| 314 | if (param == 0) { return typeToString<P1>(); } \ |
|---|
| 315 | else if (param == 1) { return typeToString<P2>(); } \ |
|---|
| 316 | else { return BLANKSTRING; } |
|---|
| 317 | #define FUNCTOR_TYPENAME_PARAM3 \ |
|---|
| 318 | if (param == 0) { return typeToString<P1>(); } \ |
|---|
| 319 | else if (param == 1) { return typeToString<P2>(); } \ |
|---|
| 320 | else if (param == 2) { return typeToString<P3>(); } \ |
|---|
| 321 | else { return BLANKSTRING; } |
|---|
| 322 | #define FUNCTOR_TYPENAME_PARAM4 \ |
|---|
| 323 | if (param == 0) { return typeToString<P1>(); } \ |
|---|
| 324 | else if (param == 1) { return typeToString<P2>(); } \ |
|---|
| 325 | else if (param == 2) { return typeToString<P3>(); } \ |
|---|
| 326 | else if (param == 3) { return typeToString<P4>(); } \ |
|---|
| 327 | else { return BLANKSTRING; } |
|---|
| 328 | #define FUNCTOR_TYPENAME_PARAM5 \ |
|---|
| 329 | if (param == 0) { return typeToString<P1>(); } \ |
|---|
| 330 | else if (param == 1) { return typeToString<P2>(); } \ |
|---|
| 331 | else if (param == 2) { return typeToString<P3>(); } \ |
|---|
| 332 | else if (param == 3) { return typeToString<P4>(); } \ |
|---|
| 333 | else if (param == 4) { return typeToString<P5>(); } \ |
|---|
| 334 | else { return BLANKSTRING; } |
|---|
| 335 | |
|---|
| 336 | #define FUNCTOR_TYPENAME_RETURN(returnvalue) FUNCTOR_TYPENAME_RETURN##returnvalue |
|---|
| 337 | #define FUNCTOR_TYPENAME_RETURN0 BLANKSTRING |
|---|
| 338 | #define FUNCTOR_TYPENAME_RETURN1 typeToString<R>() |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | #define FUNCTOR_FUNCTION_PARAMS(numparams) FUNCTOR_FUNCTION_PARAMS##numparams |
|---|
| 343 | #define FUNCTOR_FUNCTION_PARAMS0 |
|---|
| 344 | #define FUNCTOR_FUNCTION_PARAMS1 P1 param1 |
|---|
| 345 | #define FUNCTOR_FUNCTION_PARAMS2 P1 param1, P2 param2 |
|---|
| 346 | #define FUNCTOR_FUNCTION_PARAMS3 P1 param1, P2 param2, P3 param3 |
|---|
| 347 | #define FUNCTOR_FUNCTION_PARAMS4 P1 param1, P2 param2, P3 param3, P4 param4 |
|---|
| 348 | #define FUNCTOR_FUNCTION_PARAMS5 P1 param1, P2 param2, P3 param3, P4 param4, P5 param5 |
|---|
| 349 | |
|---|
| 350 | #define FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) FUNCTOR_FUNCTION_RETURNVALUE##returnvalue |
|---|
| 351 | #define FUNCTOR_FUNCTION_RETURNVALUE0 void |
|---|
| 352 | #define FUNCTOR_FUNCTION_RETURNVALUE1 R |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | #define FUNCTOR_FUNCTION_CALL(numparams) FUNCTOR_FUNCTION_CALL##numparams |
|---|
| 357 | #define FUNCTOR_FUNCTION_CALL0 |
|---|
| 358 | #define FUNCTOR_FUNCTION_CALL1 param1 |
|---|
| 359 | #define FUNCTOR_FUNCTION_CALL2 param1, param2 |
|---|
| 360 | #define FUNCTOR_FUNCTION_CALL3 param1, param2, param3 |
|---|
| 361 | #define FUNCTOR_FUNCTION_CALL4 param1, param2, param3, param4 |
|---|
| 362 | #define FUNCTOR_FUNCTION_CALL5 param1, param2, param3, param4, param5 |
|---|
| 363 | |
|---|
| 364 | #define FUNCTOR_STORE_RETURNVALUE(returnvalue, functioncall) FUNCTOR_STORE_RETURNVALUE##returnvalue(functioncall) |
|---|
| 365 | #define FUNCTOR_STORE_RETURNVALUE0(functioncall) functioncall; return MT_Type::Null |
|---|
| 366 | #define FUNCTOR_STORE_RETURNVALUE1(functioncall) return functioncall |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES##numparams(returnvalue) |
|---|
| 371 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void> |
|---|
| 372 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void> |
|---|
| 373 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void> |
|---|
| 374 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void> |
|---|
| 375 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void> |
|---|
| 376 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5> |
|---|
| 377 | |
|---|
| 378 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue |
|---|
| 379 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE0 void |
|---|
| 380 | #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE1 R |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | #define FUNCTOR_EVALUATE_PARAM(numparams) FUNCTOR_EVALUATE_PARAM##numparams |
|---|
| 385 | #define FUNCTOR_EVALUATE_PARAM0 |
|---|
| 386 | #define FUNCTOR_EVALUATE_PARAM1 \ |
|---|
| 387 | if (index == 0) { param.convert<P1>(); } |
|---|
| 388 | #define FUNCTOR_EVALUATE_PARAM2 \ |
|---|
| 389 | if (index == 0) { param.convert<P1>(); } \ |
|---|
| 390 | else if (index == 1) { param.convert<P2>(); } |
|---|
| 391 | #define FUNCTOR_EVALUATE_PARAM3 \ |
|---|
| 392 | if (index == 0) { param.convert<P1>(); } \ |
|---|
| 393 | else if (index == 1) { param.convert<P2>(); } \ |
|---|
| 394 | else if (index == 2) { param.convert<P3>(); } |
|---|
| 395 | #define FUNCTOR_EVALUATE_PARAM4 \ |
|---|
| 396 | if (index == 0) { param.convert<P1>(); } \ |
|---|
| 397 | else if (index == 1) { param.convert<P2>(); } \ |
|---|
| 398 | else if (index == 2) { param.convert<P3>(); } \ |
|---|
| 399 | else if (index == 3) { param.convert<P4>(); } |
|---|
| 400 | #define FUNCTOR_EVALUATE_PARAM5 \ |
|---|
| 401 | if (index == 0) { param.convert<P1>(); } \ |
|---|
| 402 | else if (index == 1) { param.convert<P2>(); } \ |
|---|
| 403 | else if (index == 2) { param.convert<P3>(); } \ |
|---|
| 404 | else if (index == 3) { param.convert<P4>(); } \ |
|---|
| 405 | else if (index == 4) { param.convert<P5>(); } |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | #define CREATE_STATIC_FUNCTOR(returnvalue, numparams) \ |
|---|
| 411 | FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \ |
|---|
| 412 | class FunctorStatic##returnvalue##numparams : public FunctorStatic \ |
|---|
| 413 | { \ |
|---|
| 414 | public: \ |
|---|
| 415 | FunctorStatic##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \ |
|---|
| 416 | { \ |
|---|
| 417 | this->functionPointer_ = functionPointer; \ |
|---|
| 418 | } \ |
|---|
| 419 | \ |
|---|
| 420 | MultiType operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \ |
|---|
| 421 | { \ |
|---|
| 422 | FUNCTOR_STORE_RETURNVALUE(returnvalue, (*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \ |
|---|
| 423 | } \ |
|---|
| 424 | \ |
|---|
| 425 | void evaluateParam(unsigned int index, MultiType& param) const \ |
|---|
| 426 | { \ |
|---|
| 427 | FUNCTOR_EVALUATE_PARAM(numparams); \ |
|---|
| 428 | } \ |
|---|
| 429 | \ |
|---|
| 430 | Functor::Type::Enum getType() const { return Functor::Type::Static; } \ |
|---|
| 431 | unsigned int getParamCount() const { return numparams; } \ |
|---|
| 432 | bool hasReturnvalue() const { return returnvalue; } \ |
|---|
| 433 | std::string getTypenameParam(unsigned int param) const { FUNCTOR_TYPENAME_PARAM(numparams); } \ |
|---|
| 434 | std::string getTypenameReturnvalue() const { return FUNCTOR_TYPENAME_RETURN(returnvalue); } \ |
|---|
| 435 | \ |
|---|
| 436 | const std::type_info& getHeaderIdentifier() const \ |
|---|
| 437 | { \ |
|---|
| 438 | return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \ |
|---|
| 439 | } \ |
|---|
| 440 | \ |
|---|
| 441 | private: \ |
|---|
| 442 | FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \ |
|---|
| 443 | }; \ |
|---|
| 444 | \ |
|---|
| 445 | \ |
|---|
| 446 | FUNCTOR_TEMPLATE(0, returnvalue, numparams, 0) \ |
|---|
| 447 | inline FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \ |
|---|
| 448 | { \ |
|---|
| 449 | return new FunctorStatic##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(0, returnvalue, numparams) (functionPointer); \ |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | #define CREATE_MEMBER_FUNCTOR(returnvalue, numparams) \ |
|---|
| 457 | FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \ |
|---|
| 458 | class FunctorMember##returnvalue##numparams : public FunctorMember<T> \ |
|---|
| 459 | { \ |
|---|
| 460 | public: \ |
|---|
| 461 | FunctorMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \ |
|---|
| 462 | { \ |
|---|
| 463 | this->functionPointer_ = functionPointer; \ |
|---|
| 464 | } \ |
|---|
| 465 | \ |
|---|
| 466 | MultiType operator()(T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \ |
|---|
| 467 | { \ |
|---|
| 468 | FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \ |
|---|
| 469 | } \ |
|---|
| 470 | \ |
|---|
| 471 | MultiType operator()(const T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \ |
|---|
| 472 | { \ |
|---|
| 473 | COUT(1) << "An error occurred in Functor.h:" << std::endl; \ |
|---|
| 474 | COUT(1) << "Error: Function is not const." << std::endl; \ |
|---|
| 475 | return MT_Type::Null; \ |
|---|
| 476 | } \ |
|---|
| 477 | \ |
|---|
| 478 | void evaluateParam(unsigned int index, MultiType& param) const \ |
|---|
| 479 | { \ |
|---|
| 480 | FUNCTOR_EVALUATE_PARAM(numparams); \ |
|---|
| 481 | } \ |
|---|
| 482 | \ |
|---|
| 483 | Functor::Type::Enum getType() const { return Functor::Type::Member; } \ |
|---|
| 484 | unsigned int getParamCount() const { return numparams; } \ |
|---|
| 485 | bool hasReturnvalue() const { return returnvalue; } \ |
|---|
| 486 | std::string getTypenameParam(unsigned int param) const { FUNCTOR_TYPENAME_PARAM(numparams); } \ |
|---|
| 487 | std::string getTypenameReturnvalue() const { return FUNCTOR_TYPENAME_RETURN(returnvalue); } \ |
|---|
| 488 | \ |
|---|
| 489 | const std::type_info& getHeaderIdentifier() const \ |
|---|
| 490 | { \ |
|---|
| 491 | return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \ |
|---|
| 492 | } \ |
|---|
| 493 | \ |
|---|
| 494 | private: \ |
|---|
| 495 | FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)); \ |
|---|
| 496 | }; \ |
|---|
| 497 | \ |
|---|
| 498 | \ |
|---|
| 499 | FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \ |
|---|
| 500 | class FunctorConstMember##returnvalue##numparams : public FunctorMember<T> \ |
|---|
| 501 | { \ |
|---|
| 502 | public: \ |
|---|
| 503 | FunctorConstMember##returnvalue##numparams(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \ |
|---|
| 504 | { \ |
|---|
| 505 | this->functionPointer_ = functionPointer; \ |
|---|
| 506 | } \ |
|---|
| 507 | \ |
|---|
| 508 | MultiType operator()(T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \ |
|---|
| 509 | { \ |
|---|
| 510 | FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \ |
|---|
| 511 | } \ |
|---|
| 512 | \ |
|---|
| 513 | MultiType operator()(const T* object, const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) \ |
|---|
| 514 | { \ |
|---|
| 515 | FUNCTOR_STORE_RETURNVALUE(returnvalue, (*object.*this->functionPointer_)(FUNCTOR_FUNCTION_CALL(numparams))); \ |
|---|
| 516 | } \ |
|---|
| 517 | \ |
|---|
| 518 | void evaluateParam(unsigned int index, MultiType& param) const \ |
|---|
| 519 | { \ |
|---|
| 520 | FUNCTOR_EVALUATE_PARAM(numparams); \ |
|---|
| 521 | } \ |
|---|
| 522 | \ |
|---|
| 523 | Functor::Type::Enum getType() const { return Functor::Type::ConstMember; } \ |
|---|
| 524 | unsigned int getParamCount() const { return numparams; } \ |
|---|
| 525 | bool hasReturnvalue() const { return returnvalue; } \ |
|---|
| 526 | std::string getTypenameParam(unsigned int param) const { FUNCTOR_TYPENAME_PARAM(numparams); } \ |
|---|
| 527 | std::string getTypenameReturnvalue() const { return FUNCTOR_TYPENAME_RETURN(returnvalue); } \ |
|---|
| 528 | \ |
|---|
| 529 | const std::type_info& getHeaderIdentifier() const \ |
|---|
| 530 | { \ |
|---|
| 531 | return typeid(FunctorHeaderIdentifier FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams)); \ |
|---|
| 532 | } \ |
|---|
| 533 | \ |
|---|
| 534 | private: \ |
|---|
| 535 | FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer_)(FUNCTOR_FUNCTION_PARAMS(numparams)) const; \ |
|---|
| 536 | }; \ |
|---|
| 537 | \ |
|---|
| 538 | \ |
|---|
| 539 | FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \ |
|---|
| 540 | inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams))) \ |
|---|
| 541 | { \ |
|---|
| 542 | return new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \ |
|---|
| 543 | } \ |
|---|
| 544 | \ |
|---|
| 545 | \ |
|---|
| 546 | FUNCTOR_TEMPLATE(1, returnvalue, numparams, 0) \ |
|---|
| 547 | inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const) \ |
|---|
| 548 | { \ |
|---|
| 549 | return new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \ |
|---|
| 550 | } \ |
|---|
| 551 | \ |
|---|
| 552 | FUNCTOR_TEMPLATE(1, returnvalue, numparams, 1) \ |
|---|
| 553 | inline FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)), O* object) \ |
|---|
| 554 | { \ |
|---|
| 555 | FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* functor = new FunctorMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \ |
|---|
| 556 | functor->setObject(object); \ |
|---|
| 557 | return functor; \ |
|---|
| 558 | } \ |
|---|
| 559 | \ |
|---|
| 560 | \ |
|---|
| 561 | FUNCTOR_TEMPLATE(1, returnvalue, numparams, 1) \ |
|---|
| 562 | inline FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* createFunctor(FUNCTOR_FUNCTION_RETURNVALUE(returnvalue) (T::*functionPointer)(FUNCTOR_FUNCTION_PARAMS(numparams)) const, O* object) \ |
|---|
| 563 | { \ |
|---|
| 564 | FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams)* functor = new FunctorConstMember##returnvalue##numparams FUNCTOR_TEMPLATE_CLASSES(1, returnvalue, numparams) (functionPointer); \ |
|---|
| 565 | functor->setObject(object); \ |
|---|
| 566 | return functor; \ |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | // disable annoying warning about forcing value to boolean |
|---|
| 572 | #ifdef ORXONOX_COMPILER_MSVC |
|---|
| 573 | #pragma warning(push) |
|---|
| 574 | #pragma warning(disable:4100 4800) |
|---|
| 575 | #endif |
|---|
| 576 | |
|---|
| 577 | #define CREATE_ALL_STATIC_FUNCTORS() \ |
|---|
| 578 | CREATE_STATIC_FUNCTOR(0, 0); \ |
|---|
| 579 | CREATE_STATIC_FUNCTOR(0, 1); \ |
|---|
| 580 | CREATE_STATIC_FUNCTOR(0, 2); \ |
|---|
| 581 | CREATE_STATIC_FUNCTOR(0, 3); \ |
|---|
| 582 | CREATE_STATIC_FUNCTOR(0, 4); \ |
|---|
| 583 | CREATE_STATIC_FUNCTOR(0, 5); \ |
|---|
| 584 | CREATE_STATIC_FUNCTOR(1, 0); \ |
|---|
| 585 | CREATE_STATIC_FUNCTOR(1, 1); \ |
|---|
| 586 | CREATE_STATIC_FUNCTOR(1, 2); \ |
|---|
| 587 | CREATE_STATIC_FUNCTOR(1, 3); \ |
|---|
| 588 | CREATE_STATIC_FUNCTOR(1, 4); \ |
|---|
| 589 | CREATE_STATIC_FUNCTOR(1, 5) |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | #define CREATE_ALL_MEMBER_FUNCTORS() \ |
|---|
| 593 | CREATE_MEMBER_FUNCTOR(0, 0); \ |
|---|
| 594 | CREATE_MEMBER_FUNCTOR(0, 1); \ |
|---|
| 595 | CREATE_MEMBER_FUNCTOR(0, 2); \ |
|---|
| 596 | CREATE_MEMBER_FUNCTOR(0, 3); \ |
|---|
| 597 | CREATE_MEMBER_FUNCTOR(0, 4); \ |
|---|
| 598 | CREATE_MEMBER_FUNCTOR(0, 5); \ |
|---|
| 599 | CREATE_MEMBER_FUNCTOR(1, 0); \ |
|---|
| 600 | CREATE_MEMBER_FUNCTOR(1, 1); \ |
|---|
| 601 | CREATE_MEMBER_FUNCTOR(1, 2); \ |
|---|
| 602 | CREATE_MEMBER_FUNCTOR(1, 3); \ |
|---|
| 603 | CREATE_MEMBER_FUNCTOR(1, 4); \ |
|---|
| 604 | CREATE_MEMBER_FUNCTOR(1, 5) |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | CREATE_ALL_STATIC_FUNCTORS(); |
|---|
| 608 | CREATE_ALL_MEMBER_FUNCTORS(); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | #ifdef ORXONOX_COMPILER_MSVC |
|---|
| 612 | #pragma warning(pop) |
|---|
| 613 | #endif |
|---|
| 614 | |
|---|
| 615 | #endif /* _Functor_H__ */ |
|---|