Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/core/IdentifierList.cc @ 859

Last change on this file since 859 was 859, checked in by landauf, 16 years ago

more or less a copy of the trunk

File size: 4.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/*!
29    @file IdentifierList.cc
30    @brief Implementation of the IdentifierList class.
31*/
32
33#include "Identifier.h"
34#include "IdentifierList.h"
35
36namespace orxonox
37{
38    // ###############################
39    // ###     IdentifierList      ###
40    // ###############################
41    /**
42        @brief Constructor: Sets first_ to zero.
43    */
44    IdentifierList::IdentifierList()
45    {
46        this->first_ = 0;
47    }
48
49    /**
50        @brief Destructor: Deletes all elements in the list, but NOT THE IDENTIFIERS.
51    */
52    IdentifierList::~IdentifierList()
53    {
54        IdentifierListElement* temp;
55        while (this->first_)
56        {
57            temp = this->first_->next_;
58            delete this->first_;
59            this->first_ = temp;
60        }
61    }
62
63    /**
64        @brief Adds an Identifier to the list.
65        @param identifier The Identifier to add
66    */
67    void IdentifierList::add(const Identifier* identifier)
68    {
69        IdentifierListElement* temp = this->first_;
70        this->first_ = new IdentifierListElement(identifier);
71        this->first_->next_ = temp;
72    }
73
74    /**
75        @brief Removes an Identifier from the list.
76        @param identifier The Identifier to remove
77    */
78    void IdentifierList::remove(const Identifier* identifier)
79    {
80        if (!identifier)
81            return;
82
83        // Check if we have to delete the first element
84        if (this->first_->identifier_ == identifier)
85        {
86            IdentifierListElement* temp = this->first_->next_;
87            delete this->first_;
88            this->first_ = temp;
89
90            return;
91        }
92
93        // Iterate through the list
94        IdentifierListElement* temp = this->first_;
95        while (temp->next_)
96        {
97            if (temp->next_->identifier_ == identifier)
98            {
99                IdentifierListElement* temp2 = temp->next_->next_;
100                delete temp->next_;
101                temp->next_ = temp2;
102
103                return;
104            }
105
106            temp = temp->next_;
107        }
108    }
109
110    /**
111        @brief Checks if a given Identifier is in the list and returns true if yes.
112        @param identifier The Identifier to check
113        @return True if the Identifier is in the list
114    */
115    bool IdentifierList::isInList(const Identifier* identifier) const
116    {
117        IdentifierListElement* temp = this->first_;
118        while (temp)
119        {
120            if (temp->identifier_ == identifier)
121                return true;
122
123            temp = temp->next_;
124        }
125
126        return false;
127    }
128
129    /**
130        @returns a string, containing a list of the names of all Identifiers in the list.
131    */
132    std::string IdentifierList::toString() const
133    {
134        IdentifierListElement* temp = this->first_;
135        std::string output = "";
136
137        while (temp)
138        {
139            output += temp->identifier_->getName();
140            output += " ";
141
142            temp = temp->next_;
143        }
144
145        return output;
146    }
147
148
149    // ###############################
150    // ###  IdentifierListElement  ###
151    // ###############################
152    /**
153        @brief Constructor: Creates the list-element with a given identifier.
154        @param identifier The Identifier to store
155    */
156    IdentifierListElement::IdentifierListElement(const Identifier* identifier)
157    {
158        this->identifier_ = identifier;
159        this->next_ = 0;
160    }
161}
Note: See TracBrowser for help on using the repository browser.