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
#ifndef BOOST_LEAF_ON_ERROR_HPP_INCLUDED
#define BOOST_LEAF_ON_ERROR_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/error.hpp>
 
namespace boost { namespace leaf {
 
    class error_monitor
    {
#if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
        int const uncaught_exceptions_;
#endif
        int const err_id_;
 
    public:
 
        error_monitor() noexcept:
#if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
            uncaught_exceptions_(std::uncaught_exceptions()),
#endif
            err_id_(leaf_detail::current_id())
        {
        }
 
        int check_id() const noexcept
        {
            int err_id = leaf_detail::current_id();
            if( err_id != err_id_ )
                return err_id;
            else
            {
#ifndef BOOST_LEAF_NO_EXCEPTIONS
#   if BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
                if( std::uncaught_exceptions() > uncaught_exceptions_ )
#   else
                if( std::uncaught_exception() )
#   endif
                    return leaf_detail::new_id();
#endif
                return 0;
            }
        }
 
        int get_id() const noexcept
        {
            int err_id = leaf_detail::current_id();
            if( err_id != err_id_ )
                return err_id;
            else
                return leaf_detail::new_id();
        }
 
        error_id check() const noexcept
        {
            return leaf_detail::make_error_id(check_id());
        }
 
        error_id assigned_error_id() const noexcept
        {
            return leaf_detail::make_error_id(get_id());
        }
    };
 
    ////////////////////////////////////////////
 
    namespace leaf_detail
    {
        template <int I, class Tuple>
        struct tuple_for_each_preload
        {
            BOOST_LEAF_CONSTEXPR static void trigger( Tuple & tup, int err_id ) noexcept
            {
                BOOST_LEAF_ASSERT((err_id&3)==1);
                tuple_for_each_preload<I-1,Tuple>::trigger(tup,err_id);
                std::get<I-1>(tup).trigger(err_id);
            }
        };
 
        template <class Tuple>
        struct tuple_for_each_preload<0, Tuple>
        {
            BOOST_LEAF_CONSTEXPR static void trigger( Tuple const &, int ) noexcept { }
        };
 
        template <class E>
        class preloaded_item
        {
            using decay_E = typename std::decay<E>::type;
            slot<decay_E> * s_;
            decay_E e_;
 
        public:
 
            BOOST_LEAF_CONSTEXPR preloaded_item( E && e ):
                s_(tl_slot_ptr<decay_E>::p),
                e_(std::forward<E>(e))
            {
            }
 
            BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
            {
                BOOST_LEAF_ASSERT((err_id&3)==1);
                if( s_ )
                {
                    if( !s_->has_value(err_id) )
                        s_->put(err_id, std::move(e_));
                }
#if BOOST_LEAF_DIAGNOSTICS
                else
                {
                    int c = tl_unexpected_enabled<>::counter;
                    BOOST_LEAF_ASSERT(c>=0);
                    if( c )
                        load_unexpected(err_id, std::move(e_));
                }
#endif
            }
        };
 
        template <class F>
        class deferred_item
        {
            using E = decltype(std::declval<F>()());
            slot<E> * s_;
            F f_;
 
        public:
 
            BOOST_LEAF_CONSTEXPR deferred_item( F && f ) noexcept:
                s_(tl_slot_ptr<E>::p),
                f_(std::forward<F>(f))
            {
            }
 
            BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
            {
                BOOST_LEAF_ASSERT((err_id&3)==1);
                if( s_ )
                {
                    if( !s_->has_value(err_id) )
                        s_->put(err_id, f_());
                }
#if BOOST_LEAF_DIAGNOSTICS
                else
                {
                    int c = tl_unexpected_enabled<>::counter;
                    BOOST_LEAF_ASSERT(c>=0);
                    if( c )
                        load_unexpected(err_id, std::forward<E>(f_()));
                }
#endif
            }
        };
 
        template <class F, class A0 = fn_arg_type<F,0>, int arity = function_traits<F>::arity>
        class accumulating_item;
 
        template <class F, class A0>
        class accumulating_item<F, A0 &, 1>
        {
            using E = A0;
            slot<E> * s_;
            F f_;
 
        public:
 
            BOOST_LEAF_CONSTEXPR accumulating_item( F && f ) noexcept:
                s_(tl_slot_ptr<E>::p),
                f_(std::forward<F>(f))
            {
            }
 
            BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
            {
                BOOST_LEAF_ASSERT((err_id&3)==1);
                if( s_ )
                    if( E * e = s_->has_value(err_id) )
                        (void) f_(*e);
                    else
                        (void) f_(s_->put(err_id, E()));
            }
        };
 
        template <class... Item>
        class preloaded
        {
            preloaded & operator=( preloaded const & ) = delete;
 
            std::tuple<Item...> p_;
            bool moved_;
            error_monitor id_;
 
        public:
 
            BOOST_LEAF_CONSTEXPR explicit preloaded( Item && ... i ):
                p_(std::forward<Item>(i)...),
                moved_(false)
            {
            }
 
            BOOST_LEAF_CONSTEXPR preloaded( preloaded && x ) noexcept:
                p_(std::move(x.p_)),
                moved_(false),
                id_(std::move(x.id_))
            {
                x.moved_ = true;
            }
 
            ~preloaded() noexcept
            {
                if( moved_ )
                    return;
                if( auto id = id_.check_id() )
                    tuple_for_each_preload<sizeof...(Item),decltype(p_)>::trigger(p_,id);
            }
        };
 
        template <class T, int arity = function_traits<T>::arity>
        struct deduce_item_type;
 
        template <class T>
        struct deduce_item_type<T, -1>
        {
            using type = preloaded_item<T>;
        };
 
        template <class F>
        struct deduce_item_type<F, 0>
        {
            using type = deferred_item<F>;
        };
 
        template <class F>
        struct deduce_item_type<F, 1>
        {
            using type = accumulating_item<F>;
        };
    }
 
    template <class... Item>
    BOOST_LEAF_NODISCARD BOOST_LEAF_CONSTEXPR inline
    leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>
    on_error( Item && ... i )
    {
        return leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>(std::forward<Item>(i)...);
    }
 
} }
 
#endif