Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 25, 2013, 9:08:42 PM (12 years ago)
Author:
landauf
Message:

merged core6 back to trunk

Location:
code/trunk
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/object/ObjectListBase.cc

    r9593 r9667  
    4141namespace orxonox
    4242{
     43    // ###############################
     44    // ###  ObjectListBaseElement  ###
     45    // ###############################
     46    void ObjectListBaseElement::removeFromList()
     47    {
     48        if (this->list_)
     49            this->list_->removeElement(this);
     50    }
     51
     52    // ###############################
     53    // ###     ObjectListBase      ###
     54    // ###############################
    4355    /**
    4456        @brief Constructor: Sets default values.
     
    4860        this->first_ = 0;
    4961        this->last_ = 0;
     62        this->size_ = 0;
    5063    }
    5164
     
    5568    ObjectListBase::~ObjectListBase()
    5669    {
    57         ObjectListBaseElement* temp;
    58         while (this->first_)
     70        ObjectListBaseElement* current = this->first_;
     71        while (current)
    5972        {
    60             temp = this->first_->next_;
    61             delete this->first_;
    62             this->first_ = temp;
     73            ObjectListBaseElement* next = current->next_;
     74
     75            current->list_ = 0;
     76            current->next_ = 0;
     77            current->prev_ = 0;
     78
     79            current = next;
    6380        }
    6481    }
    6582
    6683    /**
    67         @brief Increases all Iterators that currently point on the given element (because it gets removed).
    68         @param object The object that gets removed
     84        @brief Notifies all listeners that the given element is about to get removed.
     85        @param element The element that gets removed
     86        This is mainly used for iterators which point at the removed element
    6987    */
    70     void ObjectListBase::notifyIterators(Listable* object) const
     88    void ObjectListBase::notifyRemovalListeners(ObjectListBaseElement* element) const
    7189    {
    72         for (std::vector<void*>::const_iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
    73             ((Iterator<Listable>*)(*it))->incrementIfEqual(object);
    74         for (std::vector<void*>::const_iterator it = this->objectListIterators_.begin(); it != this->objectListIterators_.end(); ++it)
    75             ((ObjectListIterator<Listable>*)(*it))->incrementIfEqual(object);
     90        for (std::vector<ObjectListElementRemovalListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
     91            (*it)->removedElement(element);
    7692    }
    7793
     
    7995        @brief Adds a new object to the end of the list.
    8096        @param element The element to add
    81         @return The pointer to the new ObjectListBaseElement, needed by the MetaObjectList of the added object
    8297    */
    83     ObjectListBaseElement* ObjectListBase::addElement(ObjectListBaseElement* element)
     98    void ObjectListBase::addElement(ObjectListBaseElement* element)
    8499    {
     100        if (element->list_)
     101        {
     102            orxout(internal_error) << "Element is already registered in another list" << endl;
     103            return;
     104        }
     105
     106        if (element->objectBase_)
     107            orxout(verbose, context::object_list) << "Added object to " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
     108
    85109        if (!this->last_)
    86110        {
    87111            // If the list is empty
    88112            this->last_ = element;
    89             this->first_ = this->last_; // There's only one object in the list now
     113            this->first_ = element; // There's only one object in the list now
    90114        }
    91115        else
     
    94118            ObjectListBaseElement* temp = this->last_;
    95119            this->last_ = element;
    96             this->last_->prev_ = temp;
    97             temp->next_ = this->last_;
     120            element->prev_ = temp;
     121            temp->next_ = element;
    98122        }
    99123
    100         return this->last_;
     124        element->list_ = this;
     125        ++this->size_;
    101126    }
    102127
     128    /**
     129     * @brief Removes the element from the list
     130     */
    103131    void ObjectListBase::removeElement(ObjectListBaseElement* element)
    104132    {
    105         orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
    106         this->notifyIterators(element->objectBase_);
     133        if (element->list_ != this)
     134        {
     135            orxout(internal_error) << "Element is not registered in this list" << endl;
     136            return;
     137        }
     138
     139        if (element->objectBase_)
     140            orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
     141        this->notifyRemovalListeners(element);
    107142
    108143        if (element->next_)
     
    115150        else
    116151            this->first_ = element->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list
     152
     153        element->list_ = 0;
     154        element->next_ = 0;
     155        element->prev_ = 0;
     156        --this->size_;
    117157    }
    118158}
Note: See TracChangeset for help on using the changeset viewer.