[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 | /** |
---|
[7401] | 30 | @defgroup Factory RegisterObject() and CreateFactory() |
---|
| 31 | @ingroup Object |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
[2171] | 35 | @file |
---|
[7401] | 36 | @ingroup Object Factory |
---|
| 37 | @brief Defines several very important macros used to register objects, create factories, 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 | |
---|
[7401] | 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 |
---|
[1505] | 73 | */ |
---|
| 74 | |
---|
| 75 | #ifndef _CoreIncludes_H__ |
---|
| 76 | #define _CoreIncludes_H__ |
---|
| 77 | |
---|
| 78 | #include "CorePrereqs.h" |
---|
| 79 | |
---|
[3196] | 80 | #include "util/Debug.h" |
---|
[1505] | 81 | #include "Identifier.h" |
---|
[5929] | 82 | #include "SubclassIdentifier.h" |
---|
[1505] | 83 | #include "ClassFactory.h" |
---|
[3196] | 84 | #include "ObjectList.h" |
---|
[1505] | 85 | |
---|
| 86 | |
---|
| 87 | /** |
---|
[7401] | 88 | @brief Intern macro, containing the common parts of @c RegisterObject and @c RegisterRootObject. |
---|
[1505] | 89 | @param ClassName The name of the class |
---|
[7401] | 90 | @param bRootClass True if the class is directly derived from orxonox::OrxonoxClass |
---|
[1505] | 91 | */ |
---|
| 92 | #define InternRegisterObject(ClassName, bRootClass) \ |
---|
[3325] | 93 | if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initialiseObject(this, #ClassName, bRootClass)) \ |
---|
[1747] | 94 | return; \ |
---|
[3325] | 95 | else \ |
---|
| 96 | ((void)0) |
---|
[1505] | 97 | |
---|
| 98 | /** |
---|
[7401] | 99 | @brief Registers a newly created object in the core. Has to be called at the beginning of the constructor of @a ClassName. |
---|
[1505] | 100 | @param ClassName The name of the class |
---|
| 101 | */ |
---|
| 102 | #define RegisterObject(ClassName) \ |
---|
| 103 | InternRegisterObject(ClassName, false) |
---|
| 104 | |
---|
| 105 | /** |
---|
[7401] | 106 | @brief Registers a newly created object in the core. Has to be called at the beginning of the constructor of @a ClassName. |
---|
[1505] | 107 | @param ClassName The name of the class |
---|
[7401] | 108 | |
---|
| 109 | In contrast to RegisterObject, this is used for classes that inherit directly from |
---|
| 110 | orxonox::OrxonoxClass, namely all interfaces and orxonox::BaseObject. |
---|
[1505] | 111 | */ |
---|
| 112 | #define RegisterRootObject(ClassName) \ |
---|
[3325] | 113 | InternRegisterObject(ClassName, true) |
---|
[1505] | 114 | |
---|
| 115 | /** |
---|
[5929] | 116 | @brief Creates the Factory. |
---|
[1856] | 117 | @param ClassName The name of the class |
---|
| 118 | */ |
---|
| 119 | #define CreateFactory(ClassName) \ |
---|
[5929] | 120 | Factory* _##ClassName##Factory = new orxonox::ClassFactory<ClassName>(#ClassName, true) |
---|
[1856] | 121 | |
---|
| 122 | /** |
---|
[5929] | 123 | @brief Creates the Factory for classes which should not be loaded through XML. |
---|
[5781] | 124 | @param ClassName The name of the class |
---|
| 125 | */ |
---|
| 126 | #define CreateUnloadableFactory(ClassName) \ |
---|
[5929] | 127 | Factory* _##ClassName##Factory = new orxonox::ClassFactory<ClassName>(#ClassName, false) |
---|
[5781] | 128 | |
---|
| 129 | /** |
---|
[1505] | 130 | @brief Returns the Identifier of the given class. |
---|
| 131 | @param ClassName The name of the class |
---|
| 132 | */ |
---|
[1789] | 133 | #define Class(ClassName) \ |
---|
[2087] | 134 | orxonox::ClassIdentifier<ClassName>::getIdentifier() |
---|
[1505] | 135 | |
---|
| 136 | |
---|
[3325] | 137 | namespace orxonox |
---|
| 138 | { |
---|
| 139 | /** |
---|
[5929] | 140 | @brief Returns the Identifier with a given name. |
---|
[7401] | 141 | @param name The name of the class |
---|
[3325] | 142 | */ |
---|
| 143 | inline Identifier* ClassByString(const std::string& name) |
---|
| 144 | { |
---|
[5929] | 145 | return Identifier::getIdentifierByString(name); |
---|
[3325] | 146 | } |
---|
[1505] | 147 | |
---|
[3325] | 148 | /** |
---|
[5929] | 149 | @brief Returns the Identifier with a given lowercase name. |
---|
[7401] | 150 | @param name The lowercase name of the class |
---|
[5929] | 151 | */ |
---|
| 152 | inline Identifier* ClassByLowercaseString(const std::string& name) |
---|
| 153 | { |
---|
| 154 | return Identifier::getIdentifierByLowercaseString(name); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | @brief Returns the Identifier with a given network ID. |
---|
[7401] | 159 | @param id The network ID of the class |
---|
[3325] | 160 | */ |
---|
| 161 | inline Identifier* ClassByID(uint32_t id) |
---|
| 162 | { |
---|
[5929] | 163 | return Identifier::getIdentifierByID(id); |
---|
[3325] | 164 | } |
---|
[6423] | 165 | |
---|
| 166 | /** |
---|
| 167 | @brief Returns the Identifier with a given 'this' pointer. |
---|
| 168 | @note This of course only works with OrxonoxClasses. |
---|
| 169 | The only use is in conjunction with macros that don't know the class type. |
---|
[7401] | 170 | @param object Pointer to an OrxonoxClass |
---|
[6423] | 171 | */ |
---|
| 172 | template <class T> |
---|
| 173 | inline Identifier* ClassByObjectType(const T* object) |
---|
| 174 | { |
---|
| 175 | return ClassIdentifier<T>::getIdentifier(); |
---|
| 176 | } |
---|
[3325] | 177 | } |
---|
| 178 | |
---|
[1505] | 179 | #endif /* _CoreIncludes_H__ */ |
---|