File indexing completed on 2025-01-30 09:44:53
0001 #ifndef BOOST_LEAF_ON_ERROR_HPP_INCLUDED
0002 #define BOOST_LEAF_ON_ERROR_HPP_INCLUDED
0003
0004
0005
0006
0007
0008
0009 #include <boost/leaf/config.hpp>
0010 #include <boost/leaf/error.hpp>
0011
0012 namespace boost { namespace leaf {
0013
0014 class error_monitor
0015 {
0016 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
0017 int const uncaught_exceptions_;
0018 #endif
0019 int const err_id_;
0020
0021 public:
0022
0023 error_monitor() noexcept:
0024 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
0025 uncaught_exceptions_(std::uncaught_exceptions()),
0026 #endif
0027 err_id_(leaf_detail::current_id())
0028 {
0029 }
0030
0031 int check_id() const noexcept
0032 {
0033 int err_id = leaf_detail::current_id();
0034 if( err_id != err_id_ )
0035 return err_id;
0036 else
0037 {
0038 #ifndef BOOST_LEAF_NO_EXCEPTIONS
0039 # if BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
0040 if( std::uncaught_exceptions() > uncaught_exceptions_ )
0041 # else
0042 if( std::uncaught_exception() )
0043 # endif
0044 return leaf_detail::new_id();
0045 #endif
0046 return 0;
0047 }
0048 }
0049
0050 int get_id() const noexcept
0051 {
0052 int err_id = leaf_detail::current_id();
0053 if( err_id != err_id_ )
0054 return err_id;
0055 else
0056 return leaf_detail::new_id();
0057 }
0058
0059 error_id check() const noexcept
0060 {
0061 return leaf_detail::make_error_id(check_id());
0062 }
0063
0064 error_id assigned_error_id() const noexcept
0065 {
0066 return leaf_detail::make_error_id(get_id());
0067 }
0068 };
0069
0070
0071
0072 namespace leaf_detail
0073 {
0074 template <int I, class Tuple>
0075 struct tuple_for_each_preload
0076 {
0077 BOOST_LEAF_CONSTEXPR static void trigger( Tuple & tup, int err_id ) noexcept
0078 {
0079 BOOST_LEAF_ASSERT((err_id&3)==1);
0080 tuple_for_each_preload<I-1,Tuple>::trigger(tup,err_id);
0081 std::get<I-1>(tup).trigger(err_id);
0082 }
0083 };
0084
0085 template <class Tuple>
0086 struct tuple_for_each_preload<0, Tuple>
0087 {
0088 BOOST_LEAF_CONSTEXPR static void trigger( Tuple const &, int ) noexcept { }
0089 };
0090
0091 template <class E>
0092 class preloaded_item
0093 {
0094 using decay_E = typename std::decay<E>::type;
0095 decay_E e_;
0096
0097 public:
0098
0099 BOOST_LEAF_CONSTEXPR preloaded_item( E && e ):
0100 e_(std::forward<E>(e))
0101 {
0102 }
0103
0104 BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
0105 {
0106 (void) load_slot<true>(err_id, std::move(e_));
0107 }
0108 };
0109
0110 template <class F>
0111 class deferred_item
0112 {
0113 using E = decltype(std::declval<F>()());
0114 F f_;
0115
0116 public:
0117
0118 BOOST_LEAF_CONSTEXPR deferred_item( F && f ) noexcept:
0119 f_(std::forward<F>(f))
0120 {
0121 }
0122
0123 BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
0124 {
0125 (void) load_slot<true>(err_id, f_());
0126 }
0127 };
0128
0129 template <class F, class A0 = fn_arg_type<F,0>, int arity = function_traits<F>::arity>
0130 class accumulating_item;
0131
0132 template <class F, class A0>
0133 class accumulating_item<F, A0 &, 1>
0134 {
0135 using E = A0;
0136 F f_;
0137
0138 public:
0139
0140 BOOST_LEAF_CONSTEXPR accumulating_item( F && f ) noexcept:
0141 f_(std::forward<F>(f))
0142 {
0143 }
0144
0145 BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
0146 {
0147 accumulate_slot<true>(err_id, std::move(f_));
0148 }
0149 };
0150
0151 template <class... Item>
0152 class preloaded
0153 {
0154 preloaded & operator=( preloaded const & ) = delete;
0155
0156 std::tuple<Item...> p_;
0157 bool moved_;
0158 error_monitor id_;
0159
0160 public:
0161
0162 BOOST_LEAF_CONSTEXPR explicit preloaded( Item && ... i ):
0163 p_(std::forward<Item>(i)...),
0164 moved_(false)
0165 {
0166 }
0167
0168 BOOST_LEAF_CONSTEXPR preloaded( preloaded && x ) noexcept:
0169 p_(std::move(x.p_)),
0170 moved_(false),
0171 id_(std::move(x.id_))
0172 {
0173 x.moved_ = true;
0174 }
0175
0176 ~preloaded() noexcept
0177 {
0178 if( moved_ )
0179 return;
0180 if( auto id = id_.check_id() )
0181 tuple_for_each_preload<sizeof...(Item),decltype(p_)>::trigger(p_,id);
0182 }
0183 };
0184
0185 template <class T, int arity = function_traits<T>::arity>
0186 struct deduce_item_type;
0187
0188 template <class T>
0189 struct deduce_item_type<T, -1>
0190 {
0191 using type = preloaded_item<T>;
0192 };
0193
0194 template <class F>
0195 struct deduce_item_type<F, 0>
0196 {
0197 using type = deferred_item<F>;
0198 };
0199
0200 template <class F>
0201 struct deduce_item_type<F, 1>
0202 {
0203 using type = accumulating_item<F>;
0204 };
0205 }
0206
0207 template <class... Item>
0208 BOOST_LEAF_NODISCARD BOOST_LEAF_CONSTEXPR inline
0209 leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>
0210 on_error( Item && ... i )
0211 {
0212 return leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>(std::forward<Item>(i)...);
0213 }
0214
0215 } }
0216
0217 #endif