Skip to content

shoutanyoule/di

 
 

Repository files navigation

Boost Licence Version Build Status Build Status Coveralls Github Issues


[Boost].DI

Your C++14 one header only Dependency Injection library with no dependencies (Try it online!)

Quick start

Download

[Boost].DI requires only one file. Get the latest header here!

Include

#include <boost/di.hpp> namespace di = boost::di;

Compile

  • GCC/Clang
    $CXX -std=c++14 -O2 -fno-exceptions -fno-rtti -Wall -Werror -pedantic-errors file.cpp
  • MSVC
    cl /std:c++14 /Ox /W3 file.cpp

Quick guide - Create object graph

class ctor { public: explicit ctor(int i) : i(i) {} int i; }; struct aggregate { double d; }; class example { example(aggregate a, const ctor& c) { assert(87.0 == a.d); assert(42 == c.i); }; }; int main() { const auto injector = di::make_injector( di::bind<int>.to(42), di::bind<double>.to(87.0) ); injector.create<example>(); }

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64
 xor eax, eax retq 

Quick guide - Bind interfaces

struct interface { virtual ~iworld() noexcept = default; virtual int get() const = 0; }; class implementation : public interface { public: int get() const override { return 42; } }; struct example { example(std::shared_ptr<interface> i) { assert(42 == i->get()); } }; int main() { const auto injector = di::make_injector( di::bind<interface>.to<implementation>() ); injector.create<std::unique_ptr<example>>(); }

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64 (same as `make_unique`)
 push %rbx mov %rdi,%rbx mov $0x8,%edi callq 0x4008e0 <_Znwm@plt> movq $0x400c78,(%rax) mov %rax,(%rbx) mov %rbx,%rax pop %rbx retq 

Quick guide - Bind templates

template<class TPolicy = class TErrorPolicy> class simple_updater : TPolicy { void update() { TPolicy::on("update"); } }; template<class T = class TUpdater> class example { public: explicit example(const TUpdater& updater) : updater(updater) { } void update() { updater.update(); } private: const TUpdater& updater; }; int main() { struct throw_policy { void on(std::string_view str) { throw std::runtime_error(str); } }; const auto injector = di::make_injector( di::bind<class TErrorPolicy>.to<throw_policy>(), di::bind<class TUpdater>.to<simple_updater>() ); injector.create<example>().update(); }

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64
 xor eax, eax retq 

Quick guide - Bind concepts

struct Streamable { template<class T> auto requires(T&& t) -> decltype( int( t.read() ), t.write(int) ); }; template<class Exchange = Streamable(class ExchangeStream) class Engine = Streamable(class EngineStream)> class example { example(Exchange exchange, Engine engine) : exchange(exchange), engine(engine) { } }; int main() { const auto injector = di::make_injector( di::bind<Streamable(class ExchangeStream)>.to<exchange>(), di::bind<Streamable(class EngineStream)>.to<engine>() ); injector.create<example>(); }

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64
 xor eax, eax retq 


Documentation


Disclaimer Boost.DI is not an official Boost library.

About

[Boost].DI: C++14 Dependency Injection Library

Resources

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • C++ 83.6%
  • Java 9.3%
  • C# 4.1%
  • CMake 0.8%
  • Shell 0.8%
  • Python 0.7%
  • Makefile 0.7%