Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 9, 2008, 4:25:52 AM (16 years ago)
Author:
landauf
Message:

merged core3 back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/Iterator.h

    r1625 r1747  
    3131    @brief Definition and implementation of the Iterator class.
    3232
    33     The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type.
    34     This is the only way to access the objects stored in an ObjectList.
     33    The Iterator of a given class allows to iterate through an ObjectList. Objects in
     34    this list are casted to the template argument of the Iterator.
    3535
    3636    Usage:
    37     for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
     37    for (Iterator<myClass> it = anyidentifier->getObjects().begin(); it != anyidentifier->getObjects().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 "ObjectListBase.h"
     50#include "ObjectListIterator.h"
     51#include "OrxonoxClass.h"
    5252
    5353namespace orxonox
    5454{
    55     //! The iterator allows to iterate through an ObjectList of a given class.
    56     template <class T>
     55    //! The Iterator allows to iterate through a given ObjectList
     56    template <class T = OrxonoxClass>
    5757    class Iterator
    5858    {
     
    6161                @brief Constructor: Sets the element, whereon the iterator points, to zero.
    6262            */
    63             Iterator()
     63            inline Iterator()
    6464            {
    6565                this->element_ = 0;
    66                 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
    67             }
    68 
    69             /**
    70                 @brief Constructor: Sets the element, whereon the iterator points, to a given element.
    71                 @param element The element to start with
    72             */
    73             Iterator(ObjectListElement<T>* element)
    74             {
    75                 this->element_ = element;
    76                 ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
     66                this->list_ = 0;
     67            }
     68
     69            /**
     70                @brief Constructor: Sets this element to the exported element.
     71                @param exp The exported element
     72            */
     73            inline Iterator(const ObjectListBase::Export& exp)
     74            {
     75                this->element_ = exp.element_;
     76                this->list_ = exp.list_;
     77                this->iterator_ = this->list_->registerIterator(this);
     78            }
     79
     80            /**
     81                @brief Constructor: Sets this element to the element of another Iterator.
     82                @param other The other Iterator
     83            */
     84            inline Iterator(const Iterator<T>& other)
     85            {
     86                this->element_ = other.element_;
     87                this->list_ = other.list_;
     88                this->iterator_ = this->list_->registerIterator(this);
     89            }
     90
     91            /**
     92                @brief Constructor: Sets this element to a given element
     93                @param element The element
     94            */
     95            template <class O>
     96            inline Iterator(ObjectListElement<O>* element)
     97            {
     98                this->element_ = (element) ? (ObjectListBaseElement*)element : 0;
     99                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
     100                this->iterator_ = this->list_->registerIterator(this);
     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_) ? (ObjectListBaseElement*)other.element_ : 0;
     111                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
     112                this->iterator_ = this->list_->registerIterator(this);
    77113            }
    78114
     
    80116                @brief Unregisters the Iterator from the ObjectList.
    81117            */
    82             ~Iterator()
    83             {
    84                 ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this);
    85             }
    86 
    87             /**
    88                 @brief Assigns an element to the iterator.
     118            inline ~Iterator()
     119            {
     120                this->list_->unregisterIterator(this->iterator_);
     121            }
     122
     123            /**
     124                @brief Assigns an exported element.
     125                @param exp The exported element
     126            */
     127            inline const Iterator<T>& operator=(const ObjectListBase::Export& exp)
     128            {
     129                if (this->list_)
     130                    this->list_->unregisterIterator(this->iterator_);
     131
     132                this->element_ = exp.element_;
     133                this->list_ = exp.list_;
     134                this->iterator_ = this->list_->registerIterator(this);
     135
     136                return (*this);
     137            }
     138
     139            /**
     140                @brief Assigns the element of another Iterator.
     141                @param other The other Iterator
     142            */
     143            inline const Iterator<T>& operator=(const Iterator<T>& other)
     144            {
     145                if (this->list_)
     146                    this->list_->unregisterIterator(this->iterator_);
     147
     148                this->element_ = other.element_;
     149                this->list_ = other.list_;
     150                this->iterator_ = this->list_->registerIterator(this);
     151
     152                return (*this);
     153            }
     154
     155            /**
     156                @brief Assigns a given element.
    89157                @param element The element
    90158            */
    91             Iterator<T> operator=(ObjectListElement<T>* element)
    92             {
    93                 this->element_ = element;
     159            template <class O>
     160            inline const Iterator<T>& operator=(ObjectListElement<O>* element)
     161            {
     162                if (this->list_)
     163                    this->list_->unregisterIterator(this->iterator_);
     164
     165                this->element_ = (element) ? (ObjectListBaseElement*)element : 0;
     166                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
     167                this->iterator_ = this->list_->registerIterator(this);
     168
     169                return (*this);
    94170                return *this;
     171            }
     172
     173            /**
     174                @brief Assigns the element of an ObjectListIterator.
     175                @param other The ObjectListIterator
     176            */
     177            template <class O>
     178            inline const Iterator<T>& operator=(const ObjectListIterator<O>& other)
     179            {
     180                if (this->list_)
     181                    this->list_->unregisterIterator(this->iterator_);
     182
     183                this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0;
     184                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
     185                this->iterator_ = this->list_->registerIterator(this);
     186
     187                return (*this);
    95188            }
    96189
     
    99192                @return The Iterator itself
    100193            */
    101             Iterator<T> operator++()
     194            inline const Iterator<T>& operator++()
    102195            {
    103196                if (this->element_)
     
    110203                @return The Iterator itself
    111204            */
    112             Iterator<T> operator++(int i)
     205            inline Iterator<T> operator++(int i)
    113206            {
    114207                Iterator<T> copy = *this;
     
    122215                @return The Iterator itself
    123216            */
    124             Iterator<T> operator--()
     217            inline const Iterator<T>& operator--()
    125218            {
    126219                if (this->element_)
     
    133226                @return The Iterator itself
    134227            */
    135             Iterator<T> operator--(int i)
     228            inline Iterator<T> operator--(int i)
    136229            {
    137230                Iterator<T> copy = *this;
     
    145238                @return The object the Iterator points at
    146239            */
    147             T* operator*()
    148             {
    149                 if (this->element_)
    150                     return this->element_->object_;
     240            inline T* operator*() const
     241            {
     242                if (this->element_)
     243                    return dynamic_cast<T*>(this->element_->objectBase_);
    151244                else
    152245                    return 0;
     
    157250                @return The object the Iterator points at
    158251            */
    159             T* operator->() const
    160             {
    161                 if (this->element_)
    162                     return this->element_->object_;
     252            inline T* operator->() const
     253            {
     254                if (this->element_)
     255                    return dynamic_cast<T*>(this->element_->objectBase_);
    163256                else
    164257                    return 0;
    165 
    166258            }
    167259
    168260            /**
    169261                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
    170                 @return True if the iterator points to an existing object.
    171             */
    172             operator bool()
     262                @return True if the Iterator points to an existing object.
     263            */
     264            inline operator bool() const
    173265            {
    174266                return (this->element_ != 0);
     
    176268
    177269            /**
    178                 @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
    179                 @param compare The integer (must be zero, everything else makes no sense).
    180                 @return True if the iterator points to an existing object.
    181             */
    182             bool operator!=(ObjectListElement<T>* compare)
    183             {
    184                 return (this->element_ != compare);
    185             }
    186 
    187         private:
    188             ObjectListElement<T>* element_;     //!< The element the Iterator points at
     270                @brief Overloading of the == operator to compare with another Iterator.
     271                @param compare The other Iterator
     272                @return True if the iterators point to the same element
     273            */
     274            inline bool operator==(const Iterator<T>& compare) const
     275            {
     276                return (this->element_ == compare.element_);
     277            }
     278
     279            /**
     280                @brief Overloading of the != operator to compare with another Iterator.
     281                @param compare The other Iterator
     282                @return True if the iterators point to different elements
     283            */
     284            inline bool operator!=(const Iterator<T>& compare) const
     285            {
     286                return (this->element_ != compare.element_);
     287            }
     288
     289            /**
     290                @brief Increments the Iterator if it points at the given object.
     291                @param object The object to compare with
     292            */
     293            inline void incrementIfEqual(OrxonoxClass* object)
     294            {
     295                if (this->element_ && this->element_->objectBase_ == object)
     296                    this->operator++();
     297            }
     298
     299        protected:
     300            ObjectListBaseElement* element_;       //!< The element the Iterator points at
     301            ObjectListBase* list_;                 //!< The list wherein the element is
     302            std::list<void*>::iterator iterator_;  //!< The iterator in the notifying list of the ObjectList
    189303    };
     304
     305    typedef Iterator<OrxonoxClass> BaseIterator;
    190306}
    191307
     308// Include ObjectList.h so the user only has to include one file: Iterator.h
     309#include "ObjectList.h"
     310
    192311#endif /* _Iterator_H__ */
Note: See TracChangeset for help on using the changeset viewer.