Minimal Dependency Injection Framework for C++
Single file library for implementing the dependency injection pattern
dip::instance< Service > Struct Template Reference

Injected instance of a service. More...

#include <dip.hpp>

Public Types

typedef Service * service_type
 Type of the injected instances.
 
typedef const Service * const_service_type
 Const type of the injected instances.
 

Public Member Functions

 instance ()
 Retrieve an instance providing the service.
 
 ~instance () noexcept
 Remove the instance providing the service.
 
service_type operator-> () const noexcept
 Access the instance providing the service. More...
 
Service & operator* () const noexcept
 Get the instance providing the service. More...
 
 instance (const instance &)=delete
 
 instance (instance &&)=delete
 
instanceoperator= (const instance &)=delete
 
instanceoperator= (instance &&)=delete
 

Static Public Member Functions

static void inject (const Injector< Service > &injector) noexcept
 Inject a service provider using a custom injector. More...
 
template<class Provider , typename... _Args>
static void inject_singleton (_Args &&...__args)
 Inject a service provider with singleton life cycle. More...
 
template<class Provider , typename... _Args>
static void inject_thread_singleton (_Args &&...__args)
 Inject a service provider with per-thread singleton life cycle. More...
 
template<class Provider , typename... _Args>
static void inject_transient (_Args &&...__args)
 Inject a service provider with transient life cycle. More...
 
static void clear_injection () noexcept
 Clear the injected dependency for testing purposes. More...
 

Detailed Description

template<class Service>
struct dip::instance< Service >

Injected instance of a service.

Template Parameters
ServiceService to be injected

Member Function Documentation

◆ clear_injection()

template<class Service >
static void dip::instance< Service >::clear_injection ( )
inlinestaticnoexcept

Clear the injected dependency for testing purposes.

Warning
Do not call in production code. Will cause memory leaks unless injected instances are deleted first.

◆ inject()

template<class Service >
static void dip::instance< Service >::inject ( const Injector< Service > &  injector)
inlinestaticnoexcept

Inject a service provider using a custom injector.

Parameters
injectorService injector

◆ inject_singleton()

template<class Service >
template<class Provider , typename... _Args>
static void dip::instance< Service >::inject_singleton ( _Args &&...  __args)
inlinestatic

Inject a service provider with singleton life cycle.

Template Parameters
ProviderService provider
_ArgsConstructor parameter types
Parameters
__argsConstructor parameters

◆ inject_thread_singleton()

template<class Service >
template<class Provider , typename... _Args>
static void dip::instance< Service >::inject_thread_singleton ( _Args &&...  __args)
inlinestatic

Inject a service provider with per-thread singleton life cycle.

Template Parameters
ProviderService provider
_ArgsConstructor parameter types
Parameters
__argsConstructor parameters

◆ inject_transient()

template<class Service >
template<class Provider , typename... _Args>
static void dip::instance< Service >::inject_transient ( _Args &&...  __args)
inlinestatic

Inject a service provider with transient life cycle.

Template Parameters
ProviderService provider
_ArgsConstructor parameter types
Parameters
__argsConstructor parameters

◆ operator*()

template<class Service >
Service & dip::instance< Service >::operator* ( ) const
inlinenoexcept

Get the instance providing the service.

Returns
service_type Reference to the service provider

◆ operator->()

template<class Service >
service_type dip::instance< Service >::operator-> ( ) const
inlinenoexcept

Access the instance providing the service.

Note
Ownership is not transferred
Returns
service_type Pointer to the service provider

The documentation for this struct was generated from the following file: