liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef BOOST_LEAF_PRED_HPP_INCLUDED
#define BOOST_LEAF_PRED_HPP_INCLUDED
 
// Copyright (c) 2018-2020 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(__clang__)
#       pragma clang system_header
#   elif (__GNUC__*100+__GNUC_MINOR__>301)
#       pragma GCC system_header
#   elif defined(_MSC_VER)
#       pragma warning(push,1)
#   endif
#endif
 
#include <boost/leaf/handle_errors.hpp>
 
#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 <class MatchType, class T>
        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 <class MatchType, class T>
        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 <class MatchType, class V>
        BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, V v )
        {
            return e == v;
        }
 
        template <class MatchType, class VCar, class... VCdr>
        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 <class E, class Enum = E>
    struct condition
    {
        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.");
    };
 
    template <class Enum>
    struct condition<Enum, Enum>
    {
        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.");
    };
 
#if __cplusplus >= 201703L
    template <class ErrorCodeEnum>
    BOOST_LEAF_CONSTEXPR inline bool category( std::error_code const & ec )
    {
        static_assert(std::is_error_code_enum<ErrorCodeEnum>::value, "leaf::category requires an error code enum");
        return &ec.category() == &std::error_code(ErrorCodeEnum{}).category();
    }
#endif
 
    ////////////////////////////////////////
 
    namespace leaf_detail
    {
        template <class T>
        struct match_enum_type
        {
            using type = T;
        };
 
        template <class Enum>
        struct match_enum_type<condition<Enum, Enum>>
        {
            using type = Enum;
        };
 
        template <class E, class Enum>
        struct match_enum_type<condition<E, Enum>>
        {
            static_assert(sizeof(Enum) == 0, "leaf::condition<E, Enum> should be used with leaf::match_value<>, not with leaf::match<>");
        };
    }
 
    template <class E, BOOST_LEAF_MATCH_ARGS(match_enum_type<E>, V1, V)>
    struct match
    {
        using error_type = E;
        E matched;
 
        template <class T>
        BOOST_LEAF_CONSTEXPR static bool evaluate(T && x)
        {
            return leaf_detail::cmp_value_pack(std::forward<T>(x), V1, V...);
        }
    };
 
    template <class Enum, BOOST_LEAF_MATCH_ARGS(BOOST_LEAF_ESC(match_enum_type<condition<Enum, Enum>>), V1, V)>
    struct match<condition<Enum, Enum>, 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 <class E, BOOST_LEAF_MATCH_ARGS(match_enum_type<E>, V1, V)>
    struct is_predicate<match<E, V1, V...>>: std::true_type
    {
    };
 
    ////////////////////////////////////////
 
    namespace leaf_detail
    {
        template <class E>
        struct match_value_enum_type
        {
            using type = typename std::remove_reference<decltype(std::declval<E>().value)>::type;
        };
 
        template <class E, class Enum>
        struct match_value_enum_type<condition<E, Enum>>
        {
            using type = Enum;
        };
 
        template <class Enum>
        struct match_value_enum_type<condition<Enum, Enum>>
        {
            static_assert(sizeof(Enum)==0, "leaf::condition<Enum> should be used with leaf::match<>, not with leaf::match_value<>");
        };
    }
 
    template <class E, BOOST_LEAF_MATCH_ARGS(match_value_enum_type<E>, 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 <class E, class Enum, BOOST_LEAF_MATCH_ARGS(BOOST_LEAF_ESC(match_value_enum_type<condition<E, Enum>>), V1, V)>
    struct match_value<condition<E, Enum>, 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 <class E, BOOST_LEAF_MATCH_ARGS(match_value_enum_type<E>, V1, V)>
    struct is_predicate<match_value<E, V1, V...>>: std::true_type
    {
    };
 
    ////////////////////////////////////////
 
#if __cplusplus >= 201703L
    template <auto, auto, auto...>
    struct match_member;
 
    template <class T, class E, T E::* P, auto V1, auto... V>
    struct match_member<P, V1, V...>
    {
        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 <auto P, auto V1, auto... V>
    struct is_predicate<match_member<P, V1, V...>>: std::true_type
    {
    };
#endif
 
    ////////////////////////////////////////
 
    template <class P>
    struct if_not
    {
        using error_type = typename P::error_type;;
        decltype(std::declval<P>().matched) matched;
 
        template <class E>
        BOOST_LEAF_CONSTEXPR static bool evaluate(E && e) noexcept
        {
            return !P::evaluate(std::forward<E>(e));
        }
    };
 
    template <class P>
    struct is_predicate<if_not<P>>: std::true_type
    {
    };
 
    ////////////////////////////////////////
 
 
#ifndef BOOST_LEAF_NO_EXCEPTIONS
 
    namespace leaf_detail
    {
        template <class Ex>
        BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const * ) noexcept
        {
            return dynamic_cast<Ex const *>(&ex)!=0;
        }
 
        template <class Ex, class... ExRest>
        BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const *, ExRest const * ... ex_rest ) noexcept
        {
            return dynamic_cast<Ex const *>(&ex)!=0 || check_exception_pack(ex, ex_rest...);
        }
 
        BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ) noexcept
        {
            return true;
        }
    }
 
    template <class... Ex>
    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<Ex const *>(0)...);
        }
    };
 
    template <class Ex>
    struct catch_<Ex>
    {
        using error_type = void;
        Ex const & matched;
 
        BOOST_LEAF_CONSTEXPR static Ex const * evaluate(std::exception const & ex) noexcept
        {
            return dynamic_cast<Ex const *>(&ex);
        }
 
        explicit catch_( std::exception const & ex ):
            matched(*dynamic_cast<Ex const *>(&ex))
        {
        }
    };
 
    template <class... Ex>
    struct is_predicate<catch_<Ex...>>: std::true_type
    {
    };
 
#endif
 
} }
 
#endif