Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/core/Super.h @ 2212

Last change on this file since 2212 was 2212, checked in by landauf, 16 years ago

maybe fixed backlight crash

  • Property svn:eol-style set to native
File size: 21.4 KB
Line 
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 <iostream>
70
71#include "CorePrereqs.h"
72
73#include "util/Debug.h"
74#include "XMLIncludes.h"
75#include "Event.h"
76
77///////////////////////
78// Macro definitions //
79///////////////////////
80
81//// Common macros ////
82
83    /**
84        @brief Declares a new super-function by creating a specialized template. Add this below the class declaration of the baseclass.
85        @param functionnumber Each super-function needs a unique number, starting with zero, increasing by one
86        @param baseclass The baseclass of the super-function (~the root)
87        @param functionname The name of the super-function
88        @param purevirtualbase "true" if the function is pure virtual in the baseclass, "false" if the function is implemented (without "")
89    */
90    #define SUPER_FUNCTION(functionnumber, baseclass, functionname, purevirtualbase) \
91        template <class T, int templatehack2> \
92        struct SuperFunctionCondition<functionnumber, T, 0, templatehack2> \
93        { \
94            static void check() \
95            { \
96                SuperFunctionCondition<functionnumber, T, 0, templatehack2>::apply((T*)0); \
97                SuperFunctionCondition<functionnumber + 1, T, 0, templatehack2>::check(); \
98            } \
99            \
100            static void apply(void* temp) {} \
101            static void apply(baseclass* temp) \
102            { \
103                ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); \
104                for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it) \
105                { \
106                    if (!((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \
107                    { \
108                        COUT(5) << "Added SuperFunctionCaller for " << #functionname << ": " << ClassIdentifier<T>::getIdentifier()->getName() << " <- " << ((ClassIdentifier<T>*)(*it))->getName() << std::endl; \
109                        ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_##functionname <T>; \
110                    } \
111                } \
112            } \
113        }; \
114        \
115        SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND##purevirtualbase(functionnumber, baseclass)
116
117    #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND0(functionnumber, baseclass) SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDfalse(functionnumber, baseclass)
118    #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND1(functionnumber, baseclass) SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDtrue(functionnumber, baseclass)
119    #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDfalse(functionnumber, baseclass)
120    #define SUPER_FUNCTION_PUREVIRTUAL_WORKAROUNDtrue(functionnumber, baseclass) \
121        template <int templatehack2> \
122        struct SuperFunctionCondition<functionnumber, baseclass, 0, templatehack2> \
123        { \
124            static void check() \
125            { \
126                SuperFunctionCondition<functionnumber + 1, baseclass, 0, templatehack2>::check(); \
127            } \
128        };
129
130
131    /*
132    //// Comments about the macro ////
133
134        // Partially specialized template (templatehack is now specialized too).
135        //
136        // This ensures the compiler takes THIS template if the header-file of the super-function
137        // is included. In any other case, the compiler just uses the fallback template which is
138        // defined in this file.
139        template <class T, templatehack2>
140        struct SuperFunctionCondition<functionnumber, T, 0, templatehack2>
141        {
142            static void check()
143            {
144                // This call to the apply-function is the whole check. By calling the function with
145                // a T* pointer, the right function get's called.
146                SuperFunctionCondition<functionnumber, T, 0, templatehack2>::apply((T*)0);
147
148                // Go go the check for of next super-function (functionnumber + 1)
149                SuperFunctionCondition<functionnumber + 1, T, 0, templatehack2>::check();
150            }
151
152            // This function gets called if T is not a child of the baseclass.
153            // The function does nothing.
154            static void apply(void* temp) {}
155
156            // This function gets called if T is a child of the baseclass and can therefore be converted.
157            // The function adds a SuperFunctionCaller to the Identifier of all subclasses of T.
158            static void apply(baseclass* temp)
159            {
160                ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier();
161
162                // Iterate through all children
163                for (std::set<const Identifier*>::iterator it = identifier->getDirectChildrenIntern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it)
164                {
165                    // Check if there's not already a caller
166                    if (!((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_)
167                    {
168                        // Add the SuperFunctionCaller
169                        COUT(5) << "adding functionpointer to " << ((ClassIdentifier<T>*)(*it))->getName() << std::endl;
170                        ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_ = new SuperFunctionClassCaller_##functionname <T>;
171                    }
172                }
173            }
174        };
175        SUPER_FUNCTION_PUREVIRTUAL_WORKAROUND##purevirtualbase
176
177
178        // The following piece of code is only added if purevirtualbase = true
179
180        // Explicit specialization of the Condition template for the baseclass to avoid
181        // errors if the function is pure virtual in the baseclass.
182        template <int templatehack2> \
183        struct SuperFunctionCondition<functionnumber, baseclass, 0, templatehack2> \
184        { \
185            // The check function just behaves like the fallback - it advances to the check for the next super-function (functionnumber + 1)
186            static void check() \
187            { \
188                SuperFunctionCondition<functionnumber + 1, baseclass, 0, templatehack2>::check(); \
189            } \
190        };
191    */
192
193    // SUPER-macro: Calls Parent::functionname() where Parent is the direct parent of classname
194    #define SUPER(classname, functionname, ...) \
195        SUPER_##functionname(classname, functionname, __VA_ARGS__)
196
197    // helper macro: for functions without arguments
198    #define SUPER_NOARGS(classname, functionname) \
199        (*ClassIdentifier<classname>::getIdentifier()->superFunctionCaller_##functionname##_)(this)
200
201    // helper macro: for functions with arguments
202    #define SUPER_ARGS(classname, functionname, ...) \
203        (*ClassIdentifier<classname>::getIdentifier()->superFunctionCaller_##functionname##_)(this, __VA_ARGS__)
204
205
206//// Function-specific macros ////
207
208    /*
209        Add a macro for each super-function
210
211        Example (no arguments):
212        #define SUPER_myfunction(classname, functionname, ...) \
213            SUPER_NOARGS(classname, functionname)
214
215        Example (with arguments):
216        #define SUPER_myfunction(classname, functionname, ...) \
217            SUPER_ARGS(classname, functionname, __VA_ARGS__)
218    */
219
220    // (1/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
221    #define SUPER_XMLPort(classname, functionname, ...) \
222        SUPER_ARGS(classname, functionname, __VA_ARGS__)
223
224    #define SUPER_tick(classname, functionname, ...) \
225        SUPER_ARGS(classname, functionname, __VA_ARGS__)
226
227    #define SUPER_changedActivity(classname, functionname, ...) \
228        SUPER_NOARGS(classname, functionname)
229
230    #define SUPER_changedVisibility(classname, functionname, ...) \
231        SUPER_NOARGS(classname, functionname)
232
233    #define SUPER_processEvent(classname, functionname, ...) \
234        SUPER_ARGS(classname, functionname, __VA_ARGS__)
235
236    #define SUPER_changedScale(classname, functionname, ...) \
237        SUPER_NOARGS(classname, functionname)
238    // (1/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
239
240
241namespace orxonox
242{
243    /////////////////////////////////////////////////////////////////////////////////////////////////////
244    // This code gets included by Identifier.h and every other header file that needs a super-function //
245    /////////////////////////////////////////////////////////////////////////////////////////////////////
246
247    //// Common code ////
248
249        // Base templates
250        /**
251            @brief Creates the SuperFunctionCaller if T is a child of the super-functions baseclass.
252        */
253        template <int functionnumber, class T, int templatehack1, int templatehack2>
254        struct SuperFunctionCondition
255        {
256            static void check() {}
257        };
258
259        /**
260            @brief Initializes the SuperFunctionCaller-pointer with zero.
261        */
262        template <int functionnumber, class T>
263        struct SuperFunctionInitialization
264        {
265            static void initialize(ClassIdentifier<T>* identifier) {}
266        };
267
268        /**
269            @brief Deletes the SuperFunctionCaller.
270        */
271        template <int functionnumber, class T>
272        struct SuperFunctionDestruction
273        {
274            static void destroy(ClassIdentifier<T>* identifier) {}
275        };
276
277
278    //// Function-specific code ////
279
280        /**
281            @brief Creates the needed objects and templates to call a super-function.
282            @param functionnumber Each super-function needs a unique number, starting with zero, increasing by one
283            @param functionname The name of the super-function
284            @param hasarguments "false" if the function doesn't take any arguments, "true" if it does (without "")
285            @param ... Variadic: If the function takes arguments, add them here with type and name. Example: int myvalue, float myothervalue
286        */
287        #define SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(functionnumber, functionname, hasarguments, ...) \
288            template <class T, int templatehack1, int templatehack2> \
289            struct SuperFunctionCondition<functionnumber, T, templatehack1, templatehack2> \
290            { \
291                static void check() \
292                { \
293                    SuperFunctionCondition<functionnumber + 1, T, templatehack1, templatehack2>::check(); \
294                } \
295            }; \
296            \
297            template <class T> \
298            struct SuperFunctionInitialization<functionnumber, T> \
299            { \
300                static void initialize(ClassIdentifier<T>* identifier) \
301                { \
302                    identifier->superFunctionCaller_##functionname##_ = 0; \
303                    SuperFunctionInitialization<functionnumber + 1, T>::initialize(identifier); \
304                } \
305            }; \
306            \
307            template <class T> \
308            struct SuperFunctionDestruction<functionnumber, T> \
309            { \
310                static void destroy(ClassIdentifier<T>* identifier) \
311                { \
312                    if (identifier->superFunctionCaller_##functionname##_) \
313                        delete identifier->superFunctionCaller_##functionname##_; \
314                    SuperFunctionDestruction<functionnumber + 1, T>::destroy(identifier); \
315                } \
316            }; \
317            \
318            class _CoreExport SuperFunctionCaller_##functionname \
319            { \
320                public: \
321                    virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0; \
322                    virtual ~SuperFunctionCaller_##functionname () {} \
323            }; \
324            \
325            template <class T> \
326            class SuperFunctionClassCaller_##functionname : public SuperFunctionCaller_##functionname \
327            { \
328                public: \
329                    inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) \
330                    { \
331                        (dynamic_cast<T*>(object))->T:: functionname
332
333        /*
334            JUST ADD THE FUNCTION ARGUMENTS BETWEEN BOTH MACROS, ENCLOSED BY BRACKETS
335            EXAMPLE:
336
337              SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(0, myfunction, true, int myvalue, float myothervalue) <-- !!! DONT ADD A SEMICOLON HERE !!!
338                (myvalue, myothervalue)
339              SUPER_FUNCTION_GLOBAL_DECLARATION_PART2
340        */
341
342        #define SUPER_FUNCTION_GLOBAL_DECLARATION_PART2 \
343                                                        ; \
344                    } \
345            };
346
347        #define SUPER_CALL_ARGUMENTSfalse(...) OrxonoxClass* object
348        #define SUPER_CALL_ARGUMENTS0(...)     OrxonoxClass* object
349        #define SUPER_CALL_ARGUMENTStrue(...) OrxonoxClass* object, __VA_ARGS__
350        #define SUPER_CALL_ARGUMENTS1(...)    OrxonoxClass* object, __VA_ARGS__
351
352
353    /*
354    //// COMMENTS ABOUT THE MACRO ////
355
356        // Partially specialized template (templatehack not yet specialized, this
357        // will be done by the real condition in the header-file of the super-function)
358        // Only used as fallback
359        template <class T, int templatehack1, int templatehack2>
360        struct SuperFunctionCondition<functionnumber, T, templatehack1, templatehack2>
361        {
362            // If this function gets called, the header-file of the super function is not
363            // included, so this fallback template (templatehack not specialized) is used
364            static void check()
365            {
366                // Calls the condition-check of the next super-function (functionnumber + 1)
367                SuperFunctionCondition<functionnumber + 1, T, templatehack1, templatehack2>::check();
368            }
369        };
370
371        // Initializes the SuperFunctionCaller-pointer with zero.
372        template <class T>
373        struct SuperFunctionInitialization<functionnumber, T>
374        {
375            static void initialize(ClassIdentifier<T>* identifier)
376            {
377                identifier->superFunctionCaller_##functionname##_ = 0;
378
379                // Calls the initialization of the next super-function (functionnumber + 1)
380                SuperFunctionInitialization<functionnumber + 1, T>::initialize(identifier);
381            }
382        };
383
384        // Deletes the SuperFunctionCaller.
385        template <class T>
386        struct SuperFunctionDestruction<functionnumber, T>
387        {
388            static void destroy(ClassIdentifier<T>* identifier)
389            {
390                if (identifier->superFunctionCaller_##functionname##_)
391                    delete identifier->superFunctionCaller_##functionname##_;
392
393                // Calls the destruction of the next super-function (functionnumber + 1)
394                SuperFunctionDestruction<functionnumber + 1, T>::destroy(identifier);
395            }
396        };
397
398        // Baseclass of the super-function caller. The real call will be done by a
399        // templatized subclass through the virtual () operator.
400        class _CoreExport SuperFunctionCaller_##functionname
401        {
402            public:
403                virtual void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) ) = 0;
404                virtual ~SuperFunctionCaller_##functionname () {}
405        };
406
407        // The real super-function caller: Calls T::functionname()
408        // T should be the parent, but this will be done by the spezialized condition template
409        template <class T>
410        class SuperFunctionClassCaller_##functionname : public SuperFunctionCaller_##functionname
411        {
412            public:
413                // @brief Calls the function.
414                // @param object The object to call the function on
415                // @param ... The arguments of the function
416                inline void operator()( SUPER_CALL_ARGUMENTS##hasarguments(__VA_ARGS__) )
417                {
418                    (dynamic_cast<T*>(object))->T:: functionname ( Call the function with it's arguments );
419                }
420        }
421    */
422
423
424    //// Execute the code for each super-function ////
425
426        // (2/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
427        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(0, XMLPort, true, Element& xmlelement, XMLPort::Mode mode)
428            (xmlelement, mode)
429        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
430
431        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(1, tick, true, float dt)
432            (dt)
433        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
434
435        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(2, changedActivity, false)
436            ()
437        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
438
439        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(3, changedVisibility, false)
440            ()
441        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
442
443        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(4, processEvent, true, Event& event)
444            (event)
445        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
446
447        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(5, changedScale, false)
448            ()
449        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
450        // (2/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
451
452}
453
454#else /* _Super_H__ */
455  #ifdef SUPER_INTRUSIVE_DECLARATION_INCLUDE
456
457//////////////////////////////////////////////////////////////////////////
458// This code gets included within the declaration of ClassIdentifier<T> //
459//////////////////////////////////////////////////////////////////////////
460
461//// Common code ////
462
463    private:
464
465        template <int functionnumber, class TT, int templatehack1, int templatehack2>
466        friend struct SuperFunctionCondition;
467
468        // Creates the super-function-callers by calling the first SuperFunctionCondition check
469        // This get's called within the initialization of an Identifier
470        virtual void createSuperFunctionCaller() const
471        {
472            SuperFunctionCondition<0, T, 0, 0>::check();
473        }
474
475
476//// Function-specific code ////
477
478    public:
479        /**
480            @brief Adds a pointer to the SuperFunctionCaller as a member of ClassIdentifier.
481            @param functionname The name of the super-function
482        */
483        #ifndef SUPER_INTRUSIVE_DECLARATION
484          #define SUPER_INTRUSIVE_DECLARATION(functionname) \
485            SuperFunctionCaller_##functionname * superFunctionCaller_##functionname##_
486        #endif
487
488
489//// Execute the code for each super-function ////
490
491    // (3/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
492    SUPER_INTRUSIVE_DECLARATION(XMLPort);
493    SUPER_INTRUSIVE_DECLARATION(tick);
494    SUPER_INTRUSIVE_DECLARATION(changedActivity);
495    SUPER_INTRUSIVE_DECLARATION(changedVisibility);
496    SUPER_INTRUSIVE_DECLARATION(processEvent);
497    SUPER_INTRUSIVE_DECLARATION(changedScale);
498    // (3/3) --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <-- --> HERE <--
499
500
501    #undef SUPER_INTRUSIVE_DECLARATION_INCLUDE
502  #endif /* SUPER_INTRUSIVE_DECLARATION_INCLUDE */
503#endif /* _Super_H__ */
Note: See TracBrowser for help on using the repository browser.