#ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED #define BOOST_LEAF_CONTEXT_HPP_INCLUDED // Copyright 2018-2024 Emil Dotchevski and Reverge Studios, Inc. // 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 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) # include #endif namespace boost { namespace leaf { class error_info; class diagnostic_info; class diagnostic_details; template struct is_predicate: std::false_type { }; namespace detail { template struct is_exception: std::is_base_of::type> { }; template struct handler_argument_traits; template ::value> struct handler_argument_traits_defaults; template struct handler_argument_traits_defaults { using error_type = typename std::decay::type; using context_types = leaf_detail_mp11::mp_list; constexpr static bool always_available = false; template BOOST_LEAF_CONSTEXPR static error_type const * check( Tup const &, error_info const & ) noexcept; template BOOST_LEAF_CONSTEXPR static error_type * check( Tup &, error_info const & ) noexcept; template BOOST_LEAF_CONSTEXPR static E get( Tup & tup, error_info const & ei ) noexcept { return *check(tup, ei); } static_assert(!is_predicate::value, "Handlers must take predicate arguments by value"); static_assert(!std::is_same::value, "Handlers must take leaf::error_info arguments by const &"); static_assert(!std::is_same::value, "Handlers must take leaf::diagnostic_info arguments by const &"); static_assert(!std::is_same::value, "Handlers must take leaf::diagnostic_details arguments by const &"); }; template struct handler_argument_traits_defaults: handler_argument_traits { using base = handler_argument_traits; static_assert(!base::always_available, "Predicates can't use types that are always_available"); template BOOST_LEAF_CONSTEXPR static bool check( Tup const & tup, error_info const & ei ) noexcept { auto e = base::check(tup, ei); return e && Pred::evaluate(*e); } template BOOST_LEAF_CONSTEXPR static Pred get( Tup const & tup, error_info const & ei ) noexcept { return Pred{*base::check(tup, ei)}; } }; template struct handler_argument_always_available { using context_types = leaf_detail_mp11::mp_list; constexpr static bool always_available = true; template BOOST_LEAF_CONSTEXPR static bool check( Tup &, error_info const & ) noexcept { return true; } }; template struct handler_argument_traits: handler_argument_traits_defaults { }; template <> struct handler_argument_traits { using context_types = leaf_detail_mp11::mp_list<>; constexpr static bool always_available = false; template BOOST_LEAF_CONSTEXPR static std::exception const * check( Tup const &, error_info const & ) noexcept; }; template struct handler_argument_traits { static_assert(sizeof(E) == 0, "Error handlers may not take rvalue ref arguments"); }; template struct handler_argument_traits: handler_argument_always_available::type> { template BOOST_LEAF_CONSTEXPR static E * get( Tup & tup, error_info const & ei) noexcept { return handler_argument_traits_defaults::check(tup, ei); } }; template struct handler_argument_traits_require_by_value { static_assert(sizeof(E) == 0, "Error handlers must take this type by value"); }; } //////////////////////////////////////// namespace detail { template struct get_dispatch { static BOOST_LEAF_CONSTEXPR T const * get(T const * x) noexcept { return x; } static BOOST_LEAF_CONSTEXPR T const * get(void const *) noexcept { return nullptr; } }; template BOOST_LEAF_CONSTEXPR inline T * find_in_tuple(std::tuple<> const &) { return nullptr; } template BOOST_LEAF_CONSTEXPR inline typename std::enable_if::type const * find_in_tuple(std::tuple const & t) noexcept { return get_dispatch::get(&std::get(t)); } template BOOST_LEAF_CONSTEXPR inline typename std::enable_if::type const * find_in_tuple(std::tuple const & t) noexcept { if( T const * x = get_dispatch::get(&std::get(t)) ) return x; else return find_in_tuple(t); } } //////////////////////////////////////// namespace detail { template struct tuple_for_each { BOOST_LEAF_CONSTEXPR static void activate( Tup & tup ) noexcept { static_assert(!std::is_same(tup))>::type>::value, "Bug in LEAF: context type deduction"); tuple_for_each::activate(tup); std::get(tup).activate(); } BOOST_LEAF_CONSTEXPR static void deactivate( Tup & tup ) noexcept { static_assert(!std::is_same(tup))>::type>::value, "Bug in LEAF: context type deduction"); std::get(tup).deactivate(); tuple_for_each::deactivate(tup); } BOOST_LEAF_CONSTEXPR static void unload( Tup & tup, int err_id ) noexcept { static_assert(!std::is_same(tup))>::type>::value, "Bug in LEAF: context type deduction"); BOOST_LEAF_ASSERT(err_id != 0); auto & sl = std::get(tup); sl.unload(err_id); tuple_for_each::unload(tup, err_id); } template static void print(std::basic_ostream & os, void const * tup, error_id to_print, char const * & prefix) { BOOST_LEAF_ASSERT(tup != nullptr); tuple_for_each::print(os, tup, to_print, prefix); std::get(*static_cast(tup)).print(os, to_print, prefix); } }; template struct tuple_for_each<0, Tup> { BOOST_LEAF_CONSTEXPR static void activate( Tup & ) noexcept { } BOOST_LEAF_CONSTEXPR static void deactivate( Tup & ) noexcept { } BOOST_LEAF_CONSTEXPR static void unload( Tup &, int ) noexcept { } template BOOST_LEAF_CONSTEXPR static void print(std::basic_ostream &, void const *, error_id, char const * &) { } }; template BOOST_LEAF_CONSTEXPR void print_tuple_contents(std::basic_ostream & os, void const * tup, error_id to_print, char const * & prefix) { tuple_for_each::value, Tup>::print(os, tup, to_print, prefix); } } //////////////////////////////////////// namespace detail { template struct does_not_participate_in_context_deduction: std::is_abstract { }; template <> struct does_not_participate_in_context_deduction: std::true_type { }; template struct deduce_e_type_list; template class L, class... T> struct deduce_e_type_list> { using type = leaf_detail_mp11::mp_remove_if< leaf_detail_mp11::mp_unique< leaf_detail_mp11::mp_append::context_types...> >, does_not_participate_in_context_deduction >; }; template struct deduce_e_tuple_impl; template