ObjectFactory.h
Go to the documentation of this file.
1 #ifndef _utl_ObjectFactory_h_
2 #define _utl_ObjectFactory_h_
3 
4 #include <map>
5 
6 
7 namespace utl {
8 
25  template<typename IdentifierType, class ObjectPtrType>
27 
28  public:
29  static ObjectPtrType Unregistered(const IdentifierType&)
30  { return nullptr; }
31 
32  template<typename ArgumentType>
33  static ObjectPtrType Unregistered(const IdentifierType& /*id*/,
34  const ArgumentType& /*arg*/)
35  { return nullptr; }
36 
37  template<typename Argument1Type, typename Argument2Type>
38  static ObjectPtrType Unregistered(const IdentifierType& /*id*/,
39  const Argument1Type& /*arg1*/,
40  const Argument2Type& /*arg2*/)
41  { return nullptr; }
42 
43  };
44 
45 
67  template<class ObjPtrType,
68  typename IdentType,
69  typename CreatType = ObjPtrType (*)(),
70  class FactoryErrorPolicy = FactoryErrorIgnore<IdentType, ObjPtrType>>
71  class ObjectFactory : public FactoryErrorPolicy {
72 
73  private:
74  typedef std::map<IdentType, CreatType> RegistryType;
75 
76  public:
77  typedef ObjPtrType ObjectPtrType;
78  typedef IdentType IdentifierType;
79  typedef CreatType CreatorType;
80  typedef typename RegistryType::const_iterator Iterator;
81 
82  public:
83  ObjectFactory();
84  ~ObjectFactory();
85 
87  static
88  bool
89  Register(const IdentifierType& id,
90  const CreatorType& creator)
91  {
92  if (!fgRegistry)
93  fgRegistry = new RegistryType;
94  return fgRegistry->insert(std::make_pair(id, creator)).second;
95  }
96 
98  static unsigned int GetNumberOfCreators()
99  { return fgRegistry ? fgRegistry->size() : 0; }
100 
102  static
103  ObjectPtrType
104  Create(const IdentifierType& id)
105  {
106  if (!fgRegistry)
107  return ObjectFactory::Unregistered(id);
108 
109  const auto it = fgRegistry->find(id);
110  if (it != fgRegistry->end())
111  return (it->second)();
112  else
113  return ObjectFactory::Unregistered(id);
114  }
115 
117  template<typename ArgumentType>
118  static
119  ObjectPtrType
120  Create(const IdentifierType& id, const ArgumentType& arg)
121  {
122  if (!fgRegistry)
123  return ObjectFactory::Unregistered(id, arg);
124 
125  const auto it = fgRegistry->find(id);
126  if (it != fgRegistry->end())
127  return (it->second)(arg);
128  else
129  return ObjectFactory::Unregistered(id, arg);
130  }
131 
133  template<typename Argument1Type, typename Argument2Type>
134  static
135  ObjectPtrType
136  Create(const IdentifierType& id,
137  const Argument1Type& arg1,
138  const Argument2Type& arg2)
139  {
140  if (!fgRegistry)
141  return ObjectFactory::Unregistered(id, arg1, arg2);
142 
143  const auto it = fgRegistry->find(id);
144  if (it != fgRegistry->end())
145  return (it->second)(arg1, arg2);
146  else
147  return ObjectFactory::Unregistered(id, arg1, arg2);
148  }
149 
151  static
152  Iterator
154  {
155  if (!fgRegistry)
156  fgRegistry = new RegistryType;
157  return fgRegistry->begin();
158  }
159 
161  static
162  Iterator
163  End()
164  {
165  if (!fgRegistry)
166  fgRegistry = new RegistryType;
167  return fgRegistry->end();
168  }
169 
170  private:
171  ObjectFactory(const ObjectFactory& objectFactory);
172  ObjectFactory& operator=(const ObjectFactory& objectFactory);
173 
174  static RegistryType* fgRegistry;
175 
176  };
177 
178 
179  // the definition of the registry, which cannot be done in the class defn.
180  template<class ObjectPtrType,
181  typename IdentifierType,
182  typename CreatorType,
183  class FactoryErrorPolicy>
184  typename ObjectFactory<ObjectPtrType, IdentifierType,
185  CreatorType, FactoryErrorPolicy>::RegistryType*
186  ObjectFactory<ObjectPtrType, IdentifierType,
187  CreatorType, FactoryErrorPolicy>::fgRegistry = nullptr;
188 
189 }
190 
191 
192 #endif
RegistryType::const_iterator Iterator
Definition: ObjectFactory.h:80
static RegistryType * fgRegistry
Default error policy for ObjectFactory: return 0.
Definition: ObjectFactory.h:26
IdentType IdentifierType
Definition: ObjectFactory.h:78
static ObjectPtrType Unregistered(const IdentifierType &)
Definition: ObjectFactory.h:29
static Iterator Begin()
Begin iterator over the internal map (read only)
static ObjectPtrType Create(const IdentifierType &id, const Argument1Type &arg1, const Argument2Type &arg2)
Create an object (2-argument constructor)
static ObjectPtrType Unregistered(const IdentifierType &, const Argument1Type &, const Argument2Type &)
Definition: ObjectFactory.h:38
static Iterator End()
End iterator over the internal map (read only)
static unsigned int GetNumberOfCreators()
Get number of object types know to this factory.
Definition: ObjectFactory.h:98
ObjPtrType ObjectPtrType
Definition: ObjectFactory.h:77
static bool Register(const IdentifierType &id, const CreatorType &creator)
Register a factory function with an ID.
Definition: ObjectFactory.h:89
Template for object factory.
Definition: ObjectFactory.h:71
std::map< IdentType, CreatType > RegistryType
Definition: ObjectFactory.h:74
static ObjectPtrType Create(const IdentifierType &id, const ArgumentType &arg)
Create an object (1-argument constructor)
static ObjectPtrType Unregistered(const IdentifierType &, const ArgumentType &)
Definition: ObjectFactory.h:33
static ObjectPtrType Create(const IdentifierType &id)
Create an object (0-argument constructor)
CreatType CreatorType
Definition: ObjectFactory.h:79

, generated on Tue Sep 26 2023.