capture.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef BOOST_LEAF_CAPTURE_HPP_INCLUDED
  2. #define BOOST_LEAF_CAPTURE_HPP_INCLUDED
  3. /// Copyright (c) 2018-2021 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. #ifndef BOOST_LEAF_ENABLE_WARNINGS ///
  7. # if defined(_MSC_VER) ///
  8. # pragma warning(push,1) ///
  9. # elif defined(__clang__) ///
  10. # pragma clang system_header ///
  11. # elif (__GNUC__*100+__GNUC_MINOR__>301) ///
  12. # pragma GCC system_header ///
  13. # endif ///
  14. #endif ///
  15. #include <boost/leaf/exception.hpp>
  16. #include <boost/leaf/on_error.hpp>
  17. namespace boost { namespace leaf {
  18. namespace leaf_detail
  19. {
  20. template <class R, bool IsResult = is_result_type<R>::value>
  21. struct is_result_tag;
  22. template <class R>
  23. struct is_result_tag<R, false>
  24. {
  25. };
  26. template <class R>
  27. struct is_result_tag<R, true>
  28. {
  29. };
  30. }
  31. #ifdef BOOST_LEAF_NO_EXCEPTIONS
  32. namespace leaf_detail
  33. {
  34. template <class R, class F, class... A>
  35. inline
  36. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  37. capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) noexcept
  38. {
  39. auto active_context = activate_context(*ctx);
  40. return std::forward<F>(f)(std::forward<A>(a)...);
  41. }
  42. template <class R, class F, class... A>
  43. inline
  44. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  45. capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) noexcept
  46. {
  47. auto active_context = activate_context(*ctx);
  48. if( auto r = std::forward<F>(f)(std::forward<A>(a)...) )
  49. return r;
  50. else
  51. {
  52. ctx->captured_id_ = r.error();
  53. return std::move(ctx);
  54. }
  55. }
  56. template <class R, class Future>
  57. inline
  58. decltype(std::declval<Future>().get())
  59. future_get_impl(is_result_tag<R, false>, Future & fut) noexcept
  60. {
  61. return fut.get();
  62. }
  63. template <class R, class Future>
  64. inline
  65. decltype(std::declval<Future>().get())
  66. future_get_impl(is_result_tag<R, true>, Future & fut) noexcept
  67. {
  68. if( auto r = fut.get() )
  69. return r;
  70. else
  71. return error_id(r.error()); // unloads
  72. }
  73. }
  74. #else
  75. namespace leaf_detail
  76. {
  77. class capturing_exception:
  78. public std::exception
  79. {
  80. std::exception_ptr ex_;
  81. context_ptr ctx_;
  82. public:
  83. capturing_exception(std::exception_ptr && ex, context_ptr && ctx) noexcept:
  84. ex_(std::move(ex)),
  85. ctx_(std::move(ctx))
  86. {
  87. BOOST_LEAF_ASSERT(ex_);
  88. BOOST_LEAF_ASSERT(ctx_);
  89. BOOST_LEAF_ASSERT(ctx_->captured_id_);
  90. }
  91. [[noreturn]] void unload_and_rethrow_original_exception() const
  92. {
  93. BOOST_LEAF_ASSERT(ctx_->captured_id_);
  94. auto active_context = activate_context(*ctx_);
  95. id_factory<>::current_id = ctx_->captured_id_.value();
  96. std::rethrow_exception(ex_);
  97. }
  98. template <class CharT, class Traits>
  99. void print( std::basic_ostream<CharT, Traits> & os ) const
  100. {
  101. ctx_->print(os);
  102. }
  103. };
  104. template <class R, class F, class... A>
  105. inline
  106. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  107. capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a)
  108. {
  109. auto active_context = activate_context(*ctx);
  110. error_monitor cur_err;
  111. try
  112. {
  113. return std::forward<F>(f)(std::forward<A>(a)...);
  114. }
  115. catch( capturing_exception const & )
  116. {
  117. throw;
  118. }
  119. catch( exception_base const & e )
  120. {
  121. ctx->captured_id_ = e.get_error_id();
  122. throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
  123. }
  124. catch(...)
  125. {
  126. ctx->captured_id_ = cur_err.assigned_error_id();
  127. throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
  128. }
  129. }
  130. template <class R, class F, class... A>
  131. inline
  132. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  133. capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a)
  134. {
  135. auto active_context = activate_context(*ctx);
  136. error_monitor cur_err;
  137. try
  138. {
  139. if( auto && r = std::forward<F>(f)(std::forward<A>(a)...) )
  140. return std::move(r);
  141. else
  142. {
  143. ctx->captured_id_ = r.error();
  144. return std::move(ctx);
  145. }
  146. }
  147. catch( capturing_exception const & )
  148. {
  149. throw;
  150. }
  151. catch( exception_base const & e )
  152. {
  153. ctx->captured_id_ = e.get_error_id();
  154. throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
  155. }
  156. catch(...)
  157. {
  158. ctx->captured_id_ = cur_err.assigned_error_id();
  159. throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
  160. }
  161. }
  162. template <class R, class Future>
  163. inline
  164. decltype(std::declval<Future>().get())
  165. future_get_impl(is_result_tag<R, false>, Future & fut )
  166. {
  167. try
  168. {
  169. return fut.get();
  170. }
  171. catch( capturing_exception const & cap )
  172. {
  173. cap.unload_and_rethrow_original_exception();
  174. }
  175. }
  176. template <class R, class Future>
  177. inline
  178. decltype(std::declval<Future>().get())
  179. future_get_impl(is_result_tag<R, true>, Future & fut )
  180. {
  181. try
  182. {
  183. if( auto r = fut.get() )
  184. return r;
  185. else
  186. return error_id(r.error()); // unloads
  187. }
  188. catch( capturing_exception const & cap )
  189. {
  190. cap.unload_and_rethrow_original_exception();
  191. }
  192. }
  193. }
  194. #endif
  195. template <class F, class... A>
  196. inline
  197. decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
  198. capture(context_ptr && ctx, F && f, A... a)
  199. {
  200. using namespace leaf_detail;
  201. 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)...);
  202. }
  203. template <class Future>
  204. inline
  205. decltype(std::declval<Future>().get())
  206. future_get( Future & fut )
  207. {
  208. using namespace leaf_detail;
  209. return future_get_impl(is_result_tag<decltype(std::declval<Future>().get())>(), fut);
  210. }
  211. ////////////////////////////////////////
  212. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  213. template <class T>
  214. class result;
  215. namespace leaf_detail
  216. {
  217. inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<> )
  218. {
  219. return leaf::new_error(std::current_exception());
  220. }
  221. template <class Ex1, class... Ex>
  222. inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<Ex1,Ex...> )
  223. {
  224. if( Ex1 const * p = dynamic_cast<Ex1 const *>(&ex) )
  225. return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ }).load(*p);
  226. else
  227. return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ });
  228. }
  229. template <class T>
  230. struct deduce_exception_to_result_return_type_impl
  231. {
  232. using type = result<T>;
  233. };
  234. template <class T>
  235. struct deduce_exception_to_result_return_type_impl<result<T>>
  236. {
  237. using type = result<T>;
  238. };
  239. template <class T>
  240. using deduce_exception_to_result_return_type = typename deduce_exception_to_result_return_type_impl<T>::type;
  241. }
  242. template <class... Ex, class F>
  243. inline
  244. leaf_detail::deduce_exception_to_result_return_type<leaf_detail::fn_return_type<F>>
  245. exception_to_result( F && f ) noexcept
  246. {
  247. try
  248. {
  249. return std::forward<F>(f)();
  250. }
  251. catch( std::exception const & ex )
  252. {
  253. return leaf_detail::catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>());
  254. }
  255. catch(...)
  256. {
  257. return leaf::new_error(std::current_exception());
  258. }
  259. }
  260. #endif
  261. } }
  262. #if defined(_MSC_VER) && !defined(BOOST_LEAF_ENABLE_WARNINGS) ///
  263. #pragma warning(pop) ///
  264. #endif ///
  265. #endif