[5776] | 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 | @file |
---|
[7401] | 31 | @ingroup Class Identifier |
---|
[5776] | 32 | @brief Definition of SubclassIdentifier. |
---|
| 33 | |
---|
[7401] | 34 | @anchor SubclassIdentifierExample |
---|
| 35 | |
---|
[5776] | 36 | SubclassIdentifier is a separated class, acting like an Identifier, but has a given class. |
---|
| 37 | You can only assign Identifiers of exactly the given class or of a derivative to a SubclassIdentifier. |
---|
[7401] | 38 | |
---|
| 39 | Example: |
---|
| 40 | |
---|
| 41 | You can assign an Identifier either through the constructor or by using the assignment @c operator=: |
---|
| 42 | @code |
---|
| 43 | SubclassIdentifier<BaseClass> identifier = Class(SubClass); |
---|
| 44 | @endcode |
---|
| 45 | |
---|
| 46 | The @c operator-> is overloaded an returns the assigned Identifier. That way you can just call |
---|
| 47 | functions of the assigned Identifier by using @c ->function(): |
---|
| 48 | @code |
---|
| 49 | SubclassIdentifier<BaseClass> identifier = Class(SubClass); |
---|
| 50 | identifier->getName(); // returns "SubClass" |
---|
| 51 | @endcode |
---|
| 52 | |
---|
| 53 | There are two possibilities to create an object out of a SubclassIdentifier: Either you just use |
---|
| 54 | the @c fabricate() function of the assigned Identifier through the overloaded @c operator->, which |
---|
| 55 | returns a @c BaseObject* pointer, or you use the function of SubclassIdentifier, this time by using |
---|
| 56 | @c operator., which returns a @c BaseClass* pointer (@a BaseClass is the baseclass specified by the |
---|
| 57 | template argument): |
---|
| 58 | @code |
---|
| 59 | identifier->fabricate(); // calls Identifier::fabricate(), creates a SubClass, returns a BaseObject* pointer |
---|
| 60 | |
---|
| 61 | identifier.fabricate(); // calls SubclassIdentifier::fabricate(), creates a SubClass, returns a BaseClass* pointer |
---|
| 62 | @endcode |
---|
[5776] | 63 | */ |
---|
| 64 | |
---|
| 65 | #ifndef _SubclassIdentifier_H__ |
---|
| 66 | #define _SubclassIdentifier_H__ |
---|
| 67 | |
---|
| 68 | #include "CorePrereqs.h" |
---|
| 69 | |
---|
[5818] | 70 | #include <cstdlib> |
---|
[5776] | 71 | #include "util/Debug.h" |
---|
| 72 | #include "Identifier.h" |
---|
| 73 | |
---|
| 74 | namespace orxonox |
---|
| 75 | { |
---|
| 76 | // ############################### |
---|
| 77 | // ### SubclassIdentifier ### |
---|
| 78 | // ############################### |
---|
| 79 | /** |
---|
[7401] | 80 | @brief The SubclassIdentifier acts almost like an Identifier, but has some prerequisites. |
---|
| 81 | |
---|
[5776] | 82 | You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>. |
---|
[7401] | 83 | If you assign something else, the program prints an error. |
---|
| 84 | |
---|
| 85 | Because we know the base-type, a @c dynamic_cast is done, which makes it easier to create a new object. |
---|
| 86 | |
---|
| 87 | @see See @ref SubclassIdentifierExample "SubclassIdentifier.h" for some examples. |
---|
[5776] | 88 | */ |
---|
| 89 | template <class T> |
---|
| 90 | class SubclassIdentifier |
---|
| 91 | { |
---|
| 92 | public: |
---|
[7401] | 93 | /// Constructor: Automaticaly assigns the Identifier of the given class. |
---|
[5776] | 94 | SubclassIdentifier() |
---|
| 95 | { |
---|
| 96 | this->identifier_ = ClassIdentifier<T>::getIdentifier(); |
---|
| 97 | } |
---|
| 98 | |
---|
[7401] | 99 | /// Constructor: Assigns the given Identifier. |
---|
[5776] | 100 | SubclassIdentifier(Identifier* identifier) |
---|
| 101 | { |
---|
| 102 | this->operator=(identifier); |
---|
| 103 | } |
---|
| 104 | |
---|
[7401] | 105 | /// Copyconstructor: Assigns the identifier of another SubclassIdentifier. |
---|
[5776] | 106 | template <class O> |
---|
| 107 | SubclassIdentifier(const SubclassIdentifier<O>& identifier) |
---|
| 108 | { |
---|
| 109 | this->operator=(identifier.getIdentifier()); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | @brief Overloading of the = operator: assigns the identifier and checks its type. |
---|
| 114 | @param identifier The Identifier to assign |
---|
| 115 | @return The SubclassIdentifier itself |
---|
| 116 | */ |
---|
[7268] | 117 | SubclassIdentifier<T>& operator=(Identifier* identifier) |
---|
[5776] | 118 | { |
---|
| 119 | if (!identifier || !identifier->isA(ClassIdentifier<T>::getIdentifier())) |
---|
| 120 | { |
---|
| 121 | COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl; |
---|
| 122 | if (identifier) |
---|
| 123 | { |
---|
[6417] | 124 | COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << '!' << std::endl; |
---|
[5776] | 125 | COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl; |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | COUT(1) << "Error: Can't assign NULL identifier" << std::endl; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | this->identifier_ = identifier; |
---|
| 135 | } |
---|
| 136 | return *this; |
---|
| 137 | } |
---|
| 138 | |
---|
[7401] | 139 | /// Overloading of the = operator: assigns the identifier of another SubclassIdentifier. |
---|
[5776] | 140 | template <class O> |
---|
[7268] | 141 | SubclassIdentifier<T>& operator=(const SubclassIdentifier<O>& identifier) |
---|
[5776] | 142 | { |
---|
| 143 | return this->operator=(identifier.getIdentifier()); |
---|
| 144 | } |
---|
| 145 | |
---|
[7401] | 146 | /// Overloading of the * operator: returns the assigned identifier. |
---|
[5776] | 147 | inline Identifier* operator*() const |
---|
| 148 | { |
---|
| 149 | return this->identifier_; |
---|
| 150 | } |
---|
| 151 | |
---|
[7401] | 152 | /// Overloading of the -> operator: returns the assigned identifier. |
---|
[5776] | 153 | inline Identifier* operator->() const |
---|
| 154 | { |
---|
| 155 | return this->identifier_; |
---|
| 156 | } |
---|
| 157 | |
---|
[7401] | 158 | /// Returns the assigned identifier. This allows you to assign a SubclassIdentifier to a normal Identifier*. |
---|
[5776] | 159 | inline operator Identifier*() const |
---|
| 160 | { |
---|
| 161 | return this->identifier_; |
---|
| 162 | } |
---|
| 163 | |
---|
[7401] | 164 | /// Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T. |
---|
[5776] | 165 | T* fabricate(BaseObject* creator) const |
---|
| 166 | { |
---|
| 167 | BaseObject* newObject = this->identifier_->fabricate(creator); |
---|
| 168 | |
---|
| 169 | // Check if the creation was successful |
---|
| 170 | if (newObject) |
---|
| 171 | { |
---|
| 172 | return orxonox_cast<T*>(newObject); |
---|
| 173 | } |
---|
| 174 | else |
---|
| 175 | { |
---|
| 176 | // Something went terribly wrong |
---|
| 177 | if (this->identifier_) |
---|
| 178 | { |
---|
| 179 | COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl; |
---|
[6417] | 180 | COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << '!' << std::endl; |
---|
[5776] | 181 | COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl; |
---|
| 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl; |
---|
| 186 | COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl; |
---|
| 187 | } |
---|
| 188 | |
---|
[5777] | 189 | COUT(1) << "Aborting..." << std::endl; |
---|
| 190 | abort(); |
---|
[5776] | 191 | return 0; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
[7401] | 195 | /// Returns the assigned identifier. |
---|
[5776] | 196 | inline Identifier* getIdentifier() const |
---|
| 197 | { return this->identifier_; } |
---|
| 198 | |
---|
| 199 | private: |
---|
| 200 | Identifier* identifier_; //!< The assigned identifier |
---|
| 201 | }; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | #endif /* _SubclassIdentifier_H__ */ |
---|