Changeset 496 for code/branches/FICN/src/orxonox/core/Identifier.h
- Timestamp:
- Dec 12, 2007, 10:37:30 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/orxonox/core/Identifier.h
r384 r496 1 1 /*! 2 2 @file Identifier.h 3 @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes .3 @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes, implementation of the ClassIdentifier and SubclassIdentifier classes. 4 4 5 5 The Identifier contains all needed informations about the class it belongs to: … … 7 7 - a list with all objects 8 8 - parents and childs 9 - the factory , if available9 - the factory (if available) 10 10 - the networkID that can be synchronised with the server 11 - all configurable variables (if available) 11 12 12 13 Every object has a pointer to the Identifier of its class. This allows the use isA(...), … … 18 19 19 20 SubclassIdentifier is a separated class, acting like an Identifier, but has a given class. 20 You can only assign Identifiers of the given class ora derivative to a SubclassIdentifier.21 You can only assign Identifiers of exactly the given class or of a derivative to a SubclassIdentifier. 21 22 */ 22 23 … … 25 26 26 27 #include <iostream> 28 #include <map> 27 29 28 30 #include "IdentifierList.h" 29 31 #include "ObjectList.h" 30 32 #include "Factory.h" 31 32 #define HIERARCHY_VERBOSE false 33 33 #include "ConfigValueContainer.h" 34 #include "Debug.h" 34 35 35 36 namespace orxonox … … 46 47 - a list with all objects 47 48 - parents and childs 48 - the factory , if available49 - the factory (if available) 49 50 - the networkID that can be synchronised with the server 51 - all configurable variables (if available) 50 52 51 53 Every object has a pointer to the Identifier of its class. This allows the use isA(...), … … 62 64 friend class SubclassIdentifier; // Forward declaration 63 65 64 template <class T> 65 friend class ClassFactory; // Forward declaration 66 friend class Factory; // Forward declaration 66 67 67 68 public: … … 85 86 inline IdentifierList& getChildren() const { return *this->children_; } 86 87 87 /** @returns true, if a branch of the class-hierarchy is getting created, causing all new objects to store their parents. */88 /** @returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. */ 88 89 inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } 89 90 90 /** @returns the NetworkID to identify a class through the network. */91 /** @returns the network ID to identify a class through the network. */ 91 92 inline const unsigned int getNetworkID() const { return this->classID_; } 92 93 94 /** @brief Sets the network ID to a new value. @param id The new value */ 93 95 void setNetworkID(unsigned int id); 96 97 /** @returns the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variable */ 98 inline ConfigValueContainer* getConfigValueContainer(const std::string& varname) 99 { return this->configValues_[varname]; } 100 101 /** @brief Sets the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variablee @param container The container */ 102 inline void setConfigValueContainer(const std::string& varname, ConfigValueContainer* container) 103 { this->configValues_[varname] = container; } 94 104 95 105 private: … … 105 115 { 106 116 hierarchyCreatingCounter_s++; 107 #if HIERARCHY_VERBOSE 108 std::cout << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; 109 #endif 117 COUT(4) << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; 110 118 } 111 119 … … 116 124 { 117 125 hierarchyCreatingCounter_s--; 118 #if HIERARCHY_VERBOSE 119 std::cout << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; 120 #endif 121 } 122 123 IdentifierList parents_; //!< The Parents of the class the Identifier belongs to 124 IdentifierList* children_; //!< The Children of the class the Identifier belongs to 125 126 std::string name_; //!< The name of the class the Identifier belongs to 127 128 BaseFactory* factory_; //!< The Factory, able to create new objects of the given class 129 bool bCreatedOneObject_; //!< True if at least one object of the given type was created (used to determine the need of storing the parents) 130 static int hierarchyCreatingCounter_s; //!< Bigger than zero if at least one Identifier stores its parents (its an int instead of a bool to avoid conflicts with multithreading) 131 static unsigned int classIDcounter_s; //!< The number of unique Identifiers 132 unsigned int classID_; //!< The networkID to identify a class through the network 126 COUT(4) << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; 127 } 128 129 IdentifierList parents_; //!< The Parents of the class the Identifier belongs to 130 IdentifierList* children_; //!< The Children of the class the Identifier belongs to 131 132 std::string name_; //!< The name of the class the Identifier belongs to 133 134 BaseFactory* factory_; //!< The Factory, able to create new objects of the given class (if available) 135 bool bCreatedOneObject_; //!< True if at least one object of the given type was created (used to determine the need of storing the parents) 136 static int hierarchyCreatingCounter_s; //!< Bigger than zero if at least one Identifier stores its parents (its an int instead of a bool to avoid conflicts with multithreading) 137 static unsigned int classIDcounter_s; //!< The number of existing Identifiers 138 unsigned int classID_; //!< The network ID to identify a class through the network 139 std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer 133 140 }; 134 141 … … 164 171 165 172 /** 166 @brief Constructor: Create the ObjectList.173 @brief Constructor: Creates the ObjectList. 167 174 */ 168 175 template <class T> … … 173 180 174 181 /** 175 @brief Destructor: Delete the ObjectList, setthe singleton-pointer to zero.182 @brief Destructor: Deletes the ObjectList, sets the singleton-pointer to zero. 176 183 */ 177 184 template <class T> … … 186 193 @param parents An IdentifierList, containing the Identifiers of all parents of the class 187 194 @param name A string, containing exactly the name of the class 188 @param bRootClass True if the class is either an Interface or BaseObject itself195 @param bRootClass True if the class is either an Interface or the BaseObject itself 189 196 @return The ClassIdentifier itself 190 197 */ … … 192 199 ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass) 193 200 { 194 #if HIERARCHY_VERBOSE 195 std::cout << "*** Register Class in " << name << "-Singleton.\n"; 196 #endif 201 COUT(4) << "*** Register Class in " << name << "-Singleton.\n"; 197 202 198 203 // It's a singleton, so maybe we have to create it first 199 204 if (!pointer_s) 200 205 { 201 #if HIERARCHY_VERBOSE 202 std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n"; 203 #endif 206 COUT(4) << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n"; 204 207 pointer_s = new ClassIdentifier(); 205 208 } … … 210 213 // If no: We have to store the informations and initialize the Identifier 211 214 212 #if HIERARCHY_VERBOSE 213 std::cout << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n"; 214 #endif 215 COUT(4) << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n"; 215 216 pointer_s->name_ = name; 216 Factory::add(name, pointer_s); // Add the Identifier to the Factory217 // Factory::add(name, pointer_s); // Add the Identifier to the Factory 217 218 218 219 if (bRootClass) … … 226 227 227 228 /** 228 @returns the Identifier itself 229 @returns the Identifier itself. 229 230 */ 230 231 template <class T> … … 233 234 if (!pointer_s) 234 235 { 235 #if HIERARCHY_VERBOSE 236 std::cout << "*** Create Singleton.\n"; 237 #endif 236 COUT(4) << "*** Create Singleton.\n"; 238 237 pointer_s = new ClassIdentifier(); 239 238 } … … 249 248 void ClassIdentifier<T>::addObject(T* object) 250 249 { 251 #if HIERARCHY_VERBOSE 252 std::cout << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n"; 253 #endif 250 COUT(4) << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n"; 254 251 object->getMetaList().add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object)); 255 252 } … … 261 258 //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites. 262 259 /** 263 You can only assign Identifiers that belong to a class of at least B(or derived) to a SubclassIdentifier<T>.260 You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>. 264 261 If you assign something else, the program aborts. 265 262 Because we know the minimal type, a dynamic_cast is done, which makes it easier to create a new object. … … 286 283 if (!identifier->isA(ClassIdentifier<T>::getIdentifier())) 287 284 { 288 std::cout<< "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n";289 std::cout<< "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n";290 std::cout<< "Aborting...\n";285 COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n"; 286 COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n"; 287 COUT(1) << "Aborting...\n"; 291 288 abort(); 292 289 } … … 314 311 315 312 /** 316 @brief Creates a new object of the type of the assigned identifier and dynamic_casts it to the minimal type given by the SubclassIdentifier.313 @brief Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T. 317 314 @return The new object 318 315 */ … … 321 318 BaseObject* newObject = this->identifier_->fabricate(); 322 319 323 // Check if the creation w orked320 // Check if the creation was successful 324 321 if (newObject) 325 322 { … … 332 329 if (this->identifier_) 333 330 { 334 std::cout<< "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n";335 std::cout<< "Error: Couldn't fabricate a new Object.\n";336 std::cout<< "Aborting...\n";331 COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n"; 332 COUT(1) << "Error: Couldn't fabricate a new Object.\n"; 333 COUT(1) << "Aborting...\n"; 337 334 } 338 335 else 339 336 { 340 std::cout<< "Error: Couldn't fabricate a new Object - Identifier is undefined.\n";341 std::cout<< "Aborting...\n";337 COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined.\n"; 338 COUT(1) << "Aborting...\n"; 342 339 } 343 340
Note: See TracChangeset
for help on using the changeset viewer.