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/Output.h" |
---|
83 | #include "class/IdentifierManager.h" |
---|
84 | #include "object/ClassFactory.h" |
---|
85 | #include "object/ObjectList.h" |
---|
86 | |
---|
87 | // resolve macro conflict on windows |
---|
88 | #if defined(ORXONOX_PLATFORM_WINDOWS) |
---|
89 | # define WIN32_LEAN_AND_MEAN |
---|
90 | # include <windows.h> |
---|
91 | # undef RegisterClass |
---|
92 | #endif |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | @brief Registers the class in the framework. |
---|
97 | @param ClassName The name of the class |
---|
98 | */ |
---|
99 | #define RegisterClass(ClassName) \ |
---|
100 | RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true) |
---|
101 | |
---|
102 | /** |
---|
103 | @brief Registers the class in the framework (for classes without arguments in their constructor). |
---|
104 | @param ClassName The name of the class |
---|
105 | */ |
---|
106 | #define RegisterClassNoArgs(ClassName) \ |
---|
107 | RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryNoArgs<ClassName>(), true) |
---|
108 | |
---|
109 | /** |
---|
110 | @brief Registers the class in the framework (for classes which should not be loaded through XML). |
---|
111 | @param ClassName The name of the class |
---|
112 | */ |
---|
113 | #define RegisterUnloadableClass(ClassName) \ |
---|
114 | RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false) |
---|
115 | |
---|
116 | /** |
---|
117 | @brief Registers an abstract class in the framework. Should be used in combination with inheritsFrom(base-class-identifier). |
---|
118 | @param ClassName The name of the class |
---|
119 | */ |
---|
120 | #define RegisterAbstractClass(ClassName) \ |
---|
121 | RegisterClassWithFactory(ClassName, static_cast<ClassFactory<ClassName>*>(NULL), false) |
---|
122 | |
---|
123 | /** |
---|
124 | @brief Registers the class in the framework with a given Factory. |
---|
125 | @param ClassName The name of the class |
---|
126 | */ |
---|
127 | #define RegisterClassWithFactory(ClassName, FactoryInstance, bLoadable) \ |
---|
128 | Identifier& _##ClassName##Identifier = orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable) |
---|
129 | |
---|
130 | /** |
---|
131 | @brief Registers a newly created object in the framework. Has to be called at the beginning of the constructor of @a ClassName. |
---|
132 | @param ClassName The name of the class |
---|
133 | */ |
---|
134 | #define RegisterObject(ClassName) \ |
---|
135 | if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeObject(this)) \ |
---|
136 | return; \ |
---|
137 | else \ |
---|
138 | ((void)0) |
---|
139 | |
---|
140 | /** |
---|
141 | @brief Returns the Identifier of the given class. |
---|
142 | @param ClassName The name of the class |
---|
143 | */ |
---|
144 | #define Class(ClassName) \ |
---|
145 | orxonox::ClassIdentifier<ClassName>::getIdentifier() |
---|
146 | |
---|
147 | |
---|
148 | namespace orxonox |
---|
149 | { |
---|
150 | /** |
---|
151 | * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory. |
---|
152 | */ |
---|
153 | template <class T> |
---|
154 | inline Identifier& registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true) |
---|
155 | { |
---|
156 | return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief Registers a class in the framework. |
---|
161 | * @param name The name of the class |
---|
162 | * @param factory The factory which is able to create new instances of this class |
---|
163 | * @param bLoadable Whether the class is allowed to be loaded through XML |
---|
164 | */ |
---|
165 | template <class T> |
---|
166 | inline Identifier& registerClass(const std::string& name, Factory* factory, bool bLoadable = true) |
---|
167 | { |
---|
168 | orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl; |
---|
169 | Identifier* identifier = ClassIdentifier<T>::getIdentifier(name); |
---|
170 | identifier->setFactory(factory); |
---|
171 | identifier->setLoadable(bLoadable); |
---|
172 | return *identifier; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | @brief Returns the Identifier with a given name. |
---|
177 | @param name The name of the class |
---|
178 | */ |
---|
179 | inline Identifier* ClassByString(const std::string& name) |
---|
180 | { |
---|
181 | return IdentifierManager::getInstance().getIdentifierByString(name); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | @brief Returns the Identifier with a given lowercase name. |
---|
186 | @param name The lowercase name of the class |
---|
187 | */ |
---|
188 | inline Identifier* ClassByLowercaseString(const std::string& name) |
---|
189 | { |
---|
190 | return IdentifierManager::getInstance().getIdentifierByLowercaseString(name); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | @brief Returns the Identifier with a given network ID. |
---|
195 | @param id The network ID of the class |
---|
196 | */ |
---|
197 | inline Identifier* ClassByID(uint32_t id) |
---|
198 | { |
---|
199 | return IdentifierManager::getInstance().getIdentifierByID(id); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | @brief Returns the Identifier with a given 'this' pointer. |
---|
204 | @note This of course only works with Identifiables. |
---|
205 | The only use is in conjunction with macros that don't know the class type. |
---|
206 | @param object Pointer to an Identifiable |
---|
207 | */ |
---|
208 | template <class T> |
---|
209 | inline Identifier* ClassByObjectType(const T*) |
---|
210 | { |
---|
211 | return ClassIdentifier<T>::getIdentifier(); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | #endif /* _CoreIncludes_H__ */ |
---|