capture.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef BOOST_LEAF_CAPTURE_HPP_INCLUDED
  2. #define BOOST_LEAF_CAPTURE_HPP_INCLUDED
  3. // Copyright 2018-2022 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/exception.hpp>
  8. #include <boost/leaf/on_error.hpp>
  9. #if BOOST_LEAF_CFG_CAPTURE
  10. namespace boost { namespace leaf {
  11. namespace leaf_detail
  12. {
  13. template <class R, bool IsResult = is_result_type<R>::value>
  14. struct is_result_tag;
  15. template <class R>
  16. struct is_result_tag<R, false>
  17. {
  18. };
  19. template <class R>
  20. struct is_result_tag<R, true>
  21. {
  22. };
  23. }
  24. #ifdef BOOST_LEAF_NO_EXCEPTIONS
  25. namespace leaf_detail
  26. {
  27. template <class R, class F, class... A>
  28. inline
  29. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  30. capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) noexcept
  31. {
  32. auto active_context = activate_context(*ctx);
  33. return std::forward<F>(f)(std::forward<A>(a)...);
  34. }
  35. template <class R, class F, class... A>
  36. inline
  37. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  38. capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) noexcept
  39. {
  40. auto active_context = activate_context(*ctx);
  41. if( auto r = std::forward<F>(f)(std::forward<A>(a)...) )
  42. return r;
  43. else
  44. {
  45. ctx->captured_id_ = r.error();
  46. return std::move(ctx);
  47. }
  48. }
  49. template <class R, class Future>
  50. inline
  51. decltype(std::declval<Future>().get())
  52. future_get_impl(is_result_tag<R, false>, Future & fut) noexcept
  53. {
  54. return fut.get();
  55. }
  56. template <class R, class Future>
  57. inline
  58. decltype(std::declval<Future>().get())
  59. future_get_impl(is_result_tag<R, true>, Future & fut) noexcept
  60. {
  61. if( auto r = fut.get() )
  62. return r;
  63. else
  64. return error_id(r.error()); // unloads
  65. }
  66. }
  67. #else
  68. namespace leaf_detail
  69. {
  70. class capturing_exception:
  71. public std::exception
  72. {
  73. std::exception_ptr ex_;
  74. context_ptr ctx_;
  75. public:
  76. capturing_exception(std::exception_ptr && ex, context_ptr && ctx) noexcept:
  77. ex_(std::move(ex)),
  78. ctx_(std::move(ctx))
  79. {
  80. BOOST_LEAF_ASSERT(ex_);
  81. BOOST_LEAF_ASSERT(ctx_);
  82. BOOST_LEAF_ASSERT(ctx_->captured_id_);
  83. }
  84. [[noreturn]] void unload_and_rethrow_original_exception() const
  85. {
  86. BOOST_LEAF_ASSERT(ctx_->captured_id_);
  87. tls::write_uint<tls_tag_id_factory_current_id>(unsigned(ctx_->captured_id_.value()));
  88. ctx_->propagate(ctx_->captured_id_);
  89. std::rethrow_exception(ex_);
  90. }
  91. template <class CharT, class Traits>
  92. void print( std::basic_ostream<CharT, Traits> & os ) const
  93. {
  94. ctx_->print(os);
  95. }
  96. };
  97. template <class R, class F, class... A>
  98. inline
  99. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  100. capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a)
  101. {
  102. auto active_context = activate_context(*ctx);
  103. error_monitor cur_err;
  104. try
  105. {
  106. return std::forward<F>(f)(std::forward<A>(a)...);
  107. }
  108. catch( capturing_exception const & )
  109. {
  110. throw;
  111. }
  112. catch( exception_base const & e )
  113. {
  114. ctx->captured_id_ = e.get_error_id();
  115. leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
  116. }
  117. catch(...)
  118. {
  119. ctx->captured_id_ = cur_err.assigned_error_id();
  120. leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
  121. }
  122. }
  123. template <class R, class F, class... A>
  124. inline
  125. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  126. capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a)
  127. {
  128. auto active_context = activate_context(*ctx);
  129. error_monitor cur_err;
  130. try
  131. {
  132. if( auto && r = std::forward<F>(f)(std::forward<A>(a)...) )
  133. return std::move(r);
  134. else
  135. {
  136. ctx->captured_id_ = r.error();
  137. return std::move(ctx);
  138. }
  139. }
  140. catch( capturing_exception const & )
  141. {
  142. throw;
  143. }
  144. catch( exception_base const & e )
  145. {
  146. ctx->captured_id_ = e.get_error_id();
  147. leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
  148. }
  149. catch(...)
  150. {
  151. ctx->captured_id_ = cur_err.assigned_error_id();
  152. leaf_detail::throw_exception_impl( capturing_exception(std::current_exception(), std::move(ctx)) );
  153. }
  154. }
  155. template <class R, class Future>
  156. inline
  157. decltype(std::declval<Future>().get())
  158. future_get_impl(is_result_tag<R, false>, Future & fut )
  159. {
  160. try
  161. {
  162. return fut.get();
  163. }
  164. catch( capturing_exception const & cap )
  165. {
  166. cap.unload_and_rethrow_original_exception();
  167. }
  168. }
  169. template <class R, class Future>
  170. inline
  171. decltype(std::declval<Future>().get())
  172. future_get_impl(is_result_tag<R, true>, Future & fut )
  173. {
  174. try
  175. {
  176. if( auto r = fut.get() )
  177. return r;
  178. else
  179. return error_id(r.error()); // unloads
  180. }
  181. catch( capturing_exception const & cap )
  182. {
  183. cap.unload_and_rethrow_original_exception();
  184. }
  185. }
  186. }
  187. #endif
  188. template <class F, class... A>
  189. inline
  190. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  191. capture(context_ptr && ctx, F && f, A... a)
  192. {
  193. using namespace leaf_detail;
  194. return capture_impl(is_result_tag<decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))>(), std::move(ctx), std::forward<F>(f), std::forward<A>(a)...);
  195. }
  196. template <class Future>
  197. inline
  198. decltype(std::declval<Future>().get())
  199. future_get( Future & fut )
  200. {
  201. using namespace leaf_detail;
  202. return future_get_impl(is_result_tag<decltype(std::declval<Future>().get())>(), fut);
  203. }
  204. } }
  205. #endif
  206. #endif