#ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED #define BOOST_LEAF_CONTEXT_HPP_INCLUDED /// Copyright (c) 2018-2021 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) #ifndef BOOST_LEAF_ENABLE_WARNINGS /// # if defined(_MSC_VER) /// # pragma warning(push,1) /// # elif defined(__clang__) /// # pragma clang system_header /// # elif (__GNUC__*100+__GNUC_MINOR__>301) /// # pragma GCC system_header /// # endif /// #endif /// #include namespace boost { namespace leaf { class error_info; class diagnostic_info; class verbose_diagnostic_info; template struct is_predicate: std::false_type { }; namespace leaf_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; 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::verbose_diagnostic_info 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 error_type = E; 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 error_type = void; 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: handler_argument_always_available { template BOOST_LEAF_CONSTEXPR static error_info const & get( Tup const &, error_info const & ei ) noexcept { return ei; } }; template struct handler_argument_traits_require_by_value { static_assert(sizeof(E) == 0, "Error handlers must take this type by value"); }; } //////////////////////////////////////// namespace leaf_detail { template struct tuple_for_each { BOOST_LEAF_CONSTEXPR static void activate( Tuple & 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( Tuple & 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 propagate( Tuple & tup ) noexcept { static_assert(!std::is_same(tup))>::type>::value, "Bug in LEAF: context type deduction"); auto & sl = std::get(tup); sl.propagate(); tuple_for_each::propagate(tup); } BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int err_id ) noexcept { static_assert(!std::is_same(tup))>::type>::value, "Bug in LEAF: context type deduction"); auto & sl = std::get(tup); if( sl.has_value(err_id) ) load_slot(err_id, std::move(sl).value(err_id)); tuple_for_each::propagate_captured(tup, err_id); } static void print( std::ostream & os, void const * tup, int key_to_print ) { BOOST_LEAF_ASSERT(tup != 0); tuple_for_each::print(os, tup, key_to_print); std::get(*static_cast(tup)).print(os, key_to_print); } }; template struct tuple_for_each<0, Tuple> { BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { } BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { } BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept { } BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int ) noexcept { } static void print( std::ostream &, void const *, int ) { } }; } //////////////////////////////////////////// #if BOOST_LEAF_DIAGNOSTICS namespace leaf_detail { template struct requires_unexpected { constexpr static bool value = false; }; template struct requires_unexpected { constexpr static bool value = requires_unexpected::value; }; template struct requires_unexpected { constexpr static bool value = requires_unexpected::value; }; template struct requires_unexpected { constexpr static bool value = requires_unexpected::value; }; template <> struct requires_unexpected { constexpr static bool value = true; }; template <> struct requires_unexpected { constexpr static bool value = true; }; template struct unexpected_requested; template