OpenRTM-aist  1.2.1
Factory.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef COIL_FACTORY_H
21 #define COIL_FACTORY_H
22 
23 #include <assert.h>
24 #include <string>
25 #include <map>
26 #include <algorithm>
27 #include <vector>
28 #include <coil/Singleton.h>
29 
30 // for Windows DLL export
31 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
32 # ifdef LIBRARY_EXPORTS
33 # define EXTERN
34 # define DLL_PLUGIN __declspec(dllexport)
35 # else
36 # define EXTERN extern
37 # define DLL_PLUGIN __declspec(dllimport)
38 # endif
39 #else
40 # define DLL_PLUGIN
41 #ifndef EXTERN
42 # define EXTERN
43 #endif // ifndef EXTERN
44 #endif /* Windows */
45 
46 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
47 #pragma warning( push )
48 #pragma warning( disable : 4251 )
49 #endif
50 
51 namespace coil
52 {
64  template <class AbstractClass, class ConcreteClass>
65  AbstractClass* Creator()
66  {
67  return new ConcreteClass();
68  }
69 
81  template <class AbstractClass, class ConcreteClass>
82  void Destructor(AbstractClass*& obj)
83  {
84  if (obj == 0) { return; }
85  ConcreteClass* tmp = dynamic_cast<ConcreteClass*>(obj);
86  if (tmp == 0) { return; }
87  delete obj;
88  obj = 0;
89  }
90 
104  template <
105  class AbstractClass,
106  typename Identifier = std::string,
107  typename Compare = std::less<Identifier>,
108  typename Creator = AbstractClass* (*)(),
109  typename Destructor = void (*)(AbstractClass*&)
110  >
111  class Factory
112  {
113  class FactoryEntry;
114  public:
115 
116  typedef std::map<Identifier, FactoryEntry> FactoryMap;
117  typedef typename FactoryMap::iterator FactoryMapIt;
118  typedef std::map<AbstractClass*, FactoryEntry> ObjectMap;
119  typedef typename ObjectMap::iterator ObjectMapIt;
120 
122  {
128  UNKNOWN_ERROR
129  };
130 
154  bool hasFactory(const Identifier& id)
155  {
156  if (m_creators.count(id) == 0) { return false; }
157  return true;
158  }
159 
179  std::vector<Identifier> getIdentifiers()
180  {
181  std::vector<Identifier> idlist;
182  idlist.reserve(m_creators.size());
183 
184  FactoryMapIt it(m_creators.begin());
185  FactoryMapIt it_end(m_creators.end());
186 
187  while (it != it_end)
188  {
189  idlist.push_back(it->first);
190  ++it;
191  }
192  return idlist;
193  }
194 
226  ReturnCode addFactory(const Identifier& id,
227  Creator creator,
228  Destructor destructor)
229  {
230  if (creator == 0 || destructor == 0) { return INVALID_ARG; }
231  if (m_creators.count(id) != 0) { return ALREADY_EXISTS; }
232  FactoryEntry f(id, creator, destructor);
233  m_creators[id] = f;
234  return FACTORY_OK;
235  }
236 
262  ReturnCode removeFactory(const Identifier& id)
263  {
264  if (m_creators.count(id) == 0) { return NOT_FOUND; }
265  m_creators.erase(id);
266  return FACTORY_OK;
267  }
268 
292  AbstractClass* createObject(const Identifier& id)
293  {
294  if (m_creators.count(id) == 0) { return 0; }
295  AbstractClass* obj = m_creators[id].creator_();
296  assert(m_objects.count(obj) == 0);
297  m_objects[obj] = m_creators[id];
298  return obj;
299  }
300 
322  ReturnCode deleteObject(const Identifier& id, AbstractClass*& obj)
323  {
324  if (m_creators.count(id) == 0)
325  {
326  return deleteObject(obj);
327  }
328  m_creators[id].destructor_(obj);
329  m_objects.erase(obj);
330  return FACTORY_OK;
331  }
332 
352  ReturnCode deleteObject(AbstractClass*& obj)
353  {
354  if (m_objects.count(obj) == 0) { return NOT_FOUND; }
355  AbstractClass* tmp(obj);
356  m_objects[obj].destructor_(obj);
357  m_objects.erase(tmp);
358  return FACTORY_OK;
359  }
360 
380  std::vector<AbstractClass*> createdObjects()
381  {
382  std::vector<AbstractClass*> objects;
383  for (ObjectMapIt it(m_objects.begin()); it != m_objects.end(); ++it)
384  {
385  objects.push_back(it->first);
386  }
387  return objects;
388  }
389 
411  bool isProducerOf(AbstractClass* obj)
412  {
413  return m_objects.count(obj) != 0;
414  }
415 
439  ReturnCode objectToIdentifier(AbstractClass* obj, Identifier& id)
440  {
441  if (m_objects.count(obj) == 0) { return NOT_FOUND; }
442  id = m_objects[obj].id_;
443  return FACTORY_OK;
444  }
445 
471  Creator objectToCreator(AbstractClass* obj)
472  {
473  return m_objects[obj].creator_;
474  }
475 
501  Destructor objectToDestructor(AbstractClass* obj)
502  {
503  return m_objects[obj].destructor_;
504  }
505 
506  private:
507 
521  class FactoryEntry
522  {
523  public:
524  explicit FactoryEntry()
525  {
526  }
527 
549  FactoryEntry(Identifier id, Creator creator, Destructor destructor)
550  : id_(id), creator_(creator), destructor_(destructor)
551  {
552  }
553  std::string id_;
554  Creator creator_;
555  Destructor destructor_;
556  };
557  FactoryMap m_creators;
558  ObjectMap m_objects;
559  };
560 
561 
562 
576  template <
577  class AbstractClass,
578  typename Identifier = std::string,
579  typename Compare = std::less<Identifier>,
580  typename Creator = AbstractClass* (*)(),
581  typename Destructor = void (*)(AbstractClass*&)
582  >
585  public coil::Singleton<GlobalFactory<AbstractClass,
586  Identifier,
587  Compare,
588  Creator,
589  Destructor> >
590  {
591  public:
592 
593  private:
609  GlobalFactory(){}
610 
626  ~GlobalFactory(){}
627 
629  };
630 
631 }; // namespace coil
632 
633 
634 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
635 #pragma warning( pop )
636 #endif
637 
638 #endif // COIL_FACTORY_H
bool hasFactory(const Identifier &id)
Factory presence check.
Definition: Factory.h:154
ObjectMap::iterator ObjectMapIt
Definition: Factory.h:119
FactoryMap::iterator FactoryMapIt
Definition: Factory.h:117
AbstractClass * Creator()
Creator template.
Definition: Factory.h:65
ReturnCode removeFactory(const Identifier &id)
Remove factory.
Definition: Factory.h:262
ReturnCode
Definition: Factory.h:121
ReturnCode addFactory(const Identifier &id, Creator creator, Destructor destructor)
Add factory.
Definition: Factory.h:226
GlobalFactory template class.
Definition: Factory.h:583
Definition: Factory.h:123
std::vector< Identifier > getIdentifiers()
Get factory ID list.
Definition: Factory.h:179
Definition: Factory.h:125
Definition: Factory.h:127
Destructor objectToDestructor(AbstractClass *obj)
Getting destructor of the object.
Definition: Factory.h:501
std::vector< AbstractClass * > createdObjects()
Getting created objects.
Definition: Factory.h:380
Singleton template class.
Definition: Factory.h:126
Singleton template class.
Definition: Singleton.h:106
Definition: Factory.h:124
AbstractClass * createObject(const Identifier &id)
Create factory object.
Definition: Factory.h:292
ReturnCode objectToIdentifier(AbstractClass *obj, Identifier &id)
Getting class identifier (ID) from a object.
Definition: Factory.h:439
Factory template class.
Definition: Factory.h:111
std::map< AbstractClass *, FactoryEntry > ObjectMap
Definition: Factory.h:118
std::map< Identifier, FactoryEntry > FactoryMap
Definition: Factory.h:113
void Destructor(AbstractClass *&obj)
Destructor template.
Definition: Factory.h:82
ReturnCode deleteObject(AbstractClass *&obj)
Delete factory object.
Definition: Factory.h:352
bool isProducerOf(AbstractClass *obj)
Whether a object is a product of this factory.
Definition: Factory.h:411
Creator objectToCreator(AbstractClass *obj)
Getting destructor of the object.
Definition: Factory.h:471
ReturnCode deleteObject(const Identifier &id, AbstractClass *&obj)
Delete factory object.
Definition: Factory.h:322
Common Object Interface Layer.
Definition: Affinity.h:28