solinject  1.0.0
C++17 Dependency Injection header-only library
Macros.hpp File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define FROM_DI(class_)   (c.template GetRequiredService<class_>())
 Injects a required service from a DI container. More...
 
#define FROM_DI_OPTIONAL(class_)   (c.template GetService<class_>())
 Injects an optional service from a DI container. More...
 
#define FROM_DI_MULTIPLE(class_)   (c.template GetServices<class_>())
 Injects multiple instances of a service from a DI container. More...
 
#define FACTORY(class_, ...)
 Service factory. More...
 
#define RegisterSingletonService(container, class_, ...)   (container).template RegisterSingletonService<class_>(FACTORY(class_, __VA_ARGS__))
 Registers a service with singleton lifetime. More...
 
#define RegisterSingletonInterface(container, interface_, implementation, ...)   (container).template RegisterSingletonService<interface_>(FACTORY(implementation, __VA_ARGS__))
 Registers a service with singleton lifetime as an implementation of a specific interface. More...
 
#define RegisterTransientService(container, class_, ...)   (container).template RegisterTransientService<class_>(FACTORY(class_, __VA_ARGS__))
 Registers a service with transient lifetime. More...
 
#define RegisterTransientInterface(container, interface_, implementation, ...)   (container).template RegisterTransientService<interface_>(FACTORY(implementation, __VA_ARGS__))
 Registers a service with transient lifetime as an implementation of a specific interface. More...
 
#define RegisterSharedService(container, class_, ...)   (container).template RegisterSharedService<class_>(FACTORY(class_, __VA_ARGS__))
 Registers a service with shared lifetime. More...
 
#define RegisterSharedInterface(container, interface_, implementation, ...)   (container).template RegisterSharedService<interface_>(FACTORY(implementation, __VA_ARGS__))
 Registers a service with shared lifetime as an implementation of a specific interface. More...
 
#define RegisterScopedService(container, class_, ...)   (container).template RegisterScopedService<class_>(FACTORY(class_, __VA_ARGS__))
 Registers a service with scoped lifetime. More...
 
#define RegisterScopedInterface(container, interface_, implementation, ...)   (container).template RegisterScopedService<interface_>(FACTORY(implementation, __VA_ARGS__))
 Registers a service with scoped lifetime as an implementation of a specific interface. More...
 

Macro Definition Documentation

◆ FACTORY

#define FACTORY (   class_,
  ... 
)
Value:
[](const sol::di::Container& c) \
{ \
return std::make_shared<class_>(__VA_ARGS__); \
}

Service factory.

Parameters
class_the service type
...the service constructor parameters

◆ FROM_DI

#define FROM_DI (   class_)    (c.template GetRequiredService<class_>())

Injects a required service from a DI container.

Parameters
class_service type

The service will be injected as a std::shared_ptr. If the service is not registered, an exception will be thrown.

This macro is intended for use as a service constructor argument for the following macros:

See also
sol::di::exc::ServiceNotRegisteredException

◆ FROM_DI_MULTIPLE

#define FROM_DI_MULTIPLE (   class_)    (c.template GetServices<class_>())

Injects multiple instances of a service from a DI container.

Parameters
class_service type

The service will be injected as a std::vector< std::shared_ptr<> >. If the service is not registered, an empty std::vector< std::shared_ptr<> > will be injected.

This macro is intended for use as a service constructor argument for the following macros:

◆ FROM_DI_OPTIONAL

#define FROM_DI_OPTIONAL (   class_)    (c.template GetService<class_>())

Injects an optional service from a DI container.

Parameters
class_service type

The service will be injected as a std::shared_ptr. If the service is not registered, an empty std::shared_ptr will be injected.

Warning
Optional here means that the service may or may not be registered, and it doesn't mean that the service may be registered with nullptr or a factory function that returns nullptr.

This macro is intended for use as a service constructor argument for the following macros:

◆ RegisterScopedInterface

#define RegisterScopedInterface (   container,
  interface_,
  implementation,
  ... 
)    (container).template RegisterScopedService<interface_>(FACTORY(implementation, __VA_ARGS__))

Registers a service with scoped lifetime as an implementation of a specific interface.

Parameters
containerDI container
interface_interface type
implementationtype of the implementation of the interface
...service constructor arguments

A scoped service behaves like a singleton inside its scope and derived scopes.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterScopedService

#define RegisterScopedService (   container,
  class_,
  ... 
)    (container).template RegisterScopedService<class_>(FACTORY(class_, __VA_ARGS__))

Registers a service with scoped lifetime.

Parameters
containerDI container
class_service type
...service constructor arguments

A scoped service behaves like a singleton inside its scope and derived scopes.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterSharedInterface

#define RegisterSharedInterface (   container,
  interface_,
  implementation,
  ... 
)    (container).template RegisterSharedService<interface_>(FACTORY(implementation, __VA_ARGS__))

Registers a service with shared lifetime as an implementation of a specific interface.

Parameters
containerDI container
interface_interface type
implementationtype of the implementation of the interface
...service constructor arguments

A shared service exists while it is used. When the shared service is requested, if an instance already exists, that instance is returned; otherwise a new instance is created.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterSharedService

#define RegisterSharedService (   container,
  class_,
  ... 
)    (container).template RegisterSharedService<class_>(FACTORY(class_, __VA_ARGS__))

Registers a service with shared lifetime.

Parameters
containerDI container
class_service type
...service constructor arguments

A shared service exists while it is used. When the shared service is requested, if an instance already exists, that instance is returned; otherwise a new instance is created.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterSingletonInterface

#define RegisterSingletonInterface (   container,
  interface_,
  implementation,
  ... 
)    (container).template RegisterSingletonService<interface_>(FACTORY(implementation, __VA_ARGS__))

Registers a service with singleton lifetime as an implementation of a specific interface.

Parameters
containerDI container
interface_interface type
implementationtype of the implementation of the interface
...service constructor arguments

A singleton service is created once and used everywhere.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterSingletonService

#define RegisterSingletonService (   container,
  class_,
  ... 
)    (container).template RegisterSingletonService<class_>(FACTORY(class_, __VA_ARGS__))

Registers a service with singleton lifetime.

Parameters
containerDI container
class_service type
...service constructor arguments

A singleton service is created once and used everywhere.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterTransientInterface

#define RegisterTransientInterface (   container,
  interface_,
  implementation,
  ... 
)    (container).template RegisterTransientService<interface_>(FACTORY(implementation, __VA_ARGS__))

Registers a service with transient lifetime as an implementation of a specific interface.

Parameters
containerDI container
interface_interface type
implementationtype of the implementation of the interface
...service constructor arguments

A transient service is created each time it is requested.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE

◆ RegisterTransientService

#define RegisterTransientService (   container,
  class_,
  ... 
)    (container).template RegisterTransientService<class_>(FACTORY(class_, __VA_ARGS__))

Registers a service with transient lifetime.

Parameters
containerDI container
class_service type
...service constructor arguments

A transient service is created each time it is requested.

See also
FROM_DI
FROM_DI_OPTIONAL
FROM_DI_MULTIPLE
sol::di::Container
Dependency Injection container.
Definition: Container.hpp:48