Kangaru is an inversion of control container for C++11 and later. It supports features like operation between containers, injection via function parameter, automatic call of member functions on instance creation and much more!
Our goal is to create a DI container capable of automatic, recusive dependency injection. We also want to do most diagnostics at compile time, while keeping the simplest interface possible. On top of that, we don't want to be intrusive into user/library code.
Kangaru is a header only library because of it's extensive use of templates. The name kangaru comes from the container's feature to inject itself into a service as a dependency, and because kangaroos are awesome.
Documentation and tutorial is in the wiki and the doc
folder!
Looking for the latest stable version? Check out our release page.
Here's a quick demo to show usage of this library:
#include <kangaru/kangaru.hpp>
#include <iostream>
// Normal classes with dependency between them
struct Camera {};
struct Scene {
Camera& camera;
};
// This is the configuration of our classes.
// Structure and dependency graph is defined here.
// Camera is a single service so the service has a shared instance.
// It will be injected and returned as a reference.
struct CameraService : kgr::single_service<Camera> {};
// Scene is not single, so the container return scenes by value.
// Also, we depends on a camera to be constructed.
struct SceneService : kgr::service<Scene, kgr::dependency<CameraService>> {};
int main()
{
kgr::container container;
// The service function return instances of the normal classes.
Scene scene = container.service<SceneService>();
Camera& camera = container.service<CameraService>();
std::cout
<< std::boolalpha
<< (&scene.camera == &camera) << std::endl; // outputs true
}
Try this example online too see how it runs.
- Recursive dependency resolution
- Non intrusive. No existing classes need modification.
- You tell the container how to construct your types
- You tell the container how to store them
- You tell the container how they are injected
- Injection by setters
- Function parameter injection
- Clean and simple API
- Low runtime overhead
- Header only library
- Clean diagnostics at compile-time.
To make kangaru available on your machine, you must clone the repository and create a build directory:
$ git clone https://github.com/gracicot/kangaru.git && cd kangaru
$ mkdir build && cd build
Then use cmake to generate the makefile and export the package informations:
$ cmake ..
That's it! Link it to your project using cmake and you can already include and code!
Optionally, you can also install kangaru on your system:
# make install
You must use the find_package
function:
find_package(kangaru REQUIRED)
And then add the include dirs to your target:
target_link_libraries(<YOUR TARGET> PUBLIC kangaru)
Then you can include the library as follow:
#include <kangaru/kangaru.hpp>
There is some feature I would like to see become real. Here's a list of those, feel free to contribute!
- Tests for compile-time errors
- More test coverage
- Better messages for compile-time errors (ongoing)
- Service sources, more detail here: #41
Got suggestions or questions? Discovered a bug? Please open an issue and we'll gladly respond!
To contribute, simply open a pull request or an issue and we'll discuss together about how to make this library even more awesome!
Want to help? Pick an issue on our issue tracker!
Here's a list of projets making use of kangaru
- <to be filled>
Let me know of your projects that uses kangaru! I'll be glad to fill the list above with your project's name.