Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/object/ObjectListBase.cc @ 9606

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

object lists are now stored in a Context object instead of Identifiers

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
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/**
30    @file
31    @brief Implementation of the ObjectListBase class.
32*/
33
34#include "ObjectListBase.h"
35
36#include <set>
37#include "Iterator.h"
38#include "Listable.h"
39#include "ObjectListIterator.h"
40
41namespace orxonox
42{
43    ObjectListBaseElement::~ObjectListBaseElement()
44    {
45        if (this->list_)
46            this->list_->removeElement(this);
47    }
48
49    /**
50        @brief Constructor: Sets default values.
51    */
52    ObjectListBase::ObjectListBase()
53    {
54        this->first_ = 0;
55        this->last_ = 0;
56        this->size_ = 0;
57    }
58
59    /**
60        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
61    */
62    ObjectListBase::~ObjectListBase()
63    {
64        while (this->first_)
65            delete this->first_;
66    }
67
68    /**
69        @brief Notifies all listeners that the given element is about to get removed.
70        @param element The element that gets removed
71        This is mainly used for iterators which point at the removed element
72    */
73    void ObjectListBase::notifyRemovalListeners(ObjectListBaseElement* element) const
74    {
75        for (std::vector<ObjectListElementRemovalListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
76            (*it)->removedElement(element);
77    }
78
79    /**
80        @brief Adds a new object to the end of the list.
81        @param element The element to add
82    */
83    void ObjectListBase::addElement(ObjectListBaseElement* element)
84    {
85        if (element->list_)
86        {
87            orxout(internal_error) << "Element is already registered in another list" << endl;
88            return;
89        }
90
91        if (element->objectBase_)
92            orxout(verbose, context::object_list) << "Added object to " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
93
94        if (!this->last_)
95        {
96            // If the list is empty
97            this->last_ = element;
98            this->first_ = element; // There's only one object in the list now
99        }
100        else
101        {
102            // If the list isn't empty
103            ObjectListBaseElement* temp = this->last_;
104            this->last_ = element;
105            element->prev_ = temp;
106            temp->next_ = element;
107        }
108
109        element->list_ = this;
110        ++this->size_;
111    }
112
113    /**
114     * @brief Removes the element from the list
115     */
116    void ObjectListBase::removeElement(ObjectListBaseElement* element)
117    {
118        if (element->list_ != this)
119        {
120            orxout(internal_error) << "Element is not registered in this list" << endl;
121            return;
122        }
123
124        if (element->objectBase_)
125            orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
126        this->notifyRemovalListeners(element);
127
128        if (element->next_)
129            element->next_->prev_ = element->prev_;
130        else
131            this->last_ = element->prev_; // If there is no next_, we deleted the last object and have to update the last_ pointer of the list
132
133        if (element->prev_)
134            element->prev_->next_ = element->next_;
135        else
136            this->first_ = element->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list
137
138        element->list_ = 0;
139        element->next_ = 0;
140        element->prev_ = 0;
141        --this->size_;
142    }
143}
Note: See TracBrowser for help on using the repository browser.