on_error.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #ifndef BOOST_LEAF_ON_ERROR_HPP_INCLUDED
  2. #define BOOST_LEAF_ON_ERROR_HPP_INCLUDED
  3. // Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/leaf/config.hpp>
  7. #include <boost/leaf/error.hpp>
  8. namespace boost { namespace leaf {
  9. class error_monitor
  10. {
  11. #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
  12. int const uncaught_exceptions_;
  13. #endif
  14. int const err_id_;
  15. public:
  16. error_monitor() noexcept:
  17. #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
  18. uncaught_exceptions_(std::uncaught_exceptions()),
  19. #endif
  20. err_id_(leaf_detail::current_id())
  21. {
  22. }
  23. int check_id() const noexcept
  24. {
  25. int err_id = leaf_detail::current_id();
  26. if( err_id != err_id_ )
  27. return err_id;
  28. else
  29. {
  30. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  31. # if BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
  32. if( std::uncaught_exceptions() > uncaught_exceptions_ )
  33. # else
  34. if( std::uncaught_exception() )
  35. # endif
  36. return leaf_detail::new_id();
  37. #endif
  38. return 0;
  39. }
  40. }
  41. int get_id() const noexcept
  42. {
  43. int err_id = leaf_detail::current_id();
  44. if( err_id != err_id_ )
  45. return err_id;
  46. else
  47. return leaf_detail::new_id();
  48. }
  49. error_id check() const noexcept
  50. {
  51. return leaf_detail::make_error_id(check_id());
  52. }
  53. error_id assigned_error_id() const noexcept
  54. {
  55. return leaf_detail::make_error_id(get_id());
  56. }
  57. };
  58. ////////////////////////////////////////////
  59. namespace leaf_detail
  60. {
  61. template <int I, class Tuple>
  62. struct tuple_for_each_preload
  63. {
  64. BOOST_LEAF_CONSTEXPR static void trigger( Tuple & tup, int err_id ) noexcept
  65. {
  66. BOOST_LEAF_ASSERT((err_id&3)==1);
  67. tuple_for_each_preload<I-1,Tuple>::trigger(tup,err_id);
  68. std::get<I-1>(tup).trigger(err_id);
  69. }
  70. };
  71. template <class Tuple>
  72. struct tuple_for_each_preload<0, Tuple>
  73. {
  74. BOOST_LEAF_CONSTEXPR static void trigger( Tuple const &, int ) noexcept { }
  75. };
  76. template <class E>
  77. class preloaded_item
  78. {
  79. using decay_E = typename std::decay<E>::type;
  80. decay_E e_;
  81. public:
  82. BOOST_LEAF_CONSTEXPR preloaded_item( E && e ):
  83. e_(std::forward<E>(e))
  84. {
  85. }
  86. BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
  87. {
  88. (void) load_slot<true>(err_id, std::move(e_));
  89. }
  90. };
  91. template <class F, class ReturnType = typename function_traits<F>::return_type>
  92. class deferred_item
  93. {
  94. F f_;
  95. public:
  96. BOOST_LEAF_CONSTEXPR deferred_item( F && f ) noexcept:
  97. f_(std::forward<F>(f))
  98. {
  99. }
  100. BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
  101. {
  102. (void) load_slot_deferred<true>(err_id, f_);
  103. }
  104. };
  105. template <class F>
  106. class deferred_item<F, void>
  107. {
  108. F f_;
  109. public:
  110. BOOST_LEAF_CONSTEXPR deferred_item( F && f ) noexcept:
  111. f_(std::forward<F>(f))
  112. {
  113. }
  114. BOOST_LEAF_CONSTEXPR void trigger( int ) noexcept
  115. {
  116. f_();
  117. }
  118. };
  119. template <class F, class A0 = fn_arg_type<F,0>, int arity = function_traits<F>::arity>
  120. class accumulating_item;
  121. template <class F, class A0>
  122. class accumulating_item<F, A0 &, 1>
  123. {
  124. F f_;
  125. public:
  126. BOOST_LEAF_CONSTEXPR accumulating_item( F && f ) noexcept:
  127. f_(std::forward<F>(f))
  128. {
  129. }
  130. BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
  131. {
  132. load_slot_accumulate<true>(err_id, std::move(f_));
  133. }
  134. };
  135. template <class... Item>
  136. class preloaded
  137. {
  138. preloaded & operator=( preloaded const & ) = delete;
  139. std::tuple<Item...> p_;
  140. bool moved_;
  141. error_monitor id_;
  142. public:
  143. BOOST_LEAF_CONSTEXPR explicit preloaded( Item && ... i ):
  144. p_(std::forward<Item>(i)...),
  145. moved_(false)
  146. {
  147. }
  148. BOOST_LEAF_CONSTEXPR preloaded( preloaded && x ) noexcept:
  149. p_(std::move(x.p_)),
  150. moved_(false),
  151. id_(std::move(x.id_))
  152. {
  153. x.moved_ = true;
  154. }
  155. ~preloaded() noexcept
  156. {
  157. if( moved_ )
  158. return;
  159. if( auto id = id_.check_id() )
  160. tuple_for_each_preload<sizeof...(Item),decltype(p_)>::trigger(p_,id);
  161. }
  162. };
  163. template <class T, int arity = function_traits<T>::arity>
  164. struct deduce_item_type;
  165. template <class T>
  166. struct deduce_item_type<T, -1>
  167. {
  168. using type = preloaded_item<T>;
  169. };
  170. template <class F>
  171. struct deduce_item_type<F, 0>
  172. {
  173. using type = deferred_item<F>;
  174. };
  175. template <class F>
  176. struct deduce_item_type<F, 1>
  177. {
  178. using type = accumulating_item<F>;
  179. };
  180. }
  181. template <class... Item>
  182. BOOST_LEAF_ATTRIBUTE_NODISCARD BOOST_LEAF_CONSTEXPR inline
  183. leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>
  184. on_error( Item && ... i )
  185. {
  186. return leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>(std::forward<Item>(i)...);
  187. }
  188. } }
  189. #endif