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