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/ObjectList.h

    r1543 r1747  
    3131    @brief Definition and implementation of the ObjectList class.
    3232
    33     The ObjectList is a double-linked list, used by Identifiers to store all objects of a given class.
    34     Newly created objects are added through the RegisterObject-macro in its constructor.
     33    The ObjectList is a wrapper of an ObjectListBase of a given class.
    3534    Use Iterator<class> to iterate through all objects of the class.
    3635*/
     
    3938#define _ObjectList_H__
    4039
    41 #include <set>
    42 
    4340#include "CorePrereqs.h"
    4441
    45 #include "Iterator.h"
    4642#include "Identifier.h"
     43#include "ObjectListIterator.h"
    4744
    4845namespace orxonox
    4946{
    5047    // ###############################
    51     // ###    ObjectListElement    ###
    52     // ###############################
    53     //! The list-element of the ObjectList
    54     template <class T>
    55     class ObjectListElement
    56     {
    57         public:
    58             ObjectListElement(T* object);
    59 
    60             T* object_;                     //!< The object
    61             ObjectListElement* next_;       //!< The next element in the list
    62             ObjectListElement* prev_;       //!< The previous element in the list
    63     };
    64 
    65     /**
    66         @brief Constructor: Creates the list-element with an object.
    67         @param object The object to store
    68     */
    69     template <class T>
    70     ObjectListElement<T>::ObjectListElement(T* object)
    71     {
    72         this->object_ = object;
    73         this->next_ = 0;
    74         this->prev_ = 0;
    75     }
    76 
    77 
    78     // ###############################
    7948    // ###       ObjectList        ###
    8049    // ###############################
    81     //! The ObjectList contains all objects of a given class.
     50    //! The ObjectList contains all objects of the given class.
    8251    /**
    83         The ObjectList is used by Identifiers to store all objects of a given class.
    84         Use Iterator<class> to iterate through all objects in the list.
     52        Wraps the ObjectListBase of the corresponding Identifier.
     53        Use ObjectListIterator<class> to iterate through all objects in the list.
    8554    */
    8655    template <class T>
     
    8857    {
    8958        public:
    90             ObjectList();
    91             ~ObjectList();
     59            typedef ObjectListIterator<T> iterator;
    9260
    93             ObjectListElement<T>* add(T* object);
     61            /** @brief Returns an Iterator to the first element in the list. @return The Iterator */
     62            inline static ObjectListElement<T>* begin()
     63                { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->begin().element_); }
    9464
    95             /** @brief Returns the first element in the list. @return The first element */
    96             inline static Iterator<T> start()
    97                 { return Iterator<T>(ClassIdentifier<T>::getIdentifier()->getObjects()->first_); }
     65            /** @brief Returns an Iterator to the element after the last element in the list. @return The Iterator */
     66            inline static ObjectListElement<T>* end()
     67                { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->end().element_); }
    9868
    99             /** @brief Returns the first element in the list. @return The first element */
    100             inline static Iterator<T> begin()
    101                 { return Iterator<T>(ClassIdentifier<T>::getIdentifier()->getObjects()->first_); }
     69            /** @brief Returns an Iterator to the last element in the list. @return The Iterator */
     70            inline static ObjectListElement<T>* rbegin()
     71                { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->rbegin().element_); }
    10272
    103             /** @brief Returns the last element in the list. @return The last element */
    104             inline static Iterator<T> end()
    105                 { return Iterator<T>(ClassIdentifier<T>::getIdentifier()->getObjects()->last_); }
    106 
    107             inline void registerIterator(Iterator<T>* iterator)
    108                 { this->iterators_.insert(this->iterators_.end(), (void*)iterator); }
    109             inline void unregisterIterator(Iterator<T>* iterator)
    110                 { this->iterators_.erase((void*)iterator); }
    111             void notifyIterators(ObjectListElement<T>* element);
    112 
    113             ObjectListElement<T>* first_;       //!< The first element in the list
    114             ObjectListElement<T>* last_;        //!< The last element in the list
    115 
    116         private:
    117             std::set<void*> iterators_;  //!< A list of iterators pointing on an element in this list
     73            /** @brief Returns an Iterator to the element before the first element in the list. @return The Iterator */
     74            inline static ObjectListElement<T>* rend()
     75                { return ((ObjectListElement<T>*)ClassIdentifier<T>::getIdentifier()->getObjects()->rend().element_); }
    11876    };
    119 
    120     /**
    121         @brief Constructor: Sets default values.
    122     */
    123     template <class T>
    124     ObjectList<T>::ObjectList()
    125     {
    126         this->first_ = 0;
    127         this->last_ = 0;
    128     }
    129 
    130     /**
    131         @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
    132     */
    133     template <class T>
    134     ObjectList<T>::~ObjectList()
    135     {
    136         ObjectListElement<T>* temp;
    137         while (this->first_)
    138         {
    139             temp = this->first_->next_;
    140             delete this->first_;
    141             this->first_ = temp;
    142         }
    143     }
    144 
    145     /**
    146         @brief Increases all Iterators that currently point on the given element (because it gets removed).
    147         @param element The element that gets removed
    148     */
    149     template <class T>
    150     void ObjectList<T>::notifyIterators(ObjectListElement<T>* element)
    151     {
    152         for (std::set<void*>::iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
    153             if ((*(*((Iterator<T>*)(*it)))) == element->object_)
    154                 ++(*((Iterator<T>*)(*it)));
    155     }
    156 
    157     /**
    158         @brief Adds a new object to the end of the list.
    159         @param object The object to add
    160         @return The pointer to the new ObjectListElement, needed by the MetaObjectList of the added object
    161     */
    162     template <class T>
    163     ObjectListElement<T>* ObjectList<T>::add(T* object)
    164     {
    165         if (!this->last_)
    166         {
    167             // If the list is empty
    168             this->last_ = new ObjectListElement<T>(object);
    169             this->first_ = this->last_; // There's only one object in the list now
    170         }
    171         else
    172         {
    173             // If the list isn't empty
    174             ObjectListElement<T>* temp = this->last_;
    175             this->last_ = new ObjectListElement<T>(object);
    176             this->last_->prev_ = temp;
    177             temp->next_ = this->last_;
    178         }
    179 
    180         return this->last_;
    181     }
    18277}
    18378
Note: See TracChangeset for help on using the changeset viewer.