OpenLexocad  28.0
Factory.h
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <map>
4 #include <list>
5 
6 namespace Base
7 {
8 template <typename BaseClassType, typename ClassType>
9 BaseClassType* CreateObject()
10 {
11  return new ClassType();
12 }
13 
14 template <typename BaseClassType, typename UniqueIdType>
15 class Factory
16 {
17 protected:
18  typedef BaseClassType* (*CreateObjectFunc)();
19 
20 public:
21  typedef typename std::map<UniqueIdType, CreateObjectFunc>::const_iterator ConstIterator;
22  typedef typename std::map<UniqueIdType, CreateObjectFunc>::iterator Iterator;
23 
24  template <typename ClassType>
25  bool Register(UniqueIdType unique_id)
26  {
27  if (m_object_creator.find(unique_id) != m_object_creator.end())
28  return false;
29 
30  m_object_creator[unique_id] = &CreateObject<BaseClassType, ClassType>;
31 
32  return true;
33  }
34 
35  bool Unregister(UniqueIdType unique_id) { return (m_object_creator.erase(unique_id) == 1); }
36 
37  BaseClassType* Create(UniqueIdType unique_id)
38  {
39  Iterator iter = m_object_creator.find(unique_id);
40 
41  if (iter == m_object_creator.end())
42  return NULL;
43 
44  return ((*iter).second)();
45  }
46 
47  ConstIterator GetBegin() const { return m_object_creator.begin(); }
48  Iterator GetBegin() { return m_object_creator.begin(); }
49  ConstIterator GetEnd() const { return m_object_creator.end(); }
50  Iterator GetEnd() { return m_object_creator.end(); }
51 
52 protected:
53  std::map<UniqueIdType, CreateObjectFunc> m_object_creator;
54 };
55 
57 class LX_BASE_EXPORT AbstractProducer
58 {
59 public:
60  virtual ~AbstractProducer() = default;
62  virtual void* Produce() const = 0;
63 };
64 
71 class LX_BASE_EXPORT Factory2
72 {
73 public:
75  void AddProducer(const char* sClassName, AbstractProducer* pcProducer);
77  bool CanProduce(const char* sClassName) const;
79  std::list<std::string> CanProduce() const;
80 
81 protected:
83  void* Produce(const char* sClassName) const;
84  std::map<const std::string, AbstractProducer*> _mpcProducers;
86  Factory2(void) {}
88  virtual ~Factory2();
89 };
90 
91 // --------------------------------------------------------------------
92 
95 class LX_BASE_EXPORT ScriptFactorySingleton : public Factory2
96 {
97 public:
99  static void Destruct();
100 
101  const char* ProduceScript(const char* sScriptName) const;
102 
103 private:
104  static ScriptFactorySingleton* _pcSingleton;
105 
106  ScriptFactorySingleton() = default;
107 };
108 
110 {
112 }
113 
114 // --------------------------------------------------------------------
115 
120 class LX_BASE_EXPORT ScriptProducer : public AbstractProducer
121 {
122 public:
124  ScriptProducer(const char* name, const char* script) : mScript(script)
125  {
127  }
128 
130  virtual void* Produce() const
131  {
132  return (void*)mScript;
133  }
134 
135 private:
136  const char* mScript;
137 };
138 
139 } // namespace Base
140 
141 
142 
Base::ScriptProducer::Produce
virtual void * Produce() const
Produce an instance.
Definition: Factory.h:130
Base::Factory::Register
bool Register(UniqueIdType unique_id)
Definition: Factory.h:25
Base::Factory::Unregister
bool Unregister(UniqueIdType unique_id)
Definition: Factory.h:35
Base::Factory2::Produce
void * Produce(const char *sClassName) const
produce a class with the given name
Base::AbstractProducer
Abstract base class of all producers.
Definition: Factory.h:58
Base::Factory::GetBegin
Iterator GetBegin()
Definition: Factory.h:48
Base::Factory2::Factory2
Factory2(void)
construction
Definition: Factory.h:86
Base::ScriptFactorySingleton::Instance
static ScriptFactorySingleton & Instance(void)
Base::ScriptProducer::ScriptProducer
ScriptProducer(const char *name, const char *script)
Constructor.
Definition: Factory.h:124
Base::Factory::GetBegin
ConstIterator GetBegin() const
Definition: Factory.h:47
Base::Factory::GetEnd
ConstIterator GetEnd() const
Definition: Factory.h:49
Base::Factory::GetEnd
Iterator GetEnd()
Definition: Factory.h:50
Base::ScriptFactorySingleton::ProduceScript
const char * ProduceScript(const char *sScriptName) const
Base::Factory2::CanProduce
std::list< std::string > CanProduce() const
returns a list of all registered producer
Base::Factory::m_object_creator
std::map< UniqueIdType, CreateObjectFunc > m_object_creator
Definition: Factory.h:53
Base::Factory2::CanProduce
bool CanProduce(const char *sClassName) const
returns true if there is a producer for this class registered
Base::ScriptProducer
Definition: Factory.h:121
Base::Factory2::_mpcProducers
std::map< const std::string, AbstractProducer * > _mpcProducers
Definition: Factory.h:84
Base::ScriptFactorySingleton::Destruct
static void Destruct()
Base::Factory
Definition: Factory.h:16
Base::Factory2::AddProducer
void AddProducer(const char *sClassName, AbstractProducer *pcProducer)
Adds a new producer instance.
Base::Factory::Iterator
std::map< UniqueIdType, CreateObjectFunc >::iterator Iterator
Definition: Factory.h:22
Base::AbstractProducer::Produce
virtual void * Produce() const =0
overwritten by a concrete producer to produce the needed object
Base::Factory2
Definition: Factory.h:72
Base::Factory::Create
BaseClassType * Create(UniqueIdType unique_id)
Definition: Factory.h:37
Base::AbstractProducer::~AbstractProducer
virtual ~AbstractProducer()=default
Base::CreateObject
BaseClassType * CreateObject()
Definition: Factory.h:9
Base::ScriptFactory
ScriptFactorySingleton & ScriptFactory()
Definition: Factory.h:109
Base::ScriptFactorySingleton
Definition: Factory.h:96
Base::Factory2::~Factory2
virtual ~Factory2()
destruction
Base
Definition: AbstractXMLReader.h:5
Base::Factory::ConstIterator
std::map< UniqueIdType, CreateObjectFunc >::const_iterator ConstIterator
Definition: Factory.h:21