context.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <class E>
  103. struct handler_argument_traits_require_by_value
  104. {
  105. static_assert(sizeof(E) == 0, "Error handlers must take this type by value");
  106. };
  107. }
  108. ////////////////////////////////////////
  109. namespace leaf_detail
  110. {
  111. template <int I, class Tuple>
  112. struct tuple_for_each
  113. {
  114. BOOST_LEAF_CONSTEXPR static void activate( Tuple & tup ) noexcept
  115. {
  116. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  117. tuple_for_each<I-1,Tuple>::activate(tup);
  118. std::get<I-1>(tup).activate();
  119. }
  120. BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & tup ) noexcept
  121. {
  122. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  123. std::get<I-1>(tup).deactivate();
  124. tuple_for_each<I-1,Tuple>::deactivate(tup);
  125. }
  126. BOOST_LEAF_CONSTEXPR static void unload( Tuple & tup, int err_id ) noexcept
  127. {
  128. static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
  129. BOOST_LEAF_ASSERT(err_id != 0);
  130. auto & sl = std::get<I-1>(tup);
  131. sl.unload(err_id);
  132. tuple_for_each<I-1,Tuple>::unload(tup, err_id);
  133. }
  134. template <class CharT, class Traits>
  135. static void print( std::basic_ostream<CharT, Traits> & os, void const * tup, int err_id_to_print )
  136. {
  137. BOOST_LEAF_ASSERT(tup != nullptr);
  138. tuple_for_each<I-1,Tuple>::print(os, tup, err_id_to_print);
  139. std::get<I-1>(*static_cast<Tuple const *>(tup)).print(os, err_id_to_print);
  140. }
  141. };
  142. template <class Tuple>
  143. struct tuple_for_each<0, Tuple>
  144. {
  145. BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { }
  146. BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { }
  147. BOOST_LEAF_CONSTEXPR static void unload( Tuple &, int ) noexcept { }
  148. template <class CharT, class Traits>
  149. BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, void const *, int ) { }
  150. };
  151. }
  152. ////////////////////////////////////////////
  153. namespace leaf_detail
  154. {
  155. template <class T> struct does_not_participate_in_context_deduction: std::is_abstract<T> { };
  156. template <> struct does_not_participate_in_context_deduction<void>: std::true_type { };
  157. template <> struct does_not_participate_in_context_deduction<error_id>: std::true_type { };
  158. template <class L>
  159. struct deduce_e_type_list;
  160. template <template<class...> class L, class... T>
  161. struct deduce_e_type_list<L<T...>>
  162. {
  163. using type =
  164. leaf_detail_mp11::mp_remove_if<
  165. leaf_detail_mp11::mp_unique<
  166. leaf_detail_mp11::mp_list<typename handler_argument_traits<T>::error_type...>
  167. >,
  168. does_not_participate_in_context_deduction
  169. >;
  170. };
  171. template <class L>
  172. struct deduce_e_tuple_impl;
  173. template <template <class...> class L, class... E>
  174. struct deduce_e_tuple_impl<L<E...>>
  175. {
  176. using type = std::tuple<slot<E>...>;
  177. };
  178. template <class... E>
  179. using deduce_e_tuple = typename deduce_e_tuple_impl<typename deduce_e_type_list<leaf_detail_mp11::mp_list<E...>>::type>::type;
  180. }
  181. ////////////////////////////////////////////
  182. template <class... E>
  183. class context
  184. {
  185. context( context const & ) = delete;
  186. context & operator=( context const & ) = delete;
  187. using Tup = leaf_detail::deduce_e_tuple<E...>;
  188. Tup tup_;
  189. bool is_active_;
  190. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  191. std::thread::id thread_id_;
  192. #endif
  193. public:
  194. BOOST_LEAF_CONSTEXPR context( context && x ) noexcept:
  195. tup_(std::move(x.tup_)),
  196. is_active_(false)
  197. {
  198. BOOST_LEAF_ASSERT(!x.is_active());
  199. }
  200. BOOST_LEAF_CONSTEXPR context() noexcept:
  201. is_active_(false)
  202. {
  203. }
  204. ~context() noexcept
  205. {
  206. BOOST_LEAF_ASSERT(!is_active());
  207. }
  208. BOOST_LEAF_CONSTEXPR Tup const & tup() const noexcept
  209. {
  210. return tup_;
  211. }
  212. BOOST_LEAF_CONSTEXPR Tup & tup() noexcept
  213. {
  214. return tup_;
  215. }
  216. BOOST_LEAF_CONSTEXPR void activate() noexcept
  217. {
  218. using namespace leaf_detail;
  219. BOOST_LEAF_ASSERT(!is_active());
  220. tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_);
  221. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  222. thread_id_ = std::this_thread::get_id();
  223. #endif
  224. is_active_ = true;
  225. }
  226. BOOST_LEAF_CONSTEXPR void deactivate() noexcept
  227. {
  228. using namespace leaf_detail;
  229. BOOST_LEAF_ASSERT(is_active());
  230. is_active_ = false;
  231. #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
  232. BOOST_LEAF_ASSERT(std::this_thread::get_id() == thread_id_);
  233. thread_id_ = std::thread::id();
  234. #endif
  235. tuple_for_each<std::tuple_size<Tup>::value,Tup>::deactivate(tup_);
  236. }
  237. BOOST_LEAF_CONSTEXPR void unload(error_id id) noexcept
  238. {
  239. BOOST_LEAF_ASSERT(!is_active());
  240. leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::unload(tup_, id.value());
  241. }
  242. BOOST_LEAF_CONSTEXPR bool is_active() const noexcept
  243. {
  244. return is_active_;
  245. }
  246. template <class CharT, class Traits>
  247. void print( std::basic_ostream<CharT, Traits> & os ) const
  248. {
  249. leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::print(os, &tup_, 0);
  250. }
  251. template <class CharT, class Traits>
  252. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, context const & ctx )
  253. {
  254. ctx.print(os);
  255. return os;
  256. }
  257. template <class R, class... H>
  258. BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ) const;
  259. template <class R, class... H>
  260. BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... );
  261. };
  262. template <class Ctx>
  263. BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator<Ctx> activate_context(Ctx & ctx) noexcept
  264. {
  265. return context_activator<Ctx>(ctx);
  266. }
  267. ////////////////////////////////////////
  268. namespace leaf_detail
  269. {
  270. template <class TypeList>
  271. struct deduce_context_impl;
  272. template <template <class...> class L, class... E>
  273. struct deduce_context_impl<L<E...>>
  274. {
  275. using type = context<E...>;
  276. };
  277. template <class TypeList>
  278. using deduce_context = typename deduce_context_impl<TypeList>::type;
  279. template <class H>
  280. struct fn_mp_args_fwd
  281. {
  282. using type = fn_mp_args<H>;
  283. };
  284. template <class... H>
  285. struct fn_mp_args_fwd<std::tuple<H...> &>: fn_mp_args_fwd<std::tuple<H...>> { };
  286. template <class... H>
  287. struct fn_mp_args_fwd<std::tuple<H...> const &>: fn_mp_args_fwd<std::tuple<H...>> { };
  288. template <class... H>
  289. struct fn_mp_args_fwd<std::tuple<H...>>
  290. {
  291. using type = leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>;
  292. };
  293. template <class... H>
  294. struct context_type_from_handlers_impl
  295. {
  296. using type = deduce_context<leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>>;
  297. };
  298. }
  299. template <class... H>
  300. using context_type_from_handlers = typename leaf_detail::context_type_from_handlers_impl<H...>::type;
  301. ////////////////////////////////////////////
  302. template <class... H>
  303. BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context() noexcept
  304. {
  305. return { };
  306. }
  307. template <class... H>
  308. BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H && ... ) noexcept
  309. {
  310. return { };
  311. }
  312. ////////////////////////////////////////////
  313. #if BOOST_LEAF_CFG_CAPTURE
  314. template <class...>
  315. BOOST_LEAF_DEPRECATED("Please use try_capture_all instead of make_shared_context/capture.")
  316. inline context_ptr make_shared_context() noexcept
  317. {
  318. return std::make_shared<polymorphic_context>();
  319. }
  320. template <class... H>
  321. BOOST_LEAF_DEPRECATED("Please use try_capture_all instead of make_shared_context/capture.")
  322. inline context_ptr make_shared_context( H && ... ) noexcept
  323. {
  324. return std::make_shared<polymorphic_context>();
  325. }
  326. #endif
  327. } }
  328. #endif