Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 224


Ignore:
Timestamp:
Nov 20, 2007, 8:38:53 PM (16 years ago)
Author:
landauf
Message:
  • *cough* fixed another small bug in the object-list *cough*
  • made the object-list a template, to avoid a dynamic_cast in the Iterator
Location:
code/branches/objecthierarchie/src
Files:
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchie/src/CMakeLists.txt

    r221 r224  
    22
    33# create a few variables to simplify life
    4 SET(SRC_FILES orxonox.cc IdentifierList.cc ObjectList.cc Identifier.cc Factory.cc OrxonoxClass.cc BaseObject.cc test1.cc test2.cc test3.cc)
     4SET(SRC_FILES orxonox.cc IdentifierList.cc Identifier.cc Factory.cc OrxonoxClass.cc BaseObject.cc test1.cc test2.cc test3.cc)
    55SET(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)
    66
  • code/branches/objecthierarchie/src/Identifier.cc

    r221 r224  
    1111    {
    1212        this->bCreatedOneObject_ = false;
    13 //        this->directParents_ = new IdentifierList();
    14 //        this->allParents_ = new IdentifierList();
    15 //        this->directChildren_ = new IdentifierList();
    16 //        this->allChildren_ = new IdentifierList();
    17 //        this->objects_ = new ObjectList();
    1813    }
    1914
    2015    Identifier::~Identifier()
    2116    {
    22 //        delete this->directParents_;
    23 //        delete this->allParents_;
    24 //        delete this->directChildren_;
    25 //        delete this->allChildren_;
    26 //        delete this->objects_;
    2717        delete &this->name_;
    2818    }
     
    7666    }
    7767
    78     void Identifier::addObject(OrxonoxClass* object)
    79     {
    80         std::cout << "*** Added object to " << this->name_ << "-list.\n";
    81         this->objects_.add(object);
    82     }
    83 
    84     void Identifier::removeObject(OrxonoxClass* object)
    85     {
    86         bool bIterateForwards = !Identifier::isCreatingHierarchy();
    87 
    88         if (bIterateForwards)
    89             std::cout << "*** Removed object from " << this->name_ << "-list, iterating forwards.\n";
    90         else
    91             std::cout << "*** Removed object from " << this->name_ << "-list, iterating backwards.\n";
    92 
    93         this->objects_.remove(object, bIterateForwards);
    94 
    95         IdentifierListElement* temp = this->directParents_.first_;
    96         while (temp)
    97         {
    98             temp->identifier_->removeObject(object);
    99             temp = temp->next_;
    100         }
    101     }
    102 
    10368    bool Identifier::isA(Identifier* identifier)
    10469    {
  • code/branches/objecthierarchie/src/Identifier.h

    r221 r224  
    2626    class BaseObject;
    2727
    28     // ##### Identifier #####
     28    // ###############################
     29    // ###       Identifier        ###
     30    // ###############################
    2931    class Identifier
    3032    {
     
    3537        friend class BaseIdentifier;
    3638
    37         template <class T>
    38         friend class Iterator;
    39 
    4039        public:
    41             void addObject(OrxonoxClass* object);
    42             void removeObject(OrxonoxClass* object);
     40            virtual void removeObject(OrxonoxClass* object) {};
    4341
    4442            virtual BaseObject* fabricate() {};
     
    7371            IdentifierList allChildren_;
    7472
    75             ObjectList objects_;
    7673            std::string name_;
    7774
     
    8380
    8481
    85     // ##### ClassIdentifier #####
     82    // ###############################
     83    // ###     ClassIdentifier     ###
     84    // ###############################
    8685    template <class T>
    8786    class ClassIdentifier : public Identifier
    8887    {
     88        template <class U>
     89        friend class Iterator;
     90
    8991        public:
    9092            static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass);
     
    9294            BaseObject* fabricate();
    9395            T* fabricateClass();
     96            static void addObject(T* object);
     97            void removeObject(OrxonoxClass* object);
    9498
    9599        private:
     
    99103
    100104            static ClassIdentifier<T>* pointer_s;
    101 
     105            ObjectList<T> objects_s;
    102106    };
    103107
     
    182186    }
    183187
    184     // ##### BaseIdentifier #####
     188    template <class T>
     189    void ClassIdentifier<T>::addObject(T* object)
     190    {
     191        std::cout << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n";
     192        ClassIdentifier<T>::getIdentifier()->objects_s.add(object);
     193    }
     194
     195    template <class T>
     196    void ClassIdentifier<T>::removeObject(OrxonoxClass* object)
     197    {
     198        bool bIterateForwards = !Identifier::isCreatingHierarchy();
     199
     200        if (bIterateForwards)
     201            std::cout << "*** Removed object from " << this->name_ << "-list, iterating forwards.\n";
     202        else
     203            std::cout << "*** Removed object from " << this->name_ << "-list, iterating backwards.\n";
     204
     205        this->objects_s.remove(object, bIterateForwards);
     206
     207        IdentifierListElement* temp = this->directParents_.first_;
     208        while (temp)
     209        {
     210            temp->identifier_->removeObject(object);
     211            temp = temp->next_;
     212        }
     213    }
     214
     215
     216    // ###############################
     217    // ###     BaseIdentifier      ###
     218    // ###############################
    185219    template <class B>
    186220    class BaseIdentifier
  • code/branches/objecthierarchie/src/IdentifierIncludes.h

    r221 r224  
    1313    if (Identifier::isCreatingHierarchy() && this->getParents()) \
    1414        this->getParents()->add(this->getIdentifier()); \
    15     this->getIdentifier()->addObject(this)
     15    ClassIdentifier<ClassName>::addObject(this)
    1616
    1717#define registerRootObject(ClassName) \
     
    2727    if (Identifier::isCreatingHierarchy() && this->getParents()) \
    2828        this->getParents()->add(this->getIdentifier()); \
    29     this->getIdentifier()->addObject(this)
     29    ClassIdentifier<ClassName>::addObject(this)
    3030
    3131#define registerObject(ClassName) \
  • code/branches/objecthierarchie/src/Iterator.h

    r221 r224  
    1010            Iterator()
    1111            {
    12                 this->elementForwards_ = ClassIdentifier<T>::getIdentifier()->objects_.first_;
    13                 this->elementBackwards_ = ClassIdentifier<T>::getIdentifier()->objects_.last_;
     12                this->elementForwards_ = ClassIdentifier<T>::getIdentifier()->objects_s.first_;
     13                this->elementBackwards_ = ClassIdentifier<T>::getIdentifier()->objects_s.last_;
    1414                this->iteratingForwards_ = true;
    1515            }
     
    5151            {
    5252                if (this->iteratingForwards_)
    53                     return dynamic_cast<T*>(this->elementForwards_->object_);
     53                    return /*dynamic_cast<T*>*/(this->elementForwards_->object_);
    5454                else
    55                     return dynamic_cast<T*>(this->elementBackwards_->object_);
     55                    return /*dynamic_cast<T*>*/(this->elementBackwards_->object_);
    5656            }
    5757
     
    5959            {
    6060                if (this->iteratingForwards_)
    61                     return dynamic_cast<T*>(this->elementForwards_->object_);
     61                    return /*dynamic_cast<T*>*/(this->elementForwards_->object_);
    6262                else
    63                     return dynamic_cast<T*>(this->elementBackwards_->object_);
     63                    return /*dynamic_cast<T*>*/(this->elementBackwards_->object_);
    6464
    6565            }
     
    8686
    8787        private:
    88             ObjectListElement* elementForwards_;
    89             ObjectListElement* elementBackwards_;
     88            ObjectListElement<T>* elementForwards_;
     89            ObjectListElement<T>* elementBackwards_;
    9090            bool iteratingForwards_;
    9191    };
  • code/branches/objecthierarchie/src/ObjectList.h

    r221 r224  
    66    class OrxonoxClass;
    77
     8    // ###############################
     9    // ###    ObjectListElement    ###
     10    // ###############################
     11    template <class T>
    812    class ObjectListElement
    913    {
    1014        public:
    11             ObjectListElement(OrxonoxClass* object);
     15            ObjectListElement(T* object);
    1216            ~ObjectListElement();
    1317
    14             OrxonoxClass* object_;
     18            T* object_;
    1519            ObjectListElement* next_;
    1620            ObjectListElement* prev_;
    1721    };
    1822
     23    template <class T>
     24    ObjectListElement<T>::ObjectListElement(T* object)
     25    {
     26        this->object_ = object;
     27        this->next_ = 0;
     28        this->prev_ = 0;
     29    }
     30
     31    template <class T>
     32    ObjectListElement<T>::~ObjectListElement()
     33    {
     34    }
     35
     36
     37    // ###############################
     38    // ###       ObjectList        ###
     39    // ###############################
     40    template <class T>
    1941    class ObjectList
    2042    {
     
    2244            ObjectList();
    2345            ~ObjectList();
    24             void add(OrxonoxClass* object);
     46            void add(T* object);
    2547            void remove(OrxonoxClass* object, bool bIterateForwards = true);
    2648
    27             ObjectListElement* first_;
    28             ObjectListElement* last_;
     49            ObjectListElement<T>* first_;
     50            ObjectListElement<T>* last_;
    2951    };
     52
     53    template <class T>
     54    ObjectList<T>::ObjectList()
     55    {
     56        this->first_ = 0;
     57        this->last_ = 0;
     58    }
     59
     60    template <class T>
     61    ObjectList<T>::~ObjectList()
     62    {
     63        ObjectListElement<T>* temp;
     64        while (this->first_)
     65        {
     66            temp = this->first_->next_;
     67            delete this->first_;
     68            this->first_ = temp;
     69        }
     70    }
     71
     72    template <class T>
     73    void ObjectList<T>::add(T* object)
     74    {
     75        if (!this->last_)
     76        {
     77            this->last_ = new ObjectListElement<T>(object);
     78            this->first_ = this->last_;
     79        }
     80        else
     81        {
     82            ObjectListElement<T>* temp = this->last_;
     83            this->last_ = new ObjectListElement<T>(object);
     84            this->last_->prev_ = temp;
     85            temp->next_ = this->last_;
     86        }
     87    }
     88
     89    template <class T>
     90    void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
     91    {
     92        if (!object || !this->first_ || !this->last_)
     93            return;
     94
     95        if (this->first_ == this->last_)
     96        {
     97            if (this->first_->object_ == object)
     98            {
     99                delete this->first_;
     100                this->first_ = 0;
     101                this->last_ = 0;
     102            }
     103
     104            return;
     105        }
     106
     107        if (bIterateForwards)
     108        {
     109            if (this->first_->object_ == object)
     110            {
     111                ObjectListElement<T>* temp = this->first_->next_;
     112                delete this->first_;
     113                this->first_ = temp;
     114                this->first_->prev_ = 0;
     115
     116                return;
     117            }
     118
     119            ObjectListElement<T>* temp = this->first_;
     120            while (temp->next_)
     121            {
     122                if (temp->next_->object_ == object)
     123                {
     124                    ObjectListElement<T>* temp2 = temp->next_->next_;
     125                    delete temp->next_;
     126                    temp->next_ = temp2;
     127                    if (temp2)
     128                        temp2->prev_ = temp;
     129                    else
     130                        this->last_ = temp;
     131
     132                    return;
     133                }
     134
     135                temp = temp->next_;
     136            }
     137        }
     138        else
     139        {
     140            if (this->last_->object_ == object)
     141            {
     142                ObjectListElement<T>* temp = this->last_->prev_;
     143                delete this->last_;
     144                this->last_ = temp;
     145                this->last_->next_ = 0;
     146
     147                return;
     148            }
     149
     150            ObjectListElement<T>* temp = this->last_;
     151            while (temp->prev_)
     152            {
     153                if (temp->prev_->object_ == object)
     154                {
     155                    ObjectListElement<T>* temp2 = temp->prev_->prev_;
     156                    delete temp->prev_;
     157                    temp->prev_ = temp2;
     158                    if (temp2)
     159                        temp2->next_ = temp;
     160                    else
     161                        this->first_ = temp;
     162
     163                    return;
     164                }
     165
     166                temp = temp->prev_;
     167            }
     168        }
     169    }
    30170}
    31171
  • code/branches/objecthierarchie/src/orxonox.cc

    r221 r224  
    528528*/
    529529        std::cout << "Test 10\n";
    530         Identifier* test9_01 = Class(A1B2);
    531         Identifier* test9_02 = Class(A2);
    532         Identifier* test9_03 = Class(A3B1C1);
    533 
    534 
    535         BaseObject* test9_04 = test9_01->fabricate();
    536         BaseObject* test9_05 = test9_02->fabricate();
    537         BaseObject* test9_06 = test9_03->fabricate();
    538 
    539         BaseObject* test9_07;
     530        Identifier* test10_01 = Class(A1B2);
     531        Identifier* test10_02 = Class(A2);
     532        Identifier* test10_03 = Class(A3B1C1);
     533
     534
     535        BaseObject* test10_04 = test10_01->fabricate();
     536        BaseObject* test10_05 = test10_02->fabricate();
     537        BaseObject* test10_06 = test10_03->fabricate();
     538
     539        BaseObject* test10_07;
    540540        for (int i = 0; i < 10; i++)
    541             test9_07 = Factory("A1B1C1");
     541            test10_07 = Factory("A1B1C1");
    542542
    543543        std::cout << "1\n";
     
    569569
    570570        std::cout << "2\n";
    571         BaseObject* test9_08;
     571        BaseObject* test10_08;
     572        BaseObject* test10_09;
     573        BaseObject* test10_10;
    572574        for (int i = 0; i < 10; i++)
    573575        {
    574             test9_08 = Factory("A2B1C1");
    575             test9_08->name_ = "A2B1C1#";
    576             test9_08->name_ += ('0' + i);
     576            test10_08 = Factory("A2B1C1");
     577            test10_08->name_ = "A2B1C1#";
     578            test10_08->name_ += ('0' + i);
     579
     580            if (i == 0)
     581                test10_09 = test10_08;
     582
     583            if (i == 5)
     584                test10_10 = test10_08;
    577585        }
    578586
     
    585593
    586594        std::cout << "4\n";
     595        delete test10_08;
     596
     597        std::cout << "5\n";
     598        for (Iterator<A2B1C1> it; it; it++)
     599            std::cout << "Name: " << it->name_ << "\n";
     600
     601        std::cout << "6\n";
     602        for (Iterator<A2B1C1> it; it; it--)
     603            std::cout << "Name: " << it->name_ << "\n";
     604
     605        std::cout << "7\n";
     606        delete test10_09;
     607
     608        std::cout << "8\n";
     609        for (Iterator<A2B1C1> it; it; it++)
     610            std::cout << "Name: " << it->name_ << "\n";
     611
     612        std::cout << "9\n";
     613        for (Iterator<A2B1C1> it; it; it--)
     614            std::cout << "Name: " << it->name_ << "\n";
     615
     616        std::cout << "10\n";
     617        delete test10_10;
     618
     619        std::cout << "11\n";
     620        for (Iterator<A2B1C1> it; it; it++)
     621            std::cout << "Name: " << it->name_ << "\n";
     622
     623        std::cout << "12\n";
     624        for (Iterator<A2B1C1> it; it; it--)
     625            std::cout << "Name: " << it->name_ << "\n";
    587626
    588627      }
Note: See TracChangeset for help on using the changeset viewer.