coil/common/Factory.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef COIL_FACTORY_H
21 #define COIL_FACTORY_H
22 
23 #include <string>
24 #include <map>
25 #include <algorithm>
26 #include <vector>
27 #include <coil/Singleton.h>
28 
29 // for Windows DLL export
30 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
31 # ifdef LIBRARY_EXPORTS
32 # define EXTERN
33 # define DLL_PLUGIN __declspec(dllexport)
34 # else
35 # define EXTERN extern
36 # define DLL_PLUGIN __declspec(dllimport)
37 # endif
38 #else
39 # define DLL_PLUGIN
40 # define EXTERN
41 #endif /* Windows */
42 
43 
44 namespace coil
45 {
57  template <class AbstractClass, class ConcreteClass>
58  AbstractClass* Creator()
59  {
60  return new ConcreteClass();
61  }
62 
74  template <class AbstractClass, class ConcreteClass>
75  void Destructor(AbstractClass*& obj)
76  {
77  if (obj == 0) { return; }
78  ConcreteClass* tmp = dynamic_cast<ConcreteClass*>(obj);
79  if (tmp == 0) { return; }
80  delete obj;
81  obj = 0;
82  }
83 
97  template <
98  class AbstractClass,
99  typename Identifier = std::string,
100  typename Compare = std::less<Identifier>,
101  typename Creator = AbstractClass* (*)(),
102  typename Destructor = void (*)(AbstractClass*&)
103  >
104  class Factory
105  {
106  class FactoryEntry;
107  public:
108 
109  typedef std::map<Identifier, FactoryEntry> FactoryMap;
110  typedef typename FactoryMap::iterator FactoryMapIt;
111 
113  {
119  UNKNOWN_ERROR
120  };
121 
145  bool hasFactory(const Identifier& id)
146  {
147  if (m_creators.count(id) == 0) { return false; }
148  return true;
149  }
150 
170  std::vector<Identifier> getIdentifiers()
171  {
172  std::vector<Identifier> idlist;
173  idlist.reserve(m_creators.size());
174 
175  FactoryMapIt it(m_creators.begin());
176  FactoryMapIt it_end(m_creators.end());
177 
178  while (it != it_end)
179  {
180  idlist.push_back(it->first);
181  ++it;
182  }
183  return idlist;
184  }
185 
217  ReturnCode addFactory(const Identifier& id,
218  Creator creator,
219  Destructor destructor)
220  {
221  if (creator == 0 || destructor == 0) { return INVALID_ARG; }
222  if (m_creators.count(id) != 0) { return ALREADY_EXISTS; }
223  FactoryEntry f(creator, destructor);
224  m_creators[id] = f;
225  return FACTORY_OK;
226  }
227 
253  ReturnCode removeFactory(const Identifier& id)
254  {
255  if (m_creators.count(id) == 0) { return NOT_FOUND; }
256 
257  m_creators.erase(id);
258  return FACTORY_OK;
259  }
260 
284  AbstractClass* createObject(const Identifier& id)
285  {
286  if (m_creators.count(id) == 0) { return 0; }
287  return m_creators[id].creator_();
288  }
289 
311  void deleteObject(const Identifier& id, AbstractClass*& obj)
312  {
313  if (m_creators.count(id) == 0) { return; }
314  m_creators[id].destructor_(obj);
315  }
316 
336  void deleteObject(AbstractClass*& obj)
337  {
338  FactoryMapIt it(m_creators.begin());
339  FactoryMapIt it_end(m_creators.end());
340 
341  while (it != it_end)
342  {
343  it->second.destructor_(obj);
344  ++it;
345  }
346  }
347 
348  private:
349 
364  {
365  public:
366  explicit FactoryEntry()
367  {
368  }
369 
391  FactoryEntry(Creator creator, Destructor destructor)
392  : creator_(creator), destructor_(destructor)
393  {
394  }
395  Creator creator_;
397  };
398  FactoryMap m_creators;
399  };
400 
401 
402 
416  template <
417  class AbstractClass,
418  typename Identifier = std::string,
419  typename Compare = std::less<Identifier>,
420  typename Creator = AbstractClass* (*)(),
421  typename Destructor = void (*)(AbstractClass*&)
422  >
425  public coil::Singleton<GlobalFactory<AbstractClass,
426  Identifier,
427  Compare,
428  Creator,
429  Destructor> >
430  {
431  public:
432 
433  private:
450 
467 
469  };
470 
471 }; // namespace coil
472 #endif // COIL_FACTORY_H
bool hasFactory(const Identifier &id)
Factory presence check.
FactoryMap::iterator FactoryMapIt
AbstractClass * Creator()
Creator template.
ReturnCode removeFactory(const Identifier &id)
Remove factory.
ReturnCode addFactory(const Identifier &id, Creator creator, Destructor destructor)
Add factory.
GlobalFactory template class.
std::vector< Identifier > getIdentifiers()
Get factory ID list.
Singleton template class.
Definition: Singleton.h:106
void deleteObject(const Identifier &id, AbstractClass *&obj)
Delete factory object.
AbstractClass * createObject(const Identifier &id)
Create factory object.
Factory template class.
std::map< Identifier, FactoryEntry > FactoryMap
void Destructor(AbstractClass *&obj)
Destructor template.
~GlobalFactory()
Destructor.
FactoryEntry(Creator creator, Destructor destructor)
Constructor.
void deleteObject(AbstractClass *&obj)
Delete factory object.
GlobalFactory()
Constructor.
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43