| 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 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 |  | 
|---|
| 42 | #ifndef _ClassManager_H__ | 
|---|
| 43 | #define _ClassManager_H__ | 
|---|
| 44 |  | 
|---|
| 45 | #include "CorePrereqs.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include <string> | 
|---|
| 48 |  | 
|---|
| 49 | #include "Identifier.h" | 
|---|
| 50 | #include "IdentifierDistributor.h" | 
|---|
| 51 | #include "Debug.h" | 
|---|
| 52 |  | 
|---|
| 53 | namespace orxonox | 
|---|
| 54 | { | 
|---|
| 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 | */ | 
|---|
| 62 | template <class T> | 
|---|
| 63 | class ClassManager | 
|---|
| 64 | { | 
|---|
| 65 | public: | 
|---|
| 66 | static ClassManager<T>* getSingleton(); | 
|---|
| 67 | static ClassIdentifier<T>* getIdentifier(); | 
|---|
| 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 |  | 
|---|
| 75 | bool bInitialized_;                 //!< This is false until the ClassIdentifier gets assigned | 
|---|
| 76 | ClassIdentifier<T>* identifier_;    //!< The unique ClassIdentifier for the class T | 
|---|
| 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 |  | 
|---|
| 88 | /** | 
|---|
| 89 | @brief Returns the one and only instance of this class for the template parameter T. | 
|---|
| 90 | @return The instance | 
|---|
| 91 | */ | 
|---|
| 92 | template <class T> | 
|---|
| 93 | ClassManager<T>* ClassManager<T>::getSingleton() | 
|---|
| 94 | { | 
|---|
| 95 | static ClassManager<T> theOneAndOnlyInstance = ClassManager<T>(); | 
|---|
| 96 | return &theOneAndOnlyInstance; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 | @brief Creates the only instance of this class for the template class T and retrieves a unique Identifier for the given name. | 
|---|
| 101 | @return The unique Identifier | 
|---|
| 102 | */ | 
|---|
| 103 | template <class T> | 
|---|
| 104 | ClassIdentifier<T>* ClassManager<T>::getIdentifier() | 
|---|
| 105 | { | 
|---|
| 106 | // Check if the ClassManager is already initialized | 
|---|
| 107 | if (!ClassManager<T>::getSingleton()->bInitialized_) | 
|---|
| 108 | { | 
|---|
| 109 | // Get the name of the class | 
|---|
| 110 | std::string name = typeid(T).name(); | 
|---|
| 111 |  | 
|---|
| 112 | // It's not -> retrieve the ClassIdentifier through IdentifierDistributor | 
|---|
| 113 | COUT(4) << "*** ClassManager: Request Identifier Singleton for " << name << "." << std::endl; | 
|---|
| 114 |  | 
|---|
| 115 | // First create a ClassIdentifier in case there's no instance existing yet | 
|---|
| 116 | ClassIdentifier<T>* temp = new ClassIdentifier<T>(); | 
|---|
| 117 |  | 
|---|
| 118 | // Ask the IdentifierDistributor for the unique ClassIdentifier | 
|---|
| 119 | ClassManager<T>::getSingleton()->identifier_ = (ClassIdentifier<T>*)IdentifierDistributor::getIdentifier(name, temp); | 
|---|
| 120 |  | 
|---|
| 121 | // If the retrieved Identifier differs from our proposal, we don't need the proposal any more | 
|---|
| 122 | if (temp != ClassManager<T>::getSingleton()->identifier_) | 
|---|
| 123 | { | 
|---|
| 124 | COUT(4) << "*** ClassManager: Requested Identifier for " << name << " was already existing and got assigned." << std::endl; | 
|---|
| 125 |  | 
|---|
| 126 | // Delete the unnecessary proposal | 
|---|
| 127 | delete temp; | 
|---|
| 128 | } | 
|---|
| 129 | else | 
|---|
| 130 | { | 
|---|
| 131 | COUT(4) << "*** ClassManager: Requested Identifier for " << name << " was not yet existing and got created." << std::endl; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | ClassManager<T>::getSingleton()->bInitialized_ = true; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | // Finally return the unique ClassIdentifier | 
|---|
| 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 |  | 
|---|
| 150 | if (ClassManager<T>::getSingleton()->bInitialized_) | 
|---|
| 151 | return ClassManager<T>::getSingleton()->identifier_->getName(); | 
|---|
| 152 | else | 
|---|
| 153 | return unknownClassName; | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | #endif /* _ClassManager_H__ */ | 
|---|