BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
DependencyInjector.h
1 #pragma once
2 
3 #include <map>
4 
5 #include <Poco/Exception.h>
6 #include <Poco/AutoPtr.h>
7 #include <Poco/SharedPtr.h>
8 #include <Poco/SharedLibrary.h>
9 #include <Poco/Logger.h>
10 #include <Poco/Util/AbstractConfiguration.h>
11 
12 #include "di/DIFactory.h"
13 #include "di/DIWrapper.h"
14 #include "util/Loggable.h"
15 
16 namespace BeeeOn {
17 
18 class InstanceInfo;
19 
110 class DependencyInjector : public DIFactory, protected Loggable {
111 public:
112  typedef std::map<std::string, DIWrapper *> WrapperMap;
113  typedef std::vector<DIWrapper *> WrapperVector;
114 
116  Poco::AutoPtr<Poco::Util::AbstractConfiguration> conf,
117  const std::vector<std::string> &libraryPaths = {},
118  bool avoidEarly = false);
119 
121 
122 private:
126  DIWrapper *findImpl(const std::string &name) override;
127 
134  DIWrapper *createImpl(
135  const std::string &name,
136  bool detach) override;
137 
142  void castImpl(
143  const std::type_info &type,
144  const DIWrapper &wrapper,
145  void *target) override;
146 
151  void computeConstants();
152 
156  void createEarly();
157 
158  void destroyOne(DIWrapper *one) const;
159  void cleanup(const WrapperVector vec) const;
160  WrapperVector tryDestroy(const WrapperVector vec) const;
161  void destroyRest(const WrapperVector vec) const;
162 
167  void destroyAll();
168 
169  DIWrapper *resolveAndCreate(
170  const std::string &name,
171  bool disown);
172 
173  DIWrapper *createNoAlias(
174  const InstanceInfo &info,
175  bool disown = false);
176 
181  void loadLibrary(Poco::SharedLibrary &library, const std::string &name) const;
182 
187  DIWrapper *createNew(const InstanceInfo &info);
188 
189  DIWrapper *injectDependencies(
190  const InstanceInfo &info,
191  DIWrapper *target);
192  void injectValue(const InstanceInfo &info,
193  DIWrapper *target,
194  const std::string &key,
195  const std::string &name);
196 
197  void createAndInjectRef(
198  const std::string &targetName,
199  DIWrapper *target,
200  const std::string &name,
201  const std::string &value);
202  bool tryInjectRef(const InstanceInfo &info,
203  DIWrapper *target,
204  const std::string &key,
205  const std::string &name);
206 
207  void evalAndInjectNumber(
208  const std::string &targetName,
209  DIWrapper *target,
210  const std::string &name,
211  const std::string &value);
212  bool tryInjectNumber(const InstanceInfo &info,
213  DIWrapper *target,
214  const std::string &key,
215  const std::string &name);
216  bool tryInjectText(const InstanceInfo &info,
217  DIWrapper *target,
218  const std::string &key,
219  const std::string &name);
220  bool tryInjectTime(const InstanceInfo &info,
221  DIWrapper *target,
222  const std::string &key,
223  const std::string &name);
224  bool tryInjectList(const InstanceInfo &info,
225  DIWrapper *target,
226  const std::string &key,
227  const std::string &name);
228  bool tryInjectMap(const InstanceInfo &info,
229  DIWrapper *target,
230  const std::string &key,
231  const std::string &name);
232 
233 private:
234  WrapperMap m_set;
235  WrapperVector m_free;
236  Poco::AutoPtr<Poco::Util::AbstractConfiguration> m_conf;
237  std::vector<std::string> m_librariesPaths;
238  std::map<std::string, Poco::SharedLibrary> m_libraries;
239 };
240 
241 }
Definition: DependencyInjector.h:110
Definition: DependencyInjector.cpp:24
Definition: DIWrapper.h:386
Definition: Loggable.h:19
Implementation of DIFactory can manage and create instances by name and automatically (usually based ...
Definition: DIFactory.h:18