#ifndef BOOST_LEAF_PRED_HPP_INCLUDED #define BOOST_LEAF_PRED_HPP_INCLUDED // Copyright 2018-2023 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 __cplusplus >= 201703L # define BOOST_LEAF_MATCH_ARGS(et,v1,v) auto v1, auto... v #else # define BOOST_LEAF_MATCH_ARGS(et,v1,v) typename leaf_detail::et::type v1, typename leaf_detail::et::type... v #endif #define BOOST_LEAF_ESC(...) __VA_ARGS__ namespace boost { namespace leaf { namespace leaf_detail { #if __cplusplus >= 201703L template BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, bool (*P)(T) noexcept ) noexcept { BOOST_LEAF_ASSERT(P != nullptr); return P(e); } template BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, bool (*P)(T) ) { BOOST_LEAF_ASSERT(P != nullptr); return P(e); } #endif template BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, V v ) { return e == v; } template BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, VCar car, VCdr ... cdr ) { return cmp_value_pack(e, car) || cmp_value_pack(e, cdr...); } } //////////////////////////////////////// #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR template struct condition { static_assert(std::is_error_condition_enum::value || std::is_error_code_enum::value, "leaf::condition requires Enum to be registered either with std::is_error_condition_enum or std::is_error_code_enum."); }; template struct condition { static_assert(std::is_error_condition_enum::value || std::is_error_code_enum::value, "leaf::condition requires Enum to be registered either with std::is_error_condition_enum or std::is_error_code_enum."); }; #if __cplusplus >= 201703L template BOOST_LEAF_CONSTEXPR inline bool category( std::error_code const & ec ) { static_assert(std::is_error_code_enum::value, "leaf::category requires an error code enum"); return &ec.category() == &std::error_code(ErrorCodeEnum{}).category(); } #endif #endif //////////////////////////////////////// namespace leaf_detail { template struct match_enum_type { using type = T; }; #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR template struct match_enum_type> { using type = Enum; }; template struct match_enum_type> { static_assert(sizeof(Enum) == 0, "leaf::condition should be used with leaf::match_value<>, not with leaf::match<>"); }; #endif } template , V1, V)> struct match { using error_type = E; E matched; template BOOST_LEAF_CONSTEXPR static bool evaluate(T && x) { return leaf_detail::cmp_value_pack(std::forward(x), V1, V...); } }; #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR template >), V1, V)> struct match, V1, V...> { using error_type = std::error_code; std::error_code const & matched; BOOST_LEAF_CONSTEXPR static bool evaluate(std::error_code const & e) noexcept { return leaf_detail::cmp_value_pack(e, V1, V...); } }; #endif template , V1, V)> struct is_predicate>: std::true_type { }; //////////////////////////////////////// namespace leaf_detail { template struct match_value_enum_type { using type = typename std::remove_reference().value)>::type; }; #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR template struct match_value_enum_type> { using type = Enum; }; template struct match_value_enum_type> { static_assert(sizeof(Enum)==0, "leaf::condition should be used with leaf::match<>, not with leaf::match_value<>"); }; #endif } template , V1, V)> struct match_value { using error_type = E; E const & matched; BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) noexcept { return leaf_detail::cmp_value_pack(e.value, V1, V...); } }; #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR template >), V1, V)> struct match_value, V1, V...> { using error_type = E; E const & matched; BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) { return leaf_detail::cmp_value_pack(e.value, V1, V...); } }; #endif template , V1, V)> struct is_predicate>: std::true_type { }; //////////////////////////////////////// #if __cplusplus >= 201703L template struct match_member; template struct match_member { using error_type = E; E const & matched; BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) noexcept { return leaf_detail::cmp_value_pack(e.*P, V1, V...); } }; template struct is_predicate>: std::true_type { }; #endif //////////////////////////////////////// template struct if_not { using error_type = typename P::error_type; decltype(std::declval

().matched) matched; template BOOST_LEAF_CONSTEXPR static bool evaluate(E && e) noexcept { return !P::evaluate(std::forward(e)); } }; template struct is_predicate>: std::true_type { }; //////////////////////////////////////// #ifndef BOOST_LEAF_NO_EXCEPTIONS namespace leaf_detail { template BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const * ) noexcept { return dynamic_cast(&ex)!=nullptr; } template BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const *, ExRest const * ... ex_rest ) noexcept { return dynamic_cast(&ex)!=nullptr || check_exception_pack(ex, ex_rest...); } BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ) noexcept { return true; } } template struct catch_ { using error_type = void; std::exception const & matched; BOOST_LEAF_CONSTEXPR static bool evaluate(std::exception const & ex) noexcept { return leaf_detail::check_exception_pack(ex, static_cast(nullptr)...); } }; template struct catch_ { using error_type = void; Ex const & matched; BOOST_LEAF_CONSTEXPR static Ex const * evaluate(std::exception const & ex) noexcept { return dynamic_cast(&ex); } explicit catch_( std::exception const & ex ): matched(*dynamic_cast(&ex)) { } }; template struct is_predicate>: std::true_type { }; #endif } } #endif