context.hpp 14 KB

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