Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/CoreIncludes.h @ 9637

Last change on this file since 9637 was 9637, checked in by landauf, 11 years ago

removed constructor arguments from ClassFactories. use the helper-function registerClass() to add the factory to the corresponding identifier

  • Property svn:eol-style set to native
File size: 7.0 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 CreateFactory()
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, create factories, and to work with identifiers.
38
39    Every class needs the @c RegisterObject(class) macro in its constructor. If the class is an interface
40    or the @c BaseObject itself, it needs the macro @c RegisterRootObject(class) instead.
41
42    To allow the object being created through the factory, use the @c CreateFactory(class) macro outside
43    of the class implementation, so it gets executed statically before @c main(). This will at the same time
44    register @a class in the class-hierarchy. If you don't want @a class to be loadable, but still
45    register it, call @c CreateUnloadableFactory(class).
46
47    Example:
48    @code
49    // Create the factory for MyClass
50    CreateFactory(MyClass);
51
52    // Constructor:
53    MyClass::MyClass()
54    {
55        // Register the object in the Identifier of MyClass
56        RegisterObject(MyClass);
57    }
58    @endcode
59
60    This file also defines a number of other useful macros, like, for example, @c Class(class) which
61    returns the @ref orxonox::Identifier "Identifier" of @a class, or @c ClassByString("class") which
62    returns the Identifier of a class with name @a "class".
63
64    Example:
65    @code
66    // Assigns the Identifier of MyClass
67    Identifier* identifier = Class(MyClass);
68    @endcode
69    @code
70    // Assigns the Identifier of a class named "MyClass"
71    Identifier* identifier = ClassByString("MyClass");
72    @endcode
73*/
74
75#ifndef _CoreIncludes_H__
76#define _CoreIncludes_H__
77
78#include "CorePrereqs.h"
79
80#include "util/Output.h"
81#include "class/IdentifierManager.h"
82#include "object/ClassFactory.h"
83#include "object/ObjectList.h"
84
85
86/**
87    @brief Intern macro, containing the common parts of @c RegisterObject and @c RegisterRootObject.
88    @param ClassName The name of the class
89    @param bRootClass True if the class is directly derived from orxonox::OrxonoxClass
90*/
91#define InternRegisterObject(ClassName, bRootClass) \
92    if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initialiseObject(this, #ClassName, bRootClass)) \
93        return; \
94    else \
95        ((void)0)
96
97/**
98    @brief Registers a newly created object in the core. Has to be called at the beginning of the constructor of @a ClassName.
99    @param ClassName The name of the class
100*/
101#define RegisterObject(ClassName) \
102    InternRegisterObject(ClassName, false)
103
104/**
105    @brief Registers a newly created object in the core. Has to be called at the beginning of the constructor of @a ClassName.
106    @param ClassName The name of the class
107
108    In contrast to RegisterObject, this is used for classes that inherit directly from
109    orxonox::OrxonoxClass, namely all interfaces and orxonox::BaseObject.
110*/
111#define RegisterRootObject(ClassName) \
112    InternRegisterObject(ClassName, true)
113
114/**
115    @brief Creates and registers the Factory.
116    @param ClassName The name of the class
117*/
118#define CreateFactory(ClassName) \
119    RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true)
120
121/**
122    @brief Creates and registers the Factory for classes which should not be loaded through XML.
123    @param ClassName The name of the class
124*/
125#define CreateUnloadableFactory(ClassName) \
126    RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false)
127
128/**
129    @brief Registers a given Factory.
130    @param ClassName The name of the class
131*/
132#define RegisterFactory(ClassName, FactoryInstance, bLoadable) \
133    Identifier* _##ClassName##Identifier = orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable)
134
135/**
136    @brief Returns the Identifier of the given class.
137    @param ClassName The name of the class
138*/
139#define Class(ClassName) \
140    orxonox::ClassIdentifier<ClassName>::getIdentifier()
141
142
143namespace orxonox
144{
145    /**
146     * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory.
147     */
148    template <class T>
149    inline Identifier* registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true)
150    {
151        return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable);
152    }
153
154    /**
155     * @brief Registers a class in the framework.
156     * @param name The name of the class
157     * @param factory The factory which is able to create new instances of this class
158     * @param bLoadable Whether the class is allowed to be loaded through XML
159     */
160    template <class T>
161    inline Identifier* registerClass(const std::string& name, Factory* factory, bool bLoadable = true)
162    {
163        orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
164        Identifier* identifier = ClassIdentifier<T>::getIdentifier(name);
165        identifier->setFactory(factory);
166        identifier->setLoadable(bLoadable);
167        return identifier;
168    }
169
170    /**
171        @brief Returns the Identifier with a given name.
172        @param name The name of the class
173    */
174    inline Identifier* ClassByString(const std::string& name)
175    {
176        return IdentifierManager::getIdentifierByString(name);
177    }
178
179    /**
180        @brief Returns the Identifier with a given lowercase name.
181        @param name The lowercase name of the class
182    */
183    inline Identifier* ClassByLowercaseString(const std::string& name)
184    {
185        return IdentifierManager::getIdentifierByLowercaseString(name);
186    }
187
188    /**
189        @brief Returns the Identifier with a given network ID.
190        @param id The network ID of the class
191    */
192    inline Identifier* ClassByID(uint32_t id)
193    {
194        return IdentifierManager::getIdentifierByID(id);
195    }
196
197    /**
198        @brief Returns the Identifier with a given 'this' pointer.
199        @note This of course only works with Identifiables.
200              The only use is in conjunction with macros that don't know the class type.
201        @param object Pointer to an Identifiable
202    */
203    template <class T>
204    inline Identifier* ClassByObjectType(const T*)
205    {
206        return ClassIdentifier<T>::getIdentifier();
207    }
208}
209
210#endif /* _CoreIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.