Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

ObjectListBaseElement now stores a pointer to the object list it belongs to. Removed ObjectListBase::Export. Iterator will now always use the list from the element.

  • Property svn:eol-style set to native
File size: 3.9 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        this->list_->removeElement(this);
46    }
47
48    /**
49        @brief Constructor: Sets default values.
50    */
51    ObjectListBase::ObjectListBase()
52    {
53        this->first_ = 0;
54        this->last_ = 0;
55    }
56
57    /**
58        @brief Destructor: Deletes all list-elements, but NOT THE OBJECTS.
59    */
60    ObjectListBase::~ObjectListBase()
61    {
62        ObjectListBaseElement* temp;
63        while (this->first_)
64        {
65            temp = this->first_->next_;
66            delete this->first_;
67            this->first_ = temp;
68        }
69    }
70
71    /**
72        @brief Increases all Iterators that currently point on the given element (because it gets removed).
73        @param object The object that gets removed
74    */
75    void ObjectListBase::notifyIterators(Listable* object) const
76    {
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);
81    }
82
83    /**
84        @brief Adds a new object to the end of the list.
85        @param element The element to add
86        @return The pointer to the new ObjectListBaseElement, needed by the MetaObjectList of the added object
87    */
88    ObjectListBaseElement* ObjectListBase::addElement(ObjectListBaseElement* element)
89    {
90        if (!this->last_)
91        {
92            // If the list is empty
93            this->last_ = element;
94            this->first_ = this->last_; // There's only one object in the list now
95        }
96        else
97        {
98            // If the list isn't empty
99            ObjectListBaseElement* temp = this->last_;
100            this->last_ = element;
101            this->last_->prev_ = temp;
102            temp->next_ = this->last_;
103        }
104
105        return this->last_;
106    }
107
108    void ObjectListBase::removeElement(ObjectListBaseElement* element)
109    {
110        orxout(verbose, context::object_list) << "Removing Object from " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
111        this->notifyIterators(element->objectBase_);
112
113        if (element->next_)
114            element->next_->prev_ = element->prev_;
115        else
116            this->last_ = element->prev_; // If there is no next_, we deleted the last object and have to update the last_ pointer of the list
117
118        if (element->prev_)
119            element->prev_->next_ = element->next_;
120        else
121            this->first_ = element->next_; // If there is no prev_, we deleted the first object and have to update the first_ pointer of the list
122    }
123}
Note: See TracBrowser for help on using the repository browser.