Changeset 365 for code/branches/objecthierarchy/src/orxonox/core
- Timestamp:
- Dec 1, 2007, 4:24:56 AM (17 years ago)
- Location:
- code/branches/objecthierarchy/src/orxonox/core
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/orxonox/core/ClassFactory.h
r258 r365 1 /*! 2 @file ClassFactory.h 3 @brief Definition of the ClassFactory class 4 5 The ClassFactory is able to create new objects of a specific class. 6 */ 7 1 8 #ifndef _ClassFactory_H__ 2 9 #define _ClassFactory_H__ … … 9 16 // ### ClassFactory ### 10 17 // ############################### 18 //! The ClassFactory is able to create new objects of a specific class. 11 19 template <class T> 12 20 class ClassFactory : public BaseFactory … … 17 25 18 26 private: 19 ClassFactory() {} 20 ClassFactory(const ClassFactory& factory) {} 21 ~ClassFactory() {} 27 ClassFactory() {} // Don't create 28 ClassFactory(const ClassFactory& factory) {} // Don't copy 29 ~ClassFactory() {} // Don't delete 22 30 23 31 static T* createNewObject(); 24 32 }; 25 33 34 /** 35 @brief Adds the ClassFactory to the Identifier of the same type and creates a new object to retrieve the parents. 36 @return True, because the compiler only allows assignments before main() 37 */ 26 38 template <class T> 27 39 bool ClassFactory<T>::create() 28 40 { 41 // Add the ClassFactory to the Classidentifier of type T 29 42 ClassIdentifier<T>::getIdentifier()->addFactory(new ClassFactory<T>); 30 43 44 // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. 31 45 ClassIdentifier<T>::getIdentifier()->startCreatingHierarchy(); 32 46 #if HIERARCHY_VERBOSE … … 40 54 } 41 55 56 /** 57 @brief Creates and returns a new object of class T. 58 @return The new object 59 */ 42 60 template <class T> 43 61 BaseObject* ClassFactory<T>::fabricate() … … 46 64 } 47 65 66 /** 67 @brief Creates and returns a new object of class T; this is a wrapper for the new operator. 68 @return The new object 69 */ 48 70 template <class T> 49 71 T* ClassFactory<T>::createNewObject() -
code/branches/objecthierarchy/src/orxonox/core/Factory.cc
r362 r365 1 /*! 2 @file Factory.cc 3 @brief Implementation of the Factory class. 4 */ 5 1 6 #include "Factory.h" 2 7 #include "Identifier.h" … … 4 9 namespace orxonox 5 10 { 6 Factory* Factory::pointer_s = NULL; 11 Factory* Factory::pointer_s = NULL; // Set the static member variable pointer_s to zero 7 12 13 /** 14 @returns the Identifier with a given name. 15 @param name The name of the wanted Identifier 16 */ 8 17 Identifier* Factory::getIdentifier(const std::string& name) 9 18 { … … 14 23 } 15 24 25 /** 26 @returns the Identifier with a given networkID. 27 @param id The networkID of the wanted Identifier 28 */ 16 29 Identifier* Factory::getIdentifier(const unsigned int id) 17 30 { … … 22 35 } 23 36 37 /** 38 @brief Adds a new Identifier to both maps. 39 @param name The name of the identifier 40 @param identifier The identifier to add 41 */ 24 42 void Factory::add(const std::string& name, Identifier* identifier) 25 43 { … … 31 49 } 32 50 51 /** 52 @brief Removes the entry with the old networkID and adds a new one. 53 @param identifier The identifier to change 54 @param oldID The old networkID 55 @param newID The new networkID 56 */ 33 57 void Factory::changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID) 34 58 { -
code/branches/objecthierarchy/src/orxonox/core/Factory.h
r362 r365 1 /*! 2 @file Factory.h 3 @brief Definition of the Factory and the BaseFactory class. 4 5 The Factory is a singleton, containing two maps to map either the name or the networkID 6 of a class with the corresponding Identifier. 7 8 Usage: 9 ID(classname) or ID(networkID) returns the corresponding Identifier. 10 11 12 BaseObject is the parent of ClassFactory which is defined in ClassFactory.h. 13 It can't be defined in ClassFactory.h, because of circular dependencies. 14 */ 15 1 16 #ifndef _Factory_H__ 2 17 #define _Factory_H__ … … 7 22 namespace orxonox 8 23 { 9 class BaseObject; 10 class Identifier; 24 class BaseObject; // Forward declaration 25 class Identifier; // Forward declaration 11 26 12 27 // ############################### 13 28 // ### Factory ### 14 29 // ############################### 30 //! The Factory is used to map name or networkID of a class with its Identifier. 15 31 class Factory 16 32 { … … 22 38 23 39 private: 24 Factory() {} 25 Factory(const Factory& factory) {} 26 ~Factory() {} 40 Factory() {} // don't create 41 Factory(const Factory& factory) {} // don't copy 42 ~Factory() {} // don't delete 27 43 28 static Factory* pointer_s; 29 std::map<std::string, Identifier*> identifierStringMap_; 30 std::map<unsigned int, Identifier*> identifierNetworkIDMap_; 44 static Factory* pointer_s; //!< The pointer to the singleton 45 std::map<std::string, Identifier*> identifierStringMap_; //!< The map mapping string with Identifier 46 std::map<unsigned int, Identifier*> identifierNetworkIDMap_; //!< The map mapping networkID with Identifier 31 47 }; 32 48 … … 34 50 // ### BaseFactory ### 35 51 // ############################### 52 //! Base-class of ClassFactory. Has to be defined separate because of circular dependencies. 36 53 class BaseFactory 37 54 { -
code/branches/objecthierarchy/src/orxonox/core/Identifier.cc
r362 r365 1 /*! 2 @file Identifier.cc 3 @brief Implementation of the Identifier class. 4 */ 5 1 6 #include "Identifier.h" 2 7 … … 6 11 // ### Identifier ### 7 12 // ############################### 8 int Identifier::hierarchyCreatingCounter_s = 0; 9 unsigned int Identifier::classIDcounter_s = 0; 13 int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero 14 unsigned int Identifier::classIDcounter_s = 0; // Set the static member variable classIDcounter_s to zero 10 15 16 /** 17 @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. 18 */ 11 19 Identifier::Identifier() 12 20 { … … 18 26 } 19 27 28 /** 29 @brief Destructor: Deletes the name and the IdentifierList containing the children. 30 */ 20 31 Identifier::~Identifier() 21 32 { 22 33 delete &this->name_; 23 24 34 delete this->children_; 25 35 } 26 36 37 /** 38 @brief Initializes the Identifier with an IdentifierList containing all parents of the class the Identifier belongs to. 39 @param parents The IdentifierList containing all parents 40 */ 27 41 void Identifier::initialize(const IdentifierList* parents) 28 42 { … … 38 52 { 39 53 this->parents_.add(temp1->identifier_); 40 temp1->identifier_->getChildren().add(this); 54 temp1->identifier_->getChildren().add(this); // We're a child of our parents 41 55 42 56 temp1 = temp1->next_; … … 45 59 } 46 60 61 /** 62 @brief Creates an object of the type the Identifier belongs to. 63 @return The new object 64 */ 47 65 BaseObject* Identifier::fabricate() 48 66 { 49 67 if (this->factory_) 50 68 { 51 return this->factory_->fabricate(); 69 return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type. 52 70 } 53 71 else 54 72 { 73 // Abstract classes don't have a factory and therefore can't create new objects 55 74 std::cout << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract.\n"; 56 75 std::cout << "Aborting..."; … … 59 78 } 60 79 80 /** 81 @brief Sets the networkID to a new value and changes the entry in the Factory. 82 @param id The new networkID 83 */ 61 84 void Identifier::setNetworkID(unsigned int id) 62 85 { … … 65 88 } 66 89 90 /** 91 @returns true, if the Identifier is at least of the given type. 92 @param identifier The identifier to compare with 93 */ 67 94 bool Identifier::isA(const Identifier* identifier) const 68 95 { … … 70 97 } 71 98 99 /** 100 @returns true, if the Identifier is exactly of the given type. 101 @param identifier The identifier to compare with 102 */ 72 103 bool Identifier::isDirectlyA(const Identifier* identifier) const 73 104 { … … 75 106 } 76 107 108 /** 109 @returns true, if the assigned identifier is a child of the given identifier. 110 @param identifier The identifier to compare with 111 */ 77 112 bool Identifier::isChildOf(const Identifier* identifier) const 78 113 { … … 80 115 } 81 116 117 /** 118 @returns true, if the assigned identifier is a parent of the given identifier. 119 @param identifier The identifier to compare with 120 */ 82 121 bool Identifier::isParentOf(const Identifier* identifier) const 83 122 { -
code/branches/objecthierarchy/src/orxonox/core/Identifier.h
r362 r365 1 /*! 2 @file Identifier.h 3 @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes. 4 5 The Identifier contains all needed informations about the class it belongs to: 6 - the name 7 - a list with all objects 8 - parents and childs 9 - the factory, if available 10 - the networkID that can be synchronised with the server 11 12 Every object has a pointer to the Identifier of its class. This allows the use isA(...), 13 isDirectlyA(...), isChildOf(...) and isParentOf(...). 14 15 To create the class-hierarchy, the Identifier has some intern functions and variables. 16 17 Every Identifier is in fact a ClassIdentifier, but they are derived from Identifier. 18 19 SubclassIdentifier is a separated class, acting like an Identifier, but has a given class. 20 You can only assign Identifiers of the given class or a derivative to a SubclassIdentifier. 21 */ 22 1 23 #ifndef _Identifier_H__ 2 24 #define _Identifier_H__ … … 13 35 namespace orxonox 14 36 { 15 class BaseObject; 37 class BaseObject; // Forward declaration 16 38 17 39 // ############################### 18 40 // ### Identifier ### 19 41 // ############################### 42 //! The Identifier is used to identify the class of an object and to store informations about the class. 43 /** 44 The Identifier contains all needed informations about the class it belongs to: 45 - the name 46 - a list with all objects 47 - parents and childs 48 - the factory, if available 49 - the networkID that can be synchronised with the server 50 51 Every object has a pointer to the Identifier of its class. This allows the use isA(...), 52 isDirectlyA(...), isChildOf(...) and isParentOf(...). 53 54 You can't directly create an Identifier, it's just the base-class for ClassIdentifier. 55 */ 20 56 class Identifier 21 57 { 22 58 template <class T> 23 friend class ClassIdentifier; 59 friend class ClassIdentifier; // Forward declaration 24 60 25 61 template <class T> 26 friend class SubclassIdentifier; 62 friend class SubclassIdentifier; // Forward declaration 27 63 28 64 template <class T> 29 friend class ClassFactory; 65 friend class ClassFactory; // Forward declaration 30 66 31 67 public: 68 /** @brief Sets the Factory. @param facotry The factory to assign */ 32 69 inline void addFactory(BaseFactory* factory) { this->factory_ = factory; } 70 33 71 BaseObject* fabricate(); 34 72 … … 38 76 bool isParentOf(const Identifier* identifier) const; 39 77 78 /** @returns the name of the class the Identifier belongs to. */ 40 79 inline const std::string& getName() const { return this->name_; } 80 81 /** @returns the parents of the class the Identifier belongs to. */ 41 82 inline const IdentifierList& getParents() const { return this->parents_; } 83 84 /** @returns the children of the class the Identifier belongs to. */ 42 85 inline IdentifierList& getChildren() const { return *this->children_; } 43 86 87 /** @returns true, if a branch of the class-hierarchy is getting created, causing all new objects to store their parents. */ 44 88 inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } 45 89 90 /** @returns the NetworkID to identify a class through the network. */ 46 91 inline const unsigned int getNetworkID() const { return this->classID_; } 92 47 93 void setNetworkID(unsigned int id); 48 94 49 95 private: 50 96 Identifier(); 51 Identifier(const Identifier& identifier) {} 97 Identifier(const Identifier& identifier) {} // don't copy 52 98 virtual ~Identifier(); 53 99 void initialize(const IdentifierList* parents); 54 100 101 /** 102 @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents. 103 */ 55 104 inline static void startCreatingHierarchy() 56 105 { … … 61 110 } 62 111 112 /** 113 @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents. 114 */ 63 115 inline static void stopCreatingHierarchy() 64 116 { … … 69 121 } 70 122 71 IdentifierList parents_; 72 IdentifierList* children_; 73 74 std::string name_; 75 76 BaseFactory* factory_; 77 bool bCreatedOneObject_; 78 static int hierarchyCreatingCounter_s; 79 static unsigned int classIDcounter_s; 80 unsigned int classID_; 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 81 133 }; 82 134 … … 85 137 // ### ClassIdentifier ### 86 138 // ############################### 139 //! The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have. 140 /** 141 ClassIdentifier is a Singleton, which means that only one object of a given type T exists. 142 This makes it possible to store informations about a class, sharing them with all 143 objects of that class without defining static variables in every class. 144 */ 87 145 template <class T> 88 146 class ClassIdentifier : public Identifier … … 95 153 private: 96 154 ClassIdentifier(); 97 ClassIdentifier(const ClassIdentifier<T>& identifier) {} 155 ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy 98 156 ~ClassIdentifier(); 99 157 100 static ClassIdentifier<T>* pointer_s; 101 ObjectList<T>* objects_; 158 static ClassIdentifier<T>* pointer_s; //!< A pointer to the singleton-object 159 ObjectList<T>* objects_; //!< The ObjectList, containing all objects of type T 102 160 }; 103 161 104 162 template <class T> 105 ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL; 106 163 ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL; // Set the static member variable pointer_s to zero 164 165 /** 166 @brief Constructor: Create the ObjectList. 167 */ 107 168 template <class T> 108 169 ClassIdentifier<T>::ClassIdentifier() … … 111 172 } 112 173 174 /** 175 @brief Destructor: Delete the ObjectList, set the singleton-pointer to zero. 176 */ 113 177 template <class T> 114 178 ClassIdentifier<T>::~ClassIdentifier() … … 118 182 } 119 183 184 /** 185 @brief Registers a class, which means that the name and the parents get stored. 186 @param parents An IdentifierList, containing the Identifiers of all parents of the class 187 @param name A string, containing exactly the name of the class 188 @param bRootClass True if the class is either an Interface or BaseObject itself 189 @return The ClassIdentifier itself 190 */ 120 191 template <class T> 121 192 ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass) … … 124 195 std::cout << "*** Register Class in " << name << "-Singleton.\n"; 125 196 #endif 197 198 // It's a singleton, so maybe we have to create it first 126 199 if (!pointer_s) 127 200 { … … 132 205 } 133 206 207 // Check if at least one object of the given type was created 134 208 if (!pointer_s->bCreatedOneObject_) 135 209 { 210 // If no: We have to store the informations and initialize the Identifier 211 136 212 #if HIERARCHY_VERBOSE 137 213 std::cout << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n"; 138 214 #endif 139 215 pointer_s->name_ = name; 140 Factory::add(name, pointer_s); 216 Factory::add(name, pointer_s); // Add the Identifier to the Factory 141 217 142 218 if (bRootClass) 143 pointer_s->initialize(NULL); 219 pointer_s->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. 144 220 else 145 221 pointer_s->initialize(parents); … … 149 225 } 150 226 227 /** 228 @returns the Identifier itself 229 */ 151 230 template <class T> 152 231 ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() … … 163 242 } 164 243 244 /** 245 @brief Adds an object of the given type to the ObjectList. 246 @param object The object to add 247 */ 165 248 template <class T> 166 249 void ClassIdentifier<T>::addObject(T* object) … … 169 252 std::cout << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n"; 170 253 #endif 171 object->getMetaList() ->add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object));254 object->getMetaList().add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object)); 172 255 } 173 256 … … 176 259 // ### SubclassIdentifier ### 177 260 // ############################### 178 template <class B> 261 //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites. 262 /** 263 You can only assign Identifiers that belong to a class of at least B (or derived) to a SubclassIdentifier<T>. 264 If you assign something else, the program aborts. 265 Because we know the minimal type, a dynamic_cast is done, which makes it easier to create a new object. 266 */ 267 template <class T> 179 268 class SubclassIdentifier 180 269 { 181 270 public: 182 SubclassIdentifier(); 183 184 SubclassIdentifier<B>& operator=(Identifier* identifier) 185 { 186 if (!identifier->isA(ClassIdentifier<B>::getIdentifier())) 271 /** 272 @brief Constructor: Automaticaly assigns the Identifier of the given class. 273 */ 274 SubclassIdentifier() 275 { 276 this->identifier_ = ClassIdentifier<T>::getIdentifier(); 277 } 278 279 /** 280 @brief Overloading of the = operator: assigns the identifier and checks its type. 281 @param identifier The Identifier to assign 282 @return The SubclassIdentifier itself 283 */ 284 SubclassIdentifier<T>& operator=(Identifier* identifier) 285 { 286 if (!identifier->isA(ClassIdentifier<T>::getIdentifier())) 187 287 { 188 std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier< B>::getIdentifier()->getName() << "!\n";189 std::cout << "Error: SubclassIdentifier<" << ClassIdentifier< B>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n";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"; 190 290 std::cout << "Aborting...\n"; 191 291 abort(); … … 195 295 } 196 296 297 /** 298 @brief Overloading of the * operator: returns the assigned identifier. 299 @return The assigned identifier 300 */ 197 301 Identifier* operator*() 198 302 { … … 200 304 } 201 305 306 /** 307 @brief Overloading of the -> operator: returns the assigned identifier. 308 @return The assigned identifier 309 */ 202 310 Identifier* operator->() const 203 311 { … … 205 313 } 206 314 207 B* fabricate() 315 /** 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. 317 @return The new object 318 */ 319 T* fabricate() 208 320 { 209 321 BaseObject* newObject = this->identifier_->fabricate(); 322 323 // Check if the creation worked 210 324 if (newObject) 211 325 { 212 return dynamic_cast<B*>(newObject); 326 // Do a dynamic_cast, because an object of type T is much better than of type BaseObject 327 return dynamic_cast<T*>(newObject); 213 328 } 214 329 else 215 330 { 331 // Something went terribly wrong 216 332 if (this->identifier_) 217 333 { 218 std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier< B>::getIdentifier()->getName() << "!\n";334 std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!\n"; 219 335 std::cout << "Error: Couldn't fabricate a new Object.\n"; 220 336 std::cout << "Aborting...\n"; … … 230 346 } 231 347 348 /** @returns the assigned identifier. */ 232 349 inline const Identifier* getIdentifier() const 233 350 { return this->identifier_; } 351 352 /** @returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */ 234 353 inline bool isA(const Identifier* identifier) const 235 354 { return this->identifier_->isA(identifier); } 355 356 /** @returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */ 236 357 inline bool isDirectlyA(const Identifier* identifier) const 237 358 { return this->identifier_->isDirectlyA(identifier); } 359 360 /** @returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */ 238 361 inline bool isChildOf(const Identifier* identifier) const 239 362 { return this->identifier_->isChildOf(identifier); } 363 364 /** @returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */ 240 365 inline bool isParentOf(const Identifier* identifier) const 241 366 { return this->identifier_->isParentOf(identifier); } 242 367 243 368 private: 244 Identifier* identifier_; 369 Identifier* identifier_; //!< The assigned identifier 245 370 }; 246 247 template <class B>248 SubclassIdentifier<B>::SubclassIdentifier()249 {250 this->identifier_ = ClassIdentifier<B>::getIdentifier();251 }252 371 } 253 372 -
code/branches/objecthierarchy/src/orxonox/core/IdentifierIncludes.h
r362 r365 1 /** 2 @file IDentifierIncludes.h 3 @brief Definition of macros for the class-hierarchy and the factory. 4 5 Every class needs the RegisterObject(class) macro in its constructor. If the class is an interface 6 or the BaseObject itself, it needs the macro RegisterRootObject(class) instead. 7 8 To allow the object being created through the factory, use the CreateFactory(class) macro outside 9 the of the class implementation, so it gets executed before main(). 10 */ 11 12 // All needed header-files 1 13 #include "Identifier.h" 2 14 #include "Factory.h" … … 6 18 7 19 20 // Intern macro, containing the common parts of RegisterObject and RegisterRootObject 8 21 #define InternRegisterObject(ClassName, bRootClass) \ 9 22 this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, bRootClass)); \ … … 12 25 ClassIdentifier<ClassName>::addObject(this) 13 26 27 // Intern macro, containing the specific part of RegisterRootObject 14 28 #define InternRegisterRootObject(ClassName) \ 15 29 if (Identifier::isCreatingHierarchy() && !this->getParents()) \ … … 17 31 InternRegisterObject(ClassName, true) 18 32 33 // RegisterObject - with and without debug output 19 34 #if HIERARCHY_VERBOSE 20 35 #define RegisterObject(ClassName) \ … … 26 41 #endif 27 42 43 // RegisterRootObject - with and without debug output 28 44 #if HIERARCHY_VERBOSE 29 45 #define RegisterRootObject(ClassName) \ … … 35 51 #endif 36 52 53 // Class(ClassName) returns the Identifier of the given class 37 54 #define Class(ClassName) \ 38 55 ClassIdentifier<ClassName>::getIdentifier() 39 56 57 // Creates the entry in the Factory 40 58 #define CreateFactory(ClassName) \ 41 59 bool bCreated##ClassName##Factory = ClassFactory<ClassName>::create() 42 60 43 #define ID(NameOrID) \ 44 Factory::getIdentifier(NameOrID) 61 // ID(StringOrInt) returns the Identifier with either a given name or a given NetworkID through the factory 62 #define ID(StringOrInt) \ 63 Factory::getIdentifier(StringOrInt) -
code/branches/objecthierarchy/src/orxonox/core/IdentifierList.cc
r258 r365 1 /*! 2 @file IdentifierList.cc 3 @brief Implementation of the IdentifierList class. 4 */ 5 1 6 #include "IdentifierList.h" 2 7 #include "Identifier.h" … … 7 12 // ### IdentifierList ### 8 13 // ############################### 14 /** 15 @brief Constructor: Sets first_ to zero. 16 */ 9 17 IdentifierList::IdentifierList() 10 18 { … … 12 20 } 13 21 22 /** 23 @brief Destructor: Deletes all elements in the list, but NOT THE IDENTIFIERS. 24 */ 14 25 IdentifierList::~IdentifierList() 15 26 { … … 23 34 } 24 35 36 /** 37 @brief Adds an Identifier to the list. 38 @param identifier The Identifier to add 39 */ 25 40 void IdentifierList::add(const Identifier* identifier) 26 41 { … … 30 45 } 31 46 47 /** 48 @brief Removes an Identifier from the list. 49 @param identifier The Identifier to remove 50 */ 32 51 void IdentifierList::remove(const Identifier* identifier) 33 52 { … … 35 54 return; 36 55 56 // Check if we have to delete the first element 37 57 if (this->first_->identifier_ == identifier) 38 58 { … … 44 64 } 45 65 66 // Iterate through the list 46 67 IdentifierListElement* temp = this->first_; 47 68 while (temp->next_) … … 60 81 } 61 82 83 /** 84 @brief Checks if a given Identifier is in the list and returns true if yes. 85 @param identifier The Identifier to check 86 @return True if the Identifier is in the list 87 */ 62 88 bool IdentifierList::isInList(const Identifier* identifier) const 63 89 { … … 74 100 } 75 101 102 /** 103 @returns a string, containing the names of all Identifiers in the list. 104 */ 76 105 std::string IdentifierList::toString() const 77 106 { … … 94 123 // ### IdentifierListElement ### 95 124 // ############################### 125 /** 126 @brief Constructor: Creates the list-element with a given identifier. 127 @param identifier The Identifier to store 128 */ 96 129 IdentifierListElement::IdentifierListElement(const Identifier* identifier) 97 130 { … … 99 132 this->next_ = 0; 100 133 } 101 102 IdentifierListElement::~IdentifierListElement()103 {104 }105 134 } -
code/branches/objecthierarchy/src/orxonox/core/IdentifierList.h
r258 r365 1 1 #ifndef _IdentifierList_H__ 2 /*! 3 @file IdentifierList.h 4 @brief Definition of the IdentifierList class 5 6 The IdentifierList is a single-linked list, containing Identifiers. 7 The IdentifierList is used to store parents and childs of each Identifier. 8 */ 9 2 10 #define _IdentifierList_H__ 3 11 … … 6 14 namespace orxonox 7 15 { 8 class Identifier; 16 class Identifier; // Forward declaration 9 17 18 //! The list-element of the IdentifierList 10 19 class IdentifierListElement 11 20 { 12 21 public: 13 22 IdentifierListElement(const Identifier* identifier); 14 ~IdentifierListElement();15 23 16 const Identifier* identifier_; 17 IdentifierListElement* next_; 24 const Identifier* identifier_; //!< The identifier 25 IdentifierListElement* next_; //!< The next element in the list 18 26 }; 19 27 28 //! The IdentifierList contains Identifiers 20 29 class IdentifierList 21 30 { … … 28 37 std::string toString() const; 29 38 30 IdentifierListElement* first_; 39 IdentifierListElement* first_; //!< The first element in the list 31 40 }; 32 41 } -
code/branches/objecthierarchy/src/orxonox/core/Iterator.h
r258 r365 1 /*! 2 @file Iterator.h 3 @brief Definition of the Iterator class. 4 5 The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type. 6 This is the only way to access the objects in an ObjectList. 7 8 Usage: 9 for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it) 10 { 11 it->someFunction(...); 12 class* myObject = *it; 13 } 14 15 Warning: Don't delete an object directly through the iterator. 16 */ 17 1 18 #ifndef _Iterator_H__ 2 19 #define _Iterator_H__ … … 4 21 namespace orxonox 5 22 { 23 //! The iterator allows to iterate through an ObjectList of a given class. 6 24 template <class T> 7 25 class Iterator 8 26 { 9 27 public: 28 /** 29 @brief Constructor: Sets the element whereon the iterator points to zero. 30 */ 10 31 Iterator() 11 32 { … … 13 34 } 14 35 36 /** 37 @brief Constructor: Sets the element whereon the iterator points to a given element. 38 @param element The element to start with 39 */ 15 40 Iterator(ObjectListElement<T>* element) 16 41 { … … 18 43 } 19 44 45 /** 46 @brief Overloading of the ++it operator: Iterator iterates to the next object in the list. 47 @return The Iterator itself 48 */ 20 49 Iterator<T> operator++() 21 50 { … … 24 53 } 25 54 55 /** 56 @brief Overloading of the --it operator: Iterator iterates to the previous object in the list. 57 @return The Iterator itself 58 */ 26 59 Iterator<T> operator--() 27 60 { … … 30 63 } 31 64 65 /** 66 @brief Overloading of the *it operator: returns the pointer to the object. 67 @return The object the Iterator points at 68 */ 32 69 T* operator*() 33 70 { … … 35 72 } 36 73 74 /** 75 @brief Overloading of the it-> operator: returns the pointer to the object. 76 @return The object the Iterator points at 77 */ 37 78 T* operator->() const 38 79 { … … 41 82 } 42 83 84 /** 85 @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. 86 @return True if the iterator points to an existing object. 87 */ 43 88 operator bool() 44 89 { … … 46 91 } 47 92 93 /** 94 @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool. 95 @param compare The integer (must be zero, everything else makes no sense). 96 @return True if the iterator points to an existing object. 97 */ 48 98 bool operator!=(int compare) 49 99 { 100 // Comparing with something except zero makes no sense 50 101 if (compare != 0) 51 102 std::cout << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works.\n"; … … 55 106 56 107 private: 57 ObjectListElement<T>* element_; 108 ObjectListElement<T>* element_; //!< The element the Iterator points to 58 109 }; 59 110 } -
code/branches/objecthierarchy/src/orxonox/core/MetaObjectList.cc
r258 r365 1 /*! 2 @file MetaObjectList.cc 3 @brief Implementation of the MetaObjectList class. 4 */ 5 1 6 #include "MetaObjectList.h" 2 7 3 8 namespace orxonox 4 9 { 10 /** 11 @brief Constructor: Sets first_ to zero. 12 */ 5 13 MetaObjectList::MetaObjectList() 6 14 { … … 8 16 } 9 17 18 /** 19 @brief Destructor: Removes all elements from the list, causing them to remove the stored ObjectListElement from the ObjectList. 20 */ 10 21 MetaObjectList::~MetaObjectList() 11 22 { -
code/branches/objecthierarchy/src/orxonox/core/MetaObjectList.h
r258 r365 1 /*! 2 @file MetaObjectList.h 3 @brief Definition of the MetaObjectList class. 4 5 The MetaObjectList is a single-linked list, containing all list-elements and their 6 lists wherein the object that owns the MetaObjectList is registered. 7 This allows much faster deleting of objects. 8 */ 9 1 10 #ifndef _MetaObjectList_H__ 2 11 #define _MetaObjectList_H__ … … 7 16 namespace orxonox 8 17 { 18 //! Base-class of MetaObjectListElement, because those is a template 9 19 class BaseMetaObjectListElement 10 20 { 11 21 public: 22 /** @brief Defaultdestructor */ 12 23 virtual ~BaseMetaObjectListElement() {}; 13 24 14 BaseMetaObjectListElement* next_; 25 BaseMetaObjectListElement* next_; //!< The next Element in the list 15 26 }; 16 27 … … 18 29 // ### MetaObjectListElement ### 19 30 // ############################### 31 //! The list-element of the MetaObjectList 20 32 template <class T> 21 33 class MetaObjectListElement : public BaseMetaObjectListElement … … 25 37 ~MetaObjectListElement(); 26 38 27 ObjectListElement<T>* element_; 28 ObjectList<T>* list_; 39 ObjectListElement<T>* element_; //!< The list element, containing the object 40 ObjectList<T>* list_; //!< The list, containing the element 29 41 }; 30 42 43 /** 44 @brief Constructor: Creates the list-element with given list and element. 45 */ 31 46 template <class T> 32 47 MetaObjectListElement<T>::MetaObjectListElement(ObjectList<T>* list, ObjectListElement<T>* element) … … 37 52 } 38 53 54 /** 55 @brief Destructor: Removes the ObjectListElement from the ObjectList by linking next_ and prev_ of the ObjectListElement. 56 */ 39 57 template <class T> 40 58 MetaObjectListElement<T>::~MetaObjectListElement() … … 43 61 this->element_->next_->prev_ = this->element_->prev_; 44 62 else 45 this->list_->last_ = this->element_->prev_; 63 this->list_->last_ = this->element_->prev_; // If there is no next_, we deleted the last object and have to update the last_ pointer of the list 46 64 47 65 if (this->element_->prev_) 48 66 this->element_->prev_->next_ = this->element_->next_; 49 67 else 50 this->list_->first_ = this->element_->next_; 68 this->list_->first_ = this->element_->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list 51 69 52 70 … … 61 79 // ### ObjectList ### 62 80 // ############################### 81 //! The MetaObjectList contains ObjectListElements and their ObjectLists. 82 /** 83 The MetaObjectList is a single-linked list, containing all list-elements and their 84 lists wherein the object that owns the MetaObjectList is registered. 85 This allows much faster deleting of objects. 86 */ 63 87 class MetaObjectList 64 88 { … … 69 93 void add(ObjectList<T>* list, ObjectListElement<T>* element); 70 94 71 BaseMetaObjectListElement* first_; 95 BaseMetaObjectListElement* first_; //!< The first element in the list 72 96 }; 73 97 98 /** 99 @brief Adds an ObjectList and an element of that list to the MetaObjectList. 100 @param list The ObjectList wherein the element is 101 @param element The element wherein the object is 102 */ 74 103 template <class T> 75 104 void MetaObjectList::add(ObjectList<T>* list, ObjectListElement<T>* element) -
code/branches/objecthierarchy/src/orxonox/core/ObjectList.h
r258 r365 1 /*! 2 @file ObjectList.h 3 @brief Definition of the ObjectList class. 4 5 The ObjectList is a double-linked list, used by Identifiers to store all objects of a specific class in it. 6 Created objects are added through the RegisterObject-macro in its constructor. 7 Use Iterator<class> to iterate through all objects of the class. 8 */ 9 1 10 #ifndef _ObjectList_H__ 2 11 #define _ObjectList_H__ … … 4 13 namespace orxonox 5 14 { 6 class OrxonoxClass; 15 class OrxonoxClass; // Forward declaration 7 16 8 17 // ############################### 9 18 // ### ObjectListElement ### 10 19 // ############################### 20 //! The list-element of the ObjectList 11 21 template <class T> 12 22 class ObjectListElement … … 14 24 public: 15 25 ObjectListElement(T* object); 16 ~ObjectListElement(); 17 18 T* object_; 19 ObjectListElement* next_; 20 ObjectListElement* prev_; 26 27 T* object_; //!< The object 28 ObjectListElement* next_; //!< The next element in the list 29 ObjectListElement* prev_; //!< The previous element in the list 21 30 }; 22 31 32 /** 33 @brief Constructor: Creates the list-element with an object. 34 @param Object The object to store 35 */ 23 36 template <class T> 24 37 ObjectListElement<T>::ObjectListElement(T* object) … … 29 42 } 30 43 31 template <class T>32 ObjectListElement<T>::~ObjectListElement()33 {34 }35 36 44 37 45 // ############################### … … 39 47 // ############################### 40 48 template <class T> 41 class Iterator; 42 49 class Iterator; // Forward declaration 50 51 //! The ObjectList contains all objects of a specific class. 52 /** 53 The ObjectList is used by Identifiers to store all objects of a specific class in it. 54 Use Iterator<class> to iterate through all objects in the list. 55 */ 43 56 template <class T> 44 57 class ObjectList … … 48 61 ~ObjectList(); 49 62 ObjectListElement<T>* add(T* object); 50 void remove(OrxonoxClass* object, bool bIterateForwards = true); 51 63 // void remove(OrxonoxClass* object, bool bIterateForwards = true); 64 65 /** @returns the first element in the list */ 52 66 inline static Iterator<T> start() 53 67 { return Iterator<T>(pointer_s->first_); } 68 69 /** @returns the last element in the list */ 54 70 inline static Iterator<T> end() 55 71 { return Iterator<T>(pointer_s->last_); } 56 72 57 ObjectListElement<T>* first_; 58 ObjectListElement<T>* last_; 73 ObjectListElement<T>* first_; //!< The first element in the list 74 ObjectListElement<T>* last_; //!< The last element in the list 59 75 60 76 private: 61 static ObjectList<T>* pointer_s; 77 static ObjectList<T>* pointer_s; //!< A static pointer to the last created list (different for all T) 62 78 }; 63 79 64 80 template <class T> 65 ObjectList<T>* ObjectList<T>::pointer_s = 0; 66 81 ObjectList<T>* ObjectList<T>::pointer_s = 0; // Set the static member variable pointer_s to zero 82 83 /** 84 @brief Constructor: Sets first_ and last_ to zero and the static member variable pointer_s to _this_ 85 */ 67 86 template <class T> 68 87 ObjectList<T>::ObjectList() … … 71 90 this->last_ = 0; 72 91 92 // ObjectLists are only created by Identifiers and therefore only one ObjectList of each T will exist. 93 // Thats why pointer_s is in fact a pointer to the only ObjectList for a type, which makes it almost a singleton. 73 94 this->pointer_s = this; 74 95 } 75 96 97 /** 98 @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS. 99 */ 76 100 template <class T> 77 101 ObjectList<T>::~ObjectList() … … 86 110 } 87 111 112 /** 113 @brief Adds a new object to the end of the list. 114 @param object The object to add 115 @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object 116 */ 88 117 template <class T> 89 118 ObjectListElement<T>* ObjectList<T>::add(T* object) … … 91 120 if (!this->last_) 92 121 { 122 // If the list is empty 93 123 this->last_ = new ObjectListElement<T>(object); 94 this->first_ = this->last_; 124 this->first_ = this->last_; // There's only one object in the list now 95 125 } 96 126 else 97 127 { 128 // If the list isn't empty 98 129 ObjectListElement<T>* temp = this->last_; 99 130 this->last_ = new ObjectListElement<T>(object); … … 105 136 } 106 137 138 139 // /** 140 // @brief Removes an object from the list. 141 // @param object The object to remove 142 // @param bIterateForwards If true: Start searching the object at the beginning of the list 143 // */ 144 /* 107 145 template <class T> 108 146 void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards) … … 111 149 return; 112 150 151 // If there's only one object in the list, we have to set first_ and last_ to zero 113 152 if (this->first_ == this->last_) 114 153 { … … 123 162 } 124 163 164 // Now we are sure we have more than one element in the list 125 165 if (bIterateForwards) 126 166 { 167 // Start at the beginning of the list 168 169 // Check if it's the first object 127 170 if (this->first_->object_ == object) 128 171 { … … 135 178 } 136 179 180 // Iterate through the whole list 137 181 ObjectListElement<T>* temp = this->first_; 138 182 while (temp->next_) … … 146 190 temp2->prev_ = temp; 147 191 else 148 this->last_ = temp; 192 this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer. 149 193 150 194 return; … … 156 200 else 157 201 { 202 // Start at the end of the list 203 204 // Check if it's the last object 158 205 if (this->last_->object_ == object) 159 206 { … … 166 213 } 167 214 215 // Iterate through the whole list 168 216 ObjectListElement<T>* temp = this->last_; 169 217 while (temp->prev_) … … 177 225 temp2->next_ = temp; 178 226 else 179 this->first_ = temp; 227 this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer. 180 228 181 229 return; … … 186 234 } 187 235 } 236 */ 188 237 } 189 238 -
code/branches/objecthierarchy/src/orxonox/core/OrxonoxClass.cc
r258 r365 1 /*! 2 @file OrxonoxClass.cc 3 @brief Implementation of the OrxonoxClass Class. 4 */ 5 1 6 #include "OrxonoxClass.h" 2 7 3 8 namespace orxonox 4 9 { 10 /** @brief Constructor: Sets identifier_ and parents_ to zero. */ 5 11 OrxonoxClass::OrxonoxClass() 6 12 { 7 13 this->identifier_ = 0; 8 14 this->parents_ = 0; 9 10 this->metaList_ = new MetaObjectList;11 15 } 12 16 17 /** @brief Destructor: Deletes, if existing, the list of the parents. */ 13 18 OrxonoxClass::~OrxonoxClass() 14 19 { 20 // parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class 15 21 if (this->parents_) 16 22 delete this->parents_; 17 18 delete this->metaList_;19 23 } 20 24 } -
code/branches/objecthierarchy/src/orxonox/core/OrxonoxClass.h
r258 r365 1 /*! 2 @file OrxonoxClass.h 3 @brief Definition of the OrxonoxClass Class. 4 5 All objects and interfaces of the game-logic are derived from OrxonoxClass. 6 It stores the Identifier and the MetaObjectList and has all needed functions to create the class-hierarchy. 7 */ 8 1 9 #ifndef _OrxonoxClass_H__ 2 10 #define _OrxonoxClass_H__ … … 9 17 namespace orxonox 10 18 { 19 //! The class all objects and interfaces of the game-logic are derived from. 20 /** 21 BaseObject and Interaces are derived with 'virtual public OrxonoxClass' from OrxonoxClass. 22 OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the MetaObjectList. 23 */ 11 24 class OrxonoxClass 12 25 { … … 14 27 OrxonoxClass(); 15 28 virtual ~OrxonoxClass(); 29 30 /** @returns the Identifier of the object */ 16 31 inline Identifier* getIdentifier() const { return this->identifier_; } 32 33 /** @brief Sets the Identifier of the object. Used by the RegisterObject-macro. */ 17 34 inline void setIdentifier(Identifier* identifier) { this->identifier_ = identifier; } 35 36 /** @returns the list of all parents of the object */ 18 37 inline IdentifierList* getParents() const { return this->parents_; } 38 39 /** @brief Sets the Parents of the object. Used by the RegisterObject-macro. */ 19 40 inline void setParents(IdentifierList* parents) { this->parents_ = parents; } 20 inline MetaObjectList* getMetaList() { return this->metaList_; } 41 42 /** @returns the MetaObjectList of the object, containing a link to all ObjectLists and ObjectListElements the object is registered in. */ 43 inline MetaObjectList& getMetaList() { return this->metaList_; } 21 44 22 45 private: 23 Identifier* identifier_; 24 IdentifierList* parents_; 25 MetaObjectList * metaList_;46 Identifier* identifier_; //!< The Identifier of the object 47 IdentifierList* parents_; //!< List of all parents of the object 48 MetaObjectList metaList_; //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in 26 49 }; 27 50 }
Note: See TracChangeset
for help on using the changeset viewer.