Changeset 871 for code/trunk/src/orxonox/core/Identifier.h
- Timestamp:
- Mar 9, 2008, 4:44:36 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/core/Identifier.h
r790 r871 26 26 */ 27 27 28 /* !28 /** 29 29 @file Identifier.h 30 30 @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes, implementation of the ClassIdentifier and SubclassIdentifier classes. … … 39 39 40 40 Every object has a pointer to the Identifier of its class. This allows the use isA(...), 41 is DirectlyA(...), isChildOf(...) and isParentOf(...).41 isExactlyA(...), isChildOf(...) and isParentOf(...). 42 42 43 43 To create the class-hierarchy, the Identifier has some intern functions and variables. … … 52 52 #define _Identifier_H__ 53 53 54 #include <list> 54 55 #include <map> 55 56 #include <string> … … 59 60 60 61 #include "ObjectList.h" 61 #include "IdentifierList.h"62 62 #include "Debug.h" 63 63 #include "Iterator.h" … … 82 82 83 83 Every object has a pointer to the Identifier of its class. This allows the use isA(...), 84 is DirectlyA(...), isChildOf(...) and isParentOf(...).84 isExactlyA(...), isChildOf(...) and isParentOf(...). 85 85 86 86 You can't directly create an Identifier, it's just the base-class for ClassIdentifier. … … 89 89 { 90 90 template <class T> 91 friend class ClassIdentifier; // Forward declaration91 friend class ClassIdentifier; 92 92 93 93 template <class T> 94 friend class SubclassIdentifier; // Forward declaration95 96 friend class Factory; // Forward declaration94 friend class SubclassIdentifier; 95 96 friend class Factory; 97 97 98 98 public: … … 102 102 BaseObject* fabricate(); 103 103 bool isA(const Identifier* identifier) const; 104 bool is DirectlyA(const Identifier* identifier) const;104 bool isExactlyA(const Identifier* identifier) const; 105 105 bool isChildOf(const Identifier* identifier) const; 106 bool isDirectChildOf(const Identifier* identifier) const; 106 107 bool isParentOf(const Identifier* identifier) const; 107 108 static std::map<std::string, Identifier*>& getIdentifierMap(); 108 bool isDirectParentOf(const Identifier* identifier) const; 109 109 110 110 /** @brief Removes all objects of the corresponding class. */ 111 111 virtual void removeObjects() const = 0; 112 112 113 /** @ returns the name of the class the Identifier belongs to.*/113 /** @brief Returns the name of the class the Identifier belongs to. @return The name */ 114 114 inline const std::string& getName() const { return this->name_; } 115 115 116 /** @returns the parents of the class the Identifier belongs to. */ 117 inline const IdentifierList& getParents() const { return this->parents_; } 118 119 /** @returns the children of the class the Identifier belongs to. */ 120 inline IdentifierList& getChildren() const { return *this->children_; } 121 122 /** @returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. */ 116 /** @brief Returns the parents of the class the Identifier belongs to. @return The list of all parents */ 117 inline const std::list<const Identifier*>& getParents() const { return this->parents_; } 118 /** @brief Returns the begin-iterator of the parents-list. @return The begin-iterator */ 119 inline std::list<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); } 120 /** @brief Returns the end-iterator of the parents-list. @return The end-iterator */ 121 inline std::list<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); } 122 123 /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */ 124 inline const std::list<const Identifier*>& getChildren() const { return (*this->children_); } 125 /** @brief Returns the begin-iterator of the children-list. @return The begin-iterator */ 126 inline std::list<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_->begin(); } 127 /** @brief Returns the end-iterator of the children-list. @return The end-iterator */ 128 inline std::list<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_->end(); } 129 130 /** @brief Returns the direct parents of the class the Identifier belongs to. @return The list of all direct parents */ 131 inline const std::list<const Identifier*>& getDirectParents() const { return this->directParents_; } 132 /** @brief Returns the begin-iterator of the direct-parents-list. @return The begin-iterator */ 133 inline std::list<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); } 134 /** @brief Returns the end-iterator of the direct-parents-list. @return The end-iterator */ 135 inline std::list<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); } 136 137 /** @brief Returns the direct children the class the Identifier belongs to. @return The list of all direct children */ 138 inline const std::list<const Identifier*>& getDirectChildren() const { return (*this->directChildren_); } 139 /** @brief Returns the begin-iterator of the direct-children-list. @return The begin-iterator */ 140 inline std::list<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_->begin(); } 141 /** @brief Returns the end-iterator of the direct-children-list. @return The end-iterator */ 142 inline std::list<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_->end(); } 143 144 /** @brief Returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. @return The status of the class-hierarchy creation */ 123 145 inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } 124 146 125 /** @ returns the network ID to identify a class through the network.*/147 /** @brief Returns the network ID to identify a class through the network. @return the network ID */ 126 148 inline const unsigned int getNetworkID() const { return this->classID_; } 127 149 … … 129 151 void setNetworkID(unsigned int id); 130 152 131 /** @returns the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variable */ 132 inline ConfigValueContainer* getConfigValueContainer(const std::string& varname) 133 { return this->configValues_[varname]; } 134 135 /** @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 */ 136 inline void setConfigValueContainer(const std::string& varname, ConfigValueContainer* container) 137 { this->configValues_[varname] = container; } 153 ConfigValueContainer* getConfigValueContainer(const std::string& varname); 154 void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); 155 156 virtual XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname) = 0; 157 virtual void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) = 0; 158 159 virtual XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname) = 0; 160 virtual void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) = 0; 161 162 static bool identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list); 138 163 139 164 private: … … 141 166 Identifier(const Identifier& identifier) {} // don't copy 142 167 virtual ~Identifier(); 143 void initialize(const IdentifierList* parents); 168 void initialize(std::list<const Identifier*>* parents); 169 170 /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */ 171 inline std::list<const Identifier*>& getChildrenIntern() const { return (*this->children_); } 172 /** @brief Returns the direct children of the class the Identifier belongs to. @return The list of all direct children */ 173 inline std::list<const Identifier*>& getDirectChildrenIntern() const { return (*this->directChildren_); } 144 174 145 175 /** … … 149 179 { 150 180 hierarchyCreatingCounter_s++; 151 COUT(4) << "*** I ncreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;181 COUT(4) << "*** Identifier: Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl; 152 182 } 153 183 … … 158 188 { 159 189 hierarchyCreatingCounter_s--; 160 COUT(4) << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl; 161 } 162 163 IdentifierList parents_; //!< The Parents of the class the Identifier belongs to 164 IdentifierList* children_; //!< The Children of the class the Identifier belongs to 190 COUT(4) << "*** Identifier: Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl; 191 } 192 193 std::list<const Identifier*> parents_; //!< The parents of the class the Identifier belongs to 194 std::list<const Identifier*>* children_; //!< The children of the class the Identifier belongs to 195 196 std::list<const Identifier*> directParents_; //!< The direct parents of the class the Identifier belongs to 197 std::list<const Identifier*>* directChildren_; //!< The direct children of the class the Identifier belongs to 165 198 166 199 std::string name_; //!< The name of the class the Identifier belongs to … … 173 206 }; 174 207 208 std::ostream& operator<<(std::ostream& out, const std::list<const Identifier*>& list); 209 175 210 176 211 // ############################### … … 182 217 This makes it possible to store informations about a class, sharing them with all 183 218 objects of that class without defining static variables in every class. 219 220 To be really sure that not more than exactly one object exists (even with libraries), 221 ClassIdentifiers are only created by IdentifierDistributor. 222 223 You can access the ClassIdentifiers created by IdentifierDistributor through the 224 ClassManager singletons. 184 225 */ 185 226 template <class T> 186 227 class ClassIdentifier : public Identifier 187 228 { 229 template <class TT> 230 friend class ClassManager; 231 188 232 public: 189 static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass); 190 static void addObject(T* object); 191 static ClassIdentifier<T>* getIdentifier(); 233 ClassIdentifier<T>* registerClass(std::list<const Identifier*>* parents, const std::string& name, bool bRootClass); 234 void addObject(T* object); 192 235 void removeObjects() const; 193 236 void setName(const std::string& name); 237 inline const ObjectList<T>* getObjects() const { return this->objects_; } 238 239 XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname); 240 void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container); 241 242 XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname); 243 void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container); 194 244 195 245 private: … … 200 250 ObjectList<T>* objects_; //!< The ObjectList, containing all objects of type T 201 251 bool bSetName_; //!< True if the name is set 252 std::map<std::string, XMLPortClassParamContainer<T>*> xmlportParamContainers_; 253 std::map<std::string, XMLPortClassObjectContainer<T, class O>*> xmlportObjectContainers_; 202 254 }; 203 255 … … 208 260 ClassIdentifier<T>::ClassIdentifier() 209 261 { 210 this->objects_ = ObjectList<T>::getList(); 262 // this->objects_ = ObjectList<T>::getList(); 263 this->objects_ = new ObjectList<T>(); 211 264 this->bSetName_ = false; 212 265 } … … 214 267 /** 215 268 @brief Registers a class, which means that the name and the parents get stored. 216 @param parents A n IdentifierList, containing the Identifiers of all parents of the class269 @param parents A list, containing the Identifiers of all parents of the class 217 270 @param name A string, containing exactly the name of the class 218 271 @param bRootClass True if the class is either an Interface or the BaseObject itself … … 220 273 */ 221 274 template <class T> 222 ClassIdentifier<T>* ClassIdentifier<T>::registerClass( const IdentifierList* parents, const std::string& name, bool bRootClass)223 { 224 COUT( 4) << "***Register Class in " << name << "-Singleton." << std::endl;275 ClassIdentifier<T>* ClassIdentifier<T>::registerClass(std::list<const Identifier*>* parents, const std::string& name, bool bRootClass) 276 { 277 COUT(5) << "*** ClassIdentifier: Register Class in " << name << "-Singleton." << std::endl; 225 278 226 279 // Check if at least one object of the given type was created 227 if (! getIdentifier()->bCreatedOneObject_)280 if (!this->bCreatedOneObject_) 228 281 { 229 282 // If no: We have to store the informations and initialize the Identifier 230 getIdentifier()->setName(name);231 232 COUT(4) << "*** Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl;283 this->setName(name); 284 285 COUT(4) << "*** ClassIdentifier: Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl; 233 286 if (bRootClass) 234 getIdentifier()->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.287 this->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case. 235 288 else 236 getIdentifier()->initialize(parents);289 this->initialize(parents); 237 290 } 238 291 239 return getIdentifier(); 240 } 241 242 /** 243 @brief Creates the only instance of this class for the template class T. 244 @return The Identifier itself 245 */ 246 template <class T> 247 ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() 248 { 249 static ClassIdentifier<T> theOneAndOnlyInstance = ClassIdentifier<T>(); 250 static bool bIdentifierCreated = false; 251 252 if (!bIdentifierCreated) 253 { 254 COUT(4) << "*** Create Identifier Singleton." << std::endl; 255 bIdentifierCreated = true; 256 } 257 258 return &theOneAndOnlyInstance; 292 return this; 259 293 } 260 294 … … 266 300 void ClassIdentifier<T>::setName(const std::string& name) 267 301 { 268 // Make sure we didn't already set the name, to avoid duplicate entries in the Identifier map269 302 if (!this->bSetName_) 270 303 { 271 304 this->name_ = name; 272 this->getIdentifierMap().insert(std::pair<std::string, Identifier*>(name, this));273 305 this->bSetName_ = true; 274 306 } … … 282 314 void ClassIdentifier<T>::addObject(T* object) 283 315 { 284 COUT( 4) << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list." << std::endl;285 object->getMetaList().add( ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object));316 COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl; 317 object->getMetaList().add(this->objects_, this->objects_->add(object)); 286 318 } 287 319 … … 292 324 void ClassIdentifier<T>::removeObjects() const 293 325 { 294 for (Iterator<T> it = ObjectList<T>::start(); it;)326 for (Iterator<T> it = this->objects_->start(); it;) 295 327 delete *(it++); 296 328 } 329 330 template <class T> 331 XMLPortParamContainer* ClassIdentifier<T>::getXMLPortParamContainer(const std::string& paramname) 332 { 333 typename std::map<std::string, XMLPortClassParamContainer<T>*>::const_iterator it = xmlportParamContainers_.find(paramname); 334 if (it != xmlportParamContainers_.end()) 335 return (XMLPortParamContainer*)((*it).second); 336 else 337 return 0; 338 } 339 340 template <class T> 341 void ClassIdentifier<T>::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) 342 { 343 this->xmlportParamContainers_[paramname] = (XMLPortClassParamContainer<T>*)container; 344 } 345 346 template <class T> 347 XMLPortObjectContainer* ClassIdentifier<T>::getXMLPortObjectContainer(const std::string& sectionname) 348 { 349 typename std::map<std::string, XMLPortClassObjectContainer<T, class O>*>::const_iterator it = xmlportObjectContainers_.find(sectionname); 350 if (it != xmlportObjectContainers_.end()) 351 return (XMLPortObjectContainer*)((*it).second); 352 else 353 return 0; 354 } 355 356 template <class T> 357 void ClassIdentifier<T>::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) 358 { 359 this->xmlportObjectContainers_[sectionname] = (XMLPortClassObjectContainer<T, class O>*)container; 360 } 361 297 362 298 363 // ############################### … … 314 379 SubclassIdentifier() 315 380 { 316 this->identifier_ = ClassIdentifier<T>::getIdentifier(); 381 this->identifier_ = ClassManager<T>::getIdentifier(); 382 } 383 384 /** 385 @brief Copyconstructor: Assigns the given Identifier. 386 @param identifier The Identifier 387 */ 388 SubclassIdentifier(Identifier* identifier) 389 { 390 this->identifier_ = identifier; 317 391 } 318 392 … … 324 398 SubclassIdentifier<T>& operator=(Identifier* identifier) 325 399 { 326 if (!identifier->isA(Class Identifier<T>::getIdentifier()))400 if (!identifier->isA(ClassManager<T>::getIdentifier())) 327 401 { 328 COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl; 329 COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl; 402 COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl; 403 COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl; 404 COUT(1) << "Error: SubclassIdentifier<" << ClassManager<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl; 330 405 COUT(1) << "Aborting..." << std::endl; 331 406 abort(); … … 372 447 if (this->identifier_) 373 448 { 374 COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl; 449 COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl; 450 COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl; 375 451 COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl; 376 452 COUT(1) << "Aborting..." << std::endl; … … 378 454 else 379 455 { 456 COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl; 380 457 COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl; 381 458 COUT(1) << "Aborting..." << std::endl; … … 386 463 } 387 464 388 /** @ returns the assigned identifier.*/465 /** @brief Returns the assigned identifier. @return The identifier */ 389 466 inline const Identifier* getIdentifier() const 390 467 { return this->identifier_; } 391 468 392 /** @ returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */469 /** @brief Returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */ 393 470 inline bool isA(const Identifier* identifier) const 394 471 { return this->identifier_->isA(identifier); } 395 472 396 /** @ returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */397 inline bool is DirectlyA(const Identifier* identifier) const398 { return this->identifier_->is DirectlyA(identifier); }399 400 /** @ returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */473 /** @brief Returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */ 474 inline bool isExactlyA(const Identifier* identifier) const 475 { return this->identifier_->isExactlyA(identifier); } 476 477 /** @brief Returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */ 401 478 inline bool isChildOf(const Identifier* identifier) const 402 479 { return this->identifier_->isChildOf(identifier); } 403 480 404 /** @returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */ 481 /** @brief Returns true, if the assigned identifier is a direct child of the given identifier. @param identifier The identifier to compare with */ 482 inline bool isDirectChildOf(const Identifier* identifier) const 483 { return this->identifier_->isDirectChildOf(identifier); } 484 485 /** @brief Returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */ 405 486 inline bool isParentOf(const Identifier* identifier) const 406 487 { return this->identifier_->isParentOf(identifier); } 407 488 489 /** @brief Returns true, if the assigned identifier is a direct parent of the given identifier. @param identifier The identifier to compare with */ 490 inline bool isDirectParentOf(const Identifier* identifier) const 491 { return this->identifier_->isDirectParentOf(identifier); } 492 408 493 private: 409 Identifier* identifier_; //!< The assigned identifier494 Identifier* identifier_; //!< The assigned identifier 410 495 }; 411 496 }
Note: See TracChangeset
for help on using the changeset viewer.