1 #ifndef BOOST_LEAF_PRED_HPP_INCLUDED
2 #define BOOST_LEAF_PRED_HPP_INCLUDED
4 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_LEAF_ENABLE_WARNINGS
10 # if defined(__clang__)
11 # pragma clang system_header
12 # elif (__GNUC__*100+__GNUC_MINOR__>301)
13 # pragma GCC system_header
14 # elif defined(_MSC_VER)
15 # pragma warning(push,1)
19 #include <boost/leaf/handle_errors.hpp>
21 #if __cplusplus >= 201703L
22 # define BOOST_LEAF_MATCH_ARGS(et,v1,v) auto v1, auto... v
24 # define BOOST_LEAF_MATCH_ARGS(et,v1,v) typename leaf_detail::et::type v1, typename leaf_detail::et::type... v
26 #define BOOST_LEAF_ESC(...) __VA_ARGS__
28 namespace boost { namespace leaf {
32 #if __cplusplus >= 201703L
33 template <class MatchType, class T>
34 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, bool (*P)(T) noexcept ) noexcept
36 BOOST_LEAF_ASSERT(P != 0);
40 template <class MatchType, class T>
41 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, bool (*P)(T) )
43 BOOST_LEAF_ASSERT(P != 0);
48 template <class MatchType, class V>
49 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, V v )
54 template <class MatchType, class VCar, class... VCdr>
55 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, VCar car, VCdr ... cdr )
57 return cmp_value_pack(e, car) || cmp_value_pack(e, cdr...);
61 ////////////////////////////////////////
63 template <class E, class Enum = E>
66 static_assert(std::is_error_condition_enum<Enum>::value || std::is_error_code_enum<Enum>::value, "leaf::condition<E, Enum> requires Enum to be registered either with std::is_error_condition_enum or std::is_error_code_enum.");
70 struct condition<Enum, Enum>
72 static_assert(std::is_error_condition_enum<Enum>::value || std::is_error_code_enum<Enum>::value, "leaf::condition<Enum> requires Enum to be registered either with std::is_error_condition_enum or std::is_error_code_enum.");
75 #if __cplusplus >= 201703L
76 template <class ErrorCodeEnum>
77 BOOST_LEAF_CONSTEXPR inline bool category( std::error_code const & ec )
79 static_assert(std::is_error_code_enum<ErrorCodeEnum>::value, "leaf::category requires an error code enum");
80 return &ec.category() == &std::error_code(ErrorCodeEnum{}).category();
84 ////////////////////////////////////////
89 struct match_enum_type
95 struct match_enum_type<condition<Enum, Enum>>
100 template <class E, class Enum>
101 struct match_enum_type<condition<E, Enum>>
103 static_assert(sizeof(Enum) == 0, "leaf::condition<E, Enum> should be used with leaf::match_value<>, not with leaf::match<>");
107 template <class E, BOOST_LEAF_MATCH_ARGS(match_enum_type<E>, V1, V)>
110 using error_type = E;
114 BOOST_LEAF_CONSTEXPR static bool evaluate(T && x)
116 return leaf_detail::cmp_value_pack(std::forward<T>(x), V1, V...);
120 template <class Enum, BOOST_LEAF_MATCH_ARGS(BOOST_LEAF_ESC(match_enum_type<condition<Enum, Enum>>), V1, V)>
121 struct match<condition<Enum, Enum>, V1, V...>
123 using error_type = std::error_code;
124 std::error_code const & matched;
126 BOOST_LEAF_CONSTEXPR static bool evaluate(std::error_code const & e) noexcept
128 return leaf_detail::cmp_value_pack(e, V1, V...);
132 template <class E, BOOST_LEAF_MATCH_ARGS(match_enum_type<E>, V1, V)>
133 struct is_predicate<match<E, V1, V...>>: std::true_type
137 ////////////////////////////////////////
139 namespace leaf_detail
142 struct match_value_enum_type
144 using type = typename std::remove_reference<decltype(std::declval<E>().value)>::type;
147 template <class E, class Enum>
148 struct match_value_enum_type<condition<E, Enum>>
153 template <class Enum>
154 struct match_value_enum_type<condition<Enum, Enum>>
156 static_assert(sizeof(Enum)==0, "leaf::condition<Enum> should be used with leaf::match<>, not with leaf::match_value<>");
160 template <class E, BOOST_LEAF_MATCH_ARGS(match_value_enum_type<E>, V1, V)>
163 using error_type = E;
166 BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) noexcept
168 return leaf_detail::cmp_value_pack(e.value, V1, V...);
172 template <class E, class Enum, BOOST_LEAF_MATCH_ARGS(BOOST_LEAF_ESC(match_value_enum_type<condition<E, Enum>>), V1, V)>
173 struct match_value<condition<E, Enum>, V1, V...>
175 using error_type = E;
178 BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e)
180 return leaf_detail::cmp_value_pack(e.value, V1, V...);
184 template <class E, BOOST_LEAF_MATCH_ARGS(match_value_enum_type<E>, V1, V)>
185 struct is_predicate<match_value<E, V1, V...>>: std::true_type
189 ////////////////////////////////////////
191 #if __cplusplus >= 201703L
192 template <auto, auto, auto...>
195 template <class T, class E, T E::* P, auto V1, auto... V>
196 struct match_member<P, V1, V...>
198 using error_type = E;
201 BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) noexcept
203 return leaf_detail::cmp_value_pack(e.*P, V1, V...);
207 template <auto P, auto V1, auto... V>
208 struct is_predicate<match_member<P, V1, V...>>: std::true_type
213 ////////////////////////////////////////
218 using error_type = typename P::error_type;;
219 decltype(std::declval<P>().matched) matched;
222 BOOST_LEAF_CONSTEXPR static bool evaluate(E && e) noexcept
224 return !P::evaluate(std::forward<E>(e));
229 struct is_predicate<if_not<P>>: std::true_type
233 ////////////////////////////////////////
236 #ifndef BOOST_LEAF_NO_EXCEPTIONS
238 namespace leaf_detail
241 BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const * ) noexcept
243 return dynamic_cast<Ex const *>(&ex)!=0;
246 template <class Ex, class... ExRest>
247 BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const *, ExRest const * ... ex_rest ) noexcept
249 return dynamic_cast<Ex const *>(&ex)!=0 || check_exception_pack(ex, ex_rest...);
252 BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ) noexcept
258 template <class... Ex>
261 using error_type = void;
262 std::exception const & matched;
264 BOOST_LEAF_CONSTEXPR static bool evaluate(std::exception const & ex) noexcept
266 return leaf_detail::check_exception_pack(ex, static_cast<Ex const *>(0)...);
273 using error_type = void;
276 BOOST_LEAF_CONSTEXPR static Ex const * evaluate(std::exception const & ex) noexcept
278 return dynamic_cast<Ex const *>(&ex);
281 explicit catch_( std::exception const & ex ):
282 matched(*dynamic_cast<Ex const *>(&ex))
287 template <class... Ex>
288 struct is_predicate<catch_<Ex...>>: std::true_type