Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 218


Ignore:
Timestamp:
Nov 19, 2007, 5:57:02 AM (16 years ago)
Author:
landauf
Message:

added factory

Location:
code/branches/objecthierarchie/src
Files:
2 added
11 edited

Legend:

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

    r149 r218  
    33namespace orxonox
    44{
     5    CreateFactory(BaseObject);
     6
    57    BaseObject::BaseObject()
    68    {
  • code/branches/objecthierarchie/src/CMakeLists.txt

    r197 r218  
    22
    33# create a few variables to simplify life
    4 SET(SRC_FILES orxonox.cc IdentifierList.cc ObjectList.cc Identifier.cc ClassHierarchy.cc OrxonoxClass.cc BaseObject.cc test1.cc test2.cc test3.cc)
    5 SET(INC_FILES ClassHierarchy.h Identifier.h IdentifierList.h ObjectList.h OrxonoxClass.h BaseObject.h Test.h test1.h test2.h test3.h)
     4SET(SRC_FILES orxonox.cc IdentifierList.cc ObjectList.cc Identifier.cc Factory.cc ClassHierarchy.cc OrxonoxClass.cc BaseObject.cc test1.cc test2.cc test3.cc)
     5SET(INC_FILES IdentifierIncludes.h ClassHierarchy.h Identifier.h Factory.h IdentifierList.h ObjectList.h OrxonoxClass.h BaseObject.h Test.h test1.h test2.h test3.h)
    66
    77#Creates an executable
  • code/branches/objecthierarchie/src/ClassHierarchy.h

    r197 r218  
    1111// - ClassIdentifier
    1212// - BaseIdentifier
     13// - Factory
    1314
    1415// IN WORK:
    15 // - Factory
    1616
    1717// TO DO:
    1818// - iterate through lists
    19 // - searchtree for classname-strings
    2019
    2120
     
    3736        private:
    3837            ClassHierarchy();
     38            ClassHierarchy(const ClassHierarchy& hierarchy);
    3939            ~ClassHierarchy();
    4040            void startCreatingHierarchy() { this->hierarchyCreatingCounter_++; std::cout << "*** Increased Hierarchy-Creating-Counter to " << this->hierarchyCreatingCounter_ << "\n"; }
     
    9191        ClassIdentifier<ClassName>::getIdentifier()
    9292
    93     #define Factory(ClassName) \
    94         ClassIdentifier<ClassName>::create()
     93    #define CreateFactory(ClassName) \
     94        Identifier* global_##ClassName##_Identifier = ClassIdentifier<ClassName>::getIdentifier()
     95
     96    #define Factory(Name) \
     97        ClassFactory::fabricate(Name)
    9598}
    9699
  • code/branches/objecthierarchie/src/Identifier.h

    r197 r218  
    66#include "ObjectList.h"
    77#include "OrxonoxClass.h"
     8#include "Factory.h"
    89
    910namespace orxonox
     
    2122            void addObject(OrxonoxClass* object);
    2223            void removeObject(OrxonoxClass* object);
     24
     25            virtual OrxonoxClass* fabricate() {};
    2326
    2427            bool isA(Identifier* identifier);
     
    6164            static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass);
    6265            static ClassIdentifier<T>* getIdentifier();
     66            OrxonoxClass* fabricate();
     67            T* fabricateClass();
    6368
    6469        private:
     
    8388    {
    8489        this->pointer_ = NULL;
     90    }
     91
     92    template <class T>
     93    OrxonoxClass* ClassIdentifier<T>::fabricate()
     94    {
     95        return this->fabricateClass();
     96    }
     97
     98    template <class T>
     99    T* ClassIdentifier<T>::fabricateClass()
     100    {
     101        return new T;
    85102    }
    86103
     
    97114                pointer_->name_ = name;
    98115                pointer_->bIsAbstractClass_ = bIsAbstractClass;
     116
     117                ClassFactory::add(name, pointer_);
     118
    99119                pointer_->initialize(parents);
    100120            }
     
    124144    }
    125145
    126 
    127146    // ##### BaseIdentifier #####
    128147    template <class B>
    129     class BaseIdentifier// : public Identifier
     148    class BaseIdentifier
    130149    {
    131150        public:
    132151            BaseIdentifier();
    133152
    134             //template <class T>
    135             BaseIdentifier<B>& operator= (/*Class*/Identifier/*<T>*/* identifier)
     153            BaseIdentifier<B>& operator= (Identifier* identifier)
    136154            {
    137155                if (!identifier->isA(ClassIdentifier<B>::getIdentifier()))
     
    145163                return *this;
    146164            }
     165
     166            Identifier* operator* ()
     167            {
     168                return this->identifier_;
     169            }
     170
     171            Identifier* operator-> () const
     172            {
     173                return this->identifier_;
     174            }
     175
     176            B* fabricate()
     177            {
     178                OrxonoxClass* newObject = this->identifier_->fabricate();
     179                if (newObject)
     180                {
     181                    return dynamic_cast<B*>(newObject);
     182                }
     183                else
     184                {
     185                    if (this->identifier_)
     186                    {
     187                        std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n";
     188                        std::cout << "Error: Couldn't fabricate a new Object.\n";
     189                        std::cout << "Aborting...\n";
     190                    }
     191                    else
     192                    {
     193                        std::cout << "Error: Couldn't fabricate a new Object - Identifier is undefined.\n";
     194                        std::cout << "Aborting...\n";
     195                    }
     196
     197                    abort();
     198                }
     199            }
     200
    147201            inline Identifier* getIdentifier()
    148202                { return this->identifier_; }
  • code/branches/objecthierarchie/src/IdentifierIncludes.h

    r197 r218  
    11#include "ClassHierarchy.h"
    22#include "Identifier.h"
     3#include "Factory.h"
    34#include "IdentifierList.h"
    45#include "ObjectList.h"
  • code/branches/objecthierarchie/src/OrxonoxClass.h

    r197 r218  
    1111        public:
    1212            OrxonoxClass();
    13             ~OrxonoxClass();
     13            virtual ~OrxonoxClass();
    1414            Identifier* getIdentifier() { return this->identifier_; }
    1515            void setIdentifier(Identifier* identifier) { this->identifier_ = identifier; }
  • code/branches/objecthierarchie/src/Test.h

    r197 r218  
    22#define _Test_H__
    33
     4#include "BaseObject.h"
    45#include "IdentifierIncludes.h"
    56
     
    125126            A3B2C2() { registerObject(A3B2C2); }
    126127    };
     128
     129    CreateFactory(A1);
     130    CreateFactory(A2);
     131    CreateFactory(A3);
     132    CreateFactory(A1B1);
     133    CreateFactory(A1B2);
     134    CreateFactory(A2B1);
     135    CreateFactory(A2B2);
     136    CreateFactory(A3B1);
     137    CreateFactory(A3B2);
     138    CreateFactory(A1B1C1);
     139    CreateFactory(A1B1C2);
     140    CreateFactory(A1B2C1);
     141    CreateFactory(A2B1C1);
     142    CreateFactory(A2B2C1);
     143    CreateFactory(A3B1C1);
     144    CreateFactory(A3B1C2);
     145    CreateFactory(A3B2C1);
     146    CreateFactory(A3B2C2);
    127147}
    128148
  • code/branches/objecthierarchie/src/orxonox.cc

    r197 r218  
    3131#include <OgreCEGUIRenderer.h>
    3232
     33#include "IdentifierIncludes.h"
    3334#include "BaseObject.h"
    3435#include "Test.h"
     
    446447        test7_02 = Class(A1B1);
    447448
    448         std::cout << test7_01.getIdentifier()->getName() << "\n";
    449         std::cout << test7_02.getIdentifier()->getName() << "\n";
     449        std::cout << test7_01->getName() << "\n";
     450        std::cout << test7_02->getName() << "\n";
    450451*/
    451452/*
     
    461462        test7_05 = Class(A2);
    462463*/
    463 
     464/*
    464465        std::cout << "Test 8\n";
    465466
     
    498499        delete test8_02;
    499500        delete test8_03;
    500 
     501*/
     502
     503        std::cout << "Test 9\n";
     504        std::cout << "1\n";
     505        Identifier* test9_01 = Class(A3);
     506        OrxonoxClass* test9_02 = test9_01->fabricate();
     507        std::cout << test9_02->getIdentifier()->getName() << "\n";
     508
     509        std::cout << "\n2\n";
     510        OrxonoxClass* test9_03 = Class(A1B2)->fabricate();
     511        std::cout << test9_03->getIdentifier()->getName() << "\n";
     512
     513        std::cout << "\n3\n";
     514        BaseIdentifier<A1> test9_04;
     515        test9_04 = Class(A1B1C1);
     516        A1* test9_05 = test9_04.fabricate();
     517        std::cout << test9_05->getIdentifier()->getName() << "\n";
     518
     519        std::cout << "\n4\n";
     520        OrxonoxClass* test9_06 = Factory("A2B2");
     521        std::cout << test9_06->getIdentifier()->getName() << "\n";
     522
     523        std::cout << "\n5\n";
     524        delete test9_02;
     525        delete test9_03;
     526        delete test9_05;
     527        delete test9_06;
    501528
    502529      }
  • code/branches/objecthierarchie/src/test1.cc

    r197 r218  
    55namespace orxonox
    66{
     7    CreateFactory(Test1);
     8
    79    Test1::Test1()
    810    {
  • code/branches/objecthierarchie/src/test2.cc

    r197 r218  
    55namespace orxonox
    66{
     7    CreateFactory(Test2);
     8
    79    Test2::Test2()
    810    {
  • code/branches/objecthierarchie/src/test3.cc

    r197 r218  
    55namespace orxonox
    66{
     7    CreateFactory(Test3);
     8
    79    Test3::Test3()
    810    {
Note: See TracChangeset for help on using the changeset viewer.