Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10403 was 10396, checked in by landauf, 9 years ago

detail

  • Property svn:eol-style set to native
File size: 8.8 KB
RevLine 
[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 */
28
29/**
[9667]30    @defgroup Factory RegisterObject() and RegisterClass()
[7401]31    @ingroup Object
32*/
33
34/**
[2171]35    @file
[8858]36    @ingroup Object Factory Class Identifier
[9667]37    @brief Defines several very important macros used to register objects, register classes, and to work with identifiers.
[1505]38
[9667]39    Every class needs the @c RegisterObject(class) macro in its constructor.
[1505]40
[9667]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).
[7401]44
[9667]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
[7401]49    Example:
50    @code
[9667]51    // register MyClass
52    RegisterClass(MyClass);
[7401]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
[1505]75*/
76
77#ifndef _CoreIncludes_H__
78#define _CoreIncludes_H__
79
80#include "CorePrereqs.h"
81
[8858]82#include "util/Output.h"
[9667]83#include "class/IdentifierManager.h"
84#include "object/ClassFactory.h"
85#include "object/ObjectList.h"
[10360]86#include "module/StaticallyInitializedInstance.h"
[1505]87
[9667]88// resolve macro conflict on windows
89#if defined(ORXONOX_PLATFORM_WINDOWS)
[10208]90#   define WIN32_LEAN_AND_MEAN
[9667]91#   include <windows.h>
92#   undef RegisterClass
93#endif
[1505]94
[9667]95
[1505]96/**
[9667]97    @brief Registers the class in the framework.
[1505]98    @param ClassName The name of the class
99*/
[9667]100#define RegisterClass(ClassName) \
101    RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true)
[1505]102
103/**
[9667]104    @brief Registers the class in the framework (for classes without arguments in their constructor).
[1505]105    @param ClassName The name of the class
106*/
[9667]107#define RegisterClassNoArgs(ClassName) \
108    RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryNoArgs<ClassName>(), true)
[1505]109
110/**
[9667]111    @brief Registers the class in the framework (for classes which should not be loaded through XML).
[1505]112    @param ClassName The name of the class
[9667]113*/
114#define RegisterUnloadableClass(ClassName) \
115    RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false)
[7401]116
[9667]117/**
[10189]118    @brief Registers an abstract class in the framework. Should be used in combination with inheritsFrom(base-class-identifier).
[9667]119    @param ClassName The name of the class
[1505]120*/
[9667]121#define RegisterAbstractClass(ClassName) \
122    RegisterClassWithFactory(ClassName, static_cast<ClassFactory<ClassName>*>(NULL), false)
[1505]123
124/**
[9667]125    @brief Registers the class in the framework with a given Factory.
[1856]126    @param ClassName The name of the class
127*/
[9667]128#define RegisterClassWithFactory(ClassName, FactoryInstance, bLoadable) \
[10362]129    orxonox::SI_I& _##ClassName##Identifier = (*new orxonox::SI_I(orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable)))
[1856]130
131/**
[9667]132    @brief Registers a newly created object in the framework. Has to be called at the beginning of the constructor of @a ClassName.
[5781]133    @param ClassName The name of the class
134*/
[9667]135#define RegisterObject(ClassName) \
[10395]136    if (ClassIdentifier<ClassName>::getIdentifier()->initializeObject(this)) \
[9667]137        return; \
138    else \
139        ((void)0)
[5781]140
141/**
[1505]142    @brief Returns the Identifier of the given class.
143    @param ClassName The name of the class
144*/
[1789]145#define Class(ClassName) \
[2087]146    orxonox::ClassIdentifier<ClassName>::getIdentifier()
[1505]147
148
[3325]149namespace orxonox
150{
151    /**
[9667]152     * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory.
153     */
154    template <class T>
[10360]155    inline Identifier* registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true)
[9667]156    {
157        return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable);
158    }
159
160    /**
161     * @brief Registers a class in the framework.
162     * @param name The name of the class
163     * @param factory The factory which is able to create new instances of this class
164     * @param bLoadable Whether the class is allowed to be loaded through XML
165     */
166    template <class T>
[10360]167    inline Identifier* registerClass(const std::string& name, Factory* factory, bool bLoadable = true)
[9667]168    {
[10395]169        Identifier* identifier = new ClassIdentifier<T>(name, factory, bLoadable);
170        IdentifierManager::getInstance().addIdentifier(identifier);
[10360]171        return identifier;
[9667]172    }
173
174    /**
[5929]175        @brief Returns the Identifier with a given name.
[7401]176        @param name The name of the class
[3325]177    */
178    inline Identifier* ClassByString(const std::string& name)
179    {
[9667]180        return IdentifierManager::getInstance().getIdentifierByString(name);
[3325]181    }
[1505]182
[3325]183    /**
[5929]184        @brief Returns the Identifier with a given lowercase name.
[7401]185        @param name The lowercase name of the class
[5929]186    */
187    inline Identifier* ClassByLowercaseString(const std::string& name)
188    {
[9667]189        return IdentifierManager::getInstance().getIdentifierByLowercaseString(name);
[5929]190    }
191
192    /**
193        @brief Returns the Identifier with a given network ID.
[7401]194        @param id The network ID of the class
[3325]195    */
196    inline Identifier* ClassByID(uint32_t id)
197    {
[9667]198        return IdentifierManager::getInstance().getIdentifierByID(id);
[3325]199    }
[6423]200
201    /**
202        @brief Returns the Identifier with a given 'this' pointer.
[9667]203        @note This of course only works with Identifiables.
[6423]204              The only use is in conjunction with macros that don't know the class type.
[9667]205        @param object Pointer to an Identifiable
[6423]206    */
207    template <class T>
[8706]208    inline Identifier* ClassByObjectType(const T*)
[6423]209    {
210        return ClassIdentifier<T>::getIdentifier();
211    }
[10360]212
[10362]213
214
215
216    /**
217     * The static initializer stores the parent classes of this identifier. The corresponding identifiers are later loaded. This prevents identifiers from
218     * being used before they are completely initialized.
219     */
[10360]220    class _CoreExport StaticallyInitializedIdentifier : public StaticallyInitializedInstance
221    {
[10362]222        struct InheritsFrom
223        {
224            virtual ~InheritsFrom() {}
225            virtual Identifier* getParent() = 0;
226        };
227
228        template <class T>
229        struct InheritsFromClass : public InheritsFrom
230        {
231            virtual Identifier* getParent() { return Class(T); }
232        };
233
[10360]234        public:
235            StaticallyInitializedIdentifier(Identifier* identifier) : identifier_(identifier) {}
[10362]236            ~StaticallyInitializedIdentifier()
237            {
238                for (size_t i = 0; i < this->parents_.size(); ++i)
239                    delete parents_[i];
240            }
[10360]241
[10362]242            virtual void load()
243            {
244                for (size_t i = 0; i < this->parents_.size(); ++i)
245                    this->identifier_->inheritsFrom(this->parents_[i]->getParent());
246            }
[10360]247
248            inline Identifier& getIdentifier()
249                { return *this->identifier_; }
250
[10362]251            template <class T>
252            inline StaticallyInitializedIdentifier& inheritsFrom()
253                { this->parents_.push_back(new InheritsFromClass<T>()); return *this; }
254
[10374]255            inline StaticallyInitializedIdentifier& virtualBase()
256                { this->identifier_->setVirtualBase(true); return *this; }
257
[10360]258        private:
259            Identifier* identifier_;
[10362]260            std::vector<InheritsFrom*> parents_;
[10360]261    };
262
263    typedef StaticallyInitializedIdentifier SI_I;
[3325]264}
265
[1505]266#endif /* _CoreIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.