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