OpenLexocad  27.0
Factory.h
Go to the documentation of this file.
1 #ifndef __FACTORY_H__
2 #define __FACTORY_H__
3 
4 #include <assert.h>
5 
6 #include <map>
7 #include <set>
8 
9 namespace Base
10 {
11 template <typename BaseClassType, typename ClassType>
12 BaseClassType* CreateObject()
13 {
14  return new ClassType();
15 }
16 
17 
18 template <typename BaseClassType, typename UniqueIdType>
19 class Factory
20 {
21 protected:
22  typedef BaseClassType* (*CreateObjectFunc)();
23 
24 public:
25  typedef typename std::map<UniqueIdType, CreateObjectFunc>::const_iterator ConstIterator;
26  typedef typename std::map<UniqueIdType, CreateObjectFunc>::iterator Iterator;
27 
28 
29  template <typename ClassType>
30  bool Register(UniqueIdType unique_id)
31  {
32  if (m_object_creator.find(unique_id) != m_object_creator.end())
33  return false;
34 
35  m_object_creator[unique_id] = &CreateObject<BaseClassType, ClassType>;
36 
37  return true;
38  }
39 
40  bool Unregister(UniqueIdType unique_id) { return (m_object_creator.erase(unique_id) == 1); }
41 
42  BaseClassType* Create(UniqueIdType unique_id)
43  {
44  Iterator iter = m_object_creator.find(unique_id);
45 
46  if (iter == m_object_creator.end())
47  return NULL;
48 
49  return ((*iter).second)();
50  }
51 
52  ConstIterator GetBegin() const { return m_object_creator.begin(); }
53 
54  Iterator GetBegin() { return m_object_creator.begin(); }
55 
56  ConstIterator GetEnd() const { return m_object_creator.end(); }
57 
58  Iterator GetEnd() { return m_object_creator.end(); }
59 
60 protected:
61  std::map<UniqueIdType, CreateObjectFunc> m_object_creator;
62 };
63 
64 
65 
66 } // namespace Base
67 
68 
69 #endif // __FACTORY_H__
std::map< UniqueIdType, CreateObjectFunc >::const_iterator ConstIterator
Definition: Factory.h:25
bool Unregister(UniqueIdType unique_id)
Definition: Factory.h:40
BaseClassType * CreateObject()
Definition: Factory.h:12
Iterator GetBegin()
Definition: Factory.h:54
Iterator GetEnd()
Definition: Factory.h:58
ConstIterator GetBegin() const
Definition: Factory.h:52
ConstIterator GetEnd() const
Definition: Factory.h:56
Definition: Factory.h:19
std::map< UniqueIdType, CreateObjectFunc >::iterator Iterator
Definition: Factory.h:26
std::map< UniqueIdType, CreateObjectFunc > m_object_creator
Definition: Factory.h:61
BaseClassType * Create(UniqueIdType unique_id)
Definition: Factory.h:42
Definition: AbstractXMLReader.h:8
bool Register(UniqueIdType unique_id)
Definition: Factory.h:30