/** * 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 { template void force_linking(Annotation anno); template using void_t = void; template struct has_injectable : std::false_type { }; template struct has_injectable> : std::true_type { }; template struct singleton_storage { static T* get() { static int _[] = {0, (force_linking(Annotations{}), 0)...}; (void) _; assert(ss_data != nullptr); return ss_data; } static std::shared_ptr create() { static int _[] = {0, (force_linking(Annotations{}), 0)...}; (void) _; return ss_factory(); } protected: static T* ss_data; static std::function()> ss_factory; }; template T* singleton_storage::ss_data = nullptr; 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::value, bool> = true> T get() { return singleton_storage::create(); } template::value, bool> = true> T get() { return multiple_storage::create(); } } // namespace injector #endif