Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/class/Identifier.cc @ 9641

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

removed mutual friend declarations between Identifier and IdentifierManager

  • Property svn:eol-style set to native
File size: 13.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 Identifier class.
32*/
33
34#include "Identifier.h"
35
36#include <ostream>
37
38#include "util/StringUtils.h"
39#include "core/config/ConfigValueContainer.h"
40#include "core/XMLPort.h"
41#include "core/object/ClassFactory.h"
42
43namespace orxonox
44{
45    // ###############################
46    // ###       Identifier        ###
47    // ###############################
48    /**
49        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
50    */
51    Identifier::Identifier()
52        : classID_(IdentifierManager::getInstance().getUniqueClassId())
53    {
54        this->bCreatedOneObject_ = false;
55        this->bSetName_ = false;
56        this->factory_ = 0;
57        this->bLoadable_ = false;
58
59        this->bHasConfigValues_ = false;
60
61        // Default network ID is the class ID
62        this->networkID_ = this->classID_;
63    }
64
65    /**
66        @brief Destructor: Deletes the list containing the children.
67    */
68    Identifier::~Identifier()
69    {
70        if (this->factory_)
71            delete this->factory_;
72
73        for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it)
74            delete (it->second);
75        for (std::map<std::string, XMLPortParamContainer*>::iterator it = this->xmlportParamContainers_.begin(); it != this->xmlportParamContainers_.end(); ++it)
76            delete (it->second);
77        for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
78            delete (it->second);
79    }
80
81    /**
82        @brief Registers a class, which means that the name and the parents get stored.
83        @param parents A list, containing the Identifiers of all parents of the class
84        @param bRootClass True if the class is either an Interface or the BaseObject itself
85    */
86    void Identifier::initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass)
87    {
88        // Check if at least one object of the given type was created
89        if (!this->bCreatedOneObject_ && IdentifierManager::getInstance().isCreatingHierarchy())
90        {
91            // If no: We have to store the information and initialize the Identifier
92            orxout(verbose, context::identifier) << "Register Class in ClassIdentifier<" << this->getName() << ">-Singleton -> Initialize Singleton." << endl;
93            if (bRootClass)
94                this->initialize(0); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.
95            else
96                this->initialize(parents);
97        }
98    }
99
100    /**
101        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
102        @param parents A list containing all parents
103    */
104    void Identifier::initialize(std::set<const Identifier*>* parents)
105    {
106        orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << this->name_ << ">-Singleton." << endl;
107        this->bCreatedOneObject_ = true;
108
109        if (parents)
110        {
111            this->parents_ = (*parents);
112            this->directParents_ = (*parents);
113
114            // Iterate through all parents
115            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
116            {
117                // Tell the parent we're one of it's children
118                (*it)->children_.insert((*it)->children_.end(), this);
119
120                // Erase all parents of our parent from our direct-parent-list
121                for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
122                {
123                    // Search for the parent's parent in our direct-parent-list
124                    for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
125                    {
126                        if ((*it1) == (*it2))
127                        {
128                            // We've found a non-direct parent in our list: Erase it
129                            this->directParents_.erase(it2);
130                            break;
131                        }
132                    }
133                }
134            }
135
136            // Now iterate through all direct parents
137            for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
138            {
139                // Tell the parent we're one of it's direct children
140                (*it)->directChildren_.insert((*it)->directChildren_.end(), this);
141
142                // Create the super-function dependencies
143                (*it)->createSuperFunctionCaller();
144            }
145        }
146    }
147
148    /**
149        @brief Sets the name of the class.
150    */
151    void Identifier::setName(const std::string& name)
152    {
153        if (!this->bSetName_)
154        {
155            this->name_ = name;
156            this->bSetName_ = true;
157            IdentifierManager::getInstance().registerIdentifier(this);
158        }
159    }
160
161    void Identifier::setFactory(Factory* factory)
162    {
163        if (this->factory_)
164            delete this->factory_;
165
166        this->factory_ = factory;
167    }
168
169
170    /**
171        @brief Creates an object of the type the Identifier belongs to.
172        @return The new object
173    */
174    Identifiable* Identifier::fabricate(Context* context)
175    {
176        if (this->factory_)
177        {
178            return this->factory_->fabricate(context);
179        }
180        else
181        {
182            orxout(user_error) << "An error occurred in Identifier.cc:" << endl;
183            orxout(user_error) << "Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << endl;
184            orxout(user_error) << "Aborting..." << endl;
185            abort();
186            return 0;
187        }
188    }
189
190    /**
191        @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map.
192    */
193    void Identifier::setNetworkID(uint32_t id)
194    {
195        this->networkID_ = id;
196        IdentifierManager::getInstance().registerIdentifier(this);
197    }
198
199    /**
200        @brief Returns true, if the Identifier is at least of the given type.
201        @param identifier The identifier to compare with
202    */
203    bool Identifier::isA(const Identifier* identifier) const
204    {
205        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
206    }
207
208    /**
209        @brief Returns true, if the Identifier is exactly of the given type.
210        @param identifier The identifier to compare with
211    */
212    bool Identifier::isExactlyA(const Identifier* identifier) const
213    {
214        return (identifier == this);
215    }
216
217    /**
218        @brief Returns true, if the assigned identifier is a child of the given identifier.
219        @param identifier The identifier to compare with
220    */
221    bool Identifier::isChildOf(const Identifier* identifier) const
222    {
223        return (this->parents_.find(identifier) != this->parents_.end());
224    }
225
226    /**
227        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
228        @param identifier The identifier to compare with
229    */
230    bool Identifier::isDirectChildOf(const Identifier* identifier) const
231    {
232        return (this->directParents_.find(identifier) != this->directParents_.end());
233    }
234
235    /**
236        @brief Returns true, if the assigned identifier is a parent of the given identifier.
237        @param identifier The identifier to compare with
238    */
239    bool Identifier::isParentOf(const Identifier* identifier) const
240    {
241        return (this->children_.find(identifier) != this->children_.end());
242    }
243
244    /**
245        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
246        @param identifier The identifier to compare with
247    */
248    bool Identifier::isDirectParentOf(const Identifier* identifier) const
249    {
250        return (this->directChildren_.find(identifier) != this->directChildren_.end());
251    }
252
253    /**
254        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
255        @param varname The name of the variablee
256        @param container The container
257    */
258    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
259    {
260        std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname);
261        if (it != this->configValues_.end())
262        {
263            orxout(internal_warning) << "Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << endl;
264            delete (it->second);
265        }
266
267        this->bHasConfigValues_ = true;
268        this->configValues_[varname] = container;
269    }
270
271    /**
272        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
273        @param varname The name of the variable
274        @return The ConfigValueContainer
275    */
276    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
277    {
278        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
279        if (it != configValues_.end())
280            return it->second;
281        else
282            return 0;
283    }
284
285    /**
286        @brief Returns a XMLPortParamContainer that loads a parameter of this class.
287        @param paramname The name of the parameter
288        @return The container
289    */
290    XMLPortParamContainer* Identifier::getXMLPortParamContainer(const std::string& paramname)
291    {
292        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
293        if (it != this->xmlportParamContainers_.end())
294            return it->second;
295        else
296            return 0;
297    }
298
299    /**
300        @brief Adds a new XMLPortParamContainer that loads a parameter of this class.
301        @param paramname The name of the parameter
302        @param container The container
303    */
304    void Identifier::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
305    {
306        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
307        if (it != this->xmlportParamContainers_.end())
308        {
309            orxout(internal_warning) << "Overwriting XMLPortParamContainer in class " << this->getName() << '.' << endl;
310            delete (it->second);
311        }
312
313        this->xmlportParamContainers_[paramname] = container;
314    }
315
316    /**
317        @brief Returns a XMLPortObjectContainer that attaches an object to this class.
318        @param sectionname The name of the section that contains the attachable objects
319        @return The container
320    */
321    XMLPortObjectContainer* Identifier::getXMLPortObjectContainer(const std::string& sectionname)
322    {
323        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
324        if (it != this->xmlportObjectContainers_.end())
325            return it->second;
326        else
327            return 0;
328    }
329
330    /**
331        @brief Adds a new XMLPortObjectContainer that attaches an object to this class.
332        @param sectionname The name of the section that contains the attachable objects
333        @param container The container
334    */
335    void Identifier::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
336    {
337        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
338        if (it != this->xmlportObjectContainers_.end())
339        {
340            orxout(internal_warning) << "Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << endl;
341            delete (it->second);
342        }
343
344        this->xmlportObjectContainers_[sectionname] = container;
345    }
346
347    /**
348        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
349        @param out The outstream
350        @param list The list (or set) of Identifiers
351        @return The outstream
352    */
353    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
354    {
355        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
356        {
357            if (it != list.begin())
358                out << ' ';
359            out << (*it)->getName();
360        }
361
362        return out;
363    }
364}
Note: See TracBrowser for help on using the repository browser.