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