Changeset 9667 for code/trunk/src/libraries/core/class
- Timestamp:
- Aug 25, 2013, 9:08:42 PM (11 years ago)
- Location:
- code/trunk
- Files:
-
- 12 edited
- 3 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core6 merged: 9552-9554,9556-9574,9577-9579,9585-9593,9596-9612,9626-9662
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/class/CMakeLists.txt
r9577 r9667 3 3 Identifier.cc 4 4 IdentifierManager.cc 5 OrxonoxClass.cc 6 OrxonoxInterface.cc 5 7 ) -
code/trunk/src/libraries/core/class/Identifiable.cc
r9574 r9667 35 35 36 36 #include <cassert> 37 #include "core/CoreIncludes.h" 37 38 #include "core/object/Context.h" 38 39 #include "Identifier.h" … … 40 41 namespace orxonox 41 42 { 43 RegisterClassNoArgs(Identifiable); 44 42 45 /** 43 46 @brief Constructor: Sets the default values. … … 46 49 { 47 50 this->identifier_ = 0; 48 this->parents_ = 0; 49 // Optimisation 50 this->objectPointers_.reserve(6); 51 } 51 this->objectPointers_.reserve(6); // Optimisation 52 52 53 /** 54 @brief Destructor: Removes the object from the object-lists 55 */ 56 Identifiable::~Identifiable() 57 { 58 // parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class 59 if (this->parents_) 60 delete this->parents_; 53 RegisterObject(Identifiable); 61 54 } 62 55 -
code/trunk/src/libraries/core/class/Identifiable.h
r9574 r9667 55 55 public: 56 56 Identifiable(); 57 virtual ~Identifiable() ;57 virtual ~Identifiable() {} 58 58 59 59 /// Returns the Identifier of the object. … … 119 119 private: 120 120 Identifier* identifier_; //!< The Identifier of the object 121 std::set<const Identifier*>* parents_; //!< List of all parents of the object122 121 123 122 /// 'Fast map' that holds this-pointers of all derived types -
code/trunk/src/libraries/core/class/Identifier.cc
r9593 r9667 37 37 38 38 #include "util/StringUtils.h" 39 #include "core/CoreIncludes.h" 39 40 #include "core/config/ConfigValueContainer.h" 40 41 #include "core/XMLPort.h" … … 50 51 */ 51 52 Identifier::Identifier() 52 : classID_(IdentifierManager::classIDCounter_s++) 53 { 54 this->objects_ = new ObjectListBase(); 55 56 this->bCreatedOneObject_ = false; 57 this->bSetName_ = false; 53 : classID_(IdentifierManager::getInstance().getUniqueClassId()) 54 { 58 55 this->factory_ = 0; 56 this->bInitialized_ = false; 59 57 this->bLoadable_ = false; 60 58 … … 70 68 Identifier::~Identifier() 71 69 { 72 delete this->objects_;73 74 70 if (this->factory_) 75 71 delete this->factory_; … … 84 80 85 81 /** 86 @brief Registers a class, which means that the name and the parents get stored.87 @param parents A list, containing the Identifiers of all parents of the class88 @param bRootClass True if the class is either an Interface or the BaseObject itself89 */90 void Identifier::initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass)91 {92 // Check if at least one object of the given type was created93 if (!this->bCreatedOneObject_ && IdentifierManager::isCreatingHierarchy())94 {95 // If no: We have to store the information and initialize the Identifier96 orxout(verbose, context::identifier) << "Register Class in ClassIdentifier<" << this->getName() << ">-Singleton -> Initialize Singleton." << endl;97 if (bRootClass)98 this->initialize(0); // 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.99 else100 this->initialize(parents);101 }102 }103 104 /**105 @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.106 @param parents A list containing all parents107 */108 void Identifier::initialize(std::set<const Identifier*>* parents)109 {110 orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << this->name_ << ">-Singleton." << endl;111 this->bCreatedOneObject_ = true;112 113 if (parents)114 {115 this->parents_ = (*parents);116 this->directParents_ = (*parents);117 118 // Iterate through all parents119 for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)120 {121 // Tell the parent we're one of it's children122 (*it)->children_.insert((*it)->children_.end(), this);123 124 // Erase all parents of our parent from our direct-parent-list125 for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)126 {127 // Search for the parent's parent in our direct-parent-list128 for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)129 {130 if ((*it1) == (*it2))131 {132 // We've found a non-direct parent in our list: Erase it133 this->directParents_.erase(it2);134 break;135 }136 }137 }138 }139 140 // Now iterate through all direct parents141 for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)142 {143 // Tell the parent we're one of it's direct children144 (*it)->directChildren_.insert((*it)->directChildren_.end(), this);145 146 // Create the super-function dependencies147 (*it)->createSuperFunctionCaller();148 }149 }150 }151 152 /**153 82 @brief Sets the name of the class. 154 83 */ 155 84 void Identifier::setName(const std::string& name) 156 85 { 157 if ( !this->bSetName_)86 if (name != this->name_) 158 87 { 159 88 this->name_ = name; 160 this->bSetName_ = true; 161 IdentifierManager::getStringIdentifierMapIntern()[name] = this; 162 IdentifierManager::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this; 163 IdentifierManager::getIDIdentifierMapIntern()[this->networkID_] = this; 164 } 165 } 89 IdentifierManager::getInstance().addIdentifierToLookupMaps(this); 90 } 91 } 92 93 void Identifier::setFactory(Factory* factory) 94 { 95 if (this->factory_) 96 delete this->factory_; 97 98 this->factory_ = factory; 99 } 100 166 101 167 102 /** … … 169 104 @return The new object 170 105 */ 171 OrxonoxClass* Identifier::fabricate(BaseObject* creator)106 Identifiable* Identifier::fabricate(Context* context) 172 107 { 173 108 if (this->factory_) 174 109 { 175 return this->factory_->fabricate(c reator);110 return this->factory_->fabricate(context); 176 111 } 177 112 else … … 190 125 void Identifier::setNetworkID(uint32_t id) 191 126 { 192 // Identifier::getIDIdentifierMapIntern().erase(this->networkID_);193 IdentifierManager::getIDIdentifierMapIntern()[id] = this;194 127 this->networkID_ = id; 128 IdentifierManager::getInstance().addIdentifierToLookupMaps(this); 129 } 130 131 /** 132 * @brief Used to define the direct parents of an Identifier of an abstract class. 133 */ 134 Identifier& Identifier::inheritsFrom(Identifier* directParent) 135 { 136 if (this->parents_.empty()) 137 this->directParents_.insert(directParent); 138 else 139 orxout(internal_error) << "Trying to add " << directParent->getName() << " as a direct parent of " << this->getName() << " after the latter was already initialized" << endl; 140 141 return *this; 142 } 143 144 /** 145 * @brief Initializes the parents of this Identifier while creating the class hierarchy. 146 * @param identifiers All identifiers that were used to create an instance of this class (including this identifier itself) 147 */ 148 void Identifier::initializeParents(const std::set<const Identifier*>& identifiers) 149 { 150 if (!IdentifierManager::getInstance().isCreatingHierarchy()) 151 { 152 orxout(internal_warning) << "Identifier::initializeParents() created outside of class hierarchy creation" << endl; 153 return; 154 } 155 156 for (std::set<const Identifier*>::const_iterator it = identifiers.begin(); it != identifiers.end(); ++it) 157 if (*it != this) 158 this->parents_.insert(*it); 159 } 160 161 /** 162 * @brief Initializes the direct parents of this Identifier while creating the class hierarchy. This is only intended for abstract classes. 163 */ 164 void Identifier::initializeDirectParentsOfAbstractClass() 165 { 166 if (!IdentifierManager::getInstance().isCreatingHierarchy()) 167 { 168 orxout(internal_warning) << "Identifier::initializeDirectParentsOfAbstractClass() created outside of class hierarchy creation" << endl; 169 return; 170 } 171 172 // only Identifiable is allowed to have no parents (even tough it's currently not abstract) 173 if (this->directParents_.empty() && !this->isExactlyA(Class(Identifiable))) 174 { 175 orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl; 176 orxout(internal_error) << " If this class is not abstract, use RegisterClass(ThisClass);" << endl; 177 orxout(internal_error) << " If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl; 178 } 179 } 180 181 /** 182 * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly. 183 */ 184 void Identifier::finishInitialization() 185 { 186 if (!IdentifierManager::getInstance().isCreatingHierarchy()) 187 { 188 orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl; 189 return; 190 } 191 192 if (this->isInitialized()) 193 return; 194 195 // if no direct parents were defined, initialize them with the set of all parents 196 if (this->directParents_.empty()) 197 this->directParents_ = this->parents_; 198 199 // initialize all parents before continuing to initialize this identifier 200 for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) 201 { 202 Identifier* directParent = const_cast<Identifier*>(*it); 203 directParent->finishInitialization(); // initialize parent 204 this->parents_.insert(directParent); // direct parent is also a parent 205 this->parents_.insert(directParent->parents_.begin(), directParent->parents_.end()); // parents of direct parent are also parents 206 } 207 208 // parents of parents are no direct parents of this identifier 209 for (std::set<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent) 210 for (std::set<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent) 211 this->directParents_.erase(*it_parent_parent); 212 213 // tell all parents that this identifier is a child 214 for (std::set<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it) 215 const_cast<Identifier*>(*it)->children_.insert(this); 216 217 // tell all direct parents that this identifier is a direct child 218 for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) 219 { 220 const_cast<Identifier*>(*it)->directChildren_.insert(this); 221 222 // Create the super-function dependencies 223 (*it)->createSuperFunctionCaller(); 224 } 225 226 this->bInitialized_ = true; 195 227 } 196 228 -
code/trunk/src/libraries/core/class/Identifier.h
r9593 r9667 56 56 object->getIdentifier()->getName(); // returns "MyClass" 57 57 58 OrxonoxClass* other = object->getIdentifier()->fabricate(0); // fabricates a new instance of MyClass 59 60 61 // iterate through all objects of type MyClass: 62 ObjectListBase* objects = object->getIdentifier()->getObjects(); // get a pointer to the object-list 63 int count; 64 for (Iterator<MyClass> it = objects.begin(); it != objects.end(); ++it) // iterate through the objects 65 ++count; 66 orxout() << count << endl; // prints "2" because we created 2 instances of MyClass so far 58 Identifiable* other = object->getIdentifier()->fabricate(0); // fabricates a new instance of MyClass 67 59 68 60 … … 90 82 91 83 #include "util/Output.h" 92 #include "core/object/MetaObjectList.h"93 84 #include "core/object/ObjectList.h" 94 #include "core/object/ObjectListBase.h" 85 #include "core/object/Listable.h" 86 #include "core/object/Context.h" 87 #include "core/object/Destroyable.h" 88 #include "core/object/WeakPtr.h" 95 89 #include "IdentifierManager.h" 96 90 #include "Super.h" … … 112 106 @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>. 113 107 */ 114 class _CoreExport Identifier 115 { 116 friend class IdentifierManager; 117 108 class _CoreExport Identifier : public Destroyable 109 { 118 110 public: 111 Identifier(); 112 Identifier(const Identifier& identifier); // don't copy 113 virtual ~Identifier(); 114 119 115 /// Returns the name of the class the Identifier belongs to. 120 116 inline const std::string& getName() const { return this->name_; } 121 117 void setName(const std::string& name); 122 118 119 /// Returns the name of the class as it is returned by typeid(T).name() 120 virtual const std::string& getTypeidName() = 0; 121 123 122 /// Returns the network ID to identify a class through the network. 124 123 inline uint32_t getNetworkID() const { return this->networkID_; } … … 128 127 ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; } 129 128 130 /// Returns the list of all existing objects of this class.131 inline ObjectListBase* getObjects() const { return this->objects_; }132 133 129 /// Sets the Factory. 134 inline void addFactory(Factory* factory) { this->factory_ = factory; }130 void setFactory(Factory* factory); 135 131 /// Returns true if the Identifier has a Factory. 136 132 inline bool hasFactory() const { return (this->factory_ != 0); } 137 133 138 OrxonoxClass* fabricate(BaseObject* creator);134 Identifiable* fabricate(Context* context); 139 135 140 136 /// Returns true if the class can be loaded through XML. … … 142 138 /// Set the class to be loadable through XML or not. 143 139 inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; } 140 141 /// Returns true if the Identifier was completely initialized. 142 inline bool isInitialized() const { return this->bInitialized_; } 143 144 145 ///////////////////////////// 146 ////// Class Hierarchy ////// 147 ///////////////////////////// 148 Identifier& inheritsFrom(Identifier* directParent); 149 150 void initializeParents(const std::set<const Identifier*>& identifiers); 151 void initializeDirectParentsOfAbstractClass(); 152 void finishInitialization(); 144 153 145 154 bool isA(const Identifier* identifier) const; … … 150 159 bool isDirectParentOf(const Identifier* identifier) const; 151 160 152 153 /////////////////////////////154 ////// Class Hierarchy //////155 /////////////////////////////156 161 /// Returns the parents of the class the Identifier belongs to. 157 162 inline const std::set<const Identifier*>& getParents() const { return this->parents_; } … … 220 225 221 226 protected: 222 Identifier();223 Identifier(const Identifier& identifier); // don't copy224 virtual ~Identifier();225 226 227 virtual void createSuperFunctionCaller() const = 0; 227 228 228 void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);229 230 /// Returns the children of the class the Identifier belongs to.231 inline std::set<const Identifier*>& getChildrenIntern() const { return this->children_; }232 /// Returns the direct children of the class the Identifier belongs to.233 inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; }234 235 ObjectListBase* objects_; //!< The list of all objects of this class236 237 229 private: 238 void initialize(std::set<const Identifier*>* parents);239 240 230 std::set<const Identifier*> parents_; //!< The parents of the class the Identifier belongs to 241 mutable std::set<const Identifier*> children_;//!< The children of the class the Identifier belongs to231 std::set<const Identifier*> children_; //!< The children of the class the Identifier belongs to 242 232 243 233 std::set<const Identifier*> directParents_; //!< The direct parents of the class the Identifier belongs to 244 mutable std::set<const Identifier*> directChildren_; //!< The direct children of the class the Identifier belongs to 245 246 bool bCreatedOneObject_; //!< True if at least one object of the given type was created (used to determine the need of storing the parents) 247 bool bSetName_; //!< True if the name is set 234 std::set<const Identifier*> directChildren_; //!< The direct children of the class the Identifier belongs to 235 236 bool bInitialized_; //!< Is true if the Identifier was completely initialized 248 237 bool bLoadable_; //!< False = it's not permitted to load the object through XML 249 238 std::string name_; //!< The name of the class the Identifier belongs to … … 287 276 static ClassIdentifier<T>* getIdentifier(const std::string& name); 288 277 289 bool initiali seObject(T* object, const std::string& className, bool bRootClass);278 bool initializeObject(T* object); 290 279 291 280 void setConfigValues(T* object, Configurable*) const; … … 295 284 void addObjectToList(T* object, Identifiable*); 296 285 297 void updateConfigValues(bool updateChildren = true) const; 286 virtual void updateConfigValues(bool updateChildren = true) const; 287 288 virtual const std::string& getTypeidName() 289 { return this->typeidName_; } 298 290 299 291 private: 300 static void initialiseIdentifier(); 292 static void initializeIdentifier(); 293 301 294 ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy 302 295 ClassIdentifier() 303 296 { 297 this->typeidName_ = typeid(T).name(); 304 298 SuperFunctionInitialization<0, T>::initialize(this); 305 299 } … … 309 303 } 310 304 311 static ClassIdentifier<T>* classIdentifier_s; 305 void updateConfigValues(bool updateChildren, Listable*) const; 306 void updateConfigValues(bool updateChildren, Identifiable*) const; 307 308 std::string typeidName_; 309 static WeakPtr<ClassIdentifier<T> > classIdentifier_s; 312 310 }; 313 311 314 312 template <class T> 315 ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0;313 WeakPtr<ClassIdentifier<T> > ClassIdentifier<T>::classIdentifier_s; 316 314 317 315 /** … … 324 322 // check if the Identifier already exists 325 323 if (!ClassIdentifier<T>::classIdentifier_s) 326 ClassIdentifier<T>::initiali seIdentifier();324 ClassIdentifier<T>::initializeIdentifier(); 327 325 328 326 return ClassIdentifier<T>::classIdentifier_s; … … 346 344 */ 347 345 template <class T> 348 void ClassIdentifier<T>::initialiseIdentifier() 349 { 350 // Get the name of the class 351 std::string name = typeid(T).name(); 352 353 // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used. 346 /*static */ void ClassIdentifier<T>::initializeIdentifier() 347 { 348 // create a new identifier anyway. Will be deleted if not used. 354 349 ClassIdentifier<T>* proposal = new ClassIdentifier<T>(); 355 350 356 351 // Get the entry from the map 357 ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getI dentifierSingleton(name,proposal);352 ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getInstance().getGloballyUniqueIdentifier(proposal); 358 353 359 354 if (ClassIdentifier<T>::classIdentifier_s == proposal) 360 { 361 orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was not yet existing and got created." << endl; 362 } 355 orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was not yet existing and got created." << endl; 363 356 else 364 357 { 365 orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was already existing and got assigned." << endl; 358 orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was already existing and got assigned." << endl; 359 delete proposal; // delete proposal (it is not used anymore) 366 360 } 367 361 } … … 370 364 @brief Adds an object of the given type to the ObjectList. 371 365 @param object The object to add 372 @param className The name of the class T 373 @param bRootClass True if this is a root class (i.e. it inherits directly from OrxonoxClass) 374 */ 375 template <class T> 376 bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass) 377 { 378 if (bRootClass) 379 orxout(verbose, context::object_list) << "Register Root-Object: " << className << endl; 380 else 381 orxout(verbose, context::object_list) << "Register Object: " << className << endl; 366 */ 367 template <class T> 368 bool ClassIdentifier<T>::initializeObject(T* object) 369 { 370 orxout(verbose, context::object_list) << "Register Object: " << this->getName() << endl; 382 371 383 372 object->identifier_ = this; 384 if (IdentifierManager:: isCreatingHierarchy())373 if (IdentifierManager::getInstance().isCreatingHierarchy()) 385 374 { 386 if (bRootClass && !object->parents_) 387 object->parents_ = new std::set<const Identifier*>(); 388 389 if (object->parents_) 390 { 391 this->initializeClassHierarchy(object->parents_, bRootClass); 392 object->parents_->insert(object->parents_->end(), this); 393 } 375 IdentifierManager::getInstance().createdObject(object); 394 376 395 377 this->setConfigValues(object, object); … … 425 407 */ 426 408 template <class T> 427 void ClassIdentifier<T>::addObjectToList(T* object, Listable* )428 { 429 orxout(verbose, context::object_list) << "Added object to " << this->getName() << "-list." << endl;430 object->metaList_->add(this->objects_, this->objects_->add(object));409 void ClassIdentifier<T>::addObjectToList(T* object, Listable* listable) 410 { 411 if (listable->getContext()) 412 listable->getContext()->addObject(object); 431 413 } 432 414 … … 442 424 template <class T> 443 425 void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const 426 { 427 this->updateConfigValues(updateChildren, static_cast<T*>(NULL)); 428 } 429 430 template <class T> 431 void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Listable*) const 444 432 { 445 433 if (!this->hasConfigValues()) … … 452 440 for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it) 453 441 (*it)->updateConfigValues(false); 442 } 443 444 template <class T> 445 void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Identifiable*) const 446 { 447 // no action 454 448 } 455 449 -
code/trunk/src/libraries/core/class/IdentifierManager.cc
r9564 r9667 37 37 38 38 #include "util/StringUtils.h" 39 #include "core/CoreIncludes.h" 39 40 #include "core/config/ConfigValueContainer.h" 40 41 #include "core/XMLPort.h" … … 43 44 namespace orxonox 44 45 { 45 int IdentifierManager::hierarchyCreatingCounter_s = 0;46 unsigned int IdentifierManager::classIDCounter_s = 0;47 48 /**49 @brief Returns the identifier map with the names as received by typeid(). This is only used internally.50 */ 51 std::map<std::string, Identifier*>& IdentifierManager::getTypeIDIdentifierMap()52 { 53 static std::map<std::string, Identifier*> identifiers; //!< The map to store all Identifiers.54 return identifiers;46 /* static */ IdentifierManager& IdentifierManager::getInstance() 47 { 48 static IdentifierManager instance; 49 return instance; 50 } 51 52 IdentifierManager::IdentifierManager() 53 { 54 this->hierarchyCreatingCounter_s = 0; 55 this->classIDCounter_s = 0; 55 56 } 56 57 57 58 /** 58 59 @brief Returns an identifier by name and adds it if not available 59 @param name The name of the identifier as typeid().name() suggests60 60 @param proposal A pointer to a newly created identifier for the case of non existence in the map 61 61 @return The identifier (unique instance) 62 62 */ 63 Identifier* IdentifierManager::get IdentifierSingleton(const std::string& name,Identifier* proposal)64 { 65 std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);66 67 if (it != getTypeIDIdentifierMap().end()) 68 {69 // There is already an entry: return it and delete the proposal70 delete proposal;63 Identifier* IdentifierManager::getGloballyUniqueIdentifier(Identifier* proposal) 64 { 65 const std::string& typeidName = proposal->getTypeidName(); 66 std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName); 67 68 if (it != this->identifierByTypeidName_.end()) 69 { 70 // There is already an entry: return it 71 71 return it->second; 72 72 } … … 74 74 { 75 75 // There is no entry: put the proposal into the map and return it 76 getTypeIDIdentifierMap()[name] = proposal;76 this->identifierByTypeidName_[typeidName] = proposal; 77 77 return proposal; 78 78 } … … 80 80 81 81 /** 82 * Registers the identifier in all maps of the IdentifierManager. 83 */ 84 void IdentifierManager::addIdentifierToLookupMaps(Identifier* identifier) 85 { 86 const std::string& typeidName = identifier->getTypeidName(); 87 if (this->identifierByTypeidName_.find(typeidName) != this->identifierByTypeidName_.end()) 88 { 89 this->identifierByString_[identifier->getName()] = identifier; 90 this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier; 91 this->identifierByNetworkId_[identifier->getNetworkID()] = identifier; 92 } 93 else 94 orxout(internal_warning) << "Trying to add an identifier to lookup maps which is not known to IdentifierManager" << endl; 95 } 96 97 /** 82 98 @brief Creates the class-hierarchy by creating and destroying one object of each type. 83 99 */ … … 85 101 { 86 102 orxout(internal_status) << "Create class-hierarchy" << endl; 87 IdentifierManager::startCreatingHierarchy(); 88 for (std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getStringIdentifierMap().begin(); it != IdentifierManager::getStringIdentifierMap().end(); ++it) 89 { 90 // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. 91 if (it->second->hasFactory()) 103 this->startCreatingHierarchy(); 104 105 std::set<Identifier*> initializedIdentifiers; 106 107 // ensure root context exists before starting to create objects. if the root context is dynamically created while creating the class hierarchy, we 108 // would mistakenly assume the class of the currently created object inherits from Context 109 Context::getRootContext(); 110 111 // iterate over all identifiers, create one instance of each class and initialize the identifiers 112 { 113 Context temporaryContext(NULL); 114 for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it) 92 115 { 93 OrxonoxClass* temp = it->second->fabricate(0); 94 temp->destroy(); 116 orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << it->second->getName() << ">-Singleton." << endl; 117 // To initialize the identifier, we create a new object and delete it afterwards. 118 if (it->second->hasFactory()) 119 { 120 this->identifiersOfNewObject_.clear(); 121 Identifiable* temp = it->second->fabricate(&temporaryContext); 122 if (temp->getIdentifier() != it->second) 123 orxout(internal_error) << "Newly created object of type " << it->second->getName() << " has unexpected identifier. Did you forget to use RegisterObject(classname)?" << endl; 124 delete temp; 125 126 it->second->initializeParents(this->identifiersOfNewObject_); 127 } 128 else 129 it->second->initializeDirectParentsOfAbstractClass(); 130 131 initializedIdentifiers.insert(it->second); 95 132 } 96 } 97 IdentifierManager::stopCreatingHierarchy(); 133 134 size_t numberOfObjects = temporaryContext.getObjectList<Listable>()->size(); 135 if (numberOfObjects > 0) 136 orxout(internal_warning) << "There are still " << numberOfObjects << " listables left after creating the class hierarchy" << endl; 137 } 138 139 // finish the initialization of all identifiers 140 for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it) 141 { 142 if (initializedIdentifiers.find(it->second) != initializedIdentifiers.end()) 143 it->second->finishInitialization(); 144 else 145 orxout(internal_error) << "Identifier was registered late and is not initialized: " << it->second->getName() << " / " << it->second->getTypeidName() << endl; 146 } 147 148 this->stopCreatingHierarchy(); 98 149 orxout(internal_status) << "Finished class-hierarchy creation" << endl; 99 150 } … … 104 155 void IdentifierManager::destroyAllIdentifiers() 105 156 { 106 for (std::map<std::string, Identifier*>::iterator it = IdentifierManager::getTypeIDIdentifierMap().begin(); it != IdentifierManager::getTypeIDIdentifierMap().end(); ++it)157 for (std::map<std::string, Identifier*>::iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it) 107 158 delete (it->second); 108 } 109 110 /** 111 @brief Returns the map that stores all Identifiers with their names. 112 @return The map 113 */ 114 std::map<std::string, Identifier*>& IdentifierManager::getStringIdentifierMapIntern() 115 { 116 static std::map<std::string, Identifier*> identifierMap; 117 return identifierMap; 118 } 119 120 /** 121 @brief Returns the map that stores all Identifiers with their names in lowercase. 122 @return The map 123 */ 124 std::map<std::string, Identifier*>& IdentifierManager::getLowercaseStringIdentifierMapIntern() 125 { 126 static std::map<std::string, Identifier*> lowercaseIdentifierMap; 127 return lowercaseIdentifierMap; 128 } 129 130 /** 131 @brief Returns the map that stores all Identifiers with their network IDs. 132 @return The map 133 */ 134 std::map<uint32_t, Identifier*>& IdentifierManager::getIDIdentifierMapIntern() 135 { 136 static std::map<uint32_t, Identifier*> identifierMap; 137 return identifierMap; 159 160 this->identifierByTypeidName_.clear(); 161 this->identifierByString_.clear(); 162 this->identifierByLowercaseString_.clear(); 163 this->identifierByNetworkId_.clear(); 164 } 165 166 /** 167 * @brief Notifies the IdentifierManager about a newly created object while creating the class hierarchy. 168 */ 169 void IdentifierManager::createdObject(Identifiable* identifiable) 170 { 171 if (this->isCreatingHierarchy()) 172 this->identifiersOfNewObject_.insert(identifiable->getIdentifier()); 173 else 174 orxout(internal_warning) << "createdObject() called outside of class hierarchy creation" << endl; 138 175 } 139 176 … … 145 182 Identifier* IdentifierManager::getIdentifierByString(const std::string& name) 146 183 { 147 std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getStringIdentifierMapIntern().find(name);148 if (it != IdentifierManager::getStringIdentifierMapIntern().end())184 std::map<std::string, Identifier*>::const_iterator it = this->identifierByString_.find(name); 185 if (it != this->identifierByString_.end()) 149 186 return it->second; 150 187 else … … 159 196 Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name) 160 197 { 161 std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getLowercaseStringIdentifierMapIntern().find(name);162 if (it != IdentifierManager::getLowercaseStringIdentifierMapIntern().end())198 std::map<std::string, Identifier*>::const_iterator it = this->identifierByLowercaseString_.find(name); 199 if (it != this->identifierByLowercaseString_.end()) 163 200 return it->second; 164 201 else … … 173 210 Identifier* IdentifierManager::getIdentifierByID(const uint32_t id) 174 211 { 175 std::map<uint32_t, Identifier*>::const_iterator it = IdentifierManager::getIDIdentifierMapIntern().find(id);176 if (it != IdentifierManager::getIDIdentifierMapIntern().end())212 std::map<uint32_t, Identifier*>::const_iterator it = this->identifierByNetworkId_.find(id); 213 if (it != this->identifierByNetworkId_.end()) 177 214 return it->second; 178 215 else … … 185 222 void IdentifierManager::clearNetworkIDs() 186 223 { 187 IdentifierManager::getIDIdentifierMapIntern().clear();224 this->identifierByNetworkId_.clear(); 188 225 } 189 226 } -
code/trunk/src/libraries/core/class/IdentifierManager.h
r9564 r9667 38 38 39 39 #include <map> 40 #include <set> 40 41 #include <string> 41 42 … … 44 45 class _CoreExport IdentifierManager 45 46 { 46 friend class Identifier;47 template <class T> friend class ClassIdentifier;47 public: 48 static IdentifierManager& getInstance(); 48 49 49 public: 50 Identifier* getGloballyUniqueIdentifier(Identifier* proposal); 51 void addIdentifierToLookupMaps(Identifier* identifier); 52 53 unsigned int getUniqueClassId() 54 { return this->classIDCounter_s++; } 55 56 50 57 ///////////////////////////// 51 58 ////// Class Hierarchy ////// 52 59 ///////////////////////////// 53 static void createClassHierarchy(); 60 void createClassHierarchy(); 61 void destroyAllIdentifiers(); 62 63 void createdObject(Identifiable* identifiable); 54 64 55 65 /// Returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. 56 inline staticbool isCreatingHierarchy()66 inline bool isCreatingHierarchy() 57 67 { return (hierarchyCreatingCounter_s > 0); } 58 68 … … 61 71 ///// Identifier Map ///// 62 72 ////////////////////////// 63 static void destroyAllIdentifiers(); 73 Identifier* getIdentifierByString(const std::string& name); 74 Identifier* getIdentifierByLowercaseString(const std::string& name); 75 Identifier* getIdentifierByID(uint32_t id); 64 76 65 static Identifier* getIdentifierByString(const std::string& name); 66 static Identifier* getIdentifierByLowercaseString(const std::string& name); 67 static Identifier* getIdentifierByID(uint32_t id); 68 69 static void clearNetworkIDs(); 77 void clearNetworkIDs(); 70 78 71 79 /// Returns the map that stores all Identifiers with their names. 72 static inline const std::map<std::string, Identifier*>& getStringIdentifierMap() 73 { return IdentifierManager::getStringIdentifierMapIntern(); } 74 /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their names. 75 static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapBegin() 76 { return IdentifierManager::getStringIdentifierMap().begin(); } 77 /// Returns a const_iterator to the end of the map that stores all Identifiers with their names. 78 static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapEnd() 79 { return IdentifierManager::getStringIdentifierMap().end(); } 80 80 inline const std::map<std::string, Identifier*>& getIdentifierByStringMap() 81 { return this->identifierByString_; } 81 82 /// Returns the map that stores all Identifiers with their names in lowercase. 82 static inline const std::map<std::string, Identifier*>& getLowercaseStringIdentifierMap() 83 { return IdentifierManager::getLowercaseStringIdentifierMapIntern(); } 84 /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their names in lowercase. 85 static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapBegin() 86 { return IdentifierManager::getLowercaseStringIdentifierMap().begin(); } 87 /// Returns a const_iterator to the end of the map that stores all Identifiers with their names in lowercase. 88 static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapEnd() 89 { return IdentifierManager::getLowercaseStringIdentifierMap().end(); } 90 83 inline const std::map<std::string, Identifier*>& getIdentifierByLowercaseStringMap() 84 { return this->identifierByLowercaseString_; } 91 85 /// Returns the map that stores all Identifiers with their IDs. 92 static inline const std::map<uint32_t, Identifier*>& getIDIdentifierMap() 93 { return IdentifierManager::getIDIdentifierMapIntern(); } 94 /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their IDs. 95 static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapBegin() 96 { return IdentifierManager::getIDIdentifierMap().begin(); } 97 /// Returns a const_iterator to the end of the map that stores all Identifiers with their IDs. 98 static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapEnd() 99 { return IdentifierManager::getIDIdentifierMap().end(); } 100 101 protected: 102 static Identifier* getIdentifierSingleton(const std::string& name, Identifier* proposal); 103 104 /// Returns the map that stores all Identifiers with their names. 105 static std::map<std::string, Identifier*>& getStringIdentifierMapIntern(); 106 /// Returns the map that stores all Identifiers with their names in lowercase. 107 static std::map<std::string, Identifier*>& getLowercaseStringIdentifierMapIntern(); 108 /// Returns the map that stores all Identifiers with their network IDs. 109 static std::map<uint32_t, Identifier*>& getIDIdentifierMapIntern(); 86 inline const std::map<uint32_t, Identifier*>& getIdentifierByNetworkIdMap() 87 { return this->identifierByNetworkId_; } 110 88 111 89 private: 90 IdentifierManager(); 91 IdentifierManager(const IdentifierManager&); 92 ~IdentifierManager() {} 93 112 94 /// Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents. 113 inline staticvoid startCreatingHierarchy()95 inline void startCreatingHierarchy() 114 96 { hierarchyCreatingCounter_s++; } 115 97 /// Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents. 116 inline staticvoid stopCreatingHierarchy()98 inline void stopCreatingHierarchy() 117 99 { hierarchyCreatingCounter_s--; } 118 100 119 st atic std::map<std::string, Identifier*>& getTypeIDIdentifierMap();101 std::map<std::string, Identifier*> identifierByTypeidName_; //!< Map with the names as received by typeid(). This is only used internally. 120 102 121 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) 122 static unsigned int classIDCounter_s; //!< Static counter for the unique classIDs 103 std::map<std::string, Identifier*> identifierByString_; //!< Map that stores all Identifiers with their names. 104 std::map<std::string, Identifier*> identifierByLowercaseString_; //!< Map that stores all Identifiers with their names in lowercase. 105 std::map<uint32_t, Identifier*> identifierByNetworkId_; //!< Returns the map that stores all Identifiers with their network IDs. 106 107 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) 108 std::set<const Identifier*> identifiersOfNewObject_; //!< Used while creating the object hierarchy to keep track of the identifiers of a newly created object 109 unsigned int classIDCounter_s; //!< counter for the unique classIDs 123 110 }; 124 111 } -
code/trunk/src/libraries/core/class/OrxonoxClass.h
r9585 r9667 50 50 /** 51 51 @brief This is the class from which all objects of the game-logic (not the engine) are derived from. 52 53 The BaseObject and other classes are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass.54 52 */ 55 53 class _CoreExport OrxonoxClass : virtual public Configurable, virtual public Destroyable 56 54 { 55 public: 56 OrxonoxClass(); 57 57 }; 58 58 } -
code/trunk/src/libraries/core/class/OrxonoxInterface.h
r9585 r9667 50 50 class _CoreExport OrxonoxInterface : virtual public Configurable, virtual public Destroyable 51 51 { 52 public: 53 OrxonoxInterface(); 52 54 }; 53 55 } -
code/trunk/src/libraries/core/class/SubclassIdentifier.h
r9563 r9667 163 163 164 164 /// Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T. 165 T* fabricate( BaseObject* creator) const166 { 167 OrxonoxClass* newObject = this->identifier_->fabricate(creator);165 T* fabricate(Context* context) const 166 { 167 Identifiable* newObject = this->identifier_->fabricate(context); 168 168 169 169 // Check if the creation was successful -
code/trunk/src/libraries/core/class/Super.h
r9568 r9667 103 103 { \ 104 104 ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); \ 105 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildren Intern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it) \105 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildren().begin(); it != identifier->getDirectChildren().end(); ++it) \ 106 106 { \ 107 107 if (((ClassIdentifier<T>*)(*it))->bSuperFunctionCaller_##functionname##_isFallback_ && ((ClassIdentifier<T>*)(*it))->superFunctionCaller_##functionname##_) \ … … 171 171 172 172 // Iterate through all children 173 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildren Intern().begin(); it != identifier->getDirectChildrenIntern().end(); ++it)173 for (std::set<const Identifier*>::iterator it = identifier->getDirectChildren().begin(); it != identifier->getDirectChildren().end(); ++it) 174 174 { 175 175 // Check if the caller is a fallback-caller
Note: See TracChangeset
for help on using the changeset viewer.