Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/CoreIncludes.h @ 10380

Last change on this file since 10380 was 10379, checked in by landauf, 9 years ago

check if all classes are registered

  • Property svn:eol-style set to native
File size: 9.2 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    @defgroup Factory RegisterObject() and RegisterClass()
31    @ingroup Object
32*/
33
34/**
35    @file
36    @ingroup Object Factory Class Identifier
37    @brief Defines several very important macros used to register objects, register classes, and to work with identifiers.
38
39    Every class needs the @c RegisterObject(class) macro in its constructor.
40
41    To register @a class in the class-hierarchy, use the @c RegisterClass(class) macro outside of the class implementation,
42    so it gets executed statically before @c main(). If you don't want @a class to be loadable, but still register it, call
43    @c RegisterUnloadableClass(class).
44
45    Abstract classes are registered with @c RegisterAbstractClass(class). For abstract classes, the inheritance must be
46    defined manually with @c RegisterAbstractClass(class).inheritsFrom(Class(parent)). Multiple parent classes can be defined
47    by chaining the above command.
48
49    Example:
50    @code
51    // register MyClass
52    RegisterClass(MyClass);
53
54    // Constructor:
55    MyClass::MyClass()
56    {
57        // Register the object in the Identifier of MyClass
58        RegisterObject(MyClass);
59    }
60    @endcode
61
62    This file also defines a number of other useful macros, like, for example, @c Class(class) which
63    returns the @ref orxonox::Identifier "Identifier" of @a class, or @c ClassByString("class") which
64    returns the Identifier of a class with name @a "class".
65
66    Example:
67    @code
68    // Assigns the Identifier of MyClass
69    Identifier* identifier = Class(MyClass);
70    @endcode
71    @code
72    // Assigns the Identifier of a class named "MyClass"
73    Identifier* identifier = ClassByString("MyClass");
74    @endcode
75*/
76
77#ifndef _CoreIncludes_H__
78#define _CoreIncludes_H__
79
80#include "CorePrereqs.h"
81
82#include "util/OrxAssert.h"
83#include "util/Output.h"
84#include "class/IdentifierManager.h"
85#include "object/ClassFactory.h"
86#include "object/ObjectList.h"
87#include "module/StaticallyInitializedInstance.h"
88
89// resolve macro conflict on windows
90#if defined(ORXONOX_PLATFORM_WINDOWS)
91#   define WIN32_LEAN_AND_MEAN
92#   include <windows.h>
93#   undef RegisterClass
94#endif
95
96
97/**
98    @brief Registers the class in the framework.
99    @param ClassName The name of the class
100*/
101#define RegisterClass(ClassName) \
102    RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true)
103
104/**
105    @brief Registers the class in the framework (for classes without arguments in their constructor).
106    @param ClassName The name of the class
107*/
108#define RegisterClassNoArgs(ClassName) \
109    RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryNoArgs<ClassName>(), true)
110
111/**
112    @brief Registers the class in the framework (for classes which should not be loaded through XML).
113    @param ClassName The name of the class
114*/
115#define RegisterUnloadableClass(ClassName) \
116    RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false)
117
118/**
119    @brief Registers an abstract class in the framework. Should be used in combination with inheritsFrom(base-class-identifier).
120    @param ClassName The name of the class
121*/
122#define RegisterAbstractClass(ClassName) \
123    RegisterClassWithFactory(ClassName, static_cast<ClassFactory<ClassName>*>(NULL), false)
124
125/**
126    @brief Registers the class in the framework with a given Factory.
127    @param ClassName The name of the class
128*/
129#define RegisterClassWithFactory(ClassName, FactoryInstance, bLoadable) \
130    orxonox::SI_I& _##ClassName##Identifier = (*new orxonox::SI_I(orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable)))
131
132/**
133    @brief Registers a newly created object in the framework. Has to be called at the beginning of the constructor of @a ClassName.
134    @param ClassName The name of the class
135*/
136#define RegisterObject(ClassName) \
137    if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeObject(this)) \
138        return; \
139    else \
140        OrxVerify(ClassIdentifier<ClassName>::getIdentifier()->isRegistered(), "Assertion failed in ClassIdentifier of type " << typeid(ClassName).name()); \
141        ((void)0)
142
143/**
144    @brief Returns the Identifier of the given class.
145    @param ClassName The name of the class
146*/
147#define Class(ClassName) \
148    orxonox::ClassIdentifier<ClassName>::getIdentifier()
149
150
151namespace orxonox
152{
153    /**
154     * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory.
155     */
156    template <class T>
157    inline Identifier* registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true)
158    {
159        return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable);
160    }
161
162    /**
163     * @brief Registers a class in the framework.
164     * @param name The name of the class
165     * @param factory The factory which is able to create new instances of this class
166     * @param bLoadable Whether the class is allowed to be loaded through XML
167     */
168    template <class T>
169    inline Identifier* registerClass(const std::string& name, Factory* factory, bool bLoadable = true)
170    {
171        orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
172        Identifier* identifier = ClassIdentifier<T>::getIdentifier(name);
173        identifier->setFactory(factory);
174        identifier->setLoadable(bLoadable);
175        identifier->setRegistered(true);
176        return identifier;
177    }
178
179    /**
180        @brief Returns the Identifier with a given name.
181        @param name The name of the class
182    */
183    inline Identifier* ClassByString(const std::string& name)
184    {
185        return IdentifierManager::getInstance().getIdentifierByString(name);
186    }
187
188    /**
189        @brief Returns the Identifier with a given lowercase name.
190        @param name The lowercase name of the class
191    */
192    inline Identifier* ClassByLowercaseString(const std::string& name)
193    {
194        return IdentifierManager::getInstance().getIdentifierByLowercaseString(name);
195    }
196
197    /**
198        @brief Returns the Identifier with a given network ID.
199        @param id The network ID of the class
200    */
201    inline Identifier* ClassByID(uint32_t id)
202    {
203        return IdentifierManager::getInstance().getIdentifierByID(id);
204    }
205
206    /**
207        @brief Returns the Identifier with a given 'this' pointer.
208        @note This of course only works with Identifiables.
209              The only use is in conjunction with macros that don't know the class type.
210        @param object Pointer to an Identifiable
211    */
212    template <class T>
213    inline Identifier* ClassByObjectType(const T*)
214    {
215        return ClassIdentifier<T>::getIdentifier();
216    }
217
218
219
220
221    /**
222     * The static initializer stores the parent classes of this identifier. The corresponding identifiers are later loaded. This prevents identifiers from
223     * being used before they are completely initialized.
224     */
225    class _CoreExport StaticallyInitializedIdentifier : public StaticallyInitializedInstance
226    {
227        struct InheritsFrom
228        {
229            virtual ~InheritsFrom() {}
230            virtual Identifier* getParent() = 0;
231        };
232
233        template <class T>
234        struct InheritsFromClass : public InheritsFrom
235        {
236            virtual Identifier* getParent() { return Class(T); }
237        };
238
239        public:
240            StaticallyInitializedIdentifier(Identifier* identifier) : identifier_(identifier) {}
241            ~StaticallyInitializedIdentifier()
242            {
243                for (size_t i = 0; i < this->parents_.size(); ++i)
244                    delete parents_[i];
245            }
246
247            virtual void load()
248            {
249                for (size_t i = 0; i < this->parents_.size(); ++i)
250                    this->identifier_->inheritsFrom(this->parents_[i]->getParent());
251            }
252
253            inline Identifier& getIdentifier()
254                { return *this->identifier_; }
255
256            template <class T>
257            inline StaticallyInitializedIdentifier& inheritsFrom()
258                { this->parents_.push_back(new InheritsFromClass<T>()); return *this; }
259
260            inline StaticallyInitializedIdentifier& virtualBase()
261                { this->identifier_->setVirtualBase(true); return *this; }
262
263        private:
264            Identifier* identifier_;
265            std::vector<InheritsFrom*> parents_;
266    };
267
268    typedef StaticallyInitializedIdentifier SI_I;
269}
270
271#endif /* _CoreIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.