Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/object/ObjectListBase.cc @ 9771

Last change on this file since 9771 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 4.7 KB
RevLine 
[1574]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
[2171]30    @file
[1574]31    @brief Implementation of the ObjectListBase class.
32*/
33
[3196]34#include "ObjectListBase.h"
35
[1574]36#include <set>
[1591]37#include "Iterator.h"
[9593]38#include "Listable.h"
[3196]39#include "ObjectListIterator.h"
[1574]40
41namespace orxonox
42{
[9667]43    // ###############################
44    // ###  ObjectListBaseElement  ###
45    // ###############################
46    void ObjectListBaseElement::removeFromList()
47    {
48        if (this->list_)
49            this->list_->removeElement(this);
50    }
51
52    // ###############################
53    // ###     ObjectListBase      ###
54    // ###############################
[1574]55    /**
56        @brief Constructor: Sets default values.
57    */
[9593]58    ObjectListBase::ObjectListBase()
[1574]59    {
60        this->first_ = 0;
61        this->last_ = 0;
[9667]62        this->size_ = 0;
[1574]63    }
64
65    /**
66        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
67    */
68    ObjectListBase::~ObjectListBase()
69    {
[9667]70        ObjectListBaseElement* current = this->first_;
71        while (current)
[1574]72        {
[9667]73            ObjectListBaseElement* next = current->next_;
74
75            current->list_ = 0;
76            current->next_ = 0;
77            current->prev_ = 0;
78
79            current = next;
[1574]80        }
81    }
82
83    /**
[9667]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
[1574]87    */
[9667]88    void ObjectListBase::notifyRemovalListeners(ObjectListBaseElement* element) const
[1574]89    {
[9667]90        for (std::vector<ObjectListElementRemovalListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
91            (*it)->removedElement(element);
[1574]92    }
93
94    /**
95        @brief Adds a new object to the end of the list.
[7401]96        @param element The element to add
[1574]97    */
[9667]98    void ObjectListBase::addElement(ObjectListBaseElement* element)
[1574]99    {
[9667]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
[1574]109        if (!this->last_)
110        {
111            // If the list is empty
[1591]112            this->last_ = element;
[9667]113            this->first_ = element; // There's only one object in the list now
[1574]114        }
115        else
116        {
117            // If the list isn't empty
118            ObjectListBaseElement* temp = this->last_;
[1591]119            this->last_ = element;
[9667]120            element->prev_ = temp;
121            temp->next_ = element;
[1574]122        }
123
[9667]124        element->list_ = this;
125        ++this->size_;
[1574]126    }
[9593]127
[9667]128    /**
129     * @brief Removes the element from the list
130     */
[9593]131    void ObjectListBase::removeElement(ObjectListBaseElement* element)
132    {
[9667]133        if (element->list_ != this)
134        {
135            orxout(internal_error) << "Element is not registered in this list" << endl;
136            return;
137        }
[9593]138
[9667]139        if (element->objectBase_)
140            orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
141        this->notifyRemovalListeners(element);
142
[9593]143        if (element->next_)
144            element->next_->prev_ = element->prev_;
145        else
146            this->last_ = element->prev_; // If there is no next_, we deleted the last object and have to update the last_ pointer of the list
147
148        if (element->prev_)
149            element->prev_->next_ = element->next_;
150        else
151            this->first_ = element->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list
[9667]152
153        element->list_ = 0;
154        element->next_ = 0;
155        element->prev_ = 0;
156        --this->size_;
[9593]157    }
[1574]158}
Note: See TracBrowser for help on using the repository browser.