Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9598 for code/branches/core6


Ignore:
Timestamp:
Mar 29, 2013, 2:12:41 PM (11 years ago)
Author:
landauf
Message:

added common base template for Iterator and ObjectListIterator in order to avoid code duplication

Location:
code/branches/core6/src/libraries/core/object
Files:
1 added
2 edited

Legend:

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

    r9596 r9598  
    5757
    5858#include "ObjectListBase.h"
     59#include "IteratorBase.h"
    5960
    6061namespace orxonox
     
    6970    */
    7071    template <class T>
    71     class Iterator
     72    class Iterator : public IteratorBase<T, Iterator<T> >
    7273    {
    7374        public:
     
    7576                @brief Constructor: Sets the element, whereon the iterator points, to zero.
    7677            */
    77             inline Iterator()
    78             {
    79                 this->element_ = NULL;
    80                 this->list_ = NULL;
    81             }
     78            inline Iterator() : IteratorBase<T, Iterator<T> >(NULL) {}
     79
     80            /**
     81                @brief Constructor: Sets this element to a given element
     82                @param element The element
     83            */
     84            inline Iterator(ObjectListBaseElement* element) : IteratorBase<T, Iterator<T> >(element) {}
    8285
    8386            /**
     
    8588                @param other The other Iterator
    8689            */
    87             inline Iterator(const Iterator<T>& other)
    88             {
    89                 this->element_ = other.element_;
    90                 this->registerIterator();
    91             }
    92 
    93             /**
    94                 @brief Constructor: Sets this element to a given element
    95                 @param element The element
    96             */
    97             inline Iterator(ObjectListBaseElement* element)
    98             {
    99                 this->element_ = element;
    100                 this->registerIterator();
    101             }
    102 
    103             /**
    104                 @brief Constructor: Sets this element to the element an ObjectListIterator.
    105                 @param other The ObjectListIterator
    106             */
    107             template <class O>
    108             inline Iterator(const ObjectListIterator<O>& other)
    109             {
    110                 this->element_ = other.element_;
    111                 this->registerIterator();
    112             }
    113 
    114             /**
    115                 @brief Unregisters the Iterator from the ObjectList.
    116             */
    117             inline ~Iterator()
    118             {
    119                 this->unregisterIterator();
    120             }
    121 
    122             /**
    123                 @brief Assigns the element of another Iterator.
    124                 @param other The other Iterator
    125             */
    126             inline Iterator<T>& operator=(const Iterator<T>& other)
    127             {
    128                 this->unregisterIterator();
    129                 this->element_ = other.element_;
    130                 this->registerIterator();
    131 
    132                 return (*this);
    133             }
     90            inline Iterator(const Iterator<T>& other) : IteratorBase<T, Iterator<T> >(other) {}
    13491
    13592            /**
     
    13996            inline Iterator<T>& operator=(ObjectListBaseElement* element)
    14097            {
    141                 this->unregisterIterator();
    142                 this->element_ = element;
    143                 this->registerIterator();
    144 
     98                this->setElement(element);
    14599                return (*this);
    146             }
    147 
    148             /**
    149                 @brief Assigns the element of an ObjectListIterator.
    150                 @param other The ObjectListIterator
    151             */
    152             template <class O>
    153             inline Iterator<T>& operator=(const ObjectListIterator<O>& other)
    154             {
    155                 this->unregisterIterator();
    156                 this->element_ = other.element_;
    157                 this->registerIterator();
    158 
    159                 return (*this);
    160             }
    161 
    162             /**
    163                 @brief Overloading of the ++it operator: Iterator points to the next object in the list.
    164                 @return The Iterator itself
    165             */
    166             inline const Iterator<T>& operator++()
    167             {
    168                 this->element_ = this->element_->next_;
    169                 return *this;
    170             }
    171 
    172             /**
    173                 @brief Overloading of the it++ operator: Iterator points to the next object in the list.
    174                 @return The Iterator itself
    175             */
    176             inline Iterator<T> operator++(int)
    177             {
    178                 Iterator<T> copy = *this;
    179                 this->element_ = this->element_->next_;
    180                 return copy;
    181             }
    182 
    183             /**
    184                 @brief Overloading of the --it operator: Iterator points to the previous object in the list.
    185                 @return The Iterator itself
    186             */
    187             inline const Iterator<T>& operator--()
    188             {
    189                 this->element_ = this->element_->prev_;
    190                 return *this;
    191             }
    192 
    193             /**
    194                 @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
    195                 @return The Iterator itself
    196             */
    197             inline Iterator<T> operator--(int i)
    198             {
    199                 Iterator<T> copy = *this;
    200                 this->element_ = this->element_->prev_;
    201                 return copy;
    202100            }
    203101
     
    219117                return orxonox_cast<T*>(this->element_->objectBase_);
    220118            }
    221 
    222             /**
    223                 @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
    224                 @return True if the Iterator points to an existing object.
    225             */
    226             inline operator bool() const
    227             {
    228                 return (this->element_ != NULL);
    229             }
    230 
    231             /**
    232                 @brief Overloading of the == operator to compare with another Iterator.
    233                 @param compare The other Iterator
    234                 @return True if the iterators point to the same element
    235             */
    236             inline bool operator==(const Iterator<T>& compare) const
    237             {
    238                 return (this->element_ == compare.element_);
    239             }
    240 
    241             /**
    242                 @brief Overloading of the != operator to compare with another Iterator.
    243                 @param compare The other Iterator
    244                 @return True if the iterators point to different elements
    245             */
    246             inline bool operator!=(const Iterator<T>& compare) const
    247             {
    248                 return (this->element_ != compare.element_);
    249             }
    250 
    251             /**
    252                 @brief Increments the Iterator if it points at the given object.
    253                 @param object The object to compare with
    254             */
    255             inline void incrementIfEqual(Listable* object)
    256             {
    257                 if (this->element_ && this->element_->objectBase_ == object)
    258                     this->operator++();
    259             }
    260 
    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
    287119    };
    288120}
  • code/branches/core6/src/libraries/core/object/ObjectListIterator.h

    r9573 r9598  
    5858#include "core/class/Identifier.h"
    5959#include "ObjectList.h"
     60#include "IteratorBase.h"
    6061
    6162namespace orxonox
     
    6768    */
    6869    template <class T>
    69     class ObjectListIterator
     70    class ObjectListIterator : public IteratorBase<T, ObjectListIterator<T> >
    7071    {
    7172        template <class I>
     
    7677                @brief Constructor: Sets the element, whereon the ObjectListIterator points, to zero.
    7778            */
    78             inline ObjectListIterator()
    79             {
    80                 this->element_ = 0;
    81                 ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
    82             }
     79            inline ObjectListIterator() : IteratorBase<T, ObjectListIterator<T> >(NULL) {}
    8380
    8481            /**
     
    8683                @param element The element to start with
    8784            */
    88             inline ObjectListIterator(ObjectListElement<T>* element)
    89             {
    90                 this->element_ = element;
    91                 ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
    92             }
     85            inline ObjectListIterator(ObjectListElement<T>* element) : IteratorBase<T, ObjectListIterator<T> >(element) {}
    9386
    9487            /**
     
    9689                @param other The other ObjectListIterator
    9790            */
    98             inline ObjectListIterator(const ObjectListIterator<T>& other)
    99             {
    100                 this->element_ = other.element_;
    101                 ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
    102             }
    103 
    104             /**
    105                 @brief Unregisters the ObjectListIterator from the ObjectList.
    106             */
    107             inline ~ObjectListIterator()
    108             {
    109                 ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this);
    110             }
    111 
    112             /**
    113                 @brief Assigns an ObjectListElement.
    114                 @param element The ObjectListElement
    115             */
    116             inline ObjectListIterator<T>& operator=(ObjectListElement<T>* element)
    117             {
    118                 this->element_ = element;
    119                 return (*this);
    120             }
    121 
    122             /**
    123                 @brief Assigns the element of another ObjectListIterator.
    124                 @param other The other ObjectListIterator
    125             */
    126             inline ObjectListIterator<T>& operator=(const ObjectListIterator<T>& other)
    127             {
    128                 this->element_ = other.element_;
    129                 return (*this);
    130             }
    131 
    132             /**
    133                 @brief Overloading of the ++it operator: ObjectListIterator points to the next object in the list.
    134                 @return The ObjectListIterator itself
    135             */
    136             inline const ObjectListIterator<T>& operator++()
    137             {
    138                 this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
    139                 return *this;
    140             }
    141 
    142             /**
    143                 @brief Overloading of the it++ operator: ObjectListIterator points to the next object in the list.
    144                 @return The ObjectListIterator itself
    145             */
    146             inline ObjectListIterator<T> operator++(int)
    147             {
    148                 ObjectListIterator<T> copy = *this;
    149                 this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
    150                 return copy;
    151             }
    152 
    153             /**
    154                 @brief Overloading of the --it operator: ObjectListIterator points to the previous object in the list.
    155                 @return The ObjectListIterator itself
    156             */
    157             inline const ObjectListIterator<T>& operator--()
    158             {
    159                 this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
    160                 return *this;
    161             }
    162 
    163             /**
    164                 @brief Overloading of the it-- operator: ObjectListIterator points to the previous object in the list.
    165                 @return The ObjectListIterator itself
    166             */
    167             inline ObjectListIterator<T> operator--(int i)
    168             {
    169                 ObjectListIterator<T> copy = *this;
    170                 this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
    171                 return copy;
    172             }
     91            inline ObjectListIterator(const IteratorBase<T, ObjectListIterator<T> >& other) : IteratorBase<T, ObjectListIterator<T> >(other) {}
    17392
    17493            /**
     
    17897            inline T* operator*() const
    17998            {
    180                 return this->element_->object_;
     99                return static_cast<ObjectListElement<T>*>(this->element_)->object_;
    181100            }
    182101
     
    187106            inline T* operator->() const
    188107            {
    189                 return this->element_->object_;
     108                return static_cast<ObjectListElement<T>*>(this->element_)->object_;
    190109            }
    191 
    192             /**
    193                 @brief Overloading of the typecast-operator to bool: returns true if the ObjectListIterator points to an existing object.
    194                 @return True if the ObjectListIterator points to an existing object.
    195             */
    196             inline operator bool() const
    197             {
    198                 return (this->element_ != 0);
    199             }
    200 
    201             /**
    202                 @brief Overloading of the == operator to compare with another ObjectListIterator.
    203                 @param compare The other ObjectListIterator
    204                 @return True if the ObjectListIterator point to the same element
    205             */
    206             inline bool operator==(const ObjectListIterator<T>& compare) const
    207             {
    208                 return (this->element_ == compare.element_);
    209             }
    210 
    211             /**
    212                 @brief Overloading of the != operator to compare with another ObjectListIterator.
    213                 @param compare The other ObjectListIterator
    214                 @return True if the ObjectListIterator point to different elements
    215             */
    216             inline bool operator!=(const ObjectListIterator<T>& compare) const
    217             {
    218                 return (this->element_ != compare.element_);
    219             }
    220 
    221             /**
    222                 @brief Increments the ObjectListIterator if it points at the given object.
    223                 @param object The object to compare with
    224             */
    225             inline void incrementIfEqual(Listable* object)
    226             {
    227                 if (this->element_ && this->element_->objectBase_ == object)
    228                     this->operator++();
    229             }
    230 
    231         private:
    232             ObjectListElement<T>* element_;        //!< The element the iterator points at
    233110    };
    234111}
Note: See TracChangeset for help on using the changeset viewer.