Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/core/Factory.cc @ 551

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

funny hack, i hope this works for reto

File size: 4.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 *      ...
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/*!
29    @file Factory.cc
30    @brief Implementation of the Factory class.
31*/
32
33#include "Factory.h"
34#include "Identifier.h"
35#include "Debug.h"
36#include "../objects/BaseObject.h"
37
38namespace orxonox
39{
40    Factory* Factory::pointer1_s;
41    Factory* Factory::pointer2_s;
42    Factory* Factory::pointer3_s;
43    Factory* Factory::pointer4_s;
44    Factory* Factory::pointer5_s = NULL; // Set the static member variable pointer5_s to zero
45
46    /**
47        @brief Checks if the pointer to the only Factory-object exists and creates it, if not.
48    */
49    void Factory::checkPointer()
50    {
51        if ((!pointer5_s) || (pointer1_s != pointer2_s) || (pointer2_s != pointer3_s) || (pointer3_s != pointer4_s) || (pointer4_s != pointer5_s))
52        {
53            std::cout << pointer1_s << std::endl;
54            std::cout << pointer2_s << std::endl;
55            std::cout << pointer3_s << std::endl;
56            std::cout << pointer4_s << std::endl;
57            std::cout << pointer5_s << std::endl;
58            pointer1_s = pointer2_s = pointer3_s = pointer4_s = pointer5_s = new Factory;
59            std::cout << pointer1_s << std::endl;
60            std::cout << pointer2_s << std::endl;
61            std::cout << pointer3_s << std::endl;
62            std::cout << pointer4_s << std::endl;
63            std::cout << pointer5_s << std::endl;
64        }
65    }
66
67    /**
68        @returns the Identifier with a given name.
69        @param name The name of the wanted Identifier
70    */
71    Identifier* Factory::getIdentifier(const std::string& name)
72    {
73        Factory::checkPointer();
74
75        return pointer1_s->identifierStringMap_[name];
76    }
77
78    /**
79        @returns the Identifier with a given network ID.
80        @param id The network ID of the wanted Identifier
81    */
82    Identifier* Factory::getIdentifier(const unsigned int id)
83    {
84        Factory::checkPointer();
85
86        return pointer1_s->identifierNetworkIDMap_[id];
87    }
88
89    /**
90        @brief Adds a new Identifier to both maps.
91        @param name The name of the identifier
92        @param identifier The identifier to add
93    */
94    void Factory::add(const std::string& name, Identifier* identifier)
95    {
96        Factory::checkPointer();
97
98        pointer1_s->identifierStringMap_[name] = identifier;
99        pointer1_s->identifierNetworkIDMap_[identifier->getNetworkID()] = identifier;
100    }
101
102    /**
103        @brief Removes the entry with the old network ID and adds a new one.
104        @param identifier The identifier to change
105        @param oldID The old networkID
106        @param newID The new networkID
107    */
108    void Factory::changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID)
109    {
110        Factory::checkPointer();
111
112        pointer1_s->identifierNetworkIDMap_.erase(oldID);
113        pointer1_s->identifierNetworkIDMap_[newID] = identifier;
114    }
115
116    /**
117        @brief Creates the class-hierarchy by creating and destroying one object of each type.
118    */
119    void Factory::createClassHierarchy()
120    {
121        Factory::checkPointer();
122
123        COUT(4) << "*** Factory -> Create class-hierarchy\n";
124        std::map<std::string, Identifier*>::iterator it;
125        it = pointer1_s->identifierStringMap_.begin();
126        (*pointer1_s->identifierStringMap_.begin()).second->startCreatingHierarchy();
127        for (it = pointer1_s->identifierStringMap_.begin(); it != pointer1_s->identifierStringMap_.end(); ++it)
128        {
129            // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
130            BaseObject* temp = (*it).second->fabricate();
131            delete temp;
132        }
133        (*pointer1_s->identifierStringMap_.begin()).second->stopCreatingHierarchy();
134        COUT(4) << "*** Factory -> Finished class-hierarchy creation\n";
135    }
136}
Note: See TracBrowser for help on using the repository browser.