on_error.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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>
  92. class deferred_item
  93. {
  94. using E = decltype(std::declval<F>()());
  95. F f_;
  96. public:
  97. BOOST_LEAF_CONSTEXPR deferred_item( F && f ) noexcept:
  98. f_(std::forward<F>(f))
  99. {
  100. }
  101. BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
  102. {
  103. (void) load_slot<true>(err_id, f_());
  104. }
  105. };
  106. template <class F, class A0 = fn_arg_type<F,0>, int arity = function_traits<F>::arity>
  107. class accumulating_item;
  108. template <class F, class A0>
  109. class accumulating_item<F, A0 &, 1>
  110. {
  111. using E = A0;
  112. F f_;
  113. public:
  114. BOOST_LEAF_CONSTEXPR accumulating_item( F && f ) noexcept:
  115. f_(std::forward<F>(f))
  116. {
  117. }
  118. BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
  119. {
  120. accumulate_slot<true>(err_id, std::move(f_));
  121. }
  122. };
  123. template <class... Item>
  124. class preloaded
  125. {
  126. preloaded & operator=( preloaded const & ) = delete;
  127. std::tuple<Item...> p_;
  128. bool moved_;
  129. error_monitor id_;
  130. public:
  131. BOOST_LEAF_CONSTEXPR explicit preloaded( Item && ... i ):
  132. p_(std::forward<Item>(i)...),
  133. moved_(false)
  134. {
  135. }
  136. BOOST_LEAF_CONSTEXPR preloaded( preloaded && x ) noexcept:
  137. p_(std::move(x.p_)),
  138. moved_(false),
  139. id_(std::move(x.id_))
  140. {
  141. x.moved_ = true;
  142. }
  143. ~preloaded() noexcept
  144. {
  145. if( moved_ )
  146. return;
  147. if( auto id = id_.check_id() )
  148. tuple_for_each_preload<sizeof...(Item),decltype(p_)>::trigger(p_,id);
  149. }
  150. };
  151. template <class T, int arity = function_traits<T>::arity>
  152. struct deduce_item_type;
  153. template <class T>
  154. struct deduce_item_type<T, -1>
  155. {
  156. using type = preloaded_item<T>;
  157. };
  158. template <class F>
  159. struct deduce_item_type<F, 0>
  160. {
  161. using type = deferred_item<F>;
  162. };
  163. template <class F>
  164. struct deduce_item_type<F, 1>
  165. {
  166. using type = accumulating_item<F>;
  167. };
  168. }
  169. template <class... Item>
  170. BOOST_LEAF_NODISCARD BOOST_LEAF_CONSTEXPR inline
  171. leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>
  172. on_error( Item && ... i )
  173. {
  174. return leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>(std::forward<Item>(i)...);
  175. }
  176. } }
  177. #endif