Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/Factory.cc @ 883

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

merged core branch to 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 Factory.cc
30    @brief Implementation of the Factory class.
31*/
32
33#include "Identifier.h"
34#include "Debug.h"
35#include "BaseObject.h"
36#include "Factory.h"
37
38namespace orxonox
39{
40    /**
41        @brief Returns the Identifier with a given name.
42        @param name The name of the wanted Identifier
43        @return The Identifier
44    */
45    Identifier* Factory::getIdentifier(const std::string& name)
46    {
47        return getFactoryPointer()->identifierStringMap_[name];
48    }
49
50    /**
51        @brief Returns the Identifier with a given network ID.
52        @param id The network ID of the wanted Identifier
53        @return The Identifier
54    */
55    Identifier* Factory::getIdentifier(const unsigned int id)
56    {
57        return getFactoryPointer()->identifierNetworkIDMap_[id];
58    }
59
60    /**
61        @brief Adds a new Identifier to both maps.
62        @param name The name of the identifier
63        @param identifier The identifier to add
64    */
65    void Factory::add(const std::string& name, Identifier* identifier)
66    {
67        getFactoryPointer()->identifierStringMap_[name] = identifier;
68        getFactoryPointer()->identifierNetworkIDMap_[identifier->getNetworkID()] = identifier;
69    }
70
71    /**
72        @brief Removes the entry with the old network ID and adds a new one.
73        @param identifier The identifier to change
74        @param oldID The old networkID
75        @param newID The new networkID
76    */
77    void Factory::changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID)
78    {
79        getFactoryPointer()->identifierNetworkIDMap_.erase(oldID);
80        getFactoryPointer()->identifierNetworkIDMap_[newID] = identifier;
81    }
82
83    /**
84        @brief Creates the class-hierarchy by creating and destroying one object of each type.
85    */
86    void Factory::createClassHierarchy()
87    {
88        COUT(3) << "*** Factory: Create class-hierarchy" << std::endl;
89        std::map<std::string, Identifier*>::iterator it;
90        it = getFactoryPointer()->identifierStringMap_.begin();
91        (*getFactoryPointer()->identifierStringMap_.begin()).second->startCreatingHierarchy();
92        for (it = getFactoryPointer()->identifierStringMap_.begin(); it != getFactoryPointer()->identifierStringMap_.end(); ++it)
93        {
94            // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
95            BaseObject* temp = (*it).second->fabricate();
96            delete temp;
97        }
98        (*getFactoryPointer()->identifierStringMap_.begin()).second->stopCreatingHierarchy();
99        COUT(3) << "*** Factory: Finished class-hierarchy creation" << std::endl;
100    }
101
102    /**
103        @brief Ensures the Factory gets created in the right moment.
104        @return The Factory.
105    */
106    Factory* Factory::getFactoryPointer()
107    {
108      static Factory theOneAndOnlyFactoryInstance = Factory();
109      return &theOneAndOnlyFactoryInstance;
110    }
111
112    /**
113        @brief Returns the begin-iterator of the factory-map.
114        @return The begin-iterator
115    */
116    std::map<std::string, Identifier*>::const_iterator Factory::getFactoryBegin()
117    {
118        return Factory::getFactoryPointer()->identifierStringMap_.begin();
119    }
120
121    /**
122        @brief Returns the end-iterator of the factory-map.
123        @return The end-iterator
124    */
125    std::map<std::string, Identifier*>::const_iterator Factory::getFactoryEnd()
126    {
127        return Factory::getFactoryPointer()->identifierStringMap_.end();
128    }
129}
Note: See TracBrowser for help on using the repository browser.