Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchie/src/ClassHierarchy.h @ 172

Last change on this file since 172 was 172, checked in by landauf, 17 years ago

some features work perfectly,
some features are fucked up,
some features arent yet implemented.

x3n→hair→brightness++;

theres still a lot to do, but i can see the light on the end of the tunnel.
templates, makros, operator overloading, function pointers… the beauty of the beast. i'm in love and still dying piece by piece with every line of code i'm writing.
</film noir>

File size: 7.5 KB
RevLine 
[132]1#ifndef _ClassHierarchy_H__
2#define _ClassHierarchy_H__
3
4#include <string>
[162]5#include <iostream>
[172]6#include <assert.h>
[132]7
[172]8// DONE AND TESTED:
[132]9// - klassenhierarchie aufbauen
[172]10// - isA u.a. vergleiche
[149]11// - in listen einfügen
[172]12
13// IN WORK:
[149]14// - factory
[132]15// - klassen-identifier
16
[172]17// TO DO:
[132]18// - durch listen iterieren
19// - searchtree für classname-strings
20
[149]21
22namespace orxonox
23{
[162]24    // ##### ClassHierarchy #####
[172]25    template <class T>
26    class ClassIdentifier;
27
[162]28    class ClassHierarchy
29    {
[172]30        template <class T>
31        friend class ClassIdentifier;
32
[162]33        public:
34            static ClassHierarchy* getSingleton();
[172]35            bool isCreatingHierarchy() { return (this->hierarchyCreatingCounter_ > 0); }
[162]36
37        private:
38            ClassHierarchy();
[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
[149]47    // ##### Identifier #####
48    class IdentifierList;
[132]49    class ObjectList;
[162]50    class OrxonoxClass;
[132]51
[149]52    class Identifier
[132]53    {
[149]54        template <class T>
55        friend class ClassIdentifier;
56
[172]57        template <class T>
58        friend class BaseIdentifier;
59
[132]60        public:
[172]61            Identifier(Identifier* identifier) {};
62            ~Identifier();
[162]63            void addObject(OrxonoxClass* object);
64            void removeObject(OrxonoxClass* object);
[132]65
[149]66            bool isA(Identifier* identifier);
[172]67            bool isDirectlyA(Identifier* identifier);
[149]68            bool isChildOf(Identifier* identifier);
69            bool isDirectChildOf(Identifier* identifier);
70            bool isParentOf(Identifier* identifier);
71            bool isDirectParentOf(Identifier* identifier);
72
[172]73            std::string getName() { return this->name_; }
74            IdentifierList* getDirectParents() { return this->directParents_; }
75            IdentifierList* getAllParents() { return this->allParents_; }
76            IdentifierList* getDirectChildren() { return this->directChildren_; }
77            IdentifierList* getAllChildren() { return this->allChildren_; }
78
[150]79        private:
[149]80            Identifier();
[150]81            void initialize(IdentifierList* parents);
[149]82
83            IdentifierList* directParents_;
84            IdentifierList* allParents_;
85            IdentifierList* directChildren_;
86            IdentifierList* allChildren_;
87
88            ObjectList* objects_;
89            std::string name_;
90
91            bool bCreatedOneObject_;
[132]92    };
93
[172]94
95    // ##### ClassIdentifier #####
96    class A1;
97
[149]98    template <class T>
99    class ClassIdentifier : public Identifier
[132]100    {
101        public:
[162]102            static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass);
[150]103            static ClassIdentifier<T>* getIdentifier();
[149]104            static T* create();
[132]105
[149]106        private:
107            ClassIdentifier();
[172]108            ~ClassIdentifier();
[150]109
110            static ClassIdentifier<T>* pointer_;
111
[132]112    };
113
[149]114    template <class T>
[150]115    ClassIdentifier<T>* ClassIdentifier<T>::pointer_ = NULL;
116
117    template <class T>
[149]118    ClassIdentifier<T>::ClassIdentifier()
[132]119    {
[149]120    }
[132]121
[149]122    template <class T>
[172]123    ClassIdentifier<T>::~ClassIdentifier()
124    {
125        this->pointer_ = NULL;
126    }
127
128    template <class T>
[162]129    ClassIdentifier<T>* ClassIdentifier<T>::registerClass(IdentifierList* parents, std::string name, bool bRootClass)
[149]130    {
[162]131        std::cout << "*** Register Class in " << name << "-Singleton.\n";
[149]132        if (!pointer_)
133        {
[162]134            std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n";
135            if (parents || bRootClass)
136            {
137                pointer_ = new ClassIdentifier();
138                pointer_->name_ = name;
139                pointer_->initialize(parents);
140            }
141            else
142            {
143                pointer_ = getIdentifier();
144            }
[149]145        }
[132]146
[149]147        return pointer_;
148    }
149
150    template <class T>
[150]151    ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
[132]152    {
[172]153//        std::cout << "*** Get Identifier.\n";
[149]154        if (!pointer_)
155        {
[162]156            std::cout << "*** Get Identifier -> Create Class\n";
[172]157            ClassHierarchy::getSingleton()->startCreatingHierarchy();
[149]158            T* temp = new T();
[172]159            ClassHierarchy::getSingleton()->stopCreatingHierarchy();
[149]160            delete temp;
161        }
162
163        return pointer_;
164    }
165
166    template <class T>
167    T* ClassIdentifier<T>::create()
168    {
169        return new T();
170    }
171
[172]172
[149]173    // ##### Identifier List #####
174    class IdentifierListElement;
175
176    class IdentifierList
177    {
[132]178        public:
[149]179            IdentifierList();
180            ~IdentifierList();
181            void add(Identifier* identifier);
182            void remove(Identifier* identifier);
183            bool isInList(Identifier* identifier);
[172]184            std::string toString();
[132]185
[149]186            IdentifierListElement* first_;
[132]187    };
188
[149]189    class IdentifierListElement
[132]190    {
191        public:
[149]192            IdentifierListElement(Identifier* identifier);
[172]193            ~IdentifierListElement();
[132]194
[149]195            Identifier* identifier_;
196            IdentifierListElement* next_;
197            bool bDirect_;
[132]198    };
199
[149]200
201    // ##### Object List #####
202    class ObjectListElement;
203
[132]204    class ObjectList
205    {
206        public:
207            ObjectList();
208            ~ObjectList();
[162]209            void add(OrxonoxClass* object);
210            void remove(OrxonoxClass* object);
[132]211
[149]212            ObjectListElement* first_;
[132]213    };
214
[149]215    class ObjectListElement
[132]216    {
217        public:
[162]218            ObjectListElement(OrxonoxClass* object);
[172]219            ~ObjectListElement();
[132]220
[162]221            OrxonoxClass* object_;
[149]222            ObjectListElement* next_;
[132]223    };
224
[149]225    // ##### Macros #####
226    #define registerRootObject(ClassName) \
[162]227        std::cout << "*** Register Root-Object: " << #ClassName << "\n"; \
228        if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && !this->getParents()) \
229            this->setParents(new IdentifierList()); \
230        if (this->getIdentifier()) \
231            this->getIdentifier()->removeObject(this); \
232        this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, true)); \
233        if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && this->getParents()) \
234            this->getParents()->add(this->getIdentifier()); \
235        this->getIdentifier()->addObject(this)
[132]236
[149]237    #define registerObject(ClassName) \
[162]238        std::cout << "*** Register Object: " << #ClassName << "\n"; \
239        this->getIdentifier()->removeObject(this); \
240        this->setIdentifier(ClassIdentifier<ClassName>::registerClass(this->getParents(), #ClassName, false)); \
241        if (ClassHierarchy::getSingleton()->isCreatingHierarchy() && this->getParents()) \
242            this->getParents()->add(this->getIdentifier()); \
243        this->getIdentifier()->addObject(this)
[132]244
245    #define unregisterObject() \
[162]246        this->getIdentifier()->removeObject(this)
[132]247
[149]248    #define Class(ClassName) \
249        ClassIdentifier<ClassName>::getIdentifier()
[132]250
[149]251    #define Factory(ClassName) \
252        ClassIdentifier<ClassName>::create()
253}
254
[132]255#endif
Note: See TracBrowser for help on using the repository browser.