context.hpp 15 KB

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