[132] | 1 | #ifndef _ClassHierarchy_H__ |
---|
| 2 | #define _ClassHierarchy_H__ |
---|
| 3 | |
---|
| 4 | #include <string> |
---|
[162] | 5 | #include <iostream> |
---|
[132] | 6 | |
---|
[172] | 7 | // DONE AND TESTED: |
---|
[176] | 8 | // - build class hierarchy |
---|
| 9 | // - isA, isChildOf, ... |
---|
| 10 | // - insert into class-lists |
---|
| 11 | // - ClassIdentifier |
---|
[197] | 12 | // - BaseIdentifier |
---|
[218] | 13 | // - Factory |
---|
[172] | 14 | |
---|
| 15 | // IN WORK: |
---|
[132] | 16 | |
---|
[172] | 17 | // TO DO: |
---|
[176] | 18 | // - iterate through lists |
---|
[132] | 19 | |
---|
[149] | 20 | |
---|
| 21 | namespace orxonox |
---|
| 22 | { |
---|
[162] | 23 | // ##### ClassHierarchy ##### |
---|
[172] | 24 | template <class T> |
---|
| 25 | class ClassIdentifier; |
---|
| 26 | |
---|
[162] | 27 | class ClassHierarchy |
---|
| 28 | { |
---|
[172] | 29 | template <class T> |
---|
| 30 | friend class ClassIdentifier; |
---|
| 31 | |
---|
[162] | 32 | public: |
---|
| 33 | static ClassHierarchy* getSingleton(); |
---|
[172] | 34 | bool isCreatingHierarchy() { return (this->hierarchyCreatingCounter_ > 0); } |
---|
[162] | 35 | |
---|
| 36 | private: |
---|
| 37 | ClassHierarchy(); |
---|
[218] | 38 | ClassHierarchy(const ClassHierarchy& hierarchy); |
---|
[172] | 39 | ~ClassHierarchy(); |
---|
| 40 | void startCreatingHierarchy() { this->hierarchyCreatingCounter_++; std::cout << "*** Increased Hierarchy-Creating-Counter to " << this->hierarchyCreatingCounter_ << "\n"; } |
---|
| 41 | void stopCreatingHierarchy() { this->hierarchyCreatingCounter_--; std::cout << "*** Decreased Hierarchy-Creating-Counter to " << this->hierarchyCreatingCounter_ << "\n"; } |
---|
[162] | 42 | |
---|
| 43 | static ClassHierarchy* pointer_; |
---|
[172] | 44 | int hierarchyCreatingCounter_; |
---|
[162] | 45 | }; |
---|
| 46 | |
---|
[132] | 47 | |
---|
[149] | 48 | // ##### Macros ##### |
---|
| 49 | #define registerRootObject(ClassName) \ |
---|
[162] | 50 | std::cout << "*** Register Root-Object: " << #ClassName << "\n"; \ |
---|
| 51 | if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && !this->getParents()) \ |
---|
| 52 | this->setParents(new IdentifierList()); \ |
---|
| 53 | if (this->getIdentifier()) \ |
---|
| 54 | this->getIdentifier()->removeObject(this); \ |
---|
[197] | 55 | this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, true, false)); \ |
---|
[162] | 56 | if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && this->getParents()) \ |
---|
| 57 | this->getParents()->add(this->getIdentifier()); \ |
---|
| 58 | this->getIdentifier()->addObject(this) |
---|
[132] | 59 | |
---|
[197] | 60 | #define registerAbstractRootObject(ClassName) \ |
---|
| 61 | std::cout << "*** Register Root-Object: " << #ClassName << "\n"; \ |
---|
| 62 | if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && !this->getParents()) \ |
---|
| 63 | this->setParents(new IdentifierList()); \ |
---|
| 64 | if (this->getIdentifier()) \ |
---|
| 65 | this->getIdentifier()->removeObject(this); \ |
---|
| 66 | this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, true, true)); \ |
---|
| 67 | if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && this->getParents()) \ |
---|
| 68 | this->getParents()->add(this->getIdentifier()); \ |
---|
| 69 | this->getIdentifier()->addObject(this) |
---|
| 70 | |
---|
[149] | 71 | #define registerObject(ClassName) \ |
---|
[162] | 72 | std::cout << "*** Register Object: " << #ClassName << "\n"; \ |
---|
| 73 | this->getIdentifier()->removeObject(this); \ |
---|
[197] | 74 | this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, false, false)); \ |
---|
[162] | 75 | if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && this->getParents()) \ |
---|
| 76 | this->getParents()->add(this->getIdentifier()); \ |
---|
| 77 | this->getIdentifier()->addObject(this) |
---|
[132] | 78 | |
---|
[197] | 79 | #define registerAbstractObject(ClassName) \ |
---|
| 80 | std::cout << "*** Register Object: " << #ClassName << "\n"; \ |
---|
| 81 | this->getIdentifier()->removeObject(this); \ |
---|
| 82 | this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, false, true)); \ |
---|
| 83 | if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && this->getParents()) \ |
---|
| 84 | this->getParents()->add(this->getIdentifier()); \ |
---|
| 85 | this->getIdentifier()->addObject(this) |
---|
| 86 | |
---|
[132] | 87 | #define unregisterObject() \ |
---|
[162] | 88 | this->getIdentifier()->removeObject(this) |
---|
[132] | 89 | |
---|
[149] | 90 | #define Class(ClassName) \ |
---|
| 91 | ClassIdentifier<ClassName>::getIdentifier() |
---|
[132] | 92 | |
---|
[218] | 93 | #define CreateFactory(ClassName) \ |
---|
| 94 | Identifier* global_##ClassName##_Identifier = ClassIdentifier<ClassName>::getIdentifier() |
---|
| 95 | |
---|
| 96 | #define Factory(Name) \ |
---|
| 97 | ClassFactory::fabricate(Name) |
---|
[149] | 98 | } |
---|
| 99 | |
---|
[132] | 100 | #endif |
---|