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