/** * Copyright (c) 2021, Timothy Stack * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Timothy Stack nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @file injector.hh */ #ifndef lnav_injector_hh #define lnav_injector_hh #include #include #include #include #include #include "base/lnav_log.hh" namespace injector { enum class scope { undefined, none, singleton, }; template void force_linking(Annotation anno); template using void_t = void; template struct with_annotations { T value; }; template struct has_injectable : std::false_type {}; template struct has_injectable> : std::true_type {}; template struct singleton_storage { static scope get_scope() { return ss_scope; } static T* get() { static int _[] = {0, (force_linking(Annotations{}), 0)...}; (void) _; return ss_data; } static std::shared_ptr get_owner() { static int _[] = {0, (force_linking(Annotations{}), 0)...}; (void) _; return ss_owner; } static std::shared_ptr create() { static int _[] = {0, (force_linking(Annotations{}), 0)...}; (void) _; return ss_factory(); } protected: static scope ss_scope; static T* ss_data; static std::shared_ptr ss_owner; static std::function()> ss_factory; }; template T* singleton_storage::ss_data = nullptr; template scope singleton_storage::ss_scope = scope::undefined; template std::shared_ptr singleton_storage::ss_owner; template std::function()> singleton_storage::ss_factory; template struct Impl { using type = T; }; template struct multiple_storage { static std::vector> create() { std::vector> retval; for (const auto& pair : get_factories()) { retval.template emplace_back(pair.second()); } return retval; } protected: using factory_map_t = std::map()>>; static factory_map_t& get_factories() { static factory_map_t retval; return retval; } }; template::value, bool> = true> T get() { using plain_t = std::remove_const_t>; return *singleton_storage::get(); } template::value, bool> = true> T get() { using plain_t = std::remove_const_t>; return singleton_storage::get(); } template struct is_shared_ptr : std::false_type {}; template struct is_shared_ptr> : std::true_type {}; template struct is_vector : std::false_type {}; template struct is_vector> : std::true_type {}; template std::function()> create_from_injectable(R (*)(IArgs...), Args&... args); template::value, bool> = true, std::enable_if_t::value, bool> = true> T get(Args&... args) { typename T::element_type::injectable* i = nullptr; if (singleton_storage::get_scope() == scope::singleton) { return singleton_storage::get_owner(); } return create_from_injectable(i, args...)(); } template< typename T, typename... Annotations, std::enable_if_t::value, bool> = true, std::enable_if_t::value, bool> = true> T get() { return singleton_storage::get_owner(); } template::value, bool> = true> T get() { return multiple_storage::create(); } template std::function()> create_from_injectable(R (*)(IArgs...), Args&... args) { return [&]() { return std::make_shared(args..., ::injector::get()...); }; } } // namespace injector #endif