Changeset 1543 for code/trunk/src/core/ClassManager.h
- Timestamp:
- Jun 5, 2008, 2:18:14 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/core/ClassManager.h
r1505 r1543 1 /*2 * ORXONOX - the hottest 3D action shooter ever to exist3 * > www.orxonox.net <4 *5 *6 * License notice:7 *8 * This program is free software; you can redistribute it and/or9 * modify it under the terms of the GNU General Public License10 * as published by the Free Software Foundation; either version 211 * 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 of15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16 * GNU General Public License for more details.17 *18 * You should have received a copy of the GNU General Public License19 * along with this program; if not, write to the Free Software20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.21 *22 * Author:23 * Fabian 'x3n' Landau24 * Co-authors:25 * ...26 *27 */28 29 /**30 @file ClassManager.h31 @brief Definition and Implementation of the ClassManager template.32 33 The ClassManager is a helper-class for ClassIdentifier. Because ClassIdentifiers must34 be unique, they are created through the IdentifierDistributor-class to assure the35 uniqueness of the ClassIdentifier. But accessing Identifiers through IdentifierDistributor36 is slow, because it uses strings and a map. Thats why we use the ClassManager-template: It's37 a singleton like ClassIdentifier, but it doesn't hurt if there are multiple instances in38 different libraries, because they all store the same pointer to the unique ClassIdentifier39 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 orxonox54 {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 once58 and stores the result in a member-variable. IdentifierDistributor assures the uniqueness59 of the ClassIdentifier, even if there are multiple instances of the ClassManager-template60 in different libraries.61 */62 template <class T>63 class ClassManager64 {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 copy73 ~ClassManager() {} // don't delete74 75 bool bInitialized_; //!< This is false until the ClassIdentifier gets assigned76 ClassIdentifier<T>* identifier_; //!< The unique ClassIdentifier for the class T77 };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 instance91 */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 Identifier102 */103 template <class T>104 ClassIdentifier<T>* ClassManager<T>::getIdentifier()105 {106 // Check if the ClassManager is already initialized107 if (!ClassManager<T>::getSingleton()->bInitialized_)108 {109 // Get the name of the class110 std::string name = typeid(T).name();111 112 // It's not -> retrieve the ClassIdentifier through IdentifierDistributor113 COUT(4) << "*** ClassManager: Request Identifier Singleton for " << name << "." << std::endl;114 115 // First create a ClassIdentifier in case there's no instance existing yet116 ClassIdentifier<T>* temp = new ClassIdentifier<T>();117 118 // Ask the IdentifierDistributor for the unique ClassIdentifier119 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 more122 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 proposal127 delete temp;128 }129 else130 {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 ClassIdentifier138 return ClassManager<T>::getSingleton()->identifier_;139 }140 141 /**142 @brief Returns the name of the class the ClassManager belongs to.143 @return The name144 */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 else153 return unknownClassName;154 }155 }156 157 #endif /* _ClassManager_H__ */
Note: See TracChangeset
for help on using the changeset viewer.