Changeset 244 for code/branches/objecthierarchie
- Timestamp:
- Nov 25, 2007, 5:21:53 PM (17 years ago)
- Location:
- code/branches/objecthierarchie/src
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchie/src/BaseObject.cc
r218 r244 7 7 BaseObject::BaseObject() 8 8 { 9 registerRootObject(BaseObject);9 RegisterRootObject(BaseObject); 10 10 } 11 11 12 12 BaseObject::~BaseObject() 13 13 { 14 unregisterObject();14 UnregisterObject(); 15 15 } 16 16 } -
code/branches/objecthierarchie/src/CMakeLists.txt
r224 r244 3 3 # create a few variables to simplify life 4 4 SET(SRC_FILES orxonox.cc IdentifierList.cc Identifier.cc Factory.cc OrxonoxClass.cc BaseObject.cc test1.cc test2.cc test3.cc) 5 SET(INC_FILES IdentifierIncludes.h Identifier.h Factory.h IdentifierList.h ObjectList.h Iterator.h OrxonoxClass.h BaseObject.h Test.h test1.h test2.h test3.h)5 SET(INC_FILES IdentifierIncludes.h Identifier.h Factory.h ClassFactory.h IdentifierList.h ObjectList.h Iterator.h OrxonoxClass.h BaseObject.h Test.h test1.h test2.h test3.h) 6 6 7 7 #Creates an executable -
code/branches/objecthierarchie/src/Factory.cc
r219 r244 5 5 namespace orxonox 6 6 { 7 ClassFactory* ClassFactory::pointer_s = NULL;7 Factory* Factory::pointer_s = NULL; 8 8 9 BaseObject* ClassFactory::fabricate(const std::string& name)9 Identifier* Factory::getIdentifier(const std::string& name) 10 10 { 11 11 if (!pointer_s) 12 pointer_s = new ClassFactory;12 pointer_s = new Factory; 13 13 14 return pointer_s->identifierMap_[name] ->fabricate();14 return pointer_s->identifierMap_[name]; 15 15 } 16 16 17 void ClassFactory::add(const std::string& name, Identifier* identifier)17 void Factory::add(const std::string& name, Identifier* identifier) 18 18 { 19 19 if (!pointer_s) 20 pointer_s = new ClassFactory;20 pointer_s = new Factory; 21 21 22 22 pointer_s->identifierMap_[name] = identifier; -
code/branches/objecthierarchie/src/Factory.h
r219 r244 7 7 namespace orxonox 8 8 { 9 class BaseObject; 9 10 class Identifier; 10 class BaseObject;11 11 12 class ClassFactory 12 // ############################### 13 // ### Factory ### 14 // ############################### 15 class Factory 13 16 { 14 17 public: 15 static BaseObject* fabricate(const std::string& name);18 static Identifier* getIdentifier(const std::string& name); 16 19 static void add(const std::string& name, Identifier* identifier); 17 20 18 21 private: 19 ClassFactory() {}20 ClassFactory(const ClassFactory& factory) {}21 ~ ClassFactory() {}22 Factory() {} 23 Factory(const Factory& factory) {} 24 ~Factory() {} 22 25 23 static ClassFactory* pointer_s;26 static Factory* pointer_s; 24 27 std::map<std::string, Identifier*> identifierMap_; 28 }; 29 30 // ############################### 31 // ### BaseFactory ### 32 // ############################### 33 class BaseFactory 34 { 35 public: 36 virtual BaseObject* fabricate() = 0; 25 37 }; 26 38 } -
code/branches/objecthierarchie/src/Identifier.cc
r243 r244 11 11 { 12 12 this->bCreatedOneObject_ = false; 13 this->factory_ = 0; 13 14 14 15 this->children_ = new IdentifierList; … … 27 28 std::cout << "*** Initialize " << this->name_ << "-Singleton.\n"; 28 29 #endif 30 this->bCreatedOneObject_ = true; 31 29 32 if (parents) 30 33 { 31 this->bCreatedOneObject_ = true;32 33 34 IdentifierListElement* temp1 = parents->first_; 34 35 while (temp1) … … 39 40 temp1 = temp1->next_; 40 41 } 42 } 43 } 44 45 BaseObject* Identifier::fabricate() 46 { 47 if (this->factory_) 48 { 49 return this->factory_->fabricate(); 50 } 51 else 52 { 53 std::cout << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract.\n"; 54 std::cout << "Aborting..."; 55 abort(); 41 56 } 42 57 } -
code/branches/objecthierarchie/src/Identifier.h
r243 r244 8 8 #include "Factory.h" 9 9 10 #define HIERARCHY_VERBOSE false10 #define HIERARCHY_VERBOSE true 11 11 12 12 … … 26 26 friend class SubclassIdentifier; 27 27 28 template <class T> 29 friend class ClassFactory; 30 28 31 public: 29 32 virtual void removeObject(OrxonoxClass* object) const = 0; 30 33 virtual void removeObjectIntern(OrxonoxClass* object, bool bIterateForwards) const = 0; 31 34 32 virtual BaseObject* fabricate() const = 0; 35 inline void addFactory(BaseFactory* factory) { this->factory_ = factory; } 36 BaseObject* fabricate(); 33 37 34 38 bool isA(const Identifier* identifier) const; … … 37 41 bool isParentOf(const Identifier* identifier) const; 38 42 39 const std::string& getName() const { return this->name_; }40 const IdentifierList& getParents() const { return this->parents_; }41 IdentifierList& getChildren() const { return *this->children_; }42 43 static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }43 inline const std::string& getName() const { return this->name_; } 44 inline const IdentifierList& getParents() const { return this->parents_; } 45 inline IdentifierList& getChildren() const { return *this->children_; } 46 47 inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } 44 48 45 49 private: … … 49 53 void initialize(const IdentifierList* parents); 50 54 51 static void startCreatingHierarchy()55 inline static void startCreatingHierarchy() 52 56 { 53 57 hierarchyCreatingCounter_s++; … … 57 61 } 58 62 59 static void stopCreatingHierarchy()63 inline static void stopCreatingHierarchy() 60 64 { 61 65 hierarchyCreatingCounter_s--; … … 70 74 std::string name_; 71 75 72 bool bIsAbstractClass_;76 BaseFactory* factory_; 73 77 bool bCreatedOneObject_; 74 75 78 static int hierarchyCreatingCounter_s; 76 79 }; … … 87 90 88 91 public: 89 static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass , bool bIsAbstractClass);92 static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass); 90 93 static ClassIdentifier<T>* getIdentifier(); 91 BaseObject* fabricate() const;92 T* fabricateClass() const;93 94 static void addObject(T* object); 94 95 void removeObject(OrxonoxClass* object) const; … … 121 122 122 123 template <class T> 123 BaseObject* ClassIdentifier<T>::fabricate() const 124 { 125 return dynamic_cast<BaseObject*>(this->fabricateClass()); 126 } 127 128 template <class T> 129 T* ClassIdentifier<T>::fabricateClass() const 130 { 131 if (!this->bIsAbstractClass_) 132 { 133 return new T; 134 } 135 else 136 { 137 std::cout << "Error: Couldn't create a new Object - Class " << this->name_ << " is abstract!\n"; 138 std::cout << "Aborting...\n"; 139 abort(); 140 } 141 } 142 143 template <class T> 144 ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass, bool bIsAbstractClass) 124 ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass) 145 125 { 146 126 #if HIERARCHY_VERBOSE … … 152 132 std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n"; 153 133 #endif 154 if (parents || bRootClass)155 {156 pointer_s = new ClassIdentifier(); 157 pointer_s->name_ = name;158 pointer_s->bIsAbstractClass_ = bIsAbstractClass;159 160 ClassFactory::add(name, pointer_s);161 162 if (!bRootClass)163 pointer_s->initialize(parents);164 else 165 pointer_s->initialize(NULL);166 }134 pointer_s = new ClassIdentifier(); 135 } 136 137 if (!pointer_s->bCreatedOneObject_) 138 { 139 #if HIERARCHY_VERBOSE 140 std::cout << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n"; 141 #endif 142 pointer_s->name_ = name; 143 Factory::add(name, pointer_s); 144 145 if (bRootClass) 146 pointer_s->initialize(NULL); 167 147 else 168 { 169 pointer_s = getIdentifier(); 170 } 148 pointer_s->initialize(parents); 171 149 } 172 150 … … 177 155 ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() 178 156 { 179 #if HIERARCHY_VERBOSE180 // std::cout << "*** Get Identifier.\n";181 #endif182 157 if (!pointer_s) 183 158 { 184 159 #if HIERARCHY_VERBOSE 185 std::cout << "*** Get Identifier -> Create Class\n"; 186 #endif 187 Identifier::startCreatingHierarchy(); 188 T* temp = new T(); 189 delete temp; 190 Identifier::stopCreatingHierarchy(); 160 std::cout << "*** Create Singleton.\n"; 161 #endif 162 pointer_s = new ClassIdentifier(); 191 163 } 192 164 -
code/branches/objecthierarchie/src/IdentifierIncludes.h
r231 r244 1 1 #include "Identifier.h" 2 2 #include "Factory.h" 3 #include "ClassFactory.h" 3 4 #include "IdentifierList.h" 4 5 #include "ObjectList.h" … … 7 8 8 9 9 #define internRegisterRootObject(ClassName, bAbstract) \ 10 if (Identifier::isCreatingHierarchy() && !this->getParents()) \ 11 this->setParents(new IdentifierList()); \ 12 this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, true, bAbstract)); \ 10 #define InternRegisterObject(ClassName, bRootClass) \ 11 this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, bRootClass)); \ 13 12 if (Identifier::isCreatingHierarchy() && this->getParents()) \ 14 13 this->getParents()->add(this->getIdentifier()); \ 15 14 ClassIdentifier<ClassName>::addObject(this) 16 15 16 #define InternRegisterRootObject(ClassName) \ 17 if (Identifier::isCreatingHierarchy() && !this->getParents()) \ 18 this->setParents(new IdentifierList()); \ 19 InternRegisterObject(ClassName, true) 20 17 21 #if HIERARCHY_VERBOSE 18 #define registerRootObject(ClassName) \19 std::cout << "*** Register Root-Object: " << #ClassName << "\n"; \20 internRegisterRootObject(ClassName, false)22 #define RegisterObject(ClassName) \ 23 std::cout << "*** Register Object: " << #ClassName << "\n"; \ 24 InternRegisterObject(ClassName, false) 21 25 #else 22 #define registerRootObject(ClassName) \23 internRegisterRootObject(ClassName, false)26 #define RegisterObject(ClassName) \ 27 InternRegisterObject(ClassName, false) 24 28 #endif 25 29 26 30 #if HIERARCHY_VERBOSE 27 #define registerAbstractRootObject(ClassName) \28 std::cout << "*** Register abstractRoot-Object: " << #ClassName << "\n"; \29 internRegisterRootObject(ClassName, true)31 #define RegisterRootObject(ClassName) \ 32 std::cout << "*** Register Root-Object: " << #ClassName << "\n"; \ 33 InternRegisterRootObject(ClassName) 30 34 #else 31 #define registerAbstractRootObject(ClassName) \32 internRegisterRootObject(ClassName, true)35 #define RegisterRootObject(ClassName) \ 36 InternRegisterRootObject(ClassName) 33 37 #endif 34 38 35 #define internRegisterObject(ClassName, bAbstract) \ 36 this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, false, bAbstract)); \ 37 if (Identifier::isCreatingHierarchy() && this->getParents()) \ 38 this->getParents()->add(this->getIdentifier()); \ 39 ClassIdentifier<ClassName>::addObject(this) 40 41 #if HIERARCHY_VERBOSE 42 #define registerObject(ClassName) \ 43 std::cout << "*** Register Object: " << #ClassName << "\n"; \ 44 internRegisterObject(ClassName, false) 45 #else 46 #define registerObject(ClassName) \ 47 internRegisterObject(ClassName, false) 48 #endif 49 50 #if HIERARCHY_VERBOSE 51 #define registerAbstractObject(ClassName) \ 52 std::cout << "*** Register abstract Object: " << #ClassName << "\n"; \ 53 internRegisterObject(ClassName, true) 54 #else 55 #define registerAbstractObject(ClassName) \ 56 internRegisterObject(ClassName, true) 57 #endif 58 59 #define unregisterObject() \ 39 #define UnregisterObject() \ 60 40 this->getIdentifier()->removeObject(this) 61 41 … … 64 44 65 45 #define CreateFactory(ClassName) \ 66 Identifier* global_##ClassName##_Identifier = ClassIdentifier<ClassName>::getIdentifier()46 bool bCreated##ClassName##Factory = ClassFactory<ClassName>::create() 67 47 68 #define Factory(Name) \69 ClassFactory::fabricate(Name)48 #define ID(Name) \ 49 Factory::getIdentifier(Name) -
code/branches/objecthierarchie/src/Test.h
r219 r244 9 9 class Interface1 : virtual public OrxonoxClass 10 10 { 11 p ublic:12 Interface1() { registerAbstractRootObject(Interface1); }11 protected: 12 Interface1() { RegisterRootObject(Interface1); } 13 13 }; 14 14 15 15 class Interface2 : virtual public OrxonoxClass 16 16 { 17 p ublic:18 Interface2() { registerAbstractRootObject(Interface2); }17 protected: 18 Interface2() { RegisterRootObject(Interface2); } 19 19 }; 20 20 … … 22 22 { 23 23 public: 24 A1() { registerObject(A1); }24 A1() { RegisterObject(A1); } 25 25 }; 26 26 … … 28 28 { 29 29 public: 30 A2() { registerObject(A2); }30 A2() { RegisterObject(A2); } 31 31 }; 32 32 … … 34 34 { 35 35 public: 36 A3() { registerObject(A3); }36 A3() { RegisterObject(A3); } 37 37 }; 38 38 … … 40 40 { 41 41 public: 42 A1B1() { registerObject(A1B1); }42 A1B1() { RegisterObject(A1B1); } 43 43 }; 44 44 … … 46 46 { 47 47 public: 48 A1B2() { registerObject(A1B2); }48 A1B2() { RegisterObject(A1B2); } 49 49 }; 50 50 … … 52 52 { 53 53 public: 54 A2B1() { registerObject(A2B1); }54 A2B1() { RegisterObject(A2B1); } 55 55 }; 56 56 … … 58 58 { 59 59 public: 60 A2B2() { registerObject(A2B2); }60 A2B2() { RegisterObject(A2B2); } 61 61 }; 62 62 … … 64 64 { 65 65 public: 66 A3B1() { registerObject(A3B1); }66 A3B1() { RegisterObject(A3B1); } 67 67 }; 68 68 … … 70 70 { 71 71 public: 72 A3B2() { registerObject(A3B2); }72 A3B2() { RegisterObject(A3B2); } 73 73 }; 74 74 … … 76 76 { 77 77 public: 78 A1B1C1() { registerObject(A1B1C1); }78 A1B1C1() { RegisterObject(A1B1C1); } 79 79 }; 80 80 … … 82 82 { 83 83 public: 84 A1B1C2() { registerObject(A1B1C2); }84 A1B1C2() { RegisterObject(A1B1C2); } 85 85 }; 86 86 … … 88 88 { 89 89 public: 90 A1B2C1() { registerObject(A1B2C1); }90 A1B2C1() { RegisterObject(A1B2C1); } 91 91 }; 92 92 … … 94 94 { 95 95 public: 96 A2B1C1() { registerObject(A2B1C1); }96 A2B1C1() { RegisterObject(A2B1C1); } 97 97 }; 98 98 … … 100 100 { 101 101 public: 102 A2B2C1() { registerObject(A2B2C1); }102 A2B2C1() { RegisterObject(A2B2C1); } 103 103 }; 104 104 … … 106 106 { 107 107 public: 108 A3B1C1() { registerObject(A3B1C1); }108 A3B1C1() { RegisterObject(A3B1C1); } 109 109 }; 110 110 … … 112 112 { 113 113 public: 114 A3B1C2() { registerObject(A3B1C2); }114 A3B1C2() { RegisterObject(A3B1C2); } 115 115 }; 116 116 … … 118 118 { 119 119 public: 120 A3B2C1() { registerObject(A3B2C1); }120 A3B2C1() { RegisterObject(A3B2C1); } 121 121 }; 122 122 … … 124 124 { 125 125 public: 126 A3B2C2() { registerObject(A3B2C2); }126 A3B2C2() { RegisterObject(A3B2C2); } 127 127 }; 128 128 -
code/branches/objecthierarchie/src/orxonox.cc
r243 r244 102 102 startRenderLoop(); 103 103 */ 104 105 #define testandcout(code) \ 106 std::cout << #code << " " << code << "\n" 107 104 108 /* 105 109 std::cout << "Test 1\n"; … … 176 180 } 177 181 */ 178 179 #define testandcout(code) \180 std::cout << #code << " " << code << "\n"181 182 182 /* 183 183 std::cout << "\n"; … … 205 205 testandcout(test5_03->isA(Class(BaseObject))); 206 206 testandcout(test5_03->isA(Class(Interface1))); 207 208 207 209 208 std::cout << "\n"; … … 346 345 std::cout << "\n"; 347 346 std::cout << "A2 parent of A2B1C1: " << Class(A2)->isParentOf(Class(A2B1C1)) << "\n"; 348 349 347 */ 348 /* 350 349 std::cout << "Test 7\n"; 351 350 std::cout << "1\n"; … … 427 426 428 427 std::cout << "\n4\n"; 429 BaseObject* test9_06 = Factory("A2B2");428 BaseObject* test9_06 = ID("A2B2")->fabricate(); 430 429 std::cout << test9_06->getIdentifier()->getName() << "\n"; 431 430 … … 436 435 delete test9_06; 437 436 */ 438 437 /* 439 438 std::cout << "Test 10\n"; 440 439 Identifier* test10_01 = Class(A1B2); … … 449 448 BaseObject* test10_07; 450 449 for (int i = 0; i < 10; i++) 451 test10_07 = Factory("A1B1C1");450 test10_07 = ID("A1B1C1")->fabricate(); 452 451 453 452 std::cout << "1\n"; … … 484 483 for (int i = 0; i < 10; i++) 485 484 { 486 test10_08 = Factory("A2B1C1");485 test10_08 = ID("A2B1C1")->fabricate(); 487 486 test10_08->name_ = "A2B1C1#"; 488 487 test10_08->name_ += ('0' + i); … … 534 533 for (Iterator<A2B1C1> it; it; --it) 535 534 std::cout << "Name: " << it->name_ << "\n"; 536 535 */ 537 536 538 537 } -
code/branches/objecthierarchie/src/test1.cc
r218 r244 9 9 Test1::Test1() 10 10 { 11 registerObject(Test1);11 RegisterObject(Test1); 12 12 13 13 this->usefullClass1_ = Class(Test1); -
code/branches/objecthierarchie/src/test2.cc
r218 r244 9 9 Test2::Test2() 10 10 { 11 registerObject(Test2);11 RegisterObject(Test2); 12 12 13 13 this->usefullClass1_ = Class(Test1); -
code/branches/objecthierarchie/src/test3.cc
r218 r244 9 9 Test3::Test3() 10 10 { 11 registerObject(Test3);11 RegisterObject(Test3); 12 12 } 13 13
Note: See TracChangeset
for help on using the changeset viewer.