gfactory
gfactory.h
Go to the documentation of this file.
1 #ifndef GFACTORY_H
2 #define GFACTORY_H 1
3 
4 // c++
5 #include <map>
6 #include <string>
7 
8 // dynamic loading
9 #include "gdl.h"
10 
11 /*
12  * @class GFactoryBase
13  * @brief Base class. Use polymorphism to run Create on the
14  * derived classes
15  */
16 class GFactoryBase {
17 public:
21  virtual void *Create() = 0;
22 };
23 
28 template<class T>
29 class GFactory : public GFactoryBase {
34  virtual void *Create() override {
35  return new T();
36  }
37 };
38 
42 class GManager {
43 private:
44 
45  // the map of GFactoryBase is kept on GManager
46  std::map<std::string, GFactoryBase *> factoryMap;
47 
48  // the reason to keep this map is to keep the (DynamicLib?) pointers in memory
49  // for some reason declaring a dynamic_lib local variable in LoadAndRegisterObjectFromLibrary
50  // scope does not work
51  std::map<std::string, DynamicLib *> dlMap;
52 
53  std::string gname; // manager name
54 
61  void registerDL(std::string name) {
62  // PRAGMA TODO: make it OS independent?
63  dlMap[name] = new DynamicLib(log, name + ".gplugin");
64  log->debug(NORMAL, "Loading DL ", name);
65  }
66 
67  GLogger const *log;
68 
69 public:
73  GManager(GLogger const *logger, std::string name) : gname(name), log(logger) {
74  log->debug(CONSTRUCTOR, "Instantiating ", gname);
75  }
76 
77 public:
86  template<class Derived>
87  void RegisterObjectFactory(std::string name) {
88  factoryMap[name] = new GFactory<Derived>();
89  log->debug(NORMAL, "Registering ", name, "into factory Map");
90 
91  }
92 
102  template<class Base>
103  Base *CreateObject(std::string name) const {
104 
105  auto factory = factoryMap.find(name);
106 
107  if (factory == factoryMap.end()) {
108  log->error(EC__FACTORYNOTFOUND, "couldn't find factory ", name, " in factory Map.");
109  }
110 
111  log->debug(NORMAL, "Creating instance of ", name, " factory.");
112  return static_cast<Base *>(factory->second->Create());
113  }
114 
115 
123  template<class T>
124  T *LoadAndRegisterObjectFromLibrary(std::string name) {
125 
126  registerDL(name);
127 
128  // will return nullptr if handle is null
129  DynamicLib *dynamicLib = dlMap[name];
130  if (dynamicLib != nullptr) {
131  dlhandle thisDLHandle = dynamicLib->handle;
132  if (thisDLHandle != nullptr) {
133  return T::instantiate(thisDLHandle);
134  }
135  } else {
136  // warning message already given if plugin not found
137  log->error(EC__DLHANDLENOTFOUND, "plugin ", name, " could not be loaded.");
138  }
139  return nullptr;
140  }
141 
148  // template <class T> void destroyObject(T* object) {
149  // delete object;
150  // }
151 
158  void clearDLMap() {
159  for (auto &i: dlMap) {
160  delete i.second;
161  }
162  }
163 
164 };
165 
166 #endif
virtual void * Create()=0
Pure virtual method. Derived classes use this to instantiate themselves.
Derived from GFactoryBase, implements Create to instantiate the derived classes.
Definition: gfactory.h:29
Instantiates derived classes either statically or dynamically.
Definition: gfactory.h:42
void clearDLMap()
Definition: gfactory.h:158
Base * CreateObject(std::string name) const
Definition: gfactory.h:103
void RegisterObjectFactory(std::string name)
Definition: gfactory.h:87
T * LoadAndRegisterObjectFromLibrary(std::string name)
Definition: gfactory.h:124
GManager(GLogger const *logger, std::string name)
Definition: gfactory.h:73
#define EC__FACTORYNOTFOUND
Definition: gdl.h:25
void * dlhandle
Definition: gdl.h:15
#define EC__DLHANDLENOTFOUND
Definition: gdl.h:26
Structure to load dynamically symbols from a shared library.
Definition: gdl.h:33
dlhandle handle
Definition: gdl.h:81