// // 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 #include #include //-> #include namespace di = boost::di; //<- class interface { public: virtual ~interface() noexcept = default; virtual void dummy() = 0; }; class implementation : public interface { public: implementation() { ++ctor_calls(); } void dummy() override {} static int& ctor_calls() { static auto calls = 0; return calls; } }; template ::value, int> = 0> void create_singletons_eagerly_impl(const di::aux::type&, const TInjector& injector) { injector.template create>(); } template ::value, int> = 0> void create_singletons_eagerly_impl(const di::aux::type&, const TInjector&) {} template void create_singletons_eagerly(const di::aux::type_list&, const TInjector& injector) { [](...) {}((create_singletons_eagerly_impl(di::aux::type{}, injector), 0)...); } template void eager_singletons(const TInjector& injector) { create_singletons_eagerly(typename TInjector::deps{}, injector); } //-> auto configuration = [] { // clang-format off return di::make_injector( di::bind().to().in(di::singleton) , di::bind().to(42) ); // clang-format on }; struct example { example(int i, std::shared_ptr object) { assert(42 == i); assert(dynamic_cast(object.get())); } }; int main() { /*<>*/ auto injector = di::make_injector(configuration()); assert(0 == implementation::ctor_calls()); /*<>*/ eager_singletons(injector); assert(1 == implementation::ctor_calls()); /*<>*/ injector.create(); assert(1 == implementation::ctor_calls()); }