[805] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[805] | 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 | |
---|
[813] | 29 | /** |
---|
[809] | 30 | @file ClassManager.h |
---|
| 31 | @brief Definition and Implementation of the ClassManager template. |
---|
| 32 | |
---|
| 33 | The ClassManager is a helper-class for ClassIdentifier. Because ClassIdentifiers must |
---|
| 34 | be unique, they are created through the IdentifierDistributor-class to assure the |
---|
| 35 | uniqueness of the ClassIdentifier. But accessing Identifiers through IdentifierDistributor |
---|
| 36 | is slow, because it uses strings and a map. Thats why we use the ClassManager-template: It's |
---|
| 37 | a singleton like ClassIdentifier, but it doesn't hurt if there are multiple instances in |
---|
| 38 | different libraries, because they all store the same pointer to the unique ClassIdentifier |
---|
| 39 | which they've retrieved through IdentifierDistributor. |
---|
| 40 | */ |
---|
| 41 | |
---|
[805] | 42 | #ifndef _ClassManager_H__ |
---|
| 43 | #define _ClassManager_H__ |
---|
| 44 | |
---|
[1062] | 45 | #include "CorePrereqs.h" |
---|
| 46 | |
---|
[805] | 47 | #include <string> |
---|
| 48 | |
---|
| 49 | #include "Identifier.h" |
---|
| 50 | #include "IdentifierDistributor.h" |
---|
[1313] | 51 | #include "Debug.h" |
---|
[805] | 52 | |
---|
| 53 | namespace orxonox |
---|
| 54 | { |
---|
[809] | 55 | //! ClassManager is a helper class to allow faster access on the ClassIdentifiers. |
---|
| 56 | /** |
---|
| 57 | Because accessing the IdentifierDistributor is slow, the ClassManager accesses it once |
---|
| 58 | and stores the result in a member-variable. IdentifierDistributor assures the uniqueness |
---|
| 59 | of the ClassIdentifier, even if there are multiple instances of the ClassManager-template |
---|
| 60 | in different libraries. |
---|
| 61 | */ |
---|
[805] | 62 | template <class T> |
---|
| 63 | class ClassManager |
---|
| 64 | { |
---|
| 65 | public: |
---|
| 66 | static ClassManager<T>* getSingleton(); |
---|
[811] | 67 | static ClassIdentifier<T>* getIdentifier(); |
---|
[805] | 68 | static const std::string& getName(); |
---|
| 69 | |
---|
| 70 | private: |
---|
| 71 | ClassManager(); |
---|
| 72 | ClassManager(const ClassIdentifier<T>& identifier) {} // don't copy |
---|
| 73 | ~ClassManager() {} // don't delete |
---|
| 74 | |
---|
[809] | 75 | bool bInitialized_; //!< This is false until the ClassIdentifier gets assigned |
---|
| 76 | ClassIdentifier<T>* identifier_; //!< The unique ClassIdentifier for the class T |
---|
[805] | 77 | }; |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | @brief Constructor: Marks the ClassManager as uninitialized. |
---|
| 81 | */ |
---|
| 82 | template <class T> |
---|
| 83 | ClassManager<T>::ClassManager() |
---|
| 84 | { |
---|
| 85 | this->bInitialized_ = false; |
---|
| 86 | } |
---|
| 87 | |
---|
[806] | 88 | /** |
---|
| 89 | @brief Returns the one and only instance of this class for the template parameter T. |
---|
| 90 | @return The instance |
---|
| 91 | */ |
---|
[805] | 92 | template <class T> |
---|
| 93 | ClassManager<T>* ClassManager<T>::getSingleton() |
---|
| 94 | { |
---|
| 95 | static ClassManager<T> theOneAndOnlyInstance = ClassManager<T>(); |
---|
| 96 | return &theOneAndOnlyInstance; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
[811] | 100 | @brief Creates the only instance of this class for the template class T and retrieves a unique Identifier for the given name. |
---|
[805] | 101 | @return The unique Identifier |
---|
| 102 | */ |
---|
| 103 | template <class T> |
---|
[811] | 104 | ClassIdentifier<T>* ClassManager<T>::getIdentifier() |
---|
[805] | 105 | { |
---|
[809] | 106 | // Check if the ClassManager is already initialized |
---|
[805] | 107 | if (!ClassManager<T>::getSingleton()->bInitialized_) |
---|
| 108 | { |
---|
[811] | 109 | // Get the name of the class |
---|
| 110 | std::string name = typeid(T).name(); |
---|
| 111 | |
---|
[809] | 112 | // It's not -> retrieve the ClassIdentifier through IdentifierDistributor |
---|
[811] | 113 | COUT(4) << "*** ClassManager: Request Identifier Singleton for " << name << "." << std::endl; |
---|
[805] | 114 | |
---|
[809] | 115 | // First create a ClassIdentifier in case there's no instance existing yet |
---|
[805] | 116 | ClassIdentifier<T>* temp = new ClassIdentifier<T>(); |
---|
[809] | 117 | |
---|
| 118 | // Ask the IdentifierDistributor for the unique ClassIdentifier |
---|
[805] | 119 | ClassManager<T>::getSingleton()->identifier_ = (ClassIdentifier<T>*)IdentifierDistributor::getIdentifier(name, temp); |
---|
[809] | 120 | |
---|
| 121 | // If the retrieved Identifier differs from our proposal, we don't need the proposal any more |
---|
[805] | 122 | if (temp != ClassManager<T>::getSingleton()->identifier_) |
---|
[811] | 123 | { |
---|
| 124 | COUT(4) << "*** ClassManager: Requested Identifier for " << name << " was already existing and got assigned." << std::endl; |
---|
| 125 | |
---|
| 126 | // Delete the unnecessary proposal |
---|
[805] | 127 | delete temp; |
---|
[811] | 128 | } |
---|
| 129 | else |
---|
| 130 | { |
---|
| 131 | COUT(4) << "*** ClassManager: Requested Identifier for " << name << " was not yet existing and got created." << std::endl; |
---|
| 132 | } |
---|
[809] | 133 | |
---|
[805] | 134 | ClassManager<T>::getSingleton()->bInitialized_ = true; |
---|
| 135 | } |
---|
| 136 | |
---|
[809] | 137 | // Finally return the unique ClassIdentifier |
---|
[805] | 138 | return ClassManager<T>::getSingleton()->identifier_; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief Returns the name of the class the ClassManager belongs to. |
---|
| 143 | @return The name |
---|
| 144 | */ |
---|
| 145 | template <class T> |
---|
| 146 | const std::string& ClassManager<T>::getName() |
---|
| 147 | { |
---|
| 148 | static std::string unknownClassName = std::string("unknown"); |
---|
| 149 | |
---|
[810] | 150 | if (ClassManager<T>::getSingleton()->bInitialized_) |
---|
[805] | 151 | return ClassManager<T>::getSingleton()->identifier_->getName(); |
---|
| 152 | else |
---|
| 153 | return unknownClassName; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | #endif /* _ClassManager_H__ */ |
---|