Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9596 for code/branches/core6


Ignore:
Timestamp:
Mar 29, 2013, 12:42:24 AM (11 years ago)
Author:
landauf
Message:

ObjectListBaseElement now stores a pointer to the object list it belongs to. Removed ObjectListBase::Export. Iterator will now always use the list from the element.

Location:
code/branches/core6/src/libraries/core/object
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core6/src/libraries/core/object/Iterator.h

    r9573 r9596  
    5656#include "core/CorePrereqs.h"
    5757
    58 #include "core/class/Identifier.h"
    5958#include "ObjectListBase.h"
    6059
     
    7877            inline Iterator()
    7978            {
    80                 this->element_ = 0;
    81                 this->list_ = 0;
    82             }
    83 
    84             /**
    85                 @brief Constructor: Sets this element to the exported element.
    86                 @param exp The exported element
    87             */
    88             inline Iterator(const ObjectListBase::Export& exp)
    89             {
    90                 this->element_ = exp.element_;
    91                 this->list_ = exp.list_;
    92                 this->list_->registerIterator(this);
     79                this->element_ = NULL;
     80                this->list_ = NULL;
    9381            }
    9482
     
    10088            {
    10189                this->element_ = other.element_;
    102                 this->list_ = other.list_;
    103                 this->list_->registerIterator(this);
     90                this->registerIterator();
    10491            }
    10592
     
    10895                @param element The element
    10996            */
    110             template <class O>
    111             inline Iterator(ObjectListElement<O>* element)
     97            inline Iterator(ObjectListBaseElement* element)
    11298            {
    11399                this->element_ = element;
    114                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    115                 this->list_->registerIterator(this);
     100                this->registerIterator();
    116101            }
    117102
     
    124109            {
    125110                this->element_ = other.element_;
    126                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    127                 this->list_->registerIterator(this);
     111                this->registerIterator();
    128112            }
    129113
     
    133117            inline ~Iterator()
    134118            {
    135                 this->list_->unregisterIterator(this);
    136             }
    137 
    138             /**
    139                 @brief Assigns an exported element.
    140                 @param exp The exported element
    141             */
    142             inline Iterator<T>& operator=(const ObjectListBase::Export& exp)
    143             {
    144                 if (this->list_)
    145                     this->list_->unregisterIterator(this);
    146 
    147                 this->element_ = exp.element_;
    148                 this->list_ = exp.list_;
    149                 this->list_->registerIterator(this);
    150 
    151                 return (*this);
     119                this->unregisterIterator();
    152120            }
    153121
     
    158126            inline Iterator<T>& operator=(const Iterator<T>& other)
    159127            {
    160                 if (this->list_)
    161                     this->list_->unregisterIterator(this);
    162 
    163                 this->element_ = other.element_;
    164                 this->list_ = other.list_;
    165                 this->list_->registerIterator(this);
     128                this->unregisterIterator();
     129                this->element_ = other.element_;
     130                this->registerIterator();
    166131
    167132                return (*this);
     
    172137                @param element The element
    173138            */
    174             template <class O>
    175             inline Iterator<T>& operator=(ObjectListElement<O>* element)
    176             {
    177                 if (this->list_)
    178                     this->list_->unregisterIterator(this);
    179 
     139            inline Iterator<T>& operator=(ObjectListBaseElement* element)
     140            {
     141                this->unregisterIterator();
    180142                this->element_ = element;
    181                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    182                 this->list_->registerIterator(this);
     143                this->registerIterator();
    183144
    184145                return (*this);
     
    192153            inline Iterator<T>& operator=(const ObjectListIterator<O>& other)
    193154            {
    194                 if (this->list_)
    195                     this->list_->unregisterIterator(this);
    196 
    197                 this->element_ = other.element_;
    198                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    199                 this->list_->registerIterator(this);
     155                this->unregisterIterator();
     156                this->element_ = other.element_;
     157                this->registerIterator();
    200158
    201159                return (*this);
     
    268226            inline operator bool() const
    269227            {
    270                 return (this->element_ != 0);
     228                return (this->element_ != NULL);
    271229            }
    272230
     
    301259            }
    302260
    303         protected:
    304             ObjectListBaseElement* element_;       //!< The element the Iterator points at
    305             ObjectListBase* list_;                 //!< The list wherein the element is
     261        private:
     262            /**
     263             * @brief Registers the Iterator at the list to which it belongs
     264             */
     265            inline void registerIterator()
     266            {
     267                if (this->element_)
     268                {
     269                    this->list_ = this->element_->list_;
     270                    this->list_->registerIterator(this);
     271                }
     272                else
     273                    this->list_ = NULL;
     274            }
     275
     276            /**
     277             * @brief Unregisters the Iterator from the list (if any)
     278             */
     279            inline void unregisterIterator()
     280            {
     281                if (this->list_)
     282                    this->list_->unregisterIterator(this);
     283            }
     284
     285            ObjectListBaseElement* element_;    //!< The element the Iterator points at
     286            ObjectListBase* list_;              //!< The list in which the Iterator registered itself
    306287    };
    307288}
  • code/branches/core6/src/libraries/core/object/MetaObjectList.cc

    r9593 r9596  
    4848    MetaObjectListElement::~MetaObjectListElement()
    4949    {
    50         this->list_->removeElement(this->element_);
    5150        delete this->element_;
    5251    }
  • code/branches/core6/src/libraries/core/object/ObjectList.h

    r9557 r9596  
    7272            {
    7373                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
    74                 return static_cast<ObjectListElement<T>*>(list->begin().element_);
     74                return static_cast<ObjectListElement<T>*>(list->begin());
    7575            }
    7676
     
    7979            {
    8080                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
    81                 return static_cast<ObjectListElement<T>*>(list->end().element_);
     81                return static_cast<ObjectListElement<T>*>(list->end());
    8282            }
    8383
     
    8686            {
    8787                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
    88                 return static_cast<ObjectListElement<T>*>(list->rbegin().element_);
     88                return static_cast<ObjectListElement<T>*>(list->rbegin());
    8989            }
    9090
     
    9393            {
    9494                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
    95                 return static_cast<ObjectListElement<T>*>(list->rend().element_);
     95                return static_cast<ObjectListElement<T>*>(list->rend());
    9696            }
    9797    };
  • code/branches/core6/src/libraries/core/object/ObjectListBase.cc

    r9593 r9596  
    4141namespace orxonox
    4242{
     43    ObjectListBaseElement::~ObjectListBaseElement()
     44    {
     45        this->list_->removeElement(this);
     46    }
     47
    4348    /**
    4449        @brief Constructor: Sets default values.
  • code/branches/core6/src/libraries/core/object/ObjectListBase.h

    r9593 r9596  
    5656                @param objectBase The object to store
    5757            */
    58             ObjectListBaseElement(Listable* objectBase) : next_(0), prev_(0), objectBase_(objectBase) {}
     58            ObjectListBaseElement(Listable* object, ObjectListBase* list) : next_(0), prev_(0), objectBase_(object), list_(list) {}
     59            ~ObjectListBaseElement();
    5960
    6061            ObjectListBaseElement* next_;       //!< The next element in the list
    6162            ObjectListBaseElement* prev_;       //!< The previous element in the list
    62             Listable* objectBase_;
     63            Listable* objectBase_;              //!< The object
     64            ObjectListBase* list_;              //!< The list
    6365    };
    6466
     
    7274    {
    7375        public:
    74             ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {}
     76            ObjectListElement(T* object, ObjectListBase* list) : ObjectListBaseElement(static_cast<Listable*>(object), list), object_(object) {}
    7577            T* object_;              //!< The object
    7678    };
     
    99101            template <class T>
    100102            inline ObjectListBaseElement* add(T* object)
    101                 { return this->addElement(new ObjectListElement<T>(object)); }
     103                { return this->addElement(new ObjectListElement<T>(object, this)); }
    102104
    103105            ObjectListBaseElement* addElement(ObjectListBaseElement* element);
    104106            void removeElement(ObjectListBaseElement* element);
    105107
    106             /// Helper struct, used to export an element and the list to an instance of Iterator.
    107             struct Export
    108             {
    109                 Export(ObjectListBase* list, ObjectListBaseElement* element) : list_(list), element_(element) {}
    110                 ObjectListBase* list_;
    111                 ObjectListBaseElement* element_;
    112             };
    113 
    114108            /// Returns a pointer to the first element in the list. Works only with Iterator.
    115             inline Export begin() { return ObjectListBase::Export(this, this->first_); }
     109            inline ObjectListBaseElement* begin() { return this->first_; }
    116110            /// Returns a pointer to the element after the last element in the list. Works only with Iterator.
    117             inline Export end() { return ObjectListBase::Export(this, 0); }
     111            inline ObjectListBaseElement* end() { return 0; }
    118112            /// Returns a pointer to the last element in the list. Works only with Iterator.
    119             inline Export rbegin() { return ObjectListBase::Export(this, this->last_); }
     113            inline ObjectListBaseElement* rbegin() { return this->last_; }
    120114            /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator.
    121             inline Export rend() { return ObjectListBase::Export(this, 0); }
     115            inline ObjectListBaseElement* rend() { return 0; }
    122116
    123117            inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
Note: See TracChangeset for help on using the changeset viewer.