Git Product home page Git Product logo

di's Introduction

Boost Licence Version Linux Coveralls Github Issues Try it online


[Boost::ext].DI

Your C++14 one header only Dependency Injection library with no dependencies

Dependency Injection

https://www.youtube.com/watch?v=yVogS4NbL6U


Quick start

Download

[Boost::ext].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 {
 public:
  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>();
}

Run this example on Wandbox.

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 ~interface() 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>>();
}

Run this example on Wandbox.

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 ErrorPolicy = class TErrorPolicy>
class simple_updater {
public:
  void update() const {
    ErrorPolicy::on("update");
  }
};

template<class Updater = class TUpdater>
class example {
public:
  explicit example(const Updater& updater)
    : updater(updater)
  { }

  void update() {
    updater.update();
  }

private:
  const Updater& updater;
};

int main() {
  struct throw_policy {
    static void on(const std::string& 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();
  // Terminates with an uncaught exception because of our bound error policy
}

Run this example on Wandbox.

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 {
public:
  example(Exchange exchange, Engine engine)
    : exchange(std::move(exchange)), engine(std::move(engine))
  { }
  
private:
  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>();
}

Run this example on Wandbox.

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::ext].DI is not an official Boost library.

di's People

Contributors

abelfodil avatar alexey-bogomolov avatar archiesw avatar dvetutnev avatar gjasny avatar jaakristioja avatar jamespharvey20 avatar kanstantsin-chernik avatar kris-jusiak avatar krzysztof-jusiak avatar legalizeadulthood avatar m-gaj avatar mlimber avatar mstaz avatar nevalsar avatar nine avatar pck avatar stevefan1999-personal avatar tphnicholas avatar tralamazza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

di's Issues

session scope

//todo smart ptr instead of naked ptr

struct session
{
    template<typename T>
    struct scope
    {
         scope()
              : instance_(nullptr)
         { }

         ~scope() {
             delete instance_;
         }

         void call(const entry&)
         {
              in_scope_ = true;
         }

         void call(const exit&)
         {
              in_scope_ = false;

              delete instance_;
         }

          template<typename... Args>
          T* create(Args&& args)
          {
               if (in_scope_) {
                    return new T(args...);
               }
               return nullptr;
          }
    };
};

session<
    c1
>

injector.call<Session>(entry());
injector.create<c1>();
injector.call<Session>(exit());

scope deduction support

class impl : i {};
class c1 {CTOR(shared_ptr);};
class c2 {CTOR(unique_ptr);};

generic_module.create();//singleton
generic_module.create();//per_request

create < Type > - only shared_ptr, lvalue, ptr have sens

C c = create < shared_ptr >()
C c = create < C >()
const C& c = create < C >()
C* c = create < C* >()
const C* c = create <const C* >()

const C& c = create < const C& >() //error
const C& c = create < C& >() //error
const C& c = create < shared_ptr& >() //error

factory support

class c
{
CTOR(i*) {
}
};

class c1
{
CTOR(i*) {
}
};

di<
bind::in_call::to(42)
Factory,
, bind::in_call

injector;

factory
{
CTOR(int i) {
}

i* create(di& di) {
    switch(i) {
        return di.create<Impl1>();
    }

    return nullptr;
}

};

or

typedef id2type::bind<
E1, Impl1
E2, Impl2

factory_type;

policy: binding_correctness

Bind::in_calll

C1(double) { } //error C1 doesn't have int parameter
Compilation error requested

bidnes::correctness -> the same binding in different scopes
externals with scopes

Externals with base front end

Module <
Externals <
Annotate::With,
Annotate<Bind::InName::InCall >::With

Injector injector(Module(Set(42), Set(87)));

Named with shared_ptr

QDEPS_CTOR(Test, Named< shared_ptr, Name >)
QDEPS_CTOR(Test, Named< const shared_ptr&, Name >)
QDEPS_CTOR(Test, Named< const shared_ptr&, Name >) -> Bind<int, int_<42> >::InName

Injector.install

Injector<> injector;

injector.install(m1, m2, ...); -> Injector<M1, M2, ...>

setter injection support

class c
{
c()
: i_(0)
{ }

 void BOOST_DI_SETTER(int i) {
     i_ = i;
  }

private:
int i_;
};

di::injector<>.create();

One line binding with InCall and InName -> Apply CallStack and Name

Singleton < C >::InCall < C1, C2 >
Singleton < C >::InName < Name >
Singleton < C >::InCall < C1, C2 >::InName < Name >
Singleton < C >::InName < Name >::InCall < C1, C2 >

PerRequest < C >::InCall < C1, C2 >
PerRequest < C >::InName < Name >
PerRequest < C >::InCall < C1, C2 >::InName < Name >
PerRequest < C >::InName < Name >::InCall < C1, C2 >

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.