XUtils

Kangaru

A dependency injection container for C++11 and C++14. [MIT]


#include #include

// We define some normal classes with dependencies between them struct Camera {};

struct Scene {

Camera& camera;

};

// The following is the configuration of our user classes above. // The structure and dependency graph is defined by these configs.

// 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 {};

// Scene is not single, so the container returns scenes by value. // Also, we depend on a camera to be constructed. struct SceneService : kgr::service> {};

int main() {

kgr::container container;

// The service function returns instances of the normal classes.
Scene scene = container.service<SceneService>();
Camera& camera = container.service<CameraService>();

assert(&scene.camera == &camera); // passes, both cameras are the same instance.

}


[Try this example online](https://wandbox.org/permlink/3ekQZXqTFGRlj8ZG) to see how it runs.

#include <kangaru/kangaru.hpp>
#include <cassert>

// We define some normal classes with dependencies between them
// And we added the autowire configuration
struct Camera {
    // friend functions are faster to lookup than plain free functions
    friend auto service_map(Camera const&) -> kgr::autowire_single;
};

struct Scene {
    Camera& camera;
    
    friend auto service_map(Scene const&) -> kgr::autowire;
};

// No need for service definitions

int main()
{
    kgr::container container;
    
    // We invoke a lambda that receives injected parameters.
    // The container will figure how to wire the classes using
    // either the constructor parameters or aggregate initialization
    container.invoke([](Scene scene, Camera& camera) {
        assert(&scene.camera == &camera); // passes, both cameras are the same instance.
    });
}

Features

  • Non intrusive, no existing classes need modification
  • You tell the container how to construct your types, store and inject them
  • Injection by setters
  • Autowiring by class constructors
  • Function parameter injection
  • Clean and simple API for simple cases, flexible enough for complex cases
  • Low runtime overhead
  • Header only library
  • Clean diagnostics at compile-time

Install with vcpkg

To make kangaru available on your machine, install vcpkg. Then install the appropriate architecture. For the default, enter the following:

vcpkg install kangaru

or if you want 64-bit Windows, for example, enter:

vcpkg install kangaru:x64-windows

Adding Include Path

You must use the find_package function:

find_package(kangaru 4.3 REQUIRED)

And then add the include dirs to your target:

target_link_libraries(<YOUR TARGET> PUBLIC kangaru::kangaru)

Then you can include the library as follows:

# in your project build directory
$ cmake .. -DCMAKE_PREFIX_PATH=../../path/to/kangaru/build

Compiler Requirement

Kangaru is tested by our continuous integration with all major compiler versions. The minimum required versions are:

  • MSVC: 2015 update 3 or better
  • GCC: 4.8.5 or better
  • Clang: 3.6 or better
  • AppleClang: 7.0 or better

What’s Next

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
  • Better messages for compile-time errors (ongoing)
  • Service sources, more detail here: #41
  • Even better performance (ongoing)
  • Expose a zero-overhead interface for cases it can apply
  • Move service definitions to service map.

Got suggestions or questions? Discovered a bug? Please open an issue and we’ll gladly respond!

Running Tests

Tests are enabled using the cmake profile dev. Enabling this will make our CMake scripts to try finding the Catch2 library. We also contain a submodule for this library in our git repository in case you don’t have it available in a prefix directory. You can also enable vcpkg to download the dependencies.

Using this option adds the the test target.

You can run the tests like this:

cmake --preset dev
cmake --build --preset debug
ctest --preset debug

Who’s Using Kangaru

Here’s a list of projects making use of kangaru:

  • Our team’s game engine
  • The people I helped integrating this library into their projects
  • Surely more!

Using kangaru?

Let me know of your projects using kangaru! I’ll be glad to fill the list above with your project’s name.

Acknowledgements

A big thanks to Louis-Alexandre Vallières-Lavoie for reviewing and proposing various improvements to our documentation.


Articles

  • coming soon...