1 | /*! |
---|
2 | @file Factory.h |
---|
3 | @brief Definition of the Factory and the BaseFactory class. |
---|
4 | |
---|
5 | The Factory is a singleton, containing two maps to map either the name or the network ID |
---|
6 | of a class with the corresponding Identifier. |
---|
7 | |
---|
8 | Usage: |
---|
9 | ID(classname) or ID(networkID) returns the corresponding Identifier. |
---|
10 | |
---|
11 | |
---|
12 | BaseObject is the parent of ClassFactory which is defined in ClassFactory.h. |
---|
13 | It can't be defined in ClassFactory.h, because of circular dependencies. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef _Factory_H__ |
---|
17 | #define _Factory_H__ |
---|
18 | |
---|
19 | #include <map> |
---|
20 | #include <string> |
---|
21 | |
---|
22 | namespace orxonox |
---|
23 | { |
---|
24 | class BaseObject; // Forward declaration |
---|
25 | class Identifier; // Forward declaration |
---|
26 | |
---|
27 | // ############################### |
---|
28 | // ### Factory ### |
---|
29 | // ############################### |
---|
30 | //! The Factory is used to map the name or the network ID of a class with its Identifier. |
---|
31 | class Factory |
---|
32 | { |
---|
33 | public: |
---|
34 | static Identifier* getIdentifier(const std::string& name); |
---|
35 | static Identifier* getIdentifier(const unsigned int id); |
---|
36 | static void add(const std::string& name, Identifier* identifier); |
---|
37 | static void changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID); |
---|
38 | static void createClassHierarchy(); |
---|
39 | |
---|
40 | private: |
---|
41 | Factory() {} // don't create |
---|
42 | Factory(const Factory& factory) {} // don't copy |
---|
43 | ~Factory() {} // don't delete |
---|
44 | |
---|
45 | /** |
---|
46 | @brief Checks if the pointer to the only Factory-object exists and creates it, if not. |
---|
47 | */ |
---|
48 | inline static void checkPointer() |
---|
49 | { |
---|
50 | if (!pointer_s) |
---|
51 | pointer_s = new Factory; |
---|
52 | } |
---|
53 | |
---|
54 | static Factory* pointer_s; //!< The pointer to the singleton |
---|
55 | std::map<std::string, Identifier*> identifierStringMap_; //!< The map, mapping the name with the Identifier |
---|
56 | std::map<unsigned int, Identifier*> identifierNetworkIDMap_; //!< The map, mapping the network ID with the Identifier |
---|
57 | }; |
---|
58 | |
---|
59 | // ############################### |
---|
60 | // ### BaseFactory ### |
---|
61 | // ############################### |
---|
62 | //! Base-class of ClassFactory. Has to be defined separate because of circular dependencies. |
---|
63 | class BaseFactory |
---|
64 | { |
---|
65 | public: |
---|
66 | virtual BaseObject* fabricate() = 0; |
---|
67 | virtual ~BaseFactory() {}; |
---|
68 | }; |
---|
69 | } |
---|
70 | |
---|
71 | #endif |
---|