// // Copyright (c) 2012-2020 Kanstantsin Chernik // // 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/shared_factory.hpp" #include namespace di = boost::di; namespace ext = di::extension; struct interface1 { virtual ~interface1() noexcept = default; virtual void dummy() = 0; }; struct interface2 { virtual ~interface2() noexcept = default; virtual void dummy() = 0; }; struct implementation : interface1, interface2 { static auto& ctor_calls() { static auto calls = 0; return calls; } static auto& dtor_calls() { static auto calls = 0; return calls; } implementation() { ctor_calls()++; } ~implementation() { dtor_calls()++; } void dummy() override {} }; struct implementation_exception : interface1 { #if defined(__EXCEPTIONS) implementation_exception() { throw 0; } #endif void dummy() override {} }; int main() { assert(!implementation::ctor_calls()); assert(!implementation::dtor_calls()); { //<> // clang-format off auto injector = di::make_injector( di::bind().to(ext::shared_factory([&](const auto& inner_injector) { //<> return inner_injector.template create>(); })), //<> di::bind().to(ext::shared_factory([&](const auto& inner_injector) { static int calls = 0; assert(1 == ++calls); //<> return inner_injector.template create>(); })), di::bind().to(ext::conditional_shared_factory([&]() { static int calls = 0; assert(1 == ++calls); return true; })) ); // clang-format on //<> auto i1 = injector.create>(); auto i2 = injector.create>(); assert(i1); assert(i1 == i2); assert(1 == implementation::ctor_calls()); assert(!implementation::dtor_calls()); //<> auto i3 = injector.create>(); //<> #if defined(__EXCEPTIONS) auto exception_thrown = false; try { injector.create>(); } catch (...) { exception_thrown = true; } assert(exception_thrown); #endif auto i4 = injector.create>(); assert(i3); assert(i3 == i4); assert(1 == implementation::ctor_calls()); assert(!implementation::dtor_calls()); } //<> assert(1 == implementation::dtor_calls()); }