Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

added proper interface (ObjectListElementRemovalListener) to notify iterators about removed elements

Location:
code/branches/core6/src/libraries/core/object
Files:
3 edited

Legend:

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

    r9598 r9599  
    4747    */
    4848    template <class T, class I>
    49     class IteratorBase
     49    class IteratorBase : public ObjectListElementRemovalListener
    5050    {
    5151        public:
     
    169169
    170170            /**
    171                 @brief Increments the Iterator if it points at the given object.
     171                @brief Increments the Iterator if it points at the given element.
    172172                @param object The object to compare with
    173173            */
    174             inline void incrementIfEqual(Listable* object)
    175             {
    176                 if (this->element_ && this->element_->objectBase_ == object)
     174            virtual void removedElement(ObjectListBaseElement* element)
     175            {
     176                if (this->element_ == element)
    177177                    this->operator++();
    178178            }
     
    194194                {
    195195                    this->list_ = this->element_->list_;
    196                     this->list_->registerIterator(this);
     196                    this->list_->registerRemovalListener(this);
    197197                }
    198198                else
     
    206206            {
    207207                if (this->list_)
    208                     this->list_->unregisterIterator(this);
     208                    this->list_->unregisterRemovalListener(this);
    209209            }
    210210
  • code/branches/core6/src/libraries/core/object/ObjectListBase.cc

    r9597 r9599  
    7070
    7171    /**
    72         @brief Increases all Iterators that currently point on the given element (because it gets removed).
    73         @param object The object that gets removed
     72        @brief Notifies all listeners that the given element is about to get removed.
     73        @param element The element that gets removed
     74        This is mainly used for iterators which point at the removed element
    7475    */
    75     void ObjectListBase::notifyIterators(Listable* object) const
     76    void ObjectListBase::notifyRemovalListeners(ObjectListBaseElement* element) const
    7677    {
    77         for (std::vector<void*>::const_iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
    78             ((Iterator<Listable>*)(*it))->incrementIfEqual(object);
    79         for (std::vector<void*>::const_iterator it = this->objectListIterators_.begin(); it != this->objectListIterators_.end(); ++it)
    80             ((ObjectListIterator<Listable>*)(*it))->incrementIfEqual(object);
     78        for (std::vector<ObjectListElementRemovalListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
     79            (*it)->removedElement(element);
    8180    }
    8281
     
    109108    {
    110109        orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
    111         this->notifyIterators(element->objectBase_);
     110        this->notifyRemovalListeners(element);
    112111
    113112        if (element->next_)
  • code/branches/core6/src/libraries/core/object/ObjectListBase.h

    r9597 r9599  
    7979
    8080
     81    // ########################################
     82    // ### ObjectListElementRemovalListener ###
     83    // ########################################
     84    /// Gets called by the object list if an element is removed
     85    class _CoreExport ObjectListElementRemovalListener
     86    {
     87        public:
     88            virtual ~ObjectListElementRemovalListener() {}
     89            virtual void removedElement(ObjectListBaseElement* element) = 0;
     90    };
     91
    8192    // ###############################
    8293    // ###     ObjectListBase      ###
     
    119130            inline ObjectListBaseElement* rend() { return 0; }
    120131
    121             inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
    122             inline void unregisterIterator(void* iterator)
     132            inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); }
     133            inline void unregisterRemovalListener(ObjectListElementRemovalListener* listener)
    123134            {
    124                 for (unsigned int i = 0; i < this->iterators_.size(); ++i)
     135                for (unsigned int i = 0; i < this->listeners_.size(); ++i)
    125136                {
    126                     if (iterators_[i] == iterator)
     137                    if (listeners_[i] == listener)
    127138                    {
    128                         iterators_.erase(iterators_.begin() + i);
     139                        listeners_.erase(listeners_.begin() + i);
    129140                        break;
    130141                    }
    131142                }
    132143            }
    133             inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
    134             inline void unregisterObjectListIterator(void* iterator)
    135             {
    136                 for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
    137                 {
    138                     if (objectListIterators_[i] == iterator)
    139                     {
    140                         objectListIterators_.erase(objectListIterators_.begin() + i);
    141                         break;
    142                     }
    143                 }
    144             }
    145             void notifyIterators(Listable* object) const;
    146144
    147145        private:
    148             ObjectListBaseElement* first_;           //!< The first element in the list
    149             ObjectListBaseElement* last_;            //!< The last element in the list
    150             std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
    151             std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
     146            void notifyRemovalListeners(ObjectListBaseElement* element) const;
     147
     148            ObjectListBaseElement* first_;                              //!< The first element in the list
     149            ObjectListBaseElement* last_;                               //!< The last element in the list
     150            std::vector<ObjectListElementRemovalListener*> listeners_;  //!< A list of Iterators pointing on an element in this list
    152151    };
    153152}
Note: See TracChangeset for help on using the changeset viewer.