1 | /*! |
---|
2 | @file Factory.cc |
---|
3 | @brief Implementation of the Factory class. |
---|
4 | */ |
---|
5 | |
---|
6 | #include "Factory.h" |
---|
7 | #include "Identifier.h" |
---|
8 | #include "Debug.h" |
---|
9 | #include "../objects/BaseObject.h" |
---|
10 | |
---|
11 | namespace orxonox |
---|
12 | { |
---|
13 | Factory* Factory::pointer_s = NULL; // Set the static member variable pointer_s to zero |
---|
14 | |
---|
15 | /** |
---|
16 | @returns the Identifier with a given name. |
---|
17 | @param name The name of the wanted Identifier |
---|
18 | */ |
---|
19 | Identifier* Factory::getIdentifier(const std::string& name) |
---|
20 | { |
---|
21 | if (!pointer_s) |
---|
22 | pointer_s = new Factory; |
---|
23 | |
---|
24 | return pointer_s->identifierStringMap_[name]; |
---|
25 | } |
---|
26 | |
---|
27 | /** |
---|
28 | @returns the Identifier with a given network ID. |
---|
29 | @param id The network ID of the wanted Identifier |
---|
30 | */ |
---|
31 | Identifier* Factory::getIdentifier(const unsigned int id) |
---|
32 | { |
---|
33 | if (!pointer_s) |
---|
34 | pointer_s = new Factory; |
---|
35 | |
---|
36 | return pointer_s->identifierNetworkIDMap_[id]; |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | @brief Adds a new Identifier to both maps. |
---|
41 | @param name The name of the identifier |
---|
42 | @param identifier The identifier to add |
---|
43 | */ |
---|
44 | void Factory::add(const std::string& name, Identifier* identifier) |
---|
45 | { |
---|
46 | if (!pointer_s) |
---|
47 | pointer_s = new Factory; |
---|
48 | |
---|
49 | pointer_s->identifierStringMap_[name] = identifier; |
---|
50 | pointer_s->identifierNetworkIDMap_[identifier->getNetworkID()] = identifier; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | @brief Removes the entry with the old network ID and adds a new one. |
---|
55 | @param identifier The identifier to change |
---|
56 | @param oldID The old networkID |
---|
57 | @param newID The new networkID |
---|
58 | */ |
---|
59 | void Factory::changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID) |
---|
60 | { |
---|
61 | pointer_s->identifierNetworkIDMap_.erase(oldID); |
---|
62 | pointer_s->identifierNetworkIDMap_[newID] = identifier; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | @brief Creates the class-hierarchy by creating and destroying one object of each type. |
---|
67 | */ |
---|
68 | void Factory::createClassHierarchy() |
---|
69 | { |
---|
70 | if (!pointer_s) |
---|
71 | pointer_s = new Factory; |
---|
72 | |
---|
73 | COUT(4) << "*** Factory -> Create class-hierarchy\n"; |
---|
74 | std::map<std::string, Identifier*>::iterator it; |
---|
75 | it = pointer_s->identifierStringMap_.begin(); |
---|
76 | (*pointer_s->identifierStringMap_.begin()).second->startCreatingHierarchy(); |
---|
77 | for (it = pointer_s->identifierStringMap_.begin(); it != pointer_s->identifierStringMap_.end(); ++it) |
---|
78 | { |
---|
79 | // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. |
---|
80 | BaseObject* temp = (*it).second->fabricate(); |
---|
81 | delete temp; |
---|
82 | } |
---|
83 | (*pointer_s->identifierStringMap_.begin()).second->stopCreatingHierarchy(); |
---|
84 | COUT(4) << "*** Factory -> Finished class-hierarchy creation\n"; |
---|
85 | } |
---|
86 | } |
---|