// // Copyright (c) 2012-2020 Kris Jusiak (kris at jusiak dot net) // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "boost/di/extension/injections/factory.hpp" #include namespace di = boost::di; struct interface { virtual ~interface() noexcept = default; virtual void dummy1() = 0; }; struct implementation : interface { void dummy1() override {} }; struct implementation_exception : interface { #if defined(__EXCEPTIONS) implementation_exception() { throw 0; } #endif void dummy1() override {} }; struct implementation_with_args : interface { implementation_with_args(int i, double d) { assert(42 == i); assert(87.0 == d); } void dummy1() override {} }; struct implementation_with_injected_args : interface { implementation_with_injected_args(int i, double d) { assert(123 == i); assert(87.0 == d); } void dummy1() override {} }; /*<>*/ class example { public: example(const di::extension::ifactory& f1, const di::extension::ifactory& f2, const di::extension::ifactory& f3) { assert(dynamic_cast(f1.create().get())); assert(dynamic_cast(f2.create(42, 87.0).get())); assert(dynamic_cast(f3.create(87.0).get())); } }; int main() { //<> auto module = [] { return di::make_injector(di::bind<>().to(123)); }; // clang-format off auto injector = di::make_injector(module() // <> , di::bind().to(std::make_shared()) // <> , di::bind>().to(di::extension::factory{}) //<> , di::bind>().to(di::extension::factory{}) //<> , di::bind>().to(di::extension::factory{}) //<> , di::bind>().to(di::extension::factory{}) ); // clang-format on /*<>*/ injector.create(); /*<>*/ #if defined(__EXCEPTIONS) auto exception_thrown = false; try { injector.create(); } catch (...) { exception_thrown = true; } assert(exception_thrown); #endif // Check whether injector is affected by factory assert(injector.create>()); }