Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 19, 2007, 10:10:11 PM (18 years ago)
Author:
landauf
Message:
  • removed the "ClassHierarchy" manager-class and put its sole feature (bIsCreatingClassHierarchy_) directly into the Identifier.
  • added a dynamic_cast from OrxonoxClass to BaseObject to the Factory. OrxonoxClass is needed because several classes use Interfaces, but all classes are derived at least from BaseObject, so the cast will work.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchie/src/Identifier.h

    r218 r219  
    22#define _Identifier_H__
    33
    4 #include "ClassHierarchy.h"
     4#include <iostream>
     5
    56#include "IdentifierList.h"
    67#include "ObjectList.h"
    7 #include "OrxonoxClass.h"
     8//#include "OrxonoxClass.h"
    89#include "Factory.h"
     10
     11// DONE AND TESTED:
     12// - build class hierarchy
     13// - isA, isChildOf, ...
     14// - insert into class-lists
     15// - ClassIdentifier
     16// - BaseIdentifier
     17// - Factory
     18
     19// IN WORK:
     20
     21// TO DO:
     22// - iterate through lists
    923
    1024namespace orxonox
    1125{
     26    class BaseObject;
     27
    1228    // ##### Identifier #####
    1329    class Identifier
     
    2339            void removeObject(OrxonoxClass* object);
    2440
    25             virtual OrxonoxClass* fabricate() {};
     41            virtual BaseObject* fabricate() {};
    2642
    2743            bool isA(Identifier* identifier);
     
    3349
    3450            std::string getName() { return this->name_; }
    35             IdentifierList* getDirectParents() { return this->directParents_; }
    36             IdentifierList* getAllParents() { return this->allParents_; }
    37             IdentifierList* getDirectChildren() { return this->directChildren_; }
    38             IdentifierList* getAllChildren() { return this->allChildren_; }
     51            IdentifierList* getDirectParents() { return &(this->directParents_); }
     52            IdentifierList* getAllParents() { return &(this->allParents_); }
     53            IdentifierList* getDirectChildren() { return &(this->directChildren_); }
     54            IdentifierList* getAllChildren() { return &(this->allChildren_); }
     55
     56            static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
    3957
    4058        private:
     
    4462            void initialize(IdentifierList* parents);
    4563
    46             IdentifierList* directParents_;
    47             IdentifierList* allParents_;
    48             IdentifierList* directChildren_;
    49             IdentifierList* allChildren_;
    50 
    51             ObjectList* objects_;
     64            static void startCreatingHierarchy() { hierarchyCreatingCounter_s++; std::cout << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; }
     65            static void stopCreatingHierarchy() { hierarchyCreatingCounter_s--; std::cout << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; }
     66
     67            IdentifierList directParents_;
     68            IdentifierList allParents_;
     69            IdentifierList directChildren_;
     70            IdentifierList allChildren_;
     71
     72            ObjectList objects_;
    5273            std::string name_;
    5374
    5475            bool bIsAbstractClass_;
    5576            bool bCreatedOneObject_;
     77
     78            static int hierarchyCreatingCounter_s;
    5679    };
    5780
     
    6487            static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass);
    6588            static ClassIdentifier<T>* getIdentifier();
    66             OrxonoxClass* fabricate();
     89            BaseObject* fabricate();
    6790            T* fabricateClass();
    6891
     
    7295            ~ClassIdentifier();
    7396
    74             static ClassIdentifier<T>* pointer_;
     97            static ClassIdentifier<T>* pointer_s;
    7598
    7699    };
    77100
    78101    template <class T>
    79     ClassIdentifier<T>* ClassIdentifier<T>::pointer_ = NULL;
     102    ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL;
    80103
    81104    template <class T>
     
    87110    ClassIdentifier<T>::~ClassIdentifier()
    88111    {
    89         this->pointer_ = NULL;
    90     }
    91 
    92     template <class T>
    93     OrxonoxClass* ClassIdentifier<T>::fabricate()
    94     {
    95         return this->fabricateClass();
     112        this->pointer_s = NULL;
     113    }
     114
     115    template <class T>
     116    BaseObject* ClassIdentifier<T>::fabricate()
     117    {
     118        return dynamic_cast<BaseObject*>(this->fabricateClass());
    96119    }
    97120
     
    99122    T* ClassIdentifier<T>::fabricateClass()
    100123    {
    101         return new T;
     124        if (!this->bIsAbstractClass_)
     125        {
     126            return new T;
     127        }
     128        else
     129        {
     130            std::cout << "Error: Couldn't create a new Object - Class " << this->name_ << " is abstract!\n";
     131            std::cout << "Aborting...\n";
     132            abort();
     133        }
    102134    }
    103135
     
    106138    {
    107139        std::cout << "*** Register Class in " << name << "-Singleton.\n";
    108         if (!pointer_)
     140        if (!pointer_s)
    109141        {
    110142            std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n";
    111143            if (parents || bRootClass)
    112144            {
    113                 pointer_ = new ClassIdentifier();
    114                 pointer_->name_ = name;
    115                 pointer_->bIsAbstractClass_ = bIsAbstractClass;
    116 
    117                 ClassFactory::add(name, pointer_);
    118 
    119                 pointer_->initialize(parents);
     145                pointer_s = new ClassIdentifier();
     146                pointer_s->name_ = name;
     147                pointer_s->bIsAbstractClass_ = bIsAbstractClass;
     148
     149                ClassFactory::add(name, pointer_s);
     150
     151                pointer_s->initialize(parents);
    120152            }
    121153            else
    122154            {
    123                 pointer_ = getIdentifier();
    124             }
    125         }
    126 
    127         return pointer_;
     155                pointer_s = getIdentifier();
     156            }
     157        }
     158
     159        return pointer_s;
    128160    }
    129161
     
    132164    {
    133165//        std::cout << "*** Get Identifier.\n";
    134         if (!pointer_)
     166        if (!pointer_s)
    135167        {
    136168            std::cout << "*** Get Identifier -> Create Class\n";
    137             ClassHierarchy::getSingleton()->startCreatingHierarchy();
     169            Identifier::startCreatingHierarchy();
    138170            T* temp = new T();
    139             ClassHierarchy::getSingleton()->stopCreatingHierarchy();
     171            Identifier::stopCreatingHierarchy();
    140172            delete temp;
    141173        }
    142174
    143         return pointer_;
     175        return pointer_s;
    144176    }
    145177
     
    176208            B* fabricate()
    177209            {
    178                 OrxonoxClass* newObject = this->identifier_->fabricate();
     210                BaseObject* newObject = this->identifier_->fabricate();
    179211                if (newObject)
    180212                {
Note: See TracChangeset for help on using the changeset viewer.