Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialFS09/src/core/Factory.cc @ 9215

Last change on this file since 9215 was 2759, checked in by scheusso, 16 years ago

merged network branch (windows,multiplayer fixes) back to trunk

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