[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 | /** |
---|
[9638] | 30 | @defgroup Factory RegisterObject() and RegisterClass() |
---|
[7401] | 31 | @ingroup Object |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
[2171] | 35 | @file |
---|
[8858] | 36 | @ingroup Object Factory Class Identifier |
---|
[9638] | 37 | @brief Defines several very important macros used to register objects, register classes, and to work with identifiers. |
---|
[1505] | 38 | |
---|
[7401] | 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. |
---|
[1505] | 41 | |
---|
[9638] | 42 | To register @a class in the class-hierarchy, use the @c RegisterClass(class) macro outside of the class implementation, |
---|
| 43 | so it gets executed statically before @c main(). If you don't want @a class to be loadable, but still register it, call |
---|
| 44 | @c RegisterUnloadableClass(class). |
---|
[7401] | 45 | |
---|
| 46 | Example: |
---|
| 47 | @code |
---|
[9638] | 48 | // register MyClass |
---|
| 49 | RegisterClass(MyClass); |
---|
[7401] | 50 | |
---|
| 51 | // Constructor: |
---|
| 52 | MyClass::MyClass() |
---|
| 53 | { |
---|
| 54 | // Register the object in the Identifier of MyClass |
---|
| 55 | RegisterObject(MyClass); |
---|
| 56 | } |
---|
| 57 | @endcode |
---|
| 58 | |
---|
| 59 | This file also defines a number of other useful macros, like, for example, @c Class(class) which |
---|
| 60 | returns the @ref orxonox::Identifier "Identifier" of @a class, or @c ClassByString("class") which |
---|
| 61 | returns the Identifier of a class with name @a "class". |
---|
| 62 | |
---|
| 63 | Example: |
---|
| 64 | @code |
---|
| 65 | // Assigns the Identifier of MyClass |
---|
| 66 | Identifier* identifier = Class(MyClass); |
---|
| 67 | @endcode |
---|
| 68 | @code |
---|
| 69 | // Assigns the Identifier of a class named "MyClass" |
---|
| 70 | Identifier* identifier = ClassByString("MyClass"); |
---|
| 71 | @endcode |
---|
[1505] | 72 | */ |
---|
| 73 | |
---|
| 74 | #ifndef _CoreIncludes_H__ |
---|
| 75 | #define _CoreIncludes_H__ |
---|
| 76 | |
---|
| 77 | #include "CorePrereqs.h" |
---|
| 78 | |
---|
[8858] | 79 | #include "util/Output.h" |
---|
[9564] | 80 | #include "class/IdentifierManager.h" |
---|
[9557] | 81 | #include "object/ClassFactory.h" |
---|
| 82 | #include "object/ObjectList.h" |
---|
[1505] | 83 | |
---|
[9638] | 84 | // resolve macro conflict on windows |
---|
| 85 | #if defined(ORXONOX_PLATFORM_WINDOWS) |
---|
| 86 | # include <windows.h> |
---|
| 87 | # undef RegisterClass |
---|
| 88 | #endif |
---|
[1505] | 89 | |
---|
[9638] | 90 | |
---|
[1505] | 91 | /** |
---|
[9646] | 92 | @brief Registers the class in the framework. |
---|
[1505] | 93 | @param ClassName The name of the class |
---|
| 94 | */ |
---|
[9646] | 95 | #define RegisterClass(ClassName) \ |
---|
| 96 | RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true) |
---|
[1505] | 97 | |
---|
| 98 | /** |
---|
[9646] | 99 | @brief Registers the class in the framework (for classes without arguments in their constructor). |
---|
[1505] | 100 | @param ClassName The name of the class |
---|
| 101 | */ |
---|
[9646] | 102 | #define RegisterClassNoArgs(ClassName) \ |
---|
| 103 | RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryNoArgs<ClassName>(), true) |
---|
[1505] | 104 | |
---|
| 105 | /** |
---|
[9646] | 106 | @brief Registers the class in the framework (for classes which should not be loaded through XML). |
---|
[1505] | 107 | @param ClassName The name of the class |
---|
[9646] | 108 | */ |
---|
| 109 | #define RegisterUnloadableClass(ClassName) \ |
---|
| 110 | RegisterClassWithFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false) |
---|
[7401] | 111 | |
---|
[9646] | 112 | /** |
---|
| 113 | @brief Registers an abstract class in the framework. |
---|
| 114 | @param ClassName The name of the class |
---|
[1505] | 115 | */ |
---|
[9646] | 116 | #define RegisterAbstractClass(ClassName) \ |
---|
| 117 | RegisterClassWithFactory(ClassName, static_cast<ClassFactory<ClassName>*>(NULL), false) |
---|
[1505] | 118 | |
---|
| 119 | /** |
---|
[9646] | 120 | @brief Registers the class in the framework with a given Factory. |
---|
[1856] | 121 | @param ClassName The name of the class |
---|
| 122 | */ |
---|
[9646] | 123 | #define RegisterClassWithFactory(ClassName, FactoryInstance, bLoadable) \ |
---|
| 124 | Identifier& _##ClassName##Identifier = orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable) |
---|
[1856] | 125 | |
---|
| 126 | /** |
---|
[9646] | 127 | @brief Intern macro, containing the common parts of @c RegisterObject and @c RegisterRootObject. |
---|
[5781] | 128 | @param ClassName The name of the class |
---|
[9646] | 129 | @param bRootClass True if the class is directly derived from orxonox::OrxonoxClass |
---|
[5781] | 130 | */ |
---|
[9646] | 131 | #define InternRegisterObject(ClassName) \ |
---|
| 132 | if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeObject(this)) \ |
---|
| 133 | return; \ |
---|
| 134 | else \ |
---|
| 135 | ((void)0) |
---|
[5781] | 136 | |
---|
| 137 | /** |
---|
[9646] | 138 | @brief Registers a newly created object in the framework. Has to be called at the beginning of the constructor of @a ClassName. |
---|
[9635] | 139 | @param ClassName The name of the class |
---|
| 140 | */ |
---|
[9646] | 141 | #define RegisterObject(ClassName) \ |
---|
| 142 | InternRegisterObject(ClassName) |
---|
[9635] | 143 | |
---|
| 144 | /** |
---|
[9646] | 145 | @brief Registers a newly created object in the framework. Has to be called at the beginning of the constructor of @a ClassName. |
---|
| 146 | @param ClassName The name of the class |
---|
| 147 | |
---|
| 148 | In contrast to RegisterObject, this is used for classes that inherit directly from |
---|
| 149 | orxonox::OrxonoxClass, namely all interfaces and orxonox::BaseObject. |
---|
| 150 | */ |
---|
| 151 | #define RegisterRootObject(ClassName) \ |
---|
| 152 | InternRegisterObject(ClassName) |
---|
| 153 | |
---|
| 154 | /** |
---|
[1505] | 155 | @brief Returns the Identifier of the given class. |
---|
| 156 | @param ClassName The name of the class |
---|
| 157 | */ |
---|
[1789] | 158 | #define Class(ClassName) \ |
---|
[2087] | 159 | orxonox::ClassIdentifier<ClassName>::getIdentifier() |
---|
[1505] | 160 | |
---|
| 161 | |
---|
[3325] | 162 | namespace orxonox |
---|
| 163 | { |
---|
| 164 | /** |
---|
[9637] | 165 | * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory. |
---|
| 166 | */ |
---|
| 167 | template <class T> |
---|
[9646] | 168 | inline Identifier& registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true) |
---|
[9637] | 169 | { |
---|
| 170 | return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * @brief Registers a class in the framework. |
---|
| 175 | * @param name The name of the class |
---|
| 176 | * @param factory The factory which is able to create new instances of this class |
---|
| 177 | * @param bLoadable Whether the class is allowed to be loaded through XML |
---|
| 178 | */ |
---|
| 179 | template <class T> |
---|
[9646] | 180 | inline Identifier& registerClass(const std::string& name, Factory* factory, bool bLoadable = true) |
---|
[9637] | 181 | { |
---|
| 182 | orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl; |
---|
| 183 | Identifier* identifier = ClassIdentifier<T>::getIdentifier(name); |
---|
| 184 | identifier->setFactory(factory); |
---|
| 185 | identifier->setLoadable(bLoadable); |
---|
[9646] | 186 | return *identifier; |
---|
[9637] | 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
[5929] | 190 | @brief Returns the Identifier with a given name. |
---|
[7401] | 191 | @param name The name of the class |
---|
[3325] | 192 | */ |
---|
| 193 | inline Identifier* ClassByString(const std::string& name) |
---|
| 194 | { |
---|
[9640] | 195 | return IdentifierManager::getInstance().getIdentifierByString(name); |
---|
[3325] | 196 | } |
---|
[1505] | 197 | |
---|
[3325] | 198 | /** |
---|
[5929] | 199 | @brief Returns the Identifier with a given lowercase name. |
---|
[7401] | 200 | @param name The lowercase name of the class |
---|
[5929] | 201 | */ |
---|
| 202 | inline Identifier* ClassByLowercaseString(const std::string& name) |
---|
| 203 | { |
---|
[9640] | 204 | return IdentifierManager::getInstance().getIdentifierByLowercaseString(name); |
---|
[5929] | 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | @brief Returns the Identifier with a given network ID. |
---|
[7401] | 209 | @param id The network ID of the class |
---|
[3325] | 210 | */ |
---|
| 211 | inline Identifier* ClassByID(uint32_t id) |
---|
| 212 | { |
---|
[9640] | 213 | return IdentifierManager::getInstance().getIdentifierByID(id); |
---|
[3325] | 214 | } |
---|
[6423] | 215 | |
---|
| 216 | /** |
---|
| 217 | @brief Returns the Identifier with a given 'this' pointer. |
---|
[9567] | 218 | @note This of course only works with Identifiables. |
---|
[6423] | 219 | The only use is in conjunction with macros that don't know the class type. |
---|
[9567] | 220 | @param object Pointer to an Identifiable |
---|
[6423] | 221 | */ |
---|
| 222 | template <class T> |
---|
[8706] | 223 | inline Identifier* ClassByObjectType(const T*) |
---|
[6423] | 224 | { |
---|
| 225 | return ClassIdentifier<T>::getIdentifier(); |
---|
| 226 | } |
---|
[3325] | 227 | } |
---|
| 228 | |
---|
[1505] | 229 | #endif /* _CoreIncludes_H__ */ |
---|