solinject
1.0.0
C++17 Dependency Injection header-only library
|
C++17 Dependency Injection header-only library
std::shared_ptr<>
).[^1]: A singleton service is created once and used everywhere.
[^2]: A transient service is created each time it is requested.
[^3]: 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.
[^4]: A scoped service behaves like a singleton inside its scope and derived scopes.
solinject.hpp
contains the sol::di::Container
class, and solinject-macros.hpp
provides you some handy macros for registering services.
If your service has constructor parameters, that should be injected from the container, use the FROM_DI()
macro, the FROM_DI_OPTIONAL()
macro or the FROM_DI_MULTIPLE()
macro:
The FROM_DI()
and FROM_DI_OPTIONAL()
macros inject a single instance of the service as std::shared_ptr<T>
, while the FROM_DI_MULTIPLE()
macro injects multiple instances of the service or multiple implementations of the interface as std::vector<std::shared_ptr<T>>
.
The FROM_DI_OPTIONAL()
macro should be used when the injected service is optional.
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 returnsnullptr
.
If you have a std::shared_ptr<>
or a std::unique_ptr<>
to an instance of the service, you can register it as a singleton:
If for some reason you want to go the hard way, you can register the service directly using a lambda expression:
The GetRequiredService<>()
method will throw sol::di::exc::ServiceNotRegisteredException
if the requested service is not registered. If you prefer to get an empty std::shared_ptr<>
in such cases, use the GetService<>()
method.
If you want to use scoped services, then create a scope:
and resolve your services from this scope just like you would from a container:
A scope container can do everything a regular sol::di::Container
can do. You can register services to it (even scoped services), resolve services from it etc. You can even create a scope from a scope, and then create a scope from that scope and so on.
If you want to use config files for configuring services, then registration looks a bit different:
Config file syntax example:
There are a few ways to link solinject to your project. Here's an incomplete list of them (the most recommended first):
Add these lines to your top-level CMakeLists.txt
, replacing v1.0.0
with your preferred version:
And then link the sol::solinject
target to your project's target:
In your project folder run:
Then in your top-level CMakeLists.txt
add this submodule as subdirectory and link sol::solinject
to your project's target:
Build or download the latest release, install it to your CMake packages installation prefix and link it to your project's target:
Download the latest release source code, extract it to your project's directory for dependencies (in my example it's lib
) and add it to your project as subdirectory:
solinject is a header-only library, so you can just copy everything from this project's include
directory and paste it into your project's include
directory. Make sure to add the folder containing solinject.hpp
and solinject-macros.hpp
to your compiler include directories.
solinject is a header-only library, so building it is needed only to build tests and docs and install a cmake package, which then can be linked with find_package()
.
Clone the repository and cd
into it:
At this point you can use the BuildForAllPlatformsVS.py
script for the Visual Studio generator:
or BuildForAllPlatformsUnixMakefiles.py
for the Unix Makefiles generator:
or just build the project manually for every platform you need:
The way how you should tell cmake the platform you want depends on the generator you are using.
SPDX-License-Identifier: LGPL-3.0-or-later
Copyright (C) 2022 SemperSolus0x3d
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.