// // 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/scopes/scoped.hpp" #include namespace di = boost::di; //<- struct interface1 { virtual ~interface1() noexcept = default; virtual void dummy() = 0; }; struct implementation1 : interface1 { static auto &ctor_calls() { static auto calls = 0; return calls; } static auto &dtor_calls() { static auto calls = 0; return calls; } implementation1() { ctor_calls()++; } ~implementation1() { dtor_calls()++; } void dummy() override {} }; struct interface2 { virtual ~interface2() noexcept = default; }; struct implementation2 : interface2 { static auto &ctor_calls() { static auto calls = 0; return calls; } static int &dtor_calls() { static auto calls = 0; return calls; } implementation2() { ctor_calls()++; } ~implementation2() { dtor_calls()++; } }; //-> auto module = [] { // clang-format off return di::make_injector( di::bind().to() ); // clang-format on }; int main() { assert(!implementation1::ctor_calls()); assert(!implementation1::dtor_calls()); assert(!implementation2::ctor_calls()); assert(!implementation2::dtor_calls()); auto parent_injector = module(); /*<>*/ parent_injector.create(); assert(1 == implementation1::ctor_calls()); { /*<>*/ // clang-format off auto child_injector = di::make_injector( module() , di::bind().to().in(di::extension::scoped) ); // clang-format on child_injector.create(); assert(1 == implementation2::ctor_calls()); } // end of scoped assert(1 == implementation2::dtor_calls()); /*<>*/ parent_injector.create(); /*<>*/ assert(1 == implementation1::ctor_calls()); /*<<`implementation1` will die with the app (static storage)>>*/ assert(0 == implementation1::dtor_calls()); }