Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 9, 2008, 4:35:38 AM (16 years ago)
Author:
landauf
Message:

big change in ObjectList: separated the class into a non-template base and a template wrapper for the base. this also changes the Iterator, there is now a non-template IteratorBase. this brings much more flexibility, like iterating through all objects of a given identifier without knowing the type. however this needs a dynamic_cast, which isn't quite optimal, but I think there are much worser things than that out there. ;)

there isn't much you have to know about this, except there is no more ObjectList<myClass>::start() function but a ObjectList<myClass>::begin() to be more STLish. another thing: ObjectList<myClass>::end() points now to the element _after_ the last element, so it's possible to iterate in a for-loop until (it != ObjectList<myClass>::end()). the reason is the same as above. however, (it) as a boolean still works perfectly fine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core3/src/core/Iterator.h

    r1543 r1574  
    3535
    3636    Usage:
    37     for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
     37    for (Iterator<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::end(); ++it)
    3838    {
    3939        it->someFunction(...);
    40         class* myObject = *it;
     40        myClass* myObject = *it;
    4141    }
    42 
    43     Warning: Don't delete objects directly through the iterator.
    4442*/
    4543
     
    4947#include "CorePrereqs.h"
    5048
    51 #include "ObjectList.h"
     49#include "IteratorBase.h"
    5250
    5351namespace orxonox
    5452{
    55     //! The iterator allows to iterate through an ObjectList of a given class.
     53    //! The Iterator allows to iterate through the ObjectList of a given class.
    5654    template <class T>
    57     class Iterator
     55    class Iterator : public IteratorBase
    5856    {
    5957        public:
     
    6159                @brief Constructor: Sets the element, whereon the iterator points, to zero.
    6260            */
    63             Iterator()
     61            inline Iterator()
    6462            {
    65                 this->element_ = 0;
    6663                ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
    6764            }
     
    7168                @param element The element to start with
    7269            */
    73             Iterator(ObjectListElement<T>* element)
     70            inline Iterator(ObjectListBaseElement* element) : IteratorBase((ObjectListBaseElement*)element)
    7471            {
    75                 this->element_ = element;
    7672                ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
    7773            }
     
    8076                @brief Unregisters the Iterator from the ObjectList.
    8177            */
    82             ~Iterator()
     78            inline ~Iterator()
    8379            {
    8480                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this);
    85             }
    86 
    87             /**
    88                 @brief Assigns an element to the iterator.
    89                 @param element The element
    90             */
    91             Iterator<T> operator=(ObjectListElement<T>* element)
    92             {
    93                 this->element_ = element;
    9481            }
    9582
     
    9885                @return The Iterator itself
    9986            */
    100             Iterator<T> operator++()
     87            inline Iterator<T> operator++()
    10188            {
    102                 if (this->element_)
    103                     this->element_ = this->element_->next_;
     89                IteratorBase::operator++();
    10490                return *this;
    10591            }
     
    10995                @return The Iterator itself
    11096            */
    111             Iterator<T> operator++(int i)
     97            inline Iterator<T> operator++(int i)
    11298            {
    11399                Iterator<T> copy = *this;
    114                 if (this->element_)
    115                     this->element_ = this->element_->next_;
     100                IteratorBase::operator++();
    116101                return copy;
    117102            }
     
    121106                @return The Iterator itself
    122107            */
    123             Iterator<T> operator--()
     108            inline Iterator<T> operator--()
    124109            {
    125                 if (this->element_)
    126                     this->element_ = this->element_->prev_;
     110                IteratorBase::operator--();
    127111                return *this;
    128112            }
     
    132116                @return The Iterator itself
    133117            */
    134             Iterator<T> operator--(int i)
     118            inline Iterator<T> operator--(int i)
    135119            {
    136120                Iterator<T> copy = *this;
    137                 if (this->element_)
    138                     this->element_ = this->element_->prev_;
     121                IteratorBase::operator--();
    139122                return copy;
    140123            }
     
    144127                @return The object the Iterator points at
    145128            */
    146             T* operator*()
     129            inline T* operator*()
    147130            {
    148                 if (this->element_)
    149                     return this->element_->object_;
    150                 else
    151                     return 0;
     131                return dynamic_cast<T*>(IteratorBase::operator*());
    152132            }
    153133
     
    156136                @return The object the Iterator points at
    157137            */
    158             T* operator->() const
     138            inline T* operator->() const
    159139            {
    160                 if (this->element_)
    161                     return this->element_->object_;
    162                 else
    163                     return 0;
    164 
     140                return dynamic_cast<T*>(IteratorBase::operator->());
    165141            }
    166142
    167143            /**
    168                 @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
    169                 @return True if the iterator points to an existing object.
     144                @brief Overloading of the == operator to compare with another Iterator.
     145                @param compare The other Iterator
     146                @return True if the iterators point to the same element
    170147            */
    171             operator bool()
     148            inline bool operator==(const Iterator<T>& compare)
    172149            {
    173                 return (this->element_ != 0);
     150                return (this->element_ == compare.element_);
    174151            }
    175152
    176153            /**
    177                 @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
    178                 @param compare The integer (must be zero, everything else makes no sense).
    179                 @return True if the iterator points to an existing object.
     154                @brief Overloading of the != operator to compare with another Iterator.
     155                @param compare The other Iterator
     156                @return True if the iterators point to different elements
    180157            */
    181             bool operator!=(ObjectListElement<T>* compare)
     158            inline bool operator!=(const Iterator<T>& compare)
    182159            {
    183                 return (this->element_ != compare);
     160                return (this->element_ != compare.element_);
    184161            }
    185 
    186         private:
    187             ObjectListElement<T>* element_;     //!< The element the Iterator points at
    188162    };
    189163}
    190164
     165// Include ObjectList.h so the user only has to include one file: Iterator.h
     166#include "ObjectList.h"
     167
    191168#endif /* _Iterator_H__ */
Note: See TracChangeset for help on using the changeset viewer.