From 0af90763392fea3db0b37f610fb14bc7b2f0dec6 Mon Sep 17 00:00:00 2001 From: Mitchell Mosure Date: Wed, 3 Mar 2021 21:30:53 -0800 Subject: [PATCH] containerless version --- .bazelrc | 2 +- README.md | 56 +++----- include/mosure/binding.hpp | 5 +- include/mosure/container.hpp | 24 ++-- include/mosure/context.hpp | 6 +- include/mosure/factory.hpp | 4 +- include/mosure/injectable.hpp | 8 +- include/mosure/interfaces/icontainer.hpp | 31 ---- include/mosure/inversify.hpp | 1 - include/mosure/meta.hpp | 4 +- include/mosure/resolver.hpp | 29 ++-- single_include/mosure/inversify.hpp | 171 ++++++++--------------- test/auto_resolve.cpp | 28 ++-- test/constant_resolve.cpp | 29 +++- test/dynamic_resolve.cpp | 70 ++++------ test/speed.cpp | 12 +- 16 files changed, 184 insertions(+), 296 deletions(-) delete mode 100644 include/mosure/interfaces/icontainer.hpp diff --git a/.bazelrc b/.bazelrc index fd0eaaa..ea25108 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,3 +1,3 @@ build:windows --copt="/std:c++17" --copt="/W4" -build:linux --copt="-std=c++17" --copt="-O3" +build:linux --copt="-std=c++17" build:macos --copt="-std=c++17" diff --git a/README.md b/README.md index a78f5fb..d237441 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ [![GitHub Issues](https://img.shields.io/github/issues/mosure/inversify-cpp)](https://github.com/mosure/inversify-cpp/issues) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/mosure/inversify-cpp.svg)](http://isitmaintained.com/project/mosure/inversify-cpp "Average time to resolve an issue") -C++17 inversion of control and dependency injection container library. +C++17 inversion of control and static dependency injection library. ## Features * Constant, dynamic, and automatic resolvers @@ -22,11 +22,11 @@ C++17 inversion of control and dependency injection container library. ### Scope Scope manages the uniqueness of a dependency. -Singleton scopes are cached after the first resolution and will be returned on subsequent `container.get...` calls. +Singleton scopes are cached after the first resolution and will be returned on subsequent `inversify::get...` calls. -Resolution scopes are cached throughout the duration of a single `container.get...` call. A dependency tree with duplicate dependencies will resolve each to the same cached value. +Resolution scopes are cached throughout the duration of a single `inversify::get...` call. A dependency tree with duplicate dependencies will resolve each to the same cached value. -By default, the unique scope is used (except for constant values). The unique scope will resolve a unique dependency for each `container.get...` call. +By default, the unique scope is used (except for constant values). The unique scope will resolve a unique dependency for each `inversify::get...` call. ## Integration @@ -107,37 +107,31 @@ struct inversify::Injectable #### Configure Bindings -```cpp - -inversify::Container container; - -``` - ##### Constant Values Constant bindings are always singletons. ```cpp -container.bind().toConstantValue(10); -container.bind().toConstantValue(1.618); +inversify::bind().toConstantValue(10); +inversify::bind().toConstantValue(1.618); ``` ##### Dynamic Bindings -Dynamic bindings are resolved when calling `container.get...`. +Dynamic bindings are resolved when calling `inversify::get...`. -By default, dynamic bindings have resolution scope (e.g. each call to `container.get...` calls the factory). +By default, dynamic bindings have resolution scope (e.g. each call to `inversify::get...` calls the factory). Singleton scope dynamic bindings cache the first resolution of the binding. ```cpp -container.bind().toDynamicValue( +inversify::bind().toDynamicValue( [](const inversify::Context& ctx) { - auto foo = ctx.container.get(); - auto bar = ctx.container.get(); + auto foo = inversify::get(); + auto bar = inversify::get(); auto fizz = std::make_shared(foo, bar); @@ -153,11 +147,11 @@ Dynamic bindings can be used to resolve factory functions. ```cpp -container.bind().toDynamicValue( - [](const inversify::Context& ctx) { - return [&]() { - auto foo = ctx.container.get(); - auto bar = ctx.container.get(); +inversify::bind().toDynamicValue( + []() { + return [](const inversify::Context& ctx) { + auto foo = inversify::get(); + auto bar = inversify::get(); auto fizz = std::make_shared(foo, bar); @@ -176,8 +170,8 @@ Automatic bindings can generate instances, unique_ptr's, and shared_ptr's of a c ```cpp -container.bind().to(); -container.bind().to().inSingletonScope(); +inversify::bind().to(); +inversify::bind().to().inSingletonScope(); ``` @@ -185,20 +179,20 @@ container.bind().to().inSingletonScope(); ```cpp -auto bar = container.get(); +auto bar = inversify::get(); -auto fizz = container.get(); +auto fizz = inversify::get(); fizz->buzz(); -auto fizzFactory = container.get(); +auto fizzFactory = inversify::get(); auto anotherFizz = fizzFactory(); anotherFizz->buzz(); -auto autoFizzUnique = container.get(); +auto autoFizzUnique = inversify::get(); autoFizzUnique->buzz(); -auto autoFizzShared = container.get(); +auto autoFizzShared = inversify::get(); autoFizzShared->buzz(); ``` @@ -211,10 +205,6 @@ Use the following to run tests: > Note: run the example app in a similar way: `bazel run example --enable_platform_specific_config` -## TODOS -* More compile-time checks -* Resolution scope - ## Generating `single_include` Variant Run `python ./third_party/amalgamate/amalgamate.py -c ./third_party/amalgamate/config.json -s ./` from the root of the repository. diff --git a/include/mosure/binding.hpp b/include/mosure/binding.hpp index 2c68aff..3c4c3d6 100644 --- a/include/mosure/binding.hpp +++ b/include/mosure/binding.hpp @@ -3,7 +3,6 @@ #include #include -#include #include #include @@ -45,12 +44,12 @@ namespace mosure::inversify { template class Binding : public BindingTo { public: - inline T resolve(const Context& context) const { + inline T resolve() const { if (!this->resolver_) { throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding."); } - return this->resolver_->resolve(context); + return this->resolver_->resolve(); } }; diff --git a/include/mosure/container.hpp b/include/mosure/container.hpp index 8bd165d..bf9b538 100644 --- a/include/mosure/container.hpp +++ b/include/mosure/container.hpp @@ -1,8 +1,6 @@ #pragma once #include -#include -#include namespace mosure::inversify { @@ -12,20 +10,14 @@ namespace mosure::inversify { inline static inversify::Binding binding {}; }; - class Container : public inversify::IContainer { - public: - template - inline inversify::BindingTo& bind() { - return BindingLookup::binding; - } + template + inline static inversify::BindingTo& bind() { + return BindingLookup::binding; + } - template - inline typename T::value get() const { - return BindingLookup::binding.resolve(context_); - } - - private: - inversify::Context context_ { *this }; - }; + template + inline static typename T::value get() { + return BindingLookup::binding.resolve(); + } } diff --git a/include/mosure/context.hpp b/include/mosure/context.hpp index beba74c..fc2bfae 100644 --- a/include/mosure/context.hpp +++ b/include/mosure/context.hpp @@ -1,14 +1,10 @@ #pragma once -#include - namespace mosure::inversify { - class Container; - struct Context { - const inversify::IContainer& container; + // TODO: add resolution scope params }; } diff --git a/include/mosure/factory.hpp b/include/mosure/factory.hpp index ec98d4c..9362baf 100644 --- a/include/mosure/factory.hpp +++ b/include/mosure/factory.hpp @@ -2,12 +2,10 @@ #include -#include - namespace mosure::inversify { template - using Factory = std::function; + using Factory = std::function; } diff --git a/include/mosure/injectable.hpp b/include/mosure/injectable.hpp index b71c32d..24e5a7a 100644 --- a/include/mosure/injectable.hpp +++ b/include/mosure/injectable.hpp @@ -3,19 +3,21 @@ #include #include -#include #include #include namespace mosure::inversify { + template + static typename T::value get(); + template struct Inject { static_assert(meta::valid_inject_types_v, "inversify::Injectable dependencies must be of type inversify::Symbol"); - inline static auto resolve(const inversify::Context& context) { - return std::make_tuple(context.container.get()...); + inline static auto resolve() { + return std::make_tuple(inversify::get()...); } }; diff --git a/include/mosure/interfaces/icontainer.hpp b/include/mosure/interfaces/icontainer.hpp deleted file mode 100644 index 10bdb3c..0000000 --- a/include/mosure/interfaces/icontainer.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include - -#include - - -namespace mosure::inversify { - - template - class BindingTo; - - template - class IContainer { - public: - template - inversify::BindingTo& bind() { - auto crtpImplementation = static_cast(this); - - return crtpImplementation->template bind(); - } - - template - typename T::value get() const { - auto crtpImplementation = static_cast(this); - - return crtpImplementation->template get(); - } - }; - -} diff --git a/include/mosure/inversify.hpp b/include/mosure/inversify.hpp index c21eb2b..4e43693 100644 --- a/include/mosure/inversify.hpp +++ b/include/mosure/inversify.hpp @@ -37,7 +37,6 @@ SOFTWARE. #include #include -#include #include #include #include diff --git a/include/mosure/meta.hpp b/include/mosure/meta.hpp index a6b9801..8590334 100644 --- a/include/mosure/meta.hpp +++ b/include/mosure/meta.hpp @@ -3,6 +3,8 @@ #include #include +#include + namespace mosure::inversify::meta { @@ -14,7 +16,7 @@ namespace mosure::inversify::meta { template inline constexpr bool valid_inject_types_v = std::conjunction_v< - meta::is_specialization... + meta::is_specialization... >; template diff --git a/include/mosure/resolver.hpp b/include/mosure/resolver.hpp index 3a8d8fe..f8930a7 100644 --- a/include/mosure/resolver.hpp +++ b/include/mosure/resolver.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -16,7 +15,7 @@ namespace mosure::inversify { template class Resolver { public: - inline virtual T resolve(const inversify::Context&) = 0; + inline virtual T resolve() = 0; }; template @@ -27,7 +26,7 @@ namespace mosure::inversify { public: explicit ConstantResolver(T value) : value_(value) { } - inline T resolve(const inversify::Context&) override { + inline T resolve() override { return value_; } @@ -40,8 +39,8 @@ namespace mosure::inversify { public: explicit DynamicResolver(inversify::Factory factory) : factory_(factory) { } - inline T resolve(const inversify::Context& context) override { - return factory_(context); + inline T resolve() override { + return factory_(); } private: @@ -54,8 +53,8 @@ namespace mosure::inversify { template class AutoResolver : public Resolver { public: - inline T resolve(const inversify::Context& context) override { - return std::make_from_tuple(inversify::Injectable::resolve(context)); + inline T resolve() override { + return std::make_from_tuple(inversify::Injectable::resolve()); } }; @@ -66,12 +65,12 @@ namespace mosure::inversify { > class AutoResolver, U> : public Resolver> { public: - inline std::unique_ptr resolve(const inversify::Context& context) override { - auto expansion = [&context](auto&& ... deps){ + inline std::unique_ptr resolve() override { + auto expansion = [](auto&& ... deps){ return std::make_unique(deps...); }; - return std::apply(expansion, std::move(inversify::Injectable::resolve(context))); + return std::apply(expansion, std::move(inversify::Injectable::resolve())); } }; @@ -82,12 +81,12 @@ namespace mosure::inversify { > class AutoResolver, U> : public Resolver> { public: - inline std::shared_ptr resolve(const inversify::Context& context) override { - auto expansion = [&context](auto&& ... deps){ + inline std::shared_ptr resolve() override { + auto expansion = [](auto&& ... deps){ return std::make_shared(deps...); }; - return std::apply(expansion, std::move(inversify::Injectable::resolve(context))); + return std::apply(expansion, std::move(inversify::Injectable::resolve())); } }; @@ -101,10 +100,10 @@ namespace mosure::inversify { public: explicit CachedResolver(ResolverPtr parent) : parent_(parent) { } - inline T resolve(const inversify::Context& context) override { + inline T resolve() override { if (!hasCached_) { hasCached_ = true; - cached_ = parent_->resolve(context); + cached_ = parent_->resolve(); } return cached_; diff --git a/single_include/mosure/inversify.hpp b/single_include/mosure/inversify.hpp index 940e973..e3199e3 100644 --- a/single_include/mosure/inversify.hpp +++ b/single_include/mosure/inversify.hpp @@ -41,83 +41,16 @@ SOFTWARE. #include #include -// #include - - -// #include - - -#include - -// #include - - -#include - - -namespace mosure::inversify { - - template - struct Symbol { - static_assert(!std::is_abstract(), "inversify::Container cannot bind/get abstract class value (use a smart pointer instead)."); - - using value = Interface; - }; - -} - - - -namespace mosure::inversify { - - template - class BindingTo; - - template - class IContainer { - public: - template - inversify::BindingTo& bind() { - auto crtpImplementation = static_cast(this); - - return crtpImplementation->template bind(); - } - - template - typename T::value get() const { - auto crtpImplementation = static_cast(this); - - return crtpImplementation->template get(); - } - }; - -} - - - -namespace mosure::inversify { - - class Container; - - struct Context { - const inversify::IContainer& container; - }; - -} - // #include #include -// #include - - namespace mosure::inversify { template - using Factory = std::function; + using Factory = std::function; } @@ -129,8 +62,6 @@ namespace mosure::inversify { #include #include -// #include - // #include // #include @@ -139,14 +70,30 @@ namespace mosure::inversify { #include #include -// #include - // #include #include #include +// #include + + +#include + + +namespace mosure::inversify { + + template + struct Symbol { + static_assert(!std::is_abstract(), "inversify::Container cannot bind/get abstract class value (use a smart pointer instead)."); + + using value = Interface; + }; + +} + + namespace mosure::inversify::meta { @@ -158,7 +105,7 @@ namespace mosure::inversify::meta { template inline constexpr bool valid_inject_types_v = std::conjunction_v< - meta::is_specialization... + meta::is_specialization... >; template @@ -172,19 +119,20 @@ namespace mosure::inversify::meta { namespace mosure::inversify { + template + static typename T::value get(); + template struct Inject { static_assert(meta::valid_inject_types_v, "inversify::Injectable dependencies must be of type inversify::Symbol"); - inline static auto resolve(const inversify::Context& context) { - return std::make_tuple(context.container.get()...); + inline static auto resolve() { + return std::make_tuple(inversify::get()...); } }; - template - struct Injectable { - using Inject = inversify::Inject<>; - }; + template > + struct Injectable : Inject { }; } @@ -210,7 +158,7 @@ namespace mosure::inversify { template class Resolver { public: - virtual T resolve(const inversify::Context&) = 0; + inline virtual T resolve() = 0; }; template @@ -221,7 +169,7 @@ namespace mosure::inversify { public: explicit ConstantResolver(T value) : value_(value) { } - T resolve(const inversify::Context&) override { + inline T resolve() override { return value_; } @@ -234,8 +182,8 @@ namespace mosure::inversify { public: explicit DynamicResolver(inversify::Factory factory) : factory_(factory) { } - T resolve(const inversify::Context& context) override { - return factory_(context); + inline T resolve() override { + return factory_(); } private: @@ -248,8 +196,8 @@ namespace mosure::inversify { template class AutoResolver : public Resolver { public: - T resolve(const inversify::Context& context) override { - return std::make_from_tuple(inversify::Injectable::Inject::resolve(context)); + inline T resolve() override { + return std::make_from_tuple(inversify::Injectable::resolve()); } }; @@ -260,12 +208,12 @@ namespace mosure::inversify { > class AutoResolver, U> : public Resolver> { public: - std::unique_ptr resolve(const inversify::Context& context) override { - auto expansion = [&context](auto&& ... deps){ + inline std::unique_ptr resolve() override { + auto expansion = [](auto&& ... deps){ return std::make_unique(deps...); }; - return std::apply(expansion, std::move(inversify::Injectable::Inject::resolve(context))); + return std::apply(expansion, std::move(inversify::Injectable::resolve())); } }; @@ -276,26 +224,29 @@ namespace mosure::inversify { > class AutoResolver, U> : public Resolver> { public: - std::shared_ptr resolve(const inversify::Context& context) override { - auto expansion = [&context](auto&& ... deps){ + inline std::shared_ptr resolve() override { + auto expansion = [](auto&& ... deps){ return std::make_shared(deps...); }; - return std::apply(expansion, std::move(inversify::Injectable::Inject::resolve(context))); + return std::apply(expansion, std::move(inversify::Injectable::resolve())); } }; template class CachedResolver : public Resolver { - static_assert(std::is_copy_constructible_v, "inversify::CachedResolver requires a copy constructor. Are you caching a unique_ptr?"); + static_assert( + std::is_copy_constructible_v, + "inversify::CachedResolver requires a copy constructor. Are you caching a unique_ptr?" + ); public: explicit CachedResolver(ResolverPtr parent) : parent_(parent) { } - T resolve(const inversify::Context& context) override { + inline T resolve() override { if (!hasCached_) { hasCached_ = true; - cached_ = parent_->resolve(context); + cached_ = parent_->resolve(); } return cached_; @@ -348,12 +299,12 @@ namespace mosure::inversify { template class Binding : public BindingTo { public: - T resolve(const Context& context) const { + inline T resolve() const { if (!this->resolver_) { throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding."); } - return this->resolver_->resolve(context); + return this->resolver_->resolve(); } }; @@ -364,10 +315,6 @@ namespace mosure::inversify { // #include -// #include - -// #include - namespace mosure::inversify { @@ -377,26 +324,18 @@ namespace mosure::inversify { inline static inversify::Binding binding {}; }; - class Container : public inversify::IContainer { - public: - template - inversify::BindingTo& bind() { - return BindingLookup::binding; - } - - template - typename T::value get() const { - return BindingLookup::binding.resolve(context_); - } + template + inline static inversify::BindingTo& bind() { + return BindingLookup::binding; + } - private: - inversify::Context context_ { *this }; - }; + template + inline static typename T::value get() { + return BindingLookup::binding.resolve(); + } } -// #include - // #include // #include diff --git a/test/auto_resolve.cpp b/test/auto_resolve.cpp index f49a847..c39508b 100644 --- a/test/auto_resolve.cpp +++ b/test/auto_resolve.cpp @@ -12,15 +12,13 @@ namespace inversify = mosure::inversify; SCENARIO("container resolves automatic values", "[resolve]") { GIVEN("A container with automatic unique_ptr binding") { - inversify::Container container; + inversify::bind().toConstantValue(10); + inversify::bind().toConstantValue(1.618); - container.bind().toConstantValue(10); - container.bind().toConstantValue(1.618); - - container.bind().to(); + inversify::bind().to(); WHEN("the dependency is resolved") { - auto result = container.get(); + auto result = inversify::get(); auto foo = result->buzz(); THEN("the correct value is returned") { @@ -30,8 +28,8 @@ SCENARIO("container resolves automatic values", "[resolve]") { } WHEN("multiple dependencies are resolved") { - auto result1 = container.get(); - auto result2 = container.get(); + auto result1 = inversify::get(); + auto result2 = inversify::get(); result1->buzz(); result2->buzz(); @@ -45,15 +43,13 @@ SCENARIO("container resolves automatic values", "[resolve]") { } GIVEN("A container with automatic singleton shared_ptr binding") { - inversify::Container container; - - container.bind().toConstantValue(10); - container.bind().toConstantValue(1.618); + inversify::bind().toConstantValue(10); + inversify::bind().toConstantValue(1.618); - container.bind().to().inSingletonScope(); + inversify::bind().to().inSingletonScope(); WHEN("the dependency is resolved") { - auto result = container.get(); + auto result = inversify::get(); auto foo = result->buzz(); THEN("the correct value is returned") { @@ -63,8 +59,8 @@ SCENARIO("container resolves automatic values", "[resolve]") { } WHEN("multiple dependencies are resolved") { - auto result1 = container.get(); - auto result2 = container.get(); + auto result1 = inversify::get(); + auto result2 = inversify::get(); result1->buzz(); result2->buzz(); diff --git a/test/constant_resolve.cpp b/test/constant_resolve.cpp index 94d5a64..0cf6f16 100644 --- a/test/constant_resolve.cpp +++ b/test/constant_resolve.cpp @@ -10,12 +10,10 @@ namespace inversify = mosure::inversify; SCENARIO("container resolves constant values", "[resolve]") { GIVEN("A container with constant binding") { - inversify::Container container; - - container.bind().toConstantValue(10); + inversify::bind().toConstantValue(10); WHEN("the dependency is resolved") { - auto result = container.get(); + auto result = inversify::get(); THEN("the correct value is returned") { REQUIRE(result == 10); @@ -23,10 +21,10 @@ SCENARIO("container resolves constant values", "[resolve]") { } WHEN("the binding is redefined") { - container.bind().toConstantValue(20); + inversify::bind().toConstantValue(20); WHEN("the dependency is resolved") { - auto result = container.get(); + auto result = inversify::get(); THEN("the updated value is returned") { REQUIRE(result == 20); @@ -34,4 +32,23 @@ SCENARIO("container resolves constant values", "[resolve]") { } } } + + // // Multiple containers not supported in static variant + // GIVEN("Multiple containers with constant bindings") { + // inversify::Container container1; + // inversify::Container container2; + + // container1.bind().toConstantValue(3.1415); + // container2.bind().toConstantValue(1.0213); + + // WHEN("the dependency is resolved") { + // auto result1 = container1.get(); + // auto result2 = container1.get(); + + // THEN("the correct value is returned") { + // REQUIRE(result1 == 3.1415); + // REQUIRE(result2 == 1.0213); + // } + // } + // } } diff --git a/test/dynamic_resolve.cpp b/test/dynamic_resolve.cpp index c540a41..afe27b2 100644 --- a/test/dynamic_resolve.cpp +++ b/test/dynamic_resolve.cpp @@ -15,17 +15,15 @@ namespace inversify = mosure::inversify; SCENARIO("container resolves dynamic values", "[resolve]") { GIVEN("A container with dynamic binding") { - inversify::Container container; - - container.bind().toConstantValue(3); - container.bind().toDynamicValue([](const inversify::Context& ctx) { - auto foo = ctx.container.get(); + inversify::bind().toConstantValue(3); + inversify::bind().toDynamicValue([]() { + auto foo = inversify::get(); return foo * 1.5; }); WHEN("the dependency is resolved") { - auto result = container.get(); + auto result = inversify::get(); THEN("the correct value is returned") { REQUIRE(result == 4.5); @@ -33,14 +31,14 @@ SCENARIO("container resolves dynamic values", "[resolve]") { } WHEN("the binding is redefined") { - container.bind().toDynamicValue([](const inversify::Context& ctx) { - auto foo = ctx.container.get(); + inversify::bind().toDynamicValue([]() { + auto foo = inversify::get(); return foo * 2.5; }); WHEN("the dependency is resolved") { - auto result = container.get(); + auto result = inversify::get(); THEN("the updated value is returned") { REQUIRE(result == 7.5); @@ -50,16 +48,14 @@ SCENARIO("container resolves dynamic values", "[resolve]") { } GIVEN("A container with factory binding") { - inversify::Container container; - - container.bind().toConstantValue(10); - container.bind().toConstantValue(1.618); + inversify::bind().toConstantValue(10); + inversify::bind().toConstantValue(1.618); - container.bind().toDynamicValue( - [](const inversify::Context& ctx) { - return [&]() { - auto foo = ctx.container.get(); - auto bar = ctx.container.get(); + inversify::bind().toDynamicValue( + []() { + return []() { + auto foo = inversify::get(); + auto bar = inversify::get(); auto fizz = std::make_unique(foo, bar); @@ -69,7 +65,7 @@ SCENARIO("container resolves dynamic values", "[resolve]") { ); WHEN("the dependency is resolved") { - auto factory = container.get(); + auto factory = inversify::get(); WHEN("the factory is called") { auto result = factory(); @@ -85,15 +81,13 @@ SCENARIO("container resolves dynamic values", "[resolve]") { } GIVEN("A container with singleton dynamic binding") { - inversify::Container container; + inversify::bind().toConstantValue(10); + inversify::bind().toConstantValue(1.618); - container.bind().toConstantValue(10); - container.bind().toConstantValue(1.618); - - container.bind().toDynamicValue( - [](const inversify::Context& ctx) { - auto foo = ctx.container.get(); - auto bar = ctx.container.get(); + inversify::bind().toDynamicValue( + []() { + auto foo = inversify::get(); + auto bar = inversify::get(); auto fizz = std::make_shared(foo, bar); @@ -102,8 +96,8 @@ SCENARIO("container resolves dynamic values", "[resolve]") { ).inSingletonScope(); WHEN("multiple dependencies are resolved") { - auto fizz1 = container.get(); - auto fizz2 = container.get(); + auto fizz1 = inversify::get(); + auto fizz2 = inversify::get(); THEN("both dependency pointers are equal") { REQUIRE(fizz1 == fizz2); @@ -112,15 +106,13 @@ SCENARIO("container resolves dynamic values", "[resolve]") { } GIVEN("A container with resolution dynamic binding") { - inversify::Container container; - - container.bind().toConstantValue(10); - container.bind().toConstantValue(1.618); + inversify::bind().toConstantValue(10); + inversify::bind().toConstantValue(1.618); - container.bind().toDynamicValue( - [](const inversify::Context& ctx) { - auto foo = ctx.container.get(); - auto bar = ctx.container.get(); + inversify::bind().toDynamicValue( + []() { + auto foo = inversify::get(); + auto bar = inversify::get(); auto fizz = std::make_unique(foo, bar); @@ -129,8 +121,8 @@ SCENARIO("container resolves dynamic values", "[resolve]") { ); WHEN("multiple dependencies are resolved") { - auto fizz1 = container.get(); - auto fizz2 = container.get(); + auto fizz1 = inversify::get(); + auto fizz2 = inversify::get(); THEN("dependencies are unique") { REQUIRE(fizz1 != fizz2); diff --git a/test/speed.cpp b/test/speed.cpp index ddaac95..b6c4981 100644 --- a/test/speed.cpp +++ b/test/speed.cpp @@ -90,19 +90,17 @@ struct inversify::Injectable SCENARIO("container resolves automatic values quickly", "[performance]") { GIVEN("A container with an auto binding chain") { - inversify::Container container; - - container.bind().toConstantValue(10); - container.bind().to(); - container.bind().to(); - container.bind().to(); + inversify::bind().toConstantValue(10); + inversify::bind().to(); + inversify::bind().to(); + inversify::bind().to(); WHEN("the dependency is resolved") { const int iterations = 1000000; auto inversify_start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < iterations; ++i) { - container.get(); + inversify::get(); } auto inversify_finish = std::chrono::high_resolution_clock::now(); auto inversify_us = std::chrono::duration_cast(inversify_finish - inversify_start);