Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 14, 2008, 6:23:52 PM (16 years ago)
Author:
landauf
Message:

fixed an interesting bug in ObjectList/MetaObjectList/Iterator: it's now possible to delete multiple objects in one function-call while iterating through the objects. (and it would have crashed with std::list as well, so no stupid comments please :D)

added a feature in Timer.cc, allowing timers to tick faster than orxonox. this is from core2-branch but got lost while merging.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/core/ObjectList.h

    r1062 r1063  
    3838#ifndef _ObjectList_H__
    3939#define _ObjectList_H__
     40
     41#include <set>
    4042
    4143#include "CorePrereqs.h"
     
    9092
    9193            ObjectListElement<T>* add(T* object);
    92 //            void remove(OrxonoxClass* object, bool bIterateForwards = true);
    9394
    9495            /** @brief Returns the first element in the list. @return The first element */
     
    104105                { return Iterator<T>(ClassManager<T>::getIdentifier()->getObjects()->last_); }
    105106
     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
    106113            ObjectListElement<T>* first_;       //!< The first element in the list
    107114            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
    108118    };
    109119
     
    134144
    135145    /**
     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    /**
    136158        @brief Adds a new object to the end of the list.
    137159        @param object The object to add
     
    158180        return this->last_;
    159181    }
    160 
    161 
    162 //    /**
    163 //        @brief Removes an object from the list.
    164 //        @param object The object to remove
    165 //        @param bIterateForwards If true: Start searching the object at the beginning of the list
    166 //    */
    167     /*
    168     template <class T>
    169     void ObjectList<T>::remove(OrxonoxClass* object, bool bIterateForwards)
    170     {
    171         if (!object || !this->first_ || !this->last_)
    172             return;
    173 
    174         // If there's only one object in the list, we have to set first_ and last_ to zero
    175         if (this->first_ == this->last_)
    176         {
    177             if (this->first_->object_ == object)
    178             {
    179                 delete this->first_;
    180                 this->first_ = 0;
    181                 this->last_ = 0;
    182             }
    183 
    184             return;
    185         }
    186 
    187         // Now we are sure we have more than one element in the list
    188         if (bIterateForwards)
    189         {
    190             // Start at the beginning of the list
    191 
    192             // Check if it's the first object
    193             if (this->first_->object_ == object)
    194             {
    195                 ObjectListElement<T>* temp = this->first_->next_;
    196                 delete this->first_;
    197                 this->first_ = temp;
    198                 this->first_->prev_ = 0;
    199 
    200                 return;
    201             }
    202 
    203             // Iterate through the whole list
    204             ObjectListElement<T>* temp = this->first_;
    205             while (temp->next_)
    206             {
    207                 if (temp->next_->object_ == object)
    208                 {
    209                     ObjectListElement<T>* temp2 = temp->next_->next_;
    210                     delete temp->next_;
    211                     temp->next_ = temp2;
    212                     if (temp2)
    213                         temp2->prev_ = temp;
    214                     else
    215                         this->last_ = temp; // If there is no next_, we deleted the last element and have to update the last_ pointer.
    216 
    217                     return;
    218                 }
    219 
    220                 temp = temp->next_;
    221             }
    222         }
    223         else
    224         {
    225             // Start at the end of the list
    226 
    227             // Check if it's the last object
    228             if (this->last_->object_ == object)
    229             {
    230                 ObjectListElement<T>* temp = this->last_->prev_;
    231                 delete this->last_;
    232                 this->last_ = temp;
    233                 this->last_->next_ = 0;
    234 
    235                 return;
    236             }
    237 
    238             // Iterate through the whole list
    239             ObjectListElement<T>* temp = this->last_;
    240             while (temp->prev_)
    241             {
    242                 if (temp->prev_->object_ == object)
    243                 {
    244                     ObjectListElement<T>* temp2 = temp->prev_->prev_;
    245                     delete temp->prev_;
    246                     temp->prev_ = temp2;
    247                     if (temp2)
    248                         temp2->next_ = temp;
    249                     else
    250                         this->first_ = temp; // If there is no prev_, we deleted the first element and have to update the first_ pointer.
    251 
    252                     return;
    253                 }
    254 
    255                 temp = temp->prev_;
    256             }
    257         }
    258     }
    259     */
    260182}
    261183
Note: See TracChangeset for help on using the changeset viewer.