| 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 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file | 
|---|
| 31 | @brief Definition of all super-function related macros. | 
|---|
| 32 |  | 
|---|
| 33 | This file defines all macros needed to add a new "super-function". | 
|---|
| 34 | If you add a super-function, you can call SUPER(myclass, functionname) inside your | 
|---|
| 35 | code and the function of the parentclass gets called. This is comparable with | 
|---|
| 36 | super.functionname() in Java or other languages. | 
|---|
| 37 |  | 
|---|
| 38 | This works only with virtual functions that return nothing (void) and belong to | 
|---|
| 39 | classes that have an Identifier. Arguments however are supported. | 
|---|
| 40 |  | 
|---|
| 41 | To add a new super-function, you have process 4 steps: | 
|---|
| 42 |  | 
|---|
| 43 | 1) Add a new SUPER macro | 
|---|
| 44 | This allows you to call the super-function in your code. | 
|---|
| 45 | Location: This file (Super.h), marked with --> HERE <-- comments (1/3) | 
|---|
| 46 |  | 
|---|
| 47 | 2) Call the SUPER_FUNCTION_GLOBAL_DECLARATION_PART1/2 macros. | 
|---|
| 48 | This defines some global classes and templates, needed to create and call the super-functions. | 
|---|
| 49 | Location: This file (Super.h), marked with --> HERE <-- comments (2/3) | 
|---|
| 50 |  | 
|---|
| 51 | 3) Call the SUPER_INTRUSIVE_DECLARATION macro. | 
|---|
| 52 | This will be included into the declaration of ClassIdentifier<T>. | 
|---|
| 53 | Location: This file (Super.h), marked with --> HERE <-- comments (3/3) | 
|---|
| 54 |  | 
|---|
| 55 | 4) Call the SUPER_FUNCTION macro. | 
|---|
| 56 | This defines a partially specialized template that will decide if a class is "super" to another class. | 
|---|
| 57 | If the check returns true, a SuperFunctionCaller gets created, which will be used by the SUPER macro. | 
|---|
| 58 | You have to add this into the header-file of the baseclass of the super-function (the class that first | 
|---|
| 59 | implements the function), below the class declaration. You can't call it directly in this file, because | 
|---|
| 60 | otherwise you had to include the headerfile right here, which would cause some ugly backdependencies, | 
|---|
| 61 | include loops and slower compilation. | 
|---|
| 62 | Dont forget to include Super.h in the header-file. | 
|---|
| 63 | Location: The header-file of the baseclass (Baseclass.h), below the class declaration | 
|---|
| 64 | */ | 
|---|
| 65 |  | 
|---|
| 66 | #ifndef _Super_H__ | 
|---|
| 67 | #define _Super_H__ | 
|---|
| 68 |  | 
|---|
| 69 | #include "CorePrereqs.h" | 
|---|
| 70 |  | 
|---|
| 71 | #include "util/Debug.h" | 
|---|
| 72 | #include "Event.h" | 
|---|
| 73 |  | 
|---|
| 74 | /////////////////////// | 
|---|
| 75 | // Macro definitions // | 
|---|
| 76 | /////////////////////// | 
|---|
| 77 |  | 
|---|
| 78 | //// Common macros //// | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | @brief Declares a new super-function by creating a specialized template. Add this below the class declaration of the baseclass. | 
|---|
| 82 | @param functionnumber Each super-function needs a unique number, starting with zero, increasing by one | 
|---|
| 83 | @param baseclass The baseclass of the super-function (~the root) | 
|---|
| 84 | @param functionname The name of the super-function | 
|---|
| 85 | @param purevirtualbase "true" if the function is pure virtual in the baseclass, "false" if the function is implemented (without "") | 
|---|
| 86 | */ | 
|---|
| 87 | #define SUPER_FUNCTION(functionnumber, baseclass, functionname, purevirtualbase) \ | 
|---|
| 88 | template <class T, int templatehack2> \ | 
|---|
| 89 | struct SuperFunctionCondition<functionnumber, T, 0, templatehack2> \ | 
|---|
| 90 | { \ | 
|---|
| 91 | static void check() \ | 
|---|
| 92 | { \ | 
|---|
| 93 | SuperFunctionCondition<functionnumber, T, 0, templatehack2>::apply(static_cast<T*>(0)); \ | 
|---|
| 94 | SuperFunctionCondition<functionnumber + 1, T, 0, templatehack2>::check(); \ | 
|---|
| 95 | } \ | 
|---|
| 96 | \ | 
|---|
| 97 | static void apply(void* temp) {} \ | 
|---|
| 98 | \ | 
|---|
| 99 | static void apply(baseclass* temp) \ | 
|---|
| 100 | { \ | 
|---|
| 101 | ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); \ | 
|---|
| 102 | for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it) \ | 
|---|
| 103 | { \ | 
|---|
| 104 | if (((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ && ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \ | 
|---|
| 105 | { \ | 
|---|
| 106 | delete ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_; \ | 
|---|
| 107 | ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = 0; \ | 
|---|
| 108 | ((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ = false; \ | 
|---|
| 109 | } \ | 
|---|
| 110 | \ | 
|---|
| 111 | if (!((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \ | 
|---|
| 112 | { \ | 
|---|
| 113 | COUT(5) << "Added SuperFunctionCaller for " << #functionname << ": " << ClassIdentifier<T>::getIdentifier()->getName() << " <- " << ((ClassIdentifier<T>*)(*it))->getName() << std::endl; \ | 
|---|
| 114 | ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_##functionname <T>; \ | 
|---|
| 115 | } \ | 
|---|
| 116 | } \ | 
|---|
| 117 | } \ | 
|---|
| 118 | }; \ | 
|---|
| 119 | \ | 
|---|
| 120 | SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND##purevirtualbase(functionnumber, baseclass) | 
|---|
| 121 |  | 
|---|
| 122 | #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND0(functionnumber, baseclass) SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDfalse(functionnumber, baseclass) | 
|---|
| 123 | #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND1(functionnumber, baseclass) SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDtrue(functionnumber, baseclass) | 
|---|
| 124 | #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDfalse(functionnumber, baseclass) | 
|---|
| 125 | #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDtrue(functionnumber, baseclass) \ | 
|---|
| 126 | template <int templatehack2> \ | 
|---|
| 127 | struct SuperFunctionCondition<functionnumber, baseclass, 0, templatehack2> \ | 
|---|
| 128 | { \ | 
|---|
| 129 | static void check() \ | 
|---|
| 130 | { \ | 
|---|
| 131 | SuperFunctionCondition<functionnumber + 1, baseclass, 0, templatehack2>::check(); \ | 
|---|
| 132 | } \ | 
|---|
| 133 | }; | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | /* | 
|---|
| 137 | //// Comments about the macro //// | 
|---|
| 138 |  | 
|---|
| 139 | // Partially specialized template (templatehack is now specialized too). | 
|---|
| 140 | // | 
|---|
| 141 | // This ensures the compiler takes THIS template if the header-file of the super-function | 
|---|
| 142 | // is included. In any other case, the compiler just uses the fallback template which is | 
|---|
| 143 | // defined in this file. | 
|---|
| 144 | template <class T, templatehack2> | 
|---|
| 145 | struct SuperFunctionCondition<functionnumber, T, 0, templatehack2> | 
|---|
| 146 | { | 
|---|
| 147 | static void check() | 
|---|
| 148 | { | 
|---|
| 149 | // This call to the apply-function is the whole check. By calling the function with | 
|---|
| 150 | // a T* pointer, the right function get's called. | 
|---|
| 151 | SuperFunctionCondition<functionnumber, T, 0, templatehack2>::apply(static_cast<T*>(0)); | 
|---|
| 152 |  | 
|---|
| 153 | // Go go the check for of next super-function (functionnumber + 1) | 
|---|
| 154 | SuperFunctionCondition<functionnumber + 1, T, 0, templatehack2>::check(); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | // This function gets called if T is not a child of the baseclass. | 
|---|
| 158 | // The function does nothing. | 
|---|
| 159 | static void apply(void* temp) {} | 
|---|
| 160 |  | 
|---|
| 161 | // This function gets called if T is a child of the baseclass and can therefore be converted. | 
|---|
| 162 | // The function adds a SuperFunctionCaller to the Identifier of all subclasses of T. | 
|---|
| 163 | static void apply(baseclass* temp) | 
|---|
| 164 | { | 
|---|
| 165 | ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); | 
|---|
| 166 |  | 
|---|
| 167 | // Iterate through all children | 
|---|
| 168 | for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it) | 
|---|
| 169 | { | 
|---|
| 170 | // Check if the caller is a fallback-caller | 
|---|
| 171 | if (((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ && ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) | 
|---|
| 172 | { | 
|---|
| 173 | // Delete the fallback caller an prepare to get a real caller | 
|---|
| 174 | delete ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_; | 
|---|
| 175 | ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = 0; | 
|---|
| 176 | ((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ = false; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | // Check if there's not already a caller | 
|---|
| 180 | if (!((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) | 
|---|
| 181 | { | 
|---|
| 182 | // Add the SuperFunctionCaller | 
|---|
| 183 | COUT(5) << "adding functionpointer to " << ((ClassIdentifier<T>*)(*it))->getName() << std::endl; | 
|---|
| 184 | ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_##functionname <T>; | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 | }; | 
|---|
| 189 | SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND##purevirtualbase | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 | // The following piece of code is only added if purevirtualbase = true | 
|---|
| 193 |  | 
|---|
| 194 | // Explicit specialization of the Condition template for the baseclass to avoid | 
|---|
| 195 | // errors if the function is pure virtual in the baseclass. | 
|---|
| 196 | template <int templatehack2> \ | 
|---|
| 197 | struct SuperFunctionCondition<functionnumber, baseclass, 0, templatehack2> \ | 
|---|
| 198 | { \ | 
|---|
| 199 | // The check function acts like the fallback - it advances to the check for the next super-function (functionnumber + 1) | 
|---|
| 200 | static void check() \ | 
|---|
| 201 | { \ | 
|---|
| 202 | SuperFunctionCondition<functionnumber + 1, baseclass, 0, templatehack2>::check(); \ | 
|---|
| 203 | } \ | 
|---|
| 204 | }; | 
|---|
| 205 | */ | 
|---|
| 206 |  | 
|---|
| 207 | // SUPER-macro: Calls Parent::functionname() where Parent is the direct parent of classname | 
|---|
| 208 | #ifdef ORXONOX_COMPILER_MSVC | 
|---|
| 209 | #define SUPER(classname, functionname, ...) \ | 
|---|
| 210 | __super::functionname(__VA_ARGS__) | 
|---|
| 211 | #else | 
|---|
| 212 | #define SUPER(classname, functionname, ...) \ | 
|---|
| 213 | SUPER_##functionname(classname, functionname, __VA_ARGS__) | 
|---|
| 214 | #endif | 
|---|
| 215 |  | 
|---|
| 216 | // helper macro: for functions without arguments | 
|---|
| 217 | #define SUPER_NOARGS(classname, functionname) \ | 
|---|
| 218 | (*ClassIdentifier<classname>::getIdentifier()->superFunctionCaller_##functionname##_)(this) | 
|---|
| 219 |  | 
|---|
| 220 | // helper macro: for functions with arguments | 
|---|
| 221 | #define SUPER_ARGS(classname, functionname, ...) \ | 
|---|
| 222 | (*ClassIdentifier<classname>::getIdentifier()->superFunctionCaller_##functionname##_)(this, __VA_ARGS__) | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|
| 225 | //// Function-specific macros //// | 
|---|
| 226 |  | 
|---|
| 227 | /* | 
|---|
| 228 | Add a macro for each super-function | 
|---|
| 229 |  | 
|---|
| 230 | Example (no arguments): | 
|---|
| 231 | #define SUPER_myfunction(classname, functionname, ...) \ | 
|---|
| 232 | SUPER_NOARGS(classname, functionname) | 
|---|
| 233 |  | 
|---|
| 234 | Example (with arguments): | 
|---|
| 235 | #define SUPER_myfunction(classname, functionname, ...) \ | 
|---|
| 236 | SUPER_ARGS(classname, functionname, __VA_ARGS__) | 
|---|
| 237 | */ | 
|---|
| 238 |  | 
|---|
| 239 | // (1/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- | 
|---|
| 240 | #define SUPER_XMLPort(classname, functionname, ...) \ | 
|---|
| 241 | SUPER_ARGS(classname, functionname, __VA_ARGS__) | 
|---|
| 242 |  | 
|---|
| 243 | #define SUPER_tick(classname, functionname, ...) \ | 
|---|
| 244 | SUPER_ARGS(classname, functionname, __VA_ARGS__) | 
|---|
| 245 |  | 
|---|
| 246 | #define SUPER_changedActivity(classname, functionname, ...) \ | 
|---|
| 247 | SUPER_NOARGS(classname, functionname) | 
|---|
| 248 |  | 
|---|
| 249 | #define SUPER_changedVisibility(classname, functionname, ...) \ | 
|---|
| 250 | SUPER_NOARGS(classname, functionname) | 
|---|
| 251 |  | 
|---|
| 252 | #define SUPER_processEvent(classname, functionname, ...) \ | 
|---|
| 253 | SUPER_ARGS(classname, functionname, __VA_ARGS__) | 
|---|
| 254 |  | 
|---|
| 255 | #define SUPER_changedScale(classname, functionname, ...) \ | 
|---|
| 256 | SUPER_NOARGS(classname, functionname) | 
|---|
| 257 |  | 
|---|
| 258 | #define SUPER_changedMainState(classname, functionname, ...) \ | 
|---|
| 259 | SUPER_NOARGS(classname, functionname) | 
|---|
| 260 |  | 
|---|
| 261 | #define SUPER_changedOwner(classname, functionname, ...) \ | 
|---|
| 262 | SUPER_NOARGS(classname, functionname) | 
|---|
| 263 |  | 
|---|
| 264 | #define SUPER_changedOverlayGroup(classname, functionname, ...) \ | 
|---|
| 265 | SUPER_NOARGS(classname, functionname) | 
|---|
| 266 |  | 
|---|
| 267 | #define SUPER_changedName(classname, functionname, ...) \ | 
|---|
| 268 | SUPER_NOARGS(classname, functionname) | 
|---|
| 269 |  | 
|---|
| 270 | #define SUPER_changedGametype(classname, functionname, ...) \ | 
|---|
| 271 | SUPER_NOARGS(classname, functionname) | 
|---|
| 272 | // (1/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- | 
|---|
| 273 |  | 
|---|
| 274 |  | 
|---|
| 275 | namespace orxonox | 
|---|
| 276 | { | 
|---|
| 277 | ///////////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 278 | // This code gets included by Identifier.h and every other header file that needs a super-function // | 
|---|
| 279 | ///////////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 280 |  | 
|---|
| 281 | //// Common code //// | 
|---|
| 282 |  | 
|---|
| 283 | // Base templates | 
|---|
| 284 | /** | 
|---|
| 285 | @brief Creates the SuperFunctionCaller if T is a child of the super-functions baseclass. | 
|---|
| 286 | */ | 
|---|
| 287 | template <int functionnumber, class T, int templatehack1, int templatehack2> | 
|---|
| 288 | struct SuperFunctionCondition | 
|---|
| 289 | { | 
|---|
| 290 | static void check() {} | 
|---|
| 291 | }; | 
|---|
| 292 |  | 
|---|
| 293 | /** | 
|---|
| 294 | @brief Initializes the SuperFunctionCaller-pointer with zero. | 
|---|
| 295 | */ | 
|---|
| 296 | template <int functionnumber, class T> | 
|---|
| 297 | struct SuperFunctionInitialization | 
|---|
| 298 | { | 
|---|
| 299 | static void initialize(ClassIdentifier<T>* identifier) {} | 
|---|
| 300 | }; | 
|---|
| 301 |  | 
|---|
| 302 | /** | 
|---|
| 303 | @brief Deletes the SuperFunctionCaller. | 
|---|
| 304 | */ | 
|---|
| 305 | template <int functionnumber, class T> | 
|---|
| 306 | struct SuperFunctionDestruction | 
|---|
| 307 | { | 
|---|
| 308 | static void destroy(ClassIdentifier<T>* identifier) {} | 
|---|
| 309 | }; | 
|---|
| 310 |  | 
|---|
| 311 |  | 
|---|
| 312 | //// Function-specific code //// | 
|---|
| 313 |  | 
|---|
| 314 | /** | 
|---|
| 315 | @brief Creates the needed objects and templates to call a super-function. | 
|---|
| 316 | @param functionnumber Each super-function needs a unique number, starting with zero, increasing by one | 
|---|
| 317 | @param functionname The name of the super-function | 
|---|
| 318 | @param hasarguments "false" if the function doesn't take any arguments, "true" if it does (without "") | 
|---|
| 319 | @param ... Variadic: If the function takes arguments, add them here with type and name. Example: int myvalue, float myothervalue | 
|---|
| 320 | */ | 
|---|
| 321 | #define SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(functionnumber, functionname, hasarguments, ...) \ | 
|---|
| 322 | template <class T, int templatehack1, int templatehack2> \ | 
|---|
| 323 | struct SuperFunctionCondition<functionnumber, T, templatehack1, templatehack2> \ | 
|---|
| 324 | { \ | 
|---|
| 325 | static void check() \ | 
|---|
| 326 | { \ | 
|---|
| 327 | SuperFunctionCondition<functionnumber + 1, T, templatehack1, templatehack2>::check(); \ | 
|---|
| 328 | } \ | 
|---|
| 329 | }; \ | 
|---|
| 330 | \ | 
|---|
| 331 | class _CoreExport SuperFunctionCaller_##functionname \ | 
|---|
| 332 | { \ | 
|---|
| 333 | public: \ | 
|---|
| 334 | virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0; \ | 
|---|
| 335 | virtual ~SuperFunctionCaller_##functionname () {} \ | 
|---|
| 336 | }; \ | 
|---|
| 337 | \ | 
|---|
| 338 | template <class T> \ | 
|---|
| 339 | class SuperFunctionClassCaller_purevirtualfallback_##functionname : public SuperFunctionCaller_##functionname \ | 
|---|
| 340 | { \ | 
|---|
| 341 | public: \ | 
|---|
| 342 | inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) \ | 
|---|
| 343 | { \ | 
|---|
| 344 | } \ | 
|---|
| 345 | }; \ | 
|---|
| 346 | \ | 
|---|
| 347 | template <class T> \ | 
|---|
| 348 | struct SuperFunctionInitialization<functionnumber, T> \ | 
|---|
| 349 | { \ | 
|---|
| 350 | static void initialize(ClassIdentifier<T>* identifier) \ | 
|---|
| 351 | { \ | 
|---|
| 352 | identifier->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_purevirtualfallback_##functionname <T>; \ | 
|---|
| 353 | identifier->bSuperFunctionCaller_##functionname##_isFallback_ = true; \ | 
|---|
| 354 | SuperFunctionInitialization<functionnumber + 1, T>::initialize(identifier); \ | 
|---|
| 355 | } \ | 
|---|
| 356 | }; \ | 
|---|
| 357 | \ | 
|---|
| 358 | template <class T> \ | 
|---|
| 359 | struct SuperFunctionDestruction<functionnumber, T> \ | 
|---|
| 360 | { \ | 
|---|
| 361 | static void destroy(ClassIdentifier<T>* identifier) \ | 
|---|
| 362 | { \ | 
|---|
| 363 | if (identifier->superFunctionCaller_##functionname##_) \ | 
|---|
| 364 | delete identifier->superFunctionCaller_##functionname##_; \ | 
|---|
| 365 | SuperFunctionDestruction<functionnumber + 1, T>::destroy(identifier); \ | 
|---|
| 366 | } \ | 
|---|
| 367 | }; \ | 
|---|
| 368 | \ | 
|---|
| 369 | template <class T> \ | 
|---|
| 370 | class SuperFunctionClassCaller_##functionname : public SuperFunctionCaller_##functionname \ | 
|---|
| 371 | { \ | 
|---|
| 372 | public: \ | 
|---|
| 373 | inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) \ | 
|---|
| 374 | { \ | 
|---|
| 375 | (dynamic_cast<T*>(object))->T:: functionname | 
|---|
| 376 |  | 
|---|
| 377 | /* | 
|---|
| 378 | JUST ADD THE FUNCTION ARGUMENTS BETWEEN BOTH MACROS, ENCLOSED BY BRACKETS | 
|---|
| 379 | EXAMPLE: | 
|---|
| 380 |  | 
|---|
| 381 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(0, myfunction, true, int myvalue, float myothervalue) <-- !!! DONT ADD A SEMICOLON HERE !!! | 
|---|
| 382 | (myvalue, myothervalue) | 
|---|
| 383 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2 | 
|---|
| 384 | */ | 
|---|
| 385 |  | 
|---|
| 386 | #define SUPER_FUNCTION_GLOBAL_DECLARATION_PART2 \ | 
|---|
| 387 | ; \ | 
|---|
| 388 | } \ | 
|---|
| 389 | }; | 
|---|
| 390 |  | 
|---|
| 391 | #define SUPER_CALL_ARGUMENTSfalse(...) OrxonoxClass* object | 
|---|
| 392 | #define SUPER_CALL_ARGUMENTS0(...)     OrxonoxClass* object | 
|---|
| 393 | #define SUPER_CALL_ARGUMENTStrue(...) OrxonoxClass* object, __VA_ARGS__ | 
|---|
| 394 | #define SUPER_CALL_ARGUMENTS1(...)    OrxonoxClass* object, __VA_ARGS__ | 
|---|
| 395 |  | 
|---|
| 396 |  | 
|---|
| 397 | /* | 
|---|
| 398 | //// COMMENTS ABOUT THE MACRO //// | 
|---|
| 399 |  | 
|---|
| 400 | // Partially specialized template (templatehack not yet specialized, this | 
|---|
| 401 | // will be done by the real condition in the header-file of the super-function) | 
|---|
| 402 | // Only used as fallback | 
|---|
| 403 | template <class T, int templatehack1, int templatehack2> | 
|---|
| 404 | struct SuperFunctionCondition<functionnumber, T, templatehack1, templatehack2> | 
|---|
| 405 | { | 
|---|
| 406 | // If this function gets called, the header-file of the super function is not | 
|---|
| 407 | // included, so this fallback template (templatehack not specialized) is used | 
|---|
| 408 | static void check() | 
|---|
| 409 | { | 
|---|
| 410 | // Calls the condition-check of the next super-function (functionnumber + 1) | 
|---|
| 411 | SuperFunctionCondition<functionnumber + 1, T, templatehack1, templatehack2>::check(); | 
|---|
| 412 | } | 
|---|
| 413 | }; | 
|---|
| 414 |  | 
|---|
| 415 | // Baseclass of the super-function caller. The real call will be done by a | 
|---|
| 416 | // templatized subclass through the virtual () operator. | 
|---|
| 417 | class _CoreExport SuperFunctionCaller_##functionname | 
|---|
| 418 | { | 
|---|
| 419 | public: | 
|---|
| 420 | virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0; | 
|---|
| 421 | virtual ~SuperFunctionCaller_##functionname () {} | 
|---|
| 422 | }; | 
|---|
| 423 |  | 
|---|
| 424 | // Fallback if the base is pure virtual | 
|---|
| 425 | template <class T> | 
|---|
| 426 | class SuperFunctionClassCaller_purevirtualfallback_##functionname : public SuperFunctionCaller_##functionname | 
|---|
| 427 | { | 
|---|
| 428 | public: | 
|---|
| 429 | // Fallback does nothing | 
|---|
| 430 | inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) | 
|---|
| 431 | { | 
|---|
| 432 | } | 
|---|
| 433 | }; | 
|---|
| 434 |  | 
|---|
| 435 | // Initializes the SuperFunctionCaller-pointer with a fallback caller in case the base function is pure virtual | 
|---|
| 436 | template <class T> | 
|---|
| 437 | struct SuperFunctionInitialization<functionnumber, T> | 
|---|
| 438 | { | 
|---|
| 439 | static void initialize(ClassIdentifier<T>* identifier) | 
|---|
| 440 | { | 
|---|
| 441 | identifier->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_purevirtualfallback_##functionname <T>; | 
|---|
| 442 | identifier->bSuperFunctionCaller_##functionname##_isFallback_ = true; | 
|---|
| 443 |  | 
|---|
| 444 | // Calls the initialization of the next super-function (functionnumber + 1) | 
|---|
| 445 | SuperFunctionInitialization<functionnumber + 1, T>::initialize(identifier); | 
|---|
| 446 | } | 
|---|
| 447 | }; | 
|---|
| 448 |  | 
|---|
| 449 | // Deletes the SuperFunctionCaller. | 
|---|
| 450 | template <class T> | 
|---|
| 451 | struct SuperFunctionDestruction<functionnumber, T> | 
|---|
| 452 | { | 
|---|
| 453 | static void destroy(ClassIdentifier<T>* identifier) | 
|---|
| 454 | { | 
|---|
| 455 | if (identifier->superFunctionCaller_##functionname##_) | 
|---|
| 456 | delete identifier->superFunctionCaller_##functionname##_; | 
|---|
| 457 |  | 
|---|
| 458 | // Calls the destruction of the next super-function (functionnumber + 1) | 
|---|
| 459 | SuperFunctionDestruction<functionnumber + 1, T>::destroy(identifier); | 
|---|
| 460 | } | 
|---|
| 461 | }; | 
|---|
| 462 |  | 
|---|
| 463 | // The real super-function caller: Calls T::functionname() | 
|---|
| 464 | // T should be the parent, but this will be done by the spezialized condition template | 
|---|
| 465 | template <class T> | 
|---|
| 466 | class SuperFunctionClassCaller_##functionname : public SuperFunctionCaller_##functionname | 
|---|
| 467 | { | 
|---|
| 468 | public: | 
|---|
| 469 | // @brief Calls the function. | 
|---|
| 470 | // @param object The object to call the function on | 
|---|
| 471 | // @param ... The arguments of the function | 
|---|
| 472 | inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) | 
|---|
| 473 | { | 
|---|
| 474 | (dynamic_cast<T*>(object))->T:: functionname ( Call the function with it's arguments ); | 
|---|
| 475 | } | 
|---|
| 476 | } | 
|---|
| 477 | */ | 
|---|
| 478 |  | 
|---|
| 479 |  | 
|---|
| 480 | //// Execute the code for each super-function //// | 
|---|
| 481 |  | 
|---|
| 482 | // (2/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- | 
|---|
| 483 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(0, XMLPort, true, Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 484 | (xmlelement, mode) | 
|---|
| 485 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 486 |  | 
|---|
| 487 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(1, tick, true, float dt) | 
|---|
| 488 | (dt) | 
|---|
| 489 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 490 |  | 
|---|
| 491 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(2, changedActivity, false) | 
|---|
| 492 | () | 
|---|
| 493 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 494 |  | 
|---|
| 495 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(3, changedVisibility, false) | 
|---|
| 496 | () | 
|---|
| 497 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 498 |  | 
|---|
| 499 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(4, processEvent, true, Event& event) | 
|---|
| 500 | (event) | 
|---|
| 501 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 502 |  | 
|---|
| 503 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(5, changedScale, false) | 
|---|
| 504 | () | 
|---|
| 505 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 506 |  | 
|---|
| 507 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(6, changedMainState, false) | 
|---|
| 508 | () | 
|---|
| 509 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 510 |  | 
|---|
| 511 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(7, changedOwner, false) | 
|---|
| 512 | () | 
|---|
| 513 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 514 |  | 
|---|
| 515 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(8, changedOverlayGroup, false) | 
|---|
| 516 | () | 
|---|
| 517 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 518 |  | 
|---|
| 519 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(9, changedName, false) | 
|---|
| 520 | () | 
|---|
| 521 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 522 |  | 
|---|
| 523 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(10, changedGametype, false) | 
|---|
| 524 | () | 
|---|
| 525 | SUPER_FUNCTION_GLOBAL_DECLARATION_PART2; | 
|---|
| 526 | // (2/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- | 
|---|
| 527 |  | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | #else /* _Super_H__ */ | 
|---|
| 531 | #ifdef SUPER_INTRUSIVE_DECLARATION_INCLUDE | 
|---|
| 532 |  | 
|---|
| 533 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 534 | // This code gets included within the declaration of ClassIdentifier<T> // | 
|---|
| 535 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 536 |  | 
|---|
| 537 | //// Common code //// | 
|---|
| 538 |  | 
|---|
| 539 | private: | 
|---|
| 540 |  | 
|---|
| 541 | template <int functionnumber, class TT, int templatehack1, int templatehack2> | 
|---|
| 542 | friend struct SuperFunctionCondition; | 
|---|
| 543 |  | 
|---|
| 544 | // Creates the super-function-callers by calling the first SuperFunctionCondition check | 
|---|
| 545 | // This get's called within the initialization of an Identifier | 
|---|
| 546 | virtual void createSuperFunctionCaller() const | 
|---|
| 547 | { | 
|---|
| 548 | SuperFunctionCondition<0, T, 0, 0>::check(); | 
|---|
| 549 | } | 
|---|
| 550 |  | 
|---|
| 551 |  | 
|---|
| 552 | //// Function-specific code //// | 
|---|
| 553 |  | 
|---|
| 554 | public: | 
|---|
| 555 | /** | 
|---|
| 556 | @brief Adds a pointer to the SuperFunctionCaller as a member of ClassIdentifier. | 
|---|
| 557 | @param functionname The name of the super-function | 
|---|
| 558 | */ | 
|---|
| 559 | #ifndef SUPER_INTRUSIVE_DECLARATION | 
|---|
| 560 | #define SUPER_INTRUSIVE_DECLARATION(functionname) \ | 
|---|
| 561 | SuperFunctionCaller_##functionname * superFunctionCaller_##functionname##_; \ | 
|---|
| 562 | bool bSuperFunctionCaller_##functionname##_isFallback_ | 
|---|
| 563 | #endif | 
|---|
| 564 |  | 
|---|
| 565 |  | 
|---|
| 566 | //// Execute the code for each super-function //// | 
|---|
| 567 |  | 
|---|
| 568 | // (3/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- | 
|---|
| 569 | SUPER_INTRUSIVE_DECLARATION(XMLPort); | 
|---|
| 570 | SUPER_INTRUSIVE_DECLARATION(tick); | 
|---|
| 571 | SUPER_INTRUSIVE_DECLARATION(changedActivity); | 
|---|
| 572 | SUPER_INTRUSIVE_DECLARATION(changedVisibility); | 
|---|
| 573 | SUPER_INTRUSIVE_DECLARATION(processEvent); | 
|---|
| 574 | SUPER_INTRUSIVE_DECLARATION(changedScale); | 
|---|
| 575 | SUPER_INTRUSIVE_DECLARATION(changedMainState); | 
|---|
| 576 | SUPER_INTRUSIVE_DECLARATION(changedOwner); | 
|---|
| 577 | SUPER_INTRUSIVE_DECLARATION(changedOverlayGroup); | 
|---|
| 578 | SUPER_INTRUSIVE_DECLARATION(changedName); | 
|---|
| 579 | SUPER_INTRUSIVE_DECLARATION(changedGametype); | 
|---|
| 580 | // (3/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- | 
|---|
| 581 |  | 
|---|
| 582 |  | 
|---|
| 583 | #undef SUPER_INTRUSIVE_DECLARATION_INCLUDE | 
|---|
| 584 | #endif /* SUPER_INTRUSIVE_DECLARATION_INCLUDE */ | 
|---|
| 585 | #endif /* _Super_H__ */ | 
|---|