context.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED
  2. #define BOOST_LEAF_CONTEXT_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. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  9. # include <thread>
  10. #endif
  11. namespace boost { namespace leaf {
  12. class error_info;
  13. class diagnostic_info;
  14. class verbose_diagnostic_info;
  15. template <class>
  16. struct is_predicate: std::false_type
  17. {
  18. };
  19. namespace leaf_detail
  20. {
  21. template <class T>
  22. struct is_exception: std::is_base_of<std::exception, typename std::decay<T>::type>
  23. {
  24. };
  25. template <class E>
  26. struct handler_argument_traits;
  27. template <class E, bool IsPredicate = is_predicate<E>::value>
  28. struct handler_argument_traits_defaults;
  29. template <class E>
  30. struct handler_argument_traits_defaults<E, false>
  31. {
  32. using error_type = typename std::decay<E>::type;
  33. constexpr static bool always_available = false;
  34. template <class Tup>
  35. BOOST_LEAF_CONSTEXPR static error_type const * check( Tup const &, error_info const & ) noexcept;
  36. template <class Tup>
  37. BOOST_LEAF_CONSTEXPR static error_type * check( Tup &, error_info const & ) noexcept;
  38. template <class Tup>
  39. BOOST_LEAF_CONSTEXPR static E get( Tup & tup, error_info const & ei ) noexcept
  40. {
  41. return *check(tup, ei);
  42. }
  43. static_assert(!is_predicate<error_type>::value, "Handlers must take predicate arguments by value");
  44. static_assert(!std::is_same<E, error_info>::value, "Handlers must take leaf::error_info arguments by const &");
  45. static_assert(!std::is_same<E, diagnostic_info>::value, "Handlers must take leaf::diagnostic_info arguments by const &");
  46. static_assert(!std::is_same<E, verbose_diagnostic_info>::value, "Handlers must take leaf::verbose_diagnostic_info arguments by const &");
  47. };
  48. template <class Pred>
  49. struct handler_argument_traits_defaults<Pred, true>: handler_argument_traits<typename Pred::error_type>
  50. {
  51. using base = handler_argument_traits<typename Pred::error_type>;
  52. static_assert(!base::always_available, "Predicates can't use types that are always_available");
  53. template <class Tup>
  54. BOOST_LEAF_CONSTEXPR static bool check( Tup const & tup, error_info const & ei ) noexcept
  55. {
  56. auto e = base::check(tup, ei);
  57. return e && Pred::evaluate(*e);
  58. }
  59. template <class Tup>
  60. BOOST_LEAF_CONSTEXPR static Pred get( Tup const & tup, error_info const & ei ) noexcept
  61. {
  62. return Pred{*base::check(tup, ei)};
  63. }
  64. };
  65. template <class E>
  66. struct handler_argument_always_available
  67. {
  68. using error_type = E;
  69. constexpr static bool always_available = true;
  70. template <class Tup>
  71. BOOST_LEAF_CONSTEXPR static bool check( Tup &, error_info const & ) noexcept
  72. {
  73. return true;
  74. }
  75. };
  76. template <class E>
  77. struct handler_argument_traits: handler_argument_traits_defaults<E>
  78. {
  79. };
  80. template <>
  81. struct handler_argument_traits<void>
  82. {
  83. using error_type = void;
  84. constexpr static bool always_available = false;
  85. template <class Tup>
  86. BOOST_LEAF_CONSTEXPR static std::exception const * check( Tup const &, error_info const & ) noexcept;
  87. };
  88. template <class E>
  89. struct handler_argument_traits<E &&>
  90. {
  91. static_assert(sizeof(E) == 0, "Error handlers may not take rvalue ref arguments");
  92. };
  93. template <class E>
  94. struct handler_argument_traits<E *>: handler_argument_always_available<typename std::remove_const<E>::type>
  95. {
  96. template <class Tup>
  97. BOOST_LEAF_CONSTEXPR static E * get( Tup & tup, error_info const & ei) noexcept
  98. {
  99. return handler_argument_traits_defaults<E>::check(tup, ei);
  100. }
  101. };
  102. template <>
  103. struct handler_argument_traits<error_info const &>: handler_argument_always_available<void>
  104. {
  105. template <class Tup>
  106. BOOST_LEAF_CONSTEXPR static error_info const & get( Tup const &, error_info const & ei ) noexcept
  107. {
  108. return ei;
  109. }
  110. };
  111. template <class E>
  112. struct handler_argument_traits_require_by_value
  113. {
  114. static_assert(sizeof(E) == 0, "Error handlers must take this type by value");
  115. };
  116. }
  117. ////////////////////////////////////////
  118. namespace leaf_detail
  119. {
  120. template <int I, class Tuple>
  121. struct tuple_for_each
  122. {
  123. BOOST_LEAF_CONSTEXPR static void activate( Tuple & tup ) noexcept
  124. {
  125. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  126. tuple_for_each<I-1,Tuple>::activate(tup);
  127. std::get<I-1>(tup).activate();
  128. }
  129. BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & tup ) noexcept
  130. {
  131. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  132. std::get<I-1>(tup).deactivate();
  133. tuple_for_each<I-1,Tuple>::deactivate(tup);
  134. }
  135. BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup, int err_id ) noexcept
  136. {
  137. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  138. auto & sl = std::get<I-1>(tup);
  139. sl.propagate(err_id);
  140. tuple_for_each<I-1,Tuple>::propagate(tup, err_id);
  141. }
  142. BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int err_id ) noexcept
  143. {
  144. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  145. BOOST_LEAF_ASSERT(err_id != 0);
  146. auto & sl = std::get<I-1>(tup);
  147. if( sl.has_value(err_id) )
  148. (void) load_slot<false>(err_id, std::move(sl).value(err_id));
  149. tuple_for_each<I-1,Tuple>::propagate_captured(tup, err_id);
  150. }
  151. template <class CharT, class Traits>
  152. static void print( std::basic_ostream<CharT, Traits> & os, void const * tup, int key_to_print )
  153. {
  154. BOOST_LEAF_ASSERT(tup != nullptr);
  155. tuple_for_each<I-1,Tuple>::print(os, tup, key_to_print);
  156. std::get<I-1>(*static_cast<Tuple const *>(tup)).print(os, key_to_print);
  157. }
  158. };
  159. template <class Tuple>
  160. struct tuple_for_each<0, Tuple>
  161. {
  162. BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { }
  163. BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { }
  164. BOOST_LEAF_CONSTEXPR static void propagate( Tuple &, int ) noexcept { }
  165. BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple &, int ) noexcept { }
  166. template <class CharT, class Traits>
  167. BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, void const *, int ) { }
  168. };
  169. }
  170. ////////////////////////////////////////////
  171. #if BOOST_LEAF_CFG_DIAGNOSTICS
  172. namespace leaf_detail
  173. {
  174. template <class T> struct requires_unexpected { constexpr static bool value = false; };
  175. template <class T> struct requires_unexpected<T const> { constexpr static bool value = requires_unexpected<T>::value; };
  176. template <class T> struct requires_unexpected<T const &> { constexpr static bool value = requires_unexpected<T>::value; };
  177. template <class T> struct requires_unexpected<T const *> { constexpr static bool value = requires_unexpected<T>::value; };
  178. template <> struct requires_unexpected<e_unexpected_count> { constexpr static bool value = true; };
  179. template <> struct requires_unexpected<e_unexpected_info> { constexpr static bool value = true; };
  180. template <class L>
  181. struct unexpected_requested;
  182. template <template <class ...> class L>
  183. struct unexpected_requested<L<>>
  184. {
  185. constexpr static bool value = false;
  186. };
  187. template <template <class...> class L, template <class> class S, class Car, class... Cdr>
  188. struct unexpected_requested<L<S<Car>, S<Cdr>...>>
  189. {
  190. constexpr static bool value = requires_unexpected<Car>::value || unexpected_requested<L<S<Cdr>...>>::value;
  191. };
  192. }
  193. #endif
  194. ////////////////////////////////////////////
  195. namespace leaf_detail
  196. {
  197. template <class T> struct does_not_participate_in_context_deduction: std::is_abstract<T> { };
  198. template <> struct does_not_participate_in_context_deduction<void>: std::true_type { };
  199. template <> struct does_not_participate_in_context_deduction<error_id>: std::true_type { };
  200. template <class L>
  201. struct deduce_e_type_list;
  202. template <template<class...> class L, class... T>
  203. struct deduce_e_type_list<L<T...>>
  204. {
  205. using type =
  206. leaf_detail_mp11::mp_remove_if<
  207. leaf_detail_mp11::mp_unique<
  208. leaf_detail_mp11::mp_list<typename handler_argument_traits<T>::error_type...>
  209. >,
  210. does_not_participate_in_context_deduction
  211. >;
  212. };
  213. template <class L>
  214. struct deduce_e_tuple_impl;
  215. template <template <class...> class L, class... E>
  216. struct deduce_e_tuple_impl<L<E...>>
  217. {
  218. using type = std::tuple<slot<E>...>;
  219. };
  220. template <class... E>
  221. using deduce_e_tuple = typename deduce_e_tuple_impl<typename deduce_e_type_list<leaf_detail_mp11::mp_list<E...>>::type>::type;
  222. }
  223. ////////////////////////////////////////////
  224. template <class... E>
  225. class context
  226. {
  227. context( context const & ) = delete;
  228. context & operator=( context const & ) = delete;
  229. using Tup = leaf_detail::deduce_e_tuple<E...>;
  230. Tup tup_;
  231. bool is_active_;
  232. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  233. std::thread::id thread_id_;
  234. #endif
  235. protected:
  236. BOOST_LEAF_CONSTEXPR error_id propagate_captured_errors( error_id err_id ) noexcept
  237. {
  238. leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate_captured(tup_, err_id.value());
  239. return err_id;
  240. }
  241. public:
  242. BOOST_LEAF_CONSTEXPR context( context && x ) noexcept:
  243. tup_(std::move(x.tup_)),
  244. is_active_(false)
  245. {
  246. BOOST_LEAF_ASSERT(!x.is_active());
  247. }
  248. BOOST_LEAF_CONSTEXPR context() noexcept:
  249. is_active_(false)
  250. {
  251. }
  252. ~context() noexcept
  253. {
  254. BOOST_LEAF_ASSERT(!is_active());
  255. }
  256. BOOST_LEAF_CONSTEXPR Tup const & tup() const noexcept
  257. {
  258. return tup_;
  259. }
  260. BOOST_LEAF_CONSTEXPR Tup & tup() noexcept
  261. {
  262. return tup_;
  263. }
  264. BOOST_LEAF_CONSTEXPR void activate() noexcept
  265. {
  266. using namespace leaf_detail;
  267. BOOST_LEAF_ASSERT(!is_active());
  268. tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_);
  269. #if BOOST_LEAF_CFG_DIAGNOSTICS
  270. if( unexpected_requested<Tup>::value )
  271. tls::uint_increment<tls_tag_unexpected_enabled_counter>();
  272. #endif
  273. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  274. thread_id_ = std::this_thread::get_id();
  275. #endif
  276. is_active_ = true;
  277. }
  278. BOOST_LEAF_CONSTEXPR void deactivate() noexcept
  279. {
  280. using namespace leaf_detail;
  281. BOOST_LEAF_ASSERT(is_active());
  282. is_active_ = false;
  283. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  284. BOOST_LEAF_ASSERT(std::this_thread::get_id() == thread_id_);
  285. thread_id_ = std::thread::id();
  286. #endif
  287. #if BOOST_LEAF_CFG_DIAGNOSTICS
  288. if( unexpected_requested<Tup>::value )
  289. tls::uint_decrement<tls_tag_unexpected_enabled_counter>();
  290. #endif
  291. tuple_for_each<std::tuple_size<Tup>::value,Tup>::deactivate(tup_);
  292. }
  293. BOOST_LEAF_CONSTEXPR void propagate(error_id id) noexcept
  294. {
  295. BOOST_LEAF_ASSERT(!is_active());
  296. leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate(tup_, id.value());
  297. }
  298. BOOST_LEAF_CONSTEXPR bool is_active() const noexcept
  299. {
  300. return is_active_;
  301. }
  302. template <class CharT, class Traits>
  303. void print( std::basic_ostream<CharT, Traits> & os ) const
  304. {
  305. leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::print(os, &tup_, 0);
  306. }
  307. template <class CharT, class Traits>
  308. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, context const & ctx )
  309. {
  310. ctx.print(os);
  311. return os;
  312. }
  313. template <class R, class... H>
  314. BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ) const;
  315. template <class R, class... H>
  316. BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... );
  317. };
  318. ////////////////////////////////////////
  319. namespace leaf_detail
  320. {
  321. template <class TypeList>
  322. struct deduce_context_impl;
  323. template <template <class...> class L, class... E>
  324. struct deduce_context_impl<L<E...>>
  325. {
  326. using type = context<E...>;
  327. };
  328. template <class TypeList>
  329. using deduce_context = typename deduce_context_impl<TypeList>::type;
  330. template <class H>
  331. struct fn_mp_args_fwd
  332. {
  333. using type = fn_mp_args<H>;
  334. };
  335. template <class... H>
  336. struct fn_mp_args_fwd<std::tuple<H...> &>: fn_mp_args_fwd<std::tuple<H...>> { };
  337. template <class... H>
  338. struct fn_mp_args_fwd<std::tuple<H...> const &>: fn_mp_args_fwd<std::tuple<H...>> { };
  339. template <class... H>
  340. struct fn_mp_args_fwd<std::tuple<H...>>
  341. {
  342. using type = leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>;
  343. };
  344. template <class... H>
  345. struct context_type_from_handlers_impl
  346. {
  347. using type = deduce_context<leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>>;
  348. };
  349. template <class Ctx>
  350. struct polymorphic_context_impl: polymorphic_context, Ctx
  351. {
  352. error_id propagate_captured_errors() noexcept final override { return Ctx::propagate_captured_errors(captured_id_); }
  353. void activate() noexcept final override { Ctx::activate(); }
  354. void deactivate() noexcept final override { Ctx::deactivate(); }
  355. void propagate(error_id id) noexcept final override { Ctx::propagate(id); }
  356. bool is_active() const noexcept final override { return Ctx::is_active(); }
  357. #if BOOST_LEAF_CFG_DIAGNOSTICS
  358. void print( std::ostream & os ) const final override { return Ctx::print(os); }
  359. #endif
  360. };
  361. }
  362. template <class... H>
  363. using context_type_from_handlers = typename leaf_detail::context_type_from_handlers_impl<H...>::type;
  364. ////////////////////////////////////////////
  365. template <class... H>
  366. BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context() noexcept
  367. {
  368. return { };
  369. }
  370. template <class... H>
  371. BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H && ... ) noexcept
  372. {
  373. return { };
  374. }
  375. ////////////////////////////////////////////
  376. #if BOOST_LEAF_CFG_CAPTURE
  377. template <class... H>
  378. inline context_ptr make_shared_context() noexcept
  379. {
  380. return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>();
  381. }
  382. template <class... H>
  383. inline context_ptr make_shared_context( H && ... ) noexcept
  384. {
  385. return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>();
  386. }
  387. #endif
  388. } }
  389. #endif