Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/orxonox/core/Identifier.cc @ 945

Last change on this file since 945 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 8.7 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 Identifier.cc
30    @brief Implementation of the Identifier class.
31*/
32
33#include <ostream>
34
35#include "Identifier.h"
36#include "Factory.h"
37
38namespace orxonox
39{
40    // ###############################
41    // ###       Identifier        ###
42    // ###############################
43    int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero (this static member variable is ok: it's used in main(), not before)
44
45    /**
46        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
47    */
48    Identifier::Identifier()
49    {
50        this->bCreatedOneObject_ = false;
51        this->factory_ = 0;
52
53        this->children_ = new std::list<const Identifier*>();
54        this->directChildren_ = new std::list<const Identifier*>();
55
56        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
57        static unsigned int classIDcounter_s = 0;
58        this->classID_ = classIDcounter_s++;
59    }
60
61    /**
62        @brief Destructor: Deletes the list containing the children.
63    */
64    Identifier::~Identifier()
65    {
66        delete this->children_;
67        delete this->directChildren_;
68    }
69
70    /**
71        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
72        @param parents A list containing all parents
73    */
74    void Identifier::initialize(std::list<const Identifier*>* parents)
75    {
76        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
77        this->bCreatedOneObject_ = true;
78
79        if (parents)
80        {
81            this->parents_ = (*parents);
82            this->directParents_ = (*parents);
83
84            // Iterate through all parents
85            for (std::list<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
86            {
87                // Tell the parent we're one of it's children
88                (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this);
89
90                // Erase all parents of our parent from our direct-parent-list
91                for (std::list<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
92                {
93                    // Search for the parent's parent in our direct-parent-list
94                    for (std::list<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
95                    {
96                        if ((*it1) == (*it2))
97                        {
98                            // We've found a non-direct parent in our list: Erase it
99                            this->directParents_.erase(it2);
100                            break;
101                        }
102                    }
103                }
104            }
105
106            // Now iterate through all direct parents
107            for (std::list<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
108            {
109                // Tell the parent we're one of it's direct children
110                (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this);
111            }
112        }
113    }
114
115    /**
116        @brief Creates an object of the type the Identifier belongs to.
117        @return The new object
118    */
119    BaseObject* Identifier::fabricate()
120    {
121        if (this->factory_)
122        {
123            return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type.
124        }
125        else
126        {
127            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
128            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
129            COUT(1) << "Aborting..." << std::endl;
130            abort();
131            return NULL;
132        }
133    }
134
135    /**
136        @brief Sets the network ID to a new value and changes the entry in the Factory.
137        @param id The new network ID
138    */
139    void Identifier::setNetworkID(unsigned int id)
140    {
141        Factory::changeNetworkID(this, this->classID_, id);
142        this->classID_ = id;
143    }
144
145    /**
146        @brief Returns true, if the Identifier is at least of the given type.
147        @param identifier The identifier to compare with
148    */
149    bool Identifier::isA(const Identifier* identifier) const
150    {
151        return (identifier == this || this->identifierIsInList(identifier, this->parents_));
152    }
153
154    /**
155        @brief Returns true, if the Identifier is exactly of the given type.
156        @param identifier The identifier to compare with
157    */
158    bool Identifier::isExactlyA(const Identifier* identifier) const
159    {
160        return (identifier == this);
161    }
162
163    /**
164        @brief Returns true, if the assigned identifier is a child of the given identifier.
165        @param identifier The identifier to compare with
166    */
167    bool Identifier::isChildOf(const Identifier* identifier) const
168    {
169        return this->identifierIsInList(identifier, this->parents_);
170    }
171
172    /**
173        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
174        @param identifier The identifier to compare with
175    */
176    bool Identifier::isDirectChildOf(const Identifier* identifier) const
177    {
178        return this->identifierIsInList(identifier, this->directParents_);
179    }
180
181    /**
182        @brief Returns true, if the assigned identifier is a parent of the given identifier.
183        @param identifier The identifier to compare with
184    */
185    bool Identifier::isParentOf(const Identifier* identifier) const
186    {
187        return this->identifierIsInList(identifier, *this->children_);
188    }
189
190    /**
191        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
192        @param identifier The identifier to compare with
193    */
194    bool Identifier::isDirectParentOf(const Identifier* identifier) const
195    {
196        return this->identifierIsInList(identifier, *this->directChildren_);
197    }
198
199    /**
200        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
201        @param varname The name of the variable
202        @return The ConfigValueContainer
203    */
204    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
205    {
206        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
207        if (it != configValues_.end())
208            return ((*it).second);
209        else
210            return 0;
211    }
212
213    /**
214        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
215        @param varname The name of the variablee
216        @param container The container
217    */
218    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
219    {
220        this->configValues_[varname] = container;
221    }
222
223    /**
224        @brief Searches for a given identifier in a list and returns whether the identifier is in the list or not.
225        @param identifier The identifier to look for
226        @param list The list
227        @return True = the identifier is in the list
228    */
229    bool Identifier::identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list)
230    {
231        for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
232            if (identifier == (*it))
233                return true;
234
235        return false;
236    }
237
238    std::ostream& operator<<(std::ostream& out, const std::list<const Identifier*>& list)
239    {
240        for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
241            out << (*it)->getName() << " ";
242
243        return out;
244    }
245}
Note: See TracBrowser for help on using the repository browser.