#ifndef BOOST_LEAF_PRED_HPP_INCLUDED #define BOOST_LEAF_PRED_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 #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 != 0); 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 != 0); 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...); } } //////////////////////////////////////// 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 //////////////////////////////////////// namespace leaf_detail { template struct match_enum_type { using type = T; }; 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<>"); }; } 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...); } }; 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...); } }; 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; }; 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<>"); }; } 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...); } }; 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...); } }; 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)!=0; } template BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const *, ExRest const * ... ex_rest ) noexcept { return dynamic_cast(&ex)!=0 || 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(0)...); } }; 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 } } #if defined(_MSC_VER) && !defined(BOOST_LEAF_ENABLE_WARNINGS) /// #pragma warning(pop) /// #endif /// #endif