Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 25, 2007, 5:21:53 PM (16 years ago)
Author:
landauf
Message:

reimplementation of the factory and parts of the class-hierarchy-generating-algorithm. interfaces with protected constructors are now allowed.

File:
1 edited

Legend:

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

    r243 r244  
    88#include "Factory.h"
    99
    10 #define HIERARCHY_VERBOSE false
     10#define HIERARCHY_VERBOSE true
    1111
    1212
     
    2626        friend class SubclassIdentifier;
    2727
     28        template <class T>
     29        friend class ClassFactory;
     30
    2831        public:
    2932            virtual void removeObject(OrxonoxClass* object) const = 0;
    3033            virtual void removeObjectIntern(OrxonoxClass* object, bool bIterateForwards) const = 0;
    3134
    32             virtual BaseObject* fabricate() const = 0;
     35            inline void addFactory(BaseFactory* factory) { this->factory_ = factory; }
     36            BaseObject* fabricate();
    3337
    3438            bool isA(const Identifier* identifier) const;
     
    3741            bool isParentOf(const Identifier* identifier) const;
    3842
    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); }
    4448
    4549        private:
     
    4953            void initialize(const IdentifierList* parents);
    5054
    51             static void startCreatingHierarchy()
     55            inline static void startCreatingHierarchy()
    5256            {
    5357                hierarchyCreatingCounter_s++;
     
    5761            }
    5862
    59             static void stopCreatingHierarchy()
     63            inline static void stopCreatingHierarchy()
    6064            {
    6165                hierarchyCreatingCounter_s--;
     
    7074            std::string name_;
    7175
    72             bool bIsAbstractClass_;
     76            BaseFactory* factory_;
    7377            bool bCreatedOneObject_;
    74 
    7578            static int hierarchyCreatingCounter_s;
    7679    };
     
    8790
    8891        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);
    9093            static ClassIdentifier<T>* getIdentifier();
    91             BaseObject* fabricate() const;
    92             T* fabricateClass() const;
    9394            static void addObject(T* object);
    9495            void removeObject(OrxonoxClass* object) const;
     
    121122
    122123    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)
    145125    {
    146126#if HIERARCHY_VERBOSE
     
    152132            std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n";
    153133#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);
    167147            else
    168             {
    169                 pointer_s = getIdentifier();
    170             }
     148                pointer_s->initialize(parents);
    171149        }
    172150
     
    177155    ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
    178156    {
    179 #if HIERARCHY_VERBOSE
    180 //        std::cout << "*** Get Identifier.\n";
    181 #endif
    182157        if (!pointer_s)
    183158        {
    184159#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();
    191163        }
    192164
Note: See TracChangeset for help on using the changeset viewer.