BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Public Types | Public Member Functions
BeeeOn::DependencyInjector Class Reference

#include <DependencyInjector.h>

Inheritance diagram for BeeeOn::DependencyInjector:
BeeeOn::DIFactory BeeeOn::Loggable

Public Types

typedef std::map< std::string,
DIWrapper * > 
WrapperMap
 
typedef std::vector< DIWrapper * > WrapperVector
 
- Public Types inherited from BeeeOn::DIFactory
typedef Poco::SharedPtr
< DIFactory
Ptr
 

Public Member Functions

 DependencyInjector (Poco::AutoPtr< Poco::Util::AbstractConfiguration > conf, const std::vector< std::string > &libraryPaths={}, bool avoidEarly=false)
 
- Public Member Functions inherited from BeeeOn::DIFactory
DIWrappercreate (const std::string &name, bool detach=false)
 Create a new instance by name. If detached is true, the memory of the returned DIWrapper pointer MUST be explicitly deleted. Otherwise, each created instance is managed by the DIFactory.
 
template<typename T >
Poco::SharedPtr< T > create (const std::string &name, bool detach=false)
 Create a new instance by name and perform runtime cast to the target type if possible. More...
 
DIWrapperfind (const std::string &name)
 Find an existing instance and return it. More...
 
template<typename T >
Poco::SharedPtr< T > find (const std::string &name)
 Find an existing instance and cast it to the target type if possible. More...
 

Additional Inherited Members

- Protected Member Functions inherited from BeeeOn::DIFactory
template<typename T >
Poco::SharedPtr< T > cast (DIWrapper &wrapper)
 Cast the wrapped instance to the target type. If the cast is not possible, throw an exception.
 
- Protected Member Functions inherited from BeeeOn::Loggable
void setupLogger (Poco::Logger *logger=0) const
 
Poco::Logger & logger () const
 
 Loggable (const ClassInfo &info)
 
 Loggable (const std::type_info &info)
 
- Static Protected Member Functions inherited from BeeeOn::Loggable
static Poco::Logger & forMethod (const char *name)
 
static Poco::Logger & forClass (const ClassInfo &info)
 
static Poco::Logger & forClass (const std::type_info &info)
 
template<typename T >
static Poco::Logger & forInstance (const T *i)
 
static void configureSimple (Poco::Logger &logger, const std::string &level)
 
static void logException (Poco::Logger &logger, const Poco::Message::Priority priority, const Poco::Exception &e, const char *file, size_t line)
 

Detailed Description

DependencyInjector automates creation of application top-level instances and glues them together. It is inspired by the Java framework https://projects.spring.io/spring-framework/ but it is highly simplified for C++ and for the current purposes.

The instances are configured in a configuration file represented by the Poco::AbstractConfiguration. The XMLConfiguration is assumed. DependencyInjector handles tags:

The attributes can contain ${property} strings which are expanded automatically.

No root element or parent entry is considered, so it is usually necessary to pass a sub-configuration removing the initial prefix by AbstractConfiguration::createView.

Example configuration:

<factory>
     <instance name="main" class="BeeeOn::Main">
        <set name="userService" ref="userService">
        <set name="listenPort" number="8080">
        <set name="timeout" time="5 s">
        <add name="listeners" ref="loginListener">
        <add name="listeners" ref="errorListener">
        <set name="whitelist" list="192.168.5.5,192.168.15.1">
     </instance>
     <instance name="nameMap" class="BeeeOn::NameMap">
        <set name="mapping">
                <pair key="first" text="The First One">
                <pair key="second" text="The Second One">
        </set>
     </instance>
     <instance name="userService" class="BeeeOn::UserService">
     <instance name="loginListener" class="BeeeOn::LoginListener">
     <instance name="stdoutErrorListener" class="BeeeOn::StdoutErrorListener">
     <alias name="errorListener" ref="stdoutErrorListener">
     <instance name="plugin" class="BeeeOn::Plugin1" library="plugin">
</factory>
DependencyInjector di(config.createView("factory"));
Poco::SharedPtr<Main> main = di.create<Main>("main");

The documentation for this class was generated from the following files: