Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 244


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.

Location:
code/branches/objecthierarchie/src
Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchie/src/BaseObject.cc

    r218 r244  
    77    BaseObject::BaseObject()
    88    {
    9         registerRootObject(BaseObject);
     9        RegisterRootObject(BaseObject);
    1010    }
    1111
    1212    BaseObject::~BaseObject()
    1313    {
    14         unregisterObject();
     14        UnregisterObject();
    1515    }
    1616}
  • code/branches/objecthierarchie/src/CMakeLists.txt

    r224 r244  
    33# create a few variables to simplify life
    44SET(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)
     5SET(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)
    66
    77#Creates an executable
  • code/branches/objecthierarchie/src/Factory.cc

    r219 r244  
    55namespace orxonox
    66{
    7     ClassFactory* ClassFactory::pointer_s = NULL;
     7    Factory* Factory::pointer_s = NULL;
    88
    9     BaseObject* ClassFactory::fabricate(const std::string& name)
     9    Identifier* Factory::getIdentifier(const std::string& name)
    1010    {
    1111        if (!pointer_s)
    12             pointer_s = new ClassFactory;
     12            pointer_s = new Factory;
    1313
    14         return pointer_s->identifierMap_[name]->fabricate();
     14        return pointer_s->identifierMap_[name];
    1515    }
    1616
    17     void ClassFactory::add(const std::string& name, Identifier* identifier)
     17    void Factory::add(const std::string& name, Identifier* identifier)
    1818    {
    1919        if (!pointer_s)
    20             pointer_s = new ClassFactory;
     20            pointer_s = new Factory;
    2121
    2222        pointer_s->identifierMap_[name] = identifier;
  • code/branches/objecthierarchie/src/Factory.h

    r219 r244  
    77namespace orxonox
    88{
     9    class BaseObject;
    910    class Identifier;
    10     class BaseObject;
    1111
    12     class ClassFactory
     12    // ###############################
     13    // ###         Factory         ###
     14    // ###############################
     15    class Factory
    1316    {
    1417        public:
    15             static BaseObject* fabricate(const std::string& name);
     18            static Identifier* getIdentifier(const std::string& name);
    1619            static void add(const std::string& name, Identifier* identifier);
    1720
    1821        private:
    19             ClassFactory() {}
    20             ClassFactory(const ClassFactory& factory) {}
    21             ~ClassFactory() {}
     22            Factory() {}
     23            Factory(const Factory& factory) {}
     24            ~Factory() {}
    2225
    23             static ClassFactory* pointer_s;
     26            static Factory* pointer_s;
    2427            std::map<std::string, Identifier*> identifierMap_;
     28    };
     29
     30    // ###############################
     31    // ###       BaseFactory       ###
     32    // ###############################
     33    class BaseFactory
     34    {
     35        public:
     36            virtual BaseObject* fabricate() = 0;
    2537    };
    2638}
  • code/branches/objecthierarchie/src/Identifier.cc

    r243 r244  
    1111    {
    1212        this->bCreatedOneObject_ = false;
     13        this->factory_ = 0;
    1314
    1415        this->children_ = new IdentifierList;
     
    2728        std::cout << "*** Initialize " << this->name_ << "-Singleton.\n";
    2829#endif
     30        this->bCreatedOneObject_ = true;
     31
    2932        if (parents)
    3033        {
    31             this->bCreatedOneObject_ = true;
    32 
    3334            IdentifierListElement* temp1 = parents->first_;
    3435            while (temp1)
     
    3940                temp1 = temp1->next_;
    4041            }
     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();
    4156        }
    4257    }
  • 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
  • code/branches/objecthierarchie/src/IdentifierIncludes.h

    r231 r244  
    11#include "Identifier.h"
    22#include "Factory.h"
     3#include "ClassFactory.h"
    34#include "IdentifierList.h"
    45#include "ObjectList.h"
     
    78
    89
    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)); \
    1312    if (Identifier::isCreatingHierarchy() && this->getParents()) \
    1413        this->getParents()->add(this->getIdentifier()); \
    1514    ClassIdentifier<ClassName>::addObject(this)
    1615
     16#define InternRegisterRootObject(ClassName) \
     17    if (Identifier::isCreatingHierarchy() && !this->getParents()) \
     18        this->setParents(new IdentifierList()); \
     19    InternRegisterObject(ClassName, true)
     20
    1721#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)
    2125#else
    22 #define registerRootObject(ClassName) \
    23     internRegisterRootObject(ClassName, false)
     26#define RegisterObject(ClassName) \
     27    InternRegisterObject(ClassName, false)
    2428#endif
    2529
    2630#if HIERARCHY_VERBOSE
    27 #define registerAbstractRootObject(ClassName) \
    28     std::cout << "*** Register abstract Root-Object: " << #ClassName << "\n"; \
    29     internRegisterRootObject(ClassName, true)
     31#define RegisterRootObject(ClassName) \
     32    std::cout << "*** Register Root-Object: " << #ClassName << "\n"; \
     33    InternRegisterRootObject(ClassName)
    3034#else
    31 #define registerAbstractRootObject(ClassName) \
    32     internRegisterRootObject(ClassName, true)
     35#define RegisterRootObject(ClassName) \
     36    InternRegisterRootObject(ClassName)
    3337#endif
    3438
    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() \
    6040    this->getIdentifier()->removeObject(this)
    6141
     
    6444
    6545#define CreateFactory(ClassName) \
    66     Identifier* global_##ClassName##_Identifier = ClassIdentifier<ClassName>::getIdentifier()
     46    bool bCreated##ClassName##Factory = ClassFactory<ClassName>::create()
    6747
    68 #define Factory(Name) \
    69     ClassFactory::fabricate(Name)
     48#define ID(Name) \
     49    Factory::getIdentifier(Name)
  • code/branches/objecthierarchie/src/Test.h

    r219 r244  
    99    class Interface1 : virtual public OrxonoxClass
    1010    {
    11         public:
    12             Interface1() { registerAbstractRootObject(Interface1); }
     11        protected:
     12            Interface1() { RegisterRootObject(Interface1); }
    1313    };
    1414
    1515    class Interface2 : virtual public OrxonoxClass
    1616    {
    17         public:
    18             Interface2() { registerAbstractRootObject(Interface2); }
     17        protected:
     18            Interface2() { RegisterRootObject(Interface2); }
    1919    };
    2020
     
    2222    {
    2323        public:
    24             A1() { registerObject(A1); }
     24            A1() { RegisterObject(A1); }
    2525    };
    2626
     
    2828    {
    2929        public:
    30             A2() { registerObject(A2); }
     30            A2() { RegisterObject(A2); }
    3131    };
    3232
     
    3434    {
    3535        public:
    36             A3() { registerObject(A3); }
     36            A3() { RegisterObject(A3); }
    3737    };
    3838
     
    4040    {
    4141        public:
    42             A1B1() { registerObject(A1B1); }
     42            A1B1() { RegisterObject(A1B1); }
    4343    };
    4444
     
    4646    {
    4747        public:
    48             A1B2() { registerObject(A1B2); }
     48            A1B2() { RegisterObject(A1B2); }
    4949    };
    5050
     
    5252    {
    5353        public:
    54             A2B1() { registerObject(A2B1); }
     54            A2B1() { RegisterObject(A2B1); }
    5555    };
    5656
     
    5858    {
    5959        public:
    60             A2B2() { registerObject(A2B2); }
     60            A2B2() { RegisterObject(A2B2); }
    6161    };
    6262
     
    6464    {
    6565        public:
    66             A3B1() { registerObject(A3B1); }
     66            A3B1() { RegisterObject(A3B1); }
    6767    };
    6868
     
    7070    {
    7171        public:
    72             A3B2() { registerObject(A3B2); }
     72            A3B2() { RegisterObject(A3B2); }
    7373    };
    7474
     
    7676    {
    7777        public:
    78             A1B1C1() { registerObject(A1B1C1); }
     78            A1B1C1() { RegisterObject(A1B1C1); }
    7979    };
    8080
     
    8282    {
    8383        public:
    84             A1B1C2() { registerObject(A1B1C2); }
     84            A1B1C2() { RegisterObject(A1B1C2); }
    8585    };
    8686
     
    8888    {
    8989        public:
    90             A1B2C1() { registerObject(A1B2C1); }
     90            A1B2C1() { RegisterObject(A1B2C1); }
    9191    };
    9292
     
    9494    {
    9595        public:
    96             A2B1C1() { registerObject(A2B1C1); }
     96            A2B1C1() { RegisterObject(A2B1C1); }
    9797    };
    9898
     
    100100    {
    101101        public:
    102             A2B2C1() { registerObject(A2B2C1); }
     102            A2B2C1() { RegisterObject(A2B2C1); }
    103103    };
    104104
     
    106106    {
    107107        public:
    108             A3B1C1() { registerObject(A3B1C1); }
     108            A3B1C1() { RegisterObject(A3B1C1); }
    109109    };
    110110
     
    112112    {
    113113        public:
    114             A3B1C2() { registerObject(A3B1C2); }
     114            A3B1C2() { RegisterObject(A3B1C2); }
    115115    };
    116116
     
    118118    {
    119119        public:
    120             A3B2C1() { registerObject(A3B2C1); }
     120            A3B2C1() { RegisterObject(A3B2C1); }
    121121    };
    122122
     
    124124    {
    125125        public:
    126             A3B2C2() { registerObject(A3B2C2); }
     126            A3B2C2() { RegisterObject(A3B2C2); }
    127127    };
    128128
  • code/branches/objecthierarchie/src/orxonox.cc

    r243 r244  
    102102        startRenderLoop();
    103103*/
     104
     105        #define testandcout(code) \
     106          std::cout << #code << " " << code << "\n"
     107
    104108/*
    105109        std::cout << "Test 1\n";
     
    176180        }
    177181*/
    178 
    179         #define testandcout(code) \
    180           std::cout << #code << " " << code << "\n"
    181 
    182182/*
    183183        std::cout << "\n";
     
    205205        testandcout(test5_03->isA(Class(BaseObject)));
    206206        testandcout(test5_03->isA(Class(Interface1)));
    207 
    208207
    209208        std::cout << "\n";
     
    346345        std::cout << "\n";
    347346        std::cout << "A2 parent of A2B1C1: " << Class(A2)->isParentOf(Class(A2B1C1)) << "\n";
    348 
    349 
     347*/
     348/*
    350349        std::cout << "Test 7\n";
    351350        std::cout << "1\n";
     
    427426
    428427        std::cout << "\n4\n";
    429         BaseObject* test9_06 = Factory("A2B2");
     428        BaseObject* test9_06 = ID("A2B2")->fabricate();
    430429        std::cout << test9_06->getIdentifier()->getName() << "\n";
    431430
     
    436435        delete test9_06;
    437436*/
    438 
     437/*
    439438        std::cout << "Test 10\n";
    440439        Identifier* test10_01 = Class(A1B2);
     
    449448        BaseObject* test10_07;
    450449        for (int i = 0; i < 10; i++)
    451             test10_07 = Factory("A1B1C1");
     450            test10_07 = ID("A1B1C1")->fabricate();
    452451
    453452        std::cout << "1\n";
     
    484483        for (int i = 0; i < 10; i++)
    485484        {
    486             test10_08 = Factory("A2B1C1");
     485            test10_08 = ID("A2B1C1")->fabricate();
    487486            test10_08->name_ = "A2B1C1#";
    488487            test10_08->name_ += ('0' + i);
     
    534533        for (Iterator<A2B1C1> it; it; --it)
    535534            std::cout << "Name: " << it->name_ << "\n";
    536 
     535*/
    537536
    538537      }
  • code/branches/objecthierarchie/src/test1.cc

    r218 r244  
    99    Test1::Test1()
    1010    {
    11         registerObject(Test1);
     11        RegisterObject(Test1);
    1212
    1313        this->usefullClass1_ = Class(Test1);
  • code/branches/objecthierarchie/src/test2.cc

    r218 r244  
    99    Test2::Test2()
    1010    {
    11         registerObject(Test2);
     11        RegisterObject(Test2);
    1212
    1313        this->usefullClass1_ = Class(Test1);
  • code/branches/objecthierarchie/src/test3.cc

    r218 r244  
    99    Test3::Test3()
    1010    {
    11         registerObject(Test3);
     11        RegisterObject(Test3);
    1212    }
    1313
Note: See TracChangeset for help on using the changeset viewer.