diff --git a/README.md b/README.md index 817478e..b1db58d 100644 --- a/README.md +++ b/README.md @@ -60,14 +60,13 @@ using IFizzPtr = std::unique_ptr; ```cpp namespace symbols { - inline const inversify::Symbol foo { "Foo" }; - inline const inversify::Symbol bar { "Bar" }; - inline const inversify::Symbol fizz { "Fizz" }; - inline const inversify::Symbol fizzFactory { "FizzFactory" }; - - inline const inversify::Symbol autoFizz { "AutoFizz" }; - inline const inversify::Symbol autoFizzUnique { "AutoFizzUnique" }; - inline const inversify::Symbol autoFizzShared { "AutoFizzShared" }; + inline extern inversify::Symbol foo {}; + inline extern inversify::Symbol bar {}; + inline extern inversify::Symbol fizz {}; + inline extern inversify::Symbol> fizzFactory {}; + + inline extern inversify::Symbol autoFizzUnique {}; + inline extern inversify::Symbol autoFizzShared {}; } ``` @@ -97,8 +96,8 @@ struct Fizz : IFizz { }; inline static auto injectFizz = inversify::Injectable::inject( - inversify::Inject(symbols::foo), - inversify::Inject(symbols::bar) + symbols::foo, + symbols::bar ); ``` @@ -107,7 +106,7 @@ inline static auto injectFizz = inversify::Injectable::inject( ```cpp -inversify::Container container {}; +inversify::Container container; ``` @@ -117,8 +116,8 @@ Constant bindings are always singletons. ```cpp -container.bind(symbols::foo).toConstantValue(10); -container.bind(symbols::bar).toConstantValue(1.618); +container.bind(symbols::foo).toConstantValue(10); +container.bind(symbols::bar).toConstantValue(1.618); ``` @@ -132,10 +131,10 @@ Singleton scope dynamic bindings cache the first resolution of the binding. ```cpp -container.bind(symbols::fizz).toDynamicValue( +container.bind(symbols::fizz).toDynamicValue( [](const inversify::Context& ctx) { - auto foo = ctx.container.get(symbols::foo); - auto bar = ctx.container.get(symbols::bar); + auto foo = ctx.container.get(symbols::foo); + auto bar = ctx.container.get(symbols::bar); auto fizz = std::make_shared(foo, bar); @@ -151,11 +150,11 @@ Dynamic bindings can be used to resolve factory functions. ```cpp -container.bind>(symbols::fizzFactory).toDynamicValue( +container.bind(symbols::fizzFactory).toDynamicValue( [](const inversify::Context& ctx) { return [&]() { - auto foo = ctx.container.get(symbols::foo); - auto bar = ctx.container.get(symbols::bar); + auto foo = ctx.container.get(symbols::foo); + auto bar = ctx.container.get(symbols::bar); auto fizz = std::make_shared(foo, bar); @@ -174,9 +173,8 @@ Automatic bindings can generate instances, unique_ptr's, and shared_ptr's of a c ```cpp -container.bind(symbols::autoFizz).to(); -container.bind(symbols::autoFizzUnique).to(); -container.bind(symbols::autoFizzShared).to().inSingletonScope(); +container.bind(symbols::autoFizzUnique).to(); +container.bind(symbols::autoFizzShared).to().inSingletonScope(); ``` @@ -184,23 +182,20 @@ container.bind(symbols::autoFizzShared).to().inSingletonSc ```cpp -auto bar = container.get(symbols::bar); +auto bar = container.get(symbols::bar); -auto fizz = container.get(symbols::fizz); +auto fizz = container.get(symbols::fizz); fizz->buzz(); -auto fizzFactory = container.get>(symbols::fizzFactory); +auto fizzFactory = container.get(symbols::fizzFactory); auto anotherFizz = fizzFactory(); anotherFizz->buzz(); -auto autoFizz = container.get(symbols::autoFizz); -autoFizz.buzz(); - -auto autoFizzUnique = container.get(symbols::autoFizzUnique); +auto autoFizzUnique = container.get(symbols::autoFizzUnique); autoFizzUnique->buzz(); -auto autoFizzShared = container.get(symbols::autoFizzShared); +auto autoFizzShared = container.get(symbols::autoFizzShared); autoFizzShared->buzz(); ``` diff --git a/example/api/symbols.hpp b/example/api/symbols.hpp index 12b2927..d6af5f1 100644 --- a/example/api/symbols.hpp +++ b/example/api/symbols.hpp @@ -2,10 +2,14 @@ #include +#include +#include +#include + namespace inversify = mosure::inversify; namespace symbols { - inline const inversify::Symbol logger { "Logger" }; - inline const inversify::Symbol service { "Service" }; - inline const inversify::Symbol settings { "Settings" }; + inline extern inversify::Symbol logger {}; + inline extern inversify::Symbol service {}; + inline extern inversify::Symbol settings {}; } diff --git a/example/src/logger.hpp b/example/src/logger.hpp index 2da489d..812d40e 100644 --- a/example/src/logger.hpp +++ b/example/src/logger.hpp @@ -54,6 +54,4 @@ class Logger : public ILogger { }; namespace inversify = mosure::inversify; -inline static auto injectLogger = inversify::Injectable::inject( - inversify::Inject(symbols::settings) -); +inline static auto injectLogger = inversify::Injectable::inject(symbols::settings); diff --git a/example/src/main.cpp b/example/src/main.cpp index 426e426..2ca9f27 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -11,15 +11,15 @@ namespace inversify = mosure::inversify; int main() { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::logger).to().inSingletonScope(); - container.bind(symbols::service).to(); - container.bind(symbols::settings).to().inSingletonScope(); + container.bind(symbols::logger).to().inSingletonScope(); + container.bind(symbols::service).to(); + container.bind(symbols::settings).to().inSingletonScope(); //container.bind(symbols::logger).to().inSingletonScope(); - container.get(symbols::service)->run(); + container.get(symbols::service)->run(); return 0; } diff --git a/example/src/service.hpp b/example/src/service.hpp index 50ab4b4..eb04be1 100644 --- a/example/src/service.hpp +++ b/example/src/service.hpp @@ -25,6 +25,4 @@ class Service : public IService { }; namespace inversify = mosure::inversify; -inline static auto injectService = inversify::Injectable::inject( - inversify::Inject(symbols::logger) -); +inline static auto injectService = inversify::Injectable::inject(symbols::logger); diff --git a/include/mosure/binding.hpp b/include/mosure/binding.hpp index 60d754c..5a853b8 100644 --- a/include/mosure/binding.hpp +++ b/include/mosure/binding.hpp @@ -46,21 +46,21 @@ namespace mosure::inversify { template class Binding : public BindingTo { public: - explicit Binding(const inversify::Symbol& symbol) + explicit Binding(const inversify::Symbol& symbol) : symbol_(symbol) { } T resolve(const Context& context) const { if (!this->resolver_) { - throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding: " + symbol_); + throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding."); } return this->resolver_->resolve(context); } private: - const inversify::Symbol& symbol_; + const inversify::Symbol& symbol_; }; } diff --git a/include/mosure/container.hpp b/include/mosure/container.hpp index f6b4950..e4a1015 100644 --- a/include/mosure/container.hpp +++ b/include/mosure/container.hpp @@ -16,36 +16,36 @@ namespace mosure::inversify { class Container : public inversify::IContainer { public: template - inversify::BindingTo& bind(const inversify::Symbol& type) { + inversify::BindingTo& bind(const inversify::Symbol& type) { static_assert(!std::is_abstract(), "inversify::Container cannot bind/get abstract class value (use a smart pointer instead)."); - auto binding = inversify::Binding(type); + auto binding = inversify::Binding(type); - auto lookup = bindings_.find(type); + auto lookup = bindings_.find(type.id); if (lookup != bindings_.end()) { - bindings_.erase(type); + bindings_.erase(type.id); } - auto pair = std::make_pair(type, std::any(binding)); + auto pair = std::make_pair(type.id, std::any(binding)); bindings_.insert(pair); - return std::any_cast&>(bindings_.at(type)); + return std::any_cast&>(bindings_.at(type.id)); } template - T get(const inversify::Symbol& type) const { - auto symbolBinding = bindings_.find(type); + T get(const inversify::Symbol& type) const { + auto symbolBinding = bindings_.find(type.id); if (symbolBinding == bindings_.end()) { - throw inversify::exceptions::SymbolException(type); + throw inversify::exceptions::SymbolException(); } - auto binding = std::any_cast>(symbolBinding->second); + auto binding = std::any_cast>(symbolBinding->second); return binding.resolve(context_); } private: - std::unordered_map bindings_ { }; + std::unordered_map bindings_ { }; inversify::Context context_ { *this }; }; diff --git a/include/mosure/exceptions/symbol.hpp b/include/mosure/exceptions/symbol.hpp index b49584b..e5e1e97 100644 --- a/include/mosure/exceptions/symbol.hpp +++ b/include/mosure/exceptions/symbol.hpp @@ -2,13 +2,11 @@ #include -#include - namespace mosure::inversify::exceptions { struct SymbolException : public std::runtime_error { - explicit SymbolException(const inversify::Symbol& symbol) : std::runtime_error("inversify::Symbol not found: " + symbol) { } + explicit SymbolException() : std::runtime_error("inversify::Symbol not found.") { } }; } diff --git a/include/mosure/injectable.hpp b/include/mosure/injectable.hpp index 16fa400..6e30d01 100644 --- a/include/mosure/injectable.hpp +++ b/include/mosure/injectable.hpp @@ -12,22 +12,9 @@ namespace mosure::inversify { - struct InjectBase { - explicit InjectBase(const inversify::Symbol& symbol) : symbol(symbol) { } - - inversify::Symbol symbol; - }; - - template - struct Inject : InjectBase { - explicit Inject(inversify::Symbol symbol) : InjectBase(symbol) { } - - using value = Interface; - }; - template inline constexpr bool valid_inject_types_v = std::conjunction_v< - is_specialization... + meta::is_specialization... >; template @@ -39,7 +26,7 @@ namespace mosure::inversify { template inline static Injectable inject(Dependencies... dependencies) { - static_assert(valid_inject_types_v, "inversify::Injectable dependencies must be of type inversify::Inject"); + static_assert(valid_inject_types_v, "inversify::Injectable dependencies must be of type inversify::Symbol"); factory = [ deps = std::make_tuple(dependencies...) @@ -79,10 +66,10 @@ namespace mosure::inversify { private: template inline static typename Dependency::value resolve_dependency(const inversify::Context& context, Dependency dep) { - auto symbol = static_cast(dep).symbol; - using Interface = typename Dependency::value; - return context.container.template get(symbol); + auto symbol = static_cast>(dep); + + return context.container.get(symbol); } }; diff --git a/include/mosure/interfaces/icontainer.hpp b/include/mosure/interfaces/icontainer.hpp index d1abaa2..b580305 100644 --- a/include/mosure/interfaces/icontainer.hpp +++ b/include/mosure/interfaces/icontainer.hpp @@ -14,17 +14,17 @@ namespace mosure::inversify { class IContainer { public: template - inversify::BindingTo& bind(const inversify::Symbol& type) { + inversify::BindingTo& bind(const inversify::Symbol& type) { auto crtpImplementation = static_cast(this); - return crtpImplementation->template bind(type); + return crtpImplementation->bind(type); } template - T get(const inversify::Symbol& type) const { + T get(const inversify::Symbol& type) const { auto crtpImplementation = static_cast(this); - return crtpImplementation->template get(type); + return crtpImplementation->get(type); } }; diff --git a/include/mosure/meta.hpp b/include/mosure/meta.hpp index ff0aead..a14b925 100644 --- a/include/mosure/meta.hpp +++ b/include/mosure/meta.hpp @@ -1,9 +1,10 @@ #pragma once #include +#include -namespace mosure::inversify { +namespace mosure::inversify::meta { template class Template> struct is_specialization : std::false_type { }; diff --git a/include/mosure/resolver.hpp b/include/mosure/resolver.hpp index 1208973..ed6cadb 100644 --- a/include/mosure/resolver.hpp +++ b/include/mosure/resolver.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -101,7 +102,6 @@ namespace mosure::inversify { explicit CachedResolver(ResolverPtr parent) : parent_(parent) { } T resolve(const inversify::Context& context) override { - // TODO: add lock for multi-thread support if (!hasCached_) { hasCached_ = true; cached_ = parent_->resolve(context); @@ -112,7 +112,7 @@ namespace mosure::inversify { private: T cached_; - bool hasCached_ { false }; + std::atomic hasCached_ { false }; ResolverPtr parent_; }; diff --git a/include/mosure/symbol.hpp b/include/mosure/symbol.hpp index 9aa862c..5188454 100644 --- a/include/mosure/symbol.hpp +++ b/include/mosure/symbol.hpp @@ -1,10 +1,20 @@ #pragma once -#include +#include namespace mosure::inversify { - using Symbol = std::string; + namespace { + inline static int counter = 0; + } + + template + struct Symbol { + Symbol() : id(counter++) { } + + const int id; + using value = Interface; + }; } diff --git a/single_include/mosure/inversify.hpp b/single_include/mosure/inversify.hpp index 7e45f66..4f8b9fd 100644 --- a/single_include/mosure/inversify.hpp +++ b/single_include/mosure/inversify.hpp @@ -55,12 +55,22 @@ SOFTWARE. // #include -#include +#include namespace mosure::inversify { - using Symbol = std::string; + namespace { + inline static int counter = 0; + } + + template + struct Symbol { + Symbol() : id(counter++) { } + + const int id; + using value = Interface; + }; } @@ -75,17 +85,17 @@ namespace mosure::inversify { class IContainer { public: template - inversify::BindingTo& bind(const inversify::Symbol& type) { + inversify::BindingTo& bind(const inversify::Symbol& type) { auto crtpImplementation = static_cast(this); - return crtpImplementation->template bind(type); + return crtpImplementation->bind(type); } template - T get(const inversify::Symbol& type) const { + T get(const inversify::Symbol& type) const { auto crtpImplementation = static_cast(this); - return crtpImplementation->template get(type); + return crtpImplementation->get(type); } }; @@ -122,6 +132,7 @@ namespace mosure::inversify { // #include +#include #include #include @@ -144,9 +155,10 @@ namespace mosure::inversify { #include +#include -namespace mosure::inversify { +namespace mosure::inversify::meta { template class Template> struct is_specialization : std::false_type { }; @@ -162,22 +174,9 @@ namespace mosure::inversify { namespace mosure::inversify { - struct InjectBase { - explicit InjectBase(const inversify::Symbol& symbol) : symbol(symbol) { } - - inversify::Symbol symbol; - }; - - template - struct Inject : InjectBase { - explicit Inject(inversify::Symbol symbol) : InjectBase(symbol) { } - - using value = Interface; - }; - template inline constexpr bool valid_inject_types_v = std::conjunction_v< - is_specialization... + meta::is_specialization... >; template @@ -189,7 +188,7 @@ namespace mosure::inversify { template inline static Injectable inject(Dependencies... dependencies) { - static_assert(valid_inject_types_v, "inversify::Injectable dependencies must be of type inversify::Inject"); + static_assert(valid_inject_types_v, "inversify::Injectable dependencies must be of type inversify::Symbol"); factory = [ deps = std::make_tuple(dependencies...) @@ -229,10 +228,10 @@ namespace mosure::inversify { private: template inline static typename Dependency::value resolve_dependency(const inversify::Context& context, Dependency dep) { - auto symbol = static_cast(dep).symbol; - using Interface = typename Dependency::value; - return context.container.template get(symbol); + auto symbol = static_cast>(dep); + + return context.container.get(symbol); } }; @@ -347,7 +346,6 @@ namespace mosure::inversify { explicit CachedResolver(ResolverPtr parent) : parent_(parent) { } T resolve(const inversify::Context& context) override { - // TODO: add lock for multi-thread support if (!hasCached_) { hasCached_ = true; cached_ = parent_->resolve(context); @@ -358,7 +356,7 @@ namespace mosure::inversify { private: T cached_; - bool hasCached_ { false }; + std::atomic hasCached_ { false }; ResolverPtr parent_; }; @@ -405,21 +403,21 @@ namespace mosure::inversify { template class Binding : public BindingTo { public: - explicit Binding(const inversify::Symbol& symbol) + explicit Binding(const inversify::Symbol& symbol) : symbol_(symbol) { } T resolve(const Context& context) const { if (!this->resolver_) { - throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding: " + symbol_); + throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding."); } return this->resolver_->resolve(context); } private: - const inversify::Symbol& symbol_; + const inversify::Symbol& symbol_; }; } @@ -442,14 +440,11 @@ namespace mosure::inversify { #include -// #include - - namespace mosure::inversify::exceptions { struct SymbolException : public std::runtime_error { - explicit SymbolException(const inversify::Symbol& symbol) : std::runtime_error("inversify::Symbol not found: " + symbol) { } + explicit SymbolException() : std::runtime_error("inversify::Symbol not found.") { } }; } @@ -463,36 +458,36 @@ namespace mosure::inversify { class Container : public inversify::IContainer { public: template - inversify::BindingTo& bind(const inversify::Symbol& type) { + inversify::BindingTo& bind(const inversify::Symbol& type) { static_assert(!std::is_abstract(), "inversify::Container cannot bind/get abstract class value (use a smart pointer instead)."); - auto binding = inversify::Binding(type); + auto binding = inversify::Binding(type); - auto lookup = bindings_.find(type); + auto lookup = bindings_.find(type.id); if (lookup != bindings_.end()) { - bindings_.erase(type); + bindings_.erase(type.id); } - auto pair = std::make_pair(type, std::any(binding)); + auto pair = std::make_pair(type.id, std::any(binding)); bindings_.insert(pair); - return std::any_cast&>(bindings_.at(type)); + return std::any_cast&>(bindings_.at(type.id)); } template - T get(const inversify::Symbol& type) const { - auto symbolBinding = bindings_.find(type); + T get(const inversify::Symbol& type) const { + auto symbolBinding = bindings_.find(type.id); if (symbolBinding == bindings_.end()) { - throw inversify::exceptions::SymbolException(type); + throw inversify::exceptions::SymbolException(); } - auto binding = std::any_cast>(symbolBinding->second); + auto binding = std::any_cast>(symbolBinding->second); return binding.resolve(context_); } private: - std::unordered_map bindings_ { }; + std::unordered_map bindings_ { }; inversify::Context context_ { *this }; }; diff --git a/test/auto_resolve.cpp b/test/auto_resolve.cpp index 5f661c4..fc4da5f 100644 --- a/test/auto_resolve.cpp +++ b/test/auto_resolve.cpp @@ -11,48 +11,16 @@ namespace inversify = mosure::inversify; SCENARIO("container resolves automatic values", "[resolve]") { - GIVEN("A container with automatic binding") { - inversify::Container container {}; - - container.bind(symbols::foo).toConstantValue(10); - container.bind(symbols::bar).toConstantValue(1.618); - - container.bind(symbols::autoFizz).to(); - - WHEN("the dependency is resolved") { - auto result = container.get(symbols::autoFizz); - auto foo = result.buzz(); - - THEN("the correct value is returned") { - REQUIRE(foo == 10); - REQUIRE(result.counter == 1); - } - } - - WHEN("multiple dependencies are resolved") { - auto result1 = container.get(symbols::autoFizz); - auto result2 = container.get(symbols::autoFizz); - - result1.buzz(); - result2.buzz(); - - THEN("the values are unique") { - REQUIRE(result1.counter == 1); - REQUIRE(result2.counter == 1); - } - } - } - GIVEN("A container with automatic unique_ptr binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(10); - container.bind(symbols::bar).toConstantValue(1.618); + container.bind(symbols::foo).toConstantValue(10); + container.bind(symbols::bar).toConstantValue(1.618); - container.bind(symbols::autoFizzUnique).to(); + container.bind(symbols::autoFizzUnique).to(); WHEN("the dependency is resolved") { - auto result = container.get(symbols::autoFizzUnique); + auto result = container.get(symbols::autoFizzUnique); auto foo = result->buzz(); THEN("the correct value is returned") { @@ -62,8 +30,8 @@ SCENARIO("container resolves automatic values", "[resolve]") { } WHEN("multiple dependencies are resolved") { - auto result1 = container.get(symbols::autoFizzUnique); - auto result2 = container.get(symbols::autoFizzUnique); + auto result1 = container.get(symbols::autoFizzUnique); + auto result2 = container.get(symbols::autoFizzUnique); result1->buzz(); result2->buzz(); @@ -77,15 +45,15 @@ SCENARIO("container resolves automatic values", "[resolve]") { } GIVEN("A container with automatic singleton shared_ptr binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(10); - container.bind(symbols::bar).toConstantValue(1.618); + container.bind(symbols::foo).toConstantValue(10); + container.bind(symbols::bar).toConstantValue(1.618); - container.bind(symbols::autoFizzShared).to().inSingletonScope(); + container.bind(symbols::autoFizzShared).to().inSingletonScope(); WHEN("the dependency is resolved") { - auto result = container.get(symbols::autoFizzShared); + auto result = container.get(symbols::autoFizzShared); auto foo = result->buzz(); THEN("the correct value is returned") { @@ -95,8 +63,8 @@ SCENARIO("container resolves automatic values", "[resolve]") { } WHEN("multiple dependencies are resolved") { - auto result1 = container.get(symbols::autoFizzShared); - auto result2 = container.get(symbols::autoFizzShared); + auto result1 = container.get(symbols::autoFizzShared); + auto result2 = container.get(symbols::autoFizzShared); result1->buzz(); result2->buzz(); diff --git a/test/constant_resolve.cpp b/test/constant_resolve.cpp index 8b887a0..90d4bec 100644 --- a/test/constant_resolve.cpp +++ b/test/constant_resolve.cpp @@ -10,12 +10,12 @@ namespace inversify = mosure::inversify; SCENARIO("container resolves constant values", "[resolve]") { GIVEN("A container with constant binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(10); + container.bind(symbols::foo).toConstantValue(10); WHEN("the dependency is resolved") { - auto result = container.get(symbols::foo); + auto result = container.get(symbols::foo); THEN("the correct value is returned") { REQUIRE(result == 10); @@ -23,10 +23,10 @@ SCENARIO("container resolves constant values", "[resolve]") { } WHEN("the binding is redefined") { - container.bind(symbols::foo).toConstantValue(20); + container.bind(symbols::foo).toConstantValue(20); WHEN("the dependency is resolved") { - auto result = container.get(symbols::foo); + auto result = container.get(symbols::foo); THEN("the updated value is returned") { REQUIRE(result == 20); diff --git a/test/dynamic_resolve.cpp b/test/dynamic_resolve.cpp index d42557f..11fcd25 100644 --- a/test/dynamic_resolve.cpp +++ b/test/dynamic_resolve.cpp @@ -15,51 +15,51 @@ namespace inversify = mosure::inversify; SCENARIO("container resolves dynamic values", "[resolve]") { GIVEN("A container with dynamic binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(3.1415); - container.bind(symbols::bar).toDynamicValue([](const inversify::Context& ctx) { - auto foo = ctx.container.get(symbols::foo); + container.bind(symbols::foo).toConstantValue(3); + container.bind(symbols::bar).toDynamicValue([](const inversify::Context& ctx) { + auto foo = ctx.container.get(symbols::foo); - return (int)foo; + return foo * 1.5; }); WHEN("the dependency is resolved") { - auto result = container.get(symbols::bar); + auto result = container.get(symbols::bar); THEN("the correct value is returned") { - REQUIRE(result == 3); + REQUIRE(result == 4.5); } } WHEN("the binding is redefined") { - container.bind(symbols::bar).toDynamicValue([](const inversify::Context& ctx) { - auto foo = ctx.container.get(symbols::foo); + container.bind(symbols::bar).toDynamicValue([](const inversify::Context& ctx) { + auto foo = ctx.container.get(symbols::foo); - return (int)foo * 2; + return foo * 2.5; }); WHEN("the dependency is resolved") { - auto result = container.get(symbols::bar); + auto result = container.get(symbols::bar); THEN("the updated value is returned") { - REQUIRE(result == 6); + REQUIRE(result == 7.5); } } } } GIVEN("A container with factory binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(10); - container.bind(symbols::bar).toConstantValue(1.618); + container.bind(symbols::foo).toConstantValue(10); + container.bind(symbols::bar).toConstantValue(1.618); - container.bind>(symbols::fizzFactory).toDynamicValue( + container.bind(symbols::fizzFactory).toDynamicValue( [](const inversify::Context& ctx) { return [&]() { - auto foo = ctx.container.get(symbols::foo); - auto bar = ctx.container.get(symbols::bar); + auto foo = ctx.container.get(symbols::foo); + auto bar = ctx.container.get(symbols::bar); auto fizz = std::make_unique(foo, bar); @@ -69,7 +69,7 @@ SCENARIO("container resolves dynamic values", "[resolve]") { ); WHEN("the dependency is resolved") { - auto factory = container.get>(symbols::fizzFactory); + auto factory = container.get(symbols::fizzFactory); WHEN("the factory is called") { auto result = factory(); @@ -85,15 +85,15 @@ SCENARIO("container resolves dynamic values", "[resolve]") { } GIVEN("A container with singleton dynamic binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(10); - container.bind(symbols::bar).toConstantValue(1.618); + container.bind(symbols::foo).toConstantValue(10); + container.bind(symbols::bar).toConstantValue(1.618); - container.bind(symbols::fizz).toDynamicValue( + container.bind(symbols::fizz).toDynamicValue( [](const inversify::Context& ctx) { - auto foo = ctx.container.get(symbols::foo); - auto bar = ctx.container.get(symbols::bar); + auto foo = ctx.container.get(symbols::foo); + auto bar = ctx.container.get(symbols::bar); auto fizz = std::make_shared(foo, bar); @@ -102,8 +102,8 @@ SCENARIO("container resolves dynamic values", "[resolve]") { ).inSingletonScope(); WHEN("multiple dependencies are resolved") { - auto fizz1 = container.get(symbols::fizz); - auto fizz2 = container.get(symbols::fizz); + auto fizz1 = container.get(symbols::fizz); + auto fizz2 = container.get(symbols::fizz); THEN("both dependency pointers are equal") { REQUIRE(fizz1 == fizz2); @@ -112,15 +112,15 @@ SCENARIO("container resolves dynamic values", "[resolve]") { } GIVEN("A container with resolution dynamic binding") { - inversify::Container container {}; + inversify::Container container; - container.bind(symbols::foo).toConstantValue(10); - container.bind(symbols::bar).toConstantValue(1.618); + container.bind(symbols::foo).toConstantValue(10); + container.bind(symbols::bar).toConstantValue(1.618); - container.bind(symbols::fizz).toDynamicValue( + container.bind(symbols::fizz).toDynamicValue( [](const inversify::Context& ctx) { - auto foo = ctx.container.get(symbols::foo); - auto bar = ctx.container.get(symbols::bar); + auto foo = ctx.container.get(symbols::foo); + auto bar = ctx.container.get(symbols::bar); auto fizz = std::make_unique(foo, bar); @@ -129,8 +129,8 @@ SCENARIO("container resolves dynamic values", "[resolve]") { ); WHEN("multiple dependencies are resolved") { - auto fizz1 = container.get(symbols::fizz); - auto fizz2 = container.get(symbols::fizz); + auto fizz1 = container.get(symbols::fizz); + auto fizz2 = container.get(symbols::fizz); THEN("dependencies are unique") { REQUIRE(fizz1 != fizz2); diff --git a/test/mock/fizz.hpp b/test/mock/fizz.hpp index 87df45d..97eeed5 100644 --- a/test/mock/fizz.hpp +++ b/test/mock/fizz.hpp @@ -26,6 +26,6 @@ struct Fizz : IFizz { }; inline static auto _ = inversify::Injectable::inject( - inversify::Inject(symbols::foo), - inversify::Inject(symbols::bar) + symbols::foo, + symbols::bar ); diff --git a/test/mock/symbols.hpp b/test/mock/symbols.hpp index dc08363..e234869 100644 --- a/test/mock/symbols.hpp +++ b/test/mock/symbols.hpp @@ -1,17 +1,20 @@ #pragma once +#include + #include +#include "mock/ifizz.hpp" + namespace inversify = mosure::inversify; namespace symbols { - inline const inversify::Symbol foo { "Foo" }; - inline const inversify::Symbol bar { "Bar" }; - inline const inversify::Symbol fizz { "Fizz" }; - inline const inversify::Symbol fizzFactory { "FizzFactory" }; + inline extern inversify::Symbol foo {}; + inline extern inversify::Symbol bar {}; + inline extern inversify::Symbol fizz {}; + inline extern inversify::Symbol> fizzFactory {}; - inline const inversify::Symbol autoFizz { "AutoFizz" }; - inline const inversify::Symbol autoFizzUnique { "AutoFizzUnique" }; - inline const inversify::Symbol autoFizzShared { "AutoFizzShared" }; + inline extern inversify::Symbol autoFizzUnique {}; + inline extern inversify::Symbol autoFizzShared {}; }