error.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. #ifndef BOOST_LEAF_ERROR_HPP_INCLUDED
  2. #define BOOST_LEAF_ERROR_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/detail/optional.hpp>
  8. #include <boost/leaf/detail/function_traits.hpp>
  9. #include <boost/leaf/detail/capture_list.hpp>
  10. #include <boost/leaf/detail/print.hpp>
  11. #if BOOST_LEAF_CFG_DIAGNOSTICS
  12. # include <ostream>
  13. #endif
  14. #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
  15. # include <system_error>
  16. #endif
  17. #if BOOST_LEAF_CFG_CAPTURE
  18. # include <memory>
  19. #endif
  20. #define BOOST_LEAF_TOKEN_PASTE(x, y) x ## y
  21. #define BOOST_LEAF_TOKEN_PASTE2(x, y) BOOST_LEAF_TOKEN_PASTE(x, y)
  22. #define BOOST_LEAF_TMP BOOST_LEAF_TOKEN_PASTE2(boost_leaf_tmp_, __LINE__)
  23. #define BOOST_LEAF_ASSIGN(v,r)\
  24. auto && BOOST_LEAF_TMP = r;\
  25. static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(BOOST_LEAF_TMP)>::type>::value,\
  26. "BOOST_LEAF_ASSIGN/BOOST_LEAF_AUTO requires a result object as the second argument (see is_result_type)");\
  27. if( !BOOST_LEAF_TMP )\
  28. return BOOST_LEAF_TMP.error();\
  29. v = std::forward<decltype(BOOST_LEAF_TMP)>(BOOST_LEAF_TMP).value()
  30. #define BOOST_LEAF_AUTO(v, r)\
  31. BOOST_LEAF_ASSIGN(auto v, r)
  32. #if BOOST_LEAF_CFG_GNUC_STMTEXPR
  33. #define BOOST_LEAF_CHECK(r)\
  34. ({\
  35. auto && BOOST_LEAF_TMP = (r);\
  36. static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(BOOST_LEAF_TMP)>::type>::value,\
  37. "BOOST_LEAF_CHECK requires a result object (see is_result_type)");\
  38. if( !BOOST_LEAF_TMP )\
  39. return BOOST_LEAF_TMP.error();\
  40. std::move(BOOST_LEAF_TMP);\
  41. }).value()
  42. #else
  43. #define BOOST_LEAF_CHECK(r)\
  44. {\
  45. auto && BOOST_LEAF_TMP = (r);\
  46. static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(BOOST_LEAF_TMP)>::type>::value,\
  47. "BOOST_LEAF_CHECK requires a result object (see is_result_type)");\
  48. if( !BOOST_LEAF_TMP )\
  49. return BOOST_LEAF_TMP.error();\
  50. }
  51. #endif
  52. #define BOOST_LEAF_NEW_ERROR ::boost::leaf::leaf_detail::inject_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::new_error
  53. namespace boost { namespace leaf {
  54. struct BOOST_LEAF_SYMBOL_VISIBLE e_source_location
  55. {
  56. char const * file;
  57. int line;
  58. char const * function;
  59. template <class CharT, class Traits>
  60. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, e_source_location const & x )
  61. {
  62. return os << leaf::type<e_source_location>() << ": " << x.file << '(' << x.line << ") in function " << x.function;
  63. }
  64. };
  65. ////////////////////////////////////////
  66. class BOOST_LEAF_SYMBOL_VISIBLE error_id;
  67. namespace leaf_detail
  68. {
  69. template <class E>
  70. class BOOST_LEAF_SYMBOL_VISIBLE slot:
  71. optional<E>
  72. {
  73. slot( slot const & ) = delete;
  74. slot & operator=( slot const & ) = delete;
  75. using impl = optional<E>;
  76. slot<E> * prev_;
  77. public:
  78. BOOST_LEAF_CONSTEXPR slot() noexcept:
  79. prev_(nullptr)
  80. {
  81. }
  82. BOOST_LEAF_CONSTEXPR slot( slot && x ) noexcept:
  83. optional<E>(std::move(x)),
  84. prev_(nullptr)
  85. {
  86. BOOST_LEAF_ASSERT(x.prev_==nullptr);
  87. }
  88. ~slot() noexcept
  89. {
  90. BOOST_LEAF_ASSERT(tls::read_ptr<slot<E>>() != this);
  91. }
  92. void activate() noexcept
  93. {
  94. prev_ = tls::read_ptr<slot<E>>();
  95. tls::write_ptr<slot<E>>(this);
  96. }
  97. void deactivate() const noexcept
  98. {
  99. tls::write_ptr<slot<E>>(prev_);
  100. }
  101. void unload( int err_id ) noexcept(!BOOST_LEAF_CFG_CAPTURE);
  102. template <class CharT, class Traits>
  103. void print( std::basic_ostream<CharT, Traits> & os, int err_id_to_print ) const
  104. {
  105. if( !diagnostic<E>::is_invisible )
  106. if( int k = this->key() )
  107. {
  108. if( err_id_to_print )
  109. {
  110. if( err_id_to_print!=k )
  111. return;
  112. }
  113. else
  114. os << '[' << k << "] ";
  115. diagnostic<E>::print(os, value(k));
  116. os << '\n';
  117. }
  118. }
  119. using impl::load;
  120. using impl::has_value;
  121. using impl::value;
  122. };
  123. }
  124. ////////////////////////////////////////
  125. #if BOOST_LEAF_CFG_CAPTURE
  126. namespace leaf_detail
  127. {
  128. class BOOST_LEAF_SYMBOL_VISIBLE dynamic_allocator:
  129. capture_list
  130. {
  131. dynamic_allocator( dynamic_allocator const & ) = delete;
  132. dynamic_allocator & operator=( dynamic_allocator const & ) = delete;
  133. class capturing_node:
  134. public capture_list::node
  135. {
  136. protected:
  137. BOOST_LEAF_CONSTEXPR explicit capturing_node( capture_list::node * * & last ) noexcept:
  138. node(last)
  139. {
  140. BOOST_LEAF_ASSERT(last == &next_);
  141. BOOST_LEAF_ASSERT(next_ == nullptr);
  142. }
  143. public:
  144. virtual void deactivate() const noexcept = 0;
  145. };
  146. template <class E>
  147. class capturing_slot_node:
  148. public capturing_node,
  149. public slot<E>
  150. {
  151. using impl = slot<E>;
  152. capturing_slot_node( capturing_slot_node const & ) = delete;
  153. capturing_slot_node & operator=( capturing_slot_node const & ) = delete;
  154. void deactivate() const noexcept final override
  155. {
  156. impl::deactivate();
  157. }
  158. void unload( int err_id ) final override
  159. {
  160. impl::unload(err_id);
  161. }
  162. #if BOOST_LEAF_CFG_DIAGNOSTICS
  163. void print( std::ostream & os, int err_id_to_print ) const final override
  164. {
  165. impl::print(os, err_id_to_print);
  166. }
  167. #endif
  168. public:
  169. template <class T>
  170. BOOST_LEAF_CONSTEXPR capturing_slot_node( capture_list::node * * & last, int err_id, T && e ):
  171. capturing_node(last)
  172. {
  173. BOOST_LEAF_ASSERT(last == &next_);
  174. BOOST_LEAF_ASSERT(next_ == nullptr);
  175. impl::load(err_id, std::forward<T>(e));
  176. }
  177. };
  178. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  179. class capturing_exception_node:
  180. public capturing_node
  181. {
  182. capturing_exception_node( capturing_exception_node const & ) = delete;
  183. capturing_exception_node & operator=( capturing_exception_node const & ) = delete;
  184. void deactivate() const noexcept final override
  185. {
  186. BOOST_LEAF_ASSERT(0);
  187. }
  188. void unload( int ) final override
  189. {
  190. std::rethrow_exception(ex_);
  191. }
  192. #if BOOST_LEAF_CFG_DIAGNOSTICS
  193. void print( std::ostream &, int err_id_to_print ) const final override
  194. {
  195. }
  196. #endif
  197. std::exception_ptr const ex_;
  198. public:
  199. capturing_exception_node( capture_list::node * * & last, std::exception_ptr && ex ) noexcept:
  200. capturing_node(last),
  201. ex_(std::move(ex))
  202. {
  203. BOOST_LEAF_ASSERT(last == &next_);
  204. BOOST_LEAF_ASSERT(ex_);
  205. }
  206. };
  207. #endif
  208. node * * last_;
  209. public:
  210. dynamic_allocator() noexcept:
  211. capture_list(nullptr),
  212. last_(&first_)
  213. {
  214. BOOST_LEAF_ASSERT(first_ == nullptr);
  215. }
  216. dynamic_allocator( dynamic_allocator && other ) noexcept:
  217. capture_list(std::move(other)),
  218. last_(other.last_ == &other.first_? &first_ : other.last_)
  219. {
  220. BOOST_LEAF_ASSERT(last_ != nullptr);
  221. BOOST_LEAF_ASSERT(*last_ == nullptr);
  222. BOOST_LEAF_ASSERT(other.first_ == nullptr);
  223. other.last_ = &other.first_;
  224. }
  225. template <class E>
  226. typename std::decay<E>::type & dynamic_load(int err_id, E && e)
  227. {
  228. using T = typename std::decay<E>::type;
  229. BOOST_LEAF_ASSERT(last_ != nullptr);
  230. BOOST_LEAF_ASSERT(*last_ == nullptr);
  231. BOOST_LEAF_ASSERT(tls::read_ptr<slot<T>>() == nullptr);
  232. capturing_slot_node<T> * csn = new capturing_slot_node<T>(last_, err_id, std::forward<E>(e));
  233. csn->activate();
  234. return csn->value(err_id);
  235. }
  236. void deactivate() const noexcept
  237. {
  238. for_each(
  239. []( capture_list::node const & n )
  240. {
  241. static_cast<capturing_node const &>(n).deactivate();
  242. } );
  243. }
  244. template <class LeafResult>
  245. LeafResult extract_capture_list(int err_id) noexcept
  246. {
  247. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  248. if( std::exception_ptr ex = std::current_exception() )
  249. (void) new capturing_exception_node(last_, std::move(ex));
  250. #endif
  251. leaf_detail::capture_list::node * const f = first_;
  252. first_ = nullptr;
  253. last_ = &first_;
  254. return { err_id, capture_list(f) };
  255. }
  256. using capture_list::unload;
  257. using capture_list::print;
  258. };
  259. template <>
  260. struct diagnostic<dynamic_allocator, false, false, false>
  261. {
  262. static constexpr bool is_invisible = true;
  263. template <class CharT, class Traits>
  264. BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, dynamic_allocator const & )
  265. {
  266. }
  267. };
  268. template <>
  269. inline void slot<dynamic_allocator>::deactivate() const noexcept
  270. {
  271. if( dynamic_allocator const * c = this->has_value() )
  272. c->deactivate();
  273. tls::write_ptr<slot<dynamic_allocator>>(prev_);
  274. }
  275. template <>
  276. inline void slot<dynamic_allocator>::unload( int err_id ) noexcept(false)
  277. {
  278. BOOST_LEAF_ASSERT(err_id);
  279. if( dynamic_allocator * da1 = this->has_value() )
  280. da1->unload(err_id);
  281. }
  282. template <class E>
  283. inline void dynamic_load_( int err_id, E && e )
  284. {
  285. if( slot<dynamic_allocator> * sl = tls::read_ptr<slot<dynamic_allocator>>() )
  286. {
  287. if( dynamic_allocator * c = sl->has_value() )
  288. c->dynamic_load(err_id, std::forward<E>(e));
  289. else
  290. sl->load(err_id).dynamic_load(err_id, std::forward<E>(e));
  291. }
  292. }
  293. template <class E, class F>
  294. inline void dynamic_accumulate_( int err_id, F && f )
  295. {
  296. if( slot<dynamic_allocator> * sl = tls::read_ptr<slot<dynamic_allocator>>() )
  297. {
  298. if( dynamic_allocator * c = sl->has_value(err_id) )
  299. (void) std::forward<F>(f)(c->dynamic_load(err_id, E{}));
  300. else
  301. (void) std::forward<F>(f)(sl->load(err_id).dynamic_load(err_id, E{}));
  302. }
  303. }
  304. template <bool OnError, class E>
  305. inline void dynamic_load( int err_id, E && e ) noexcept(OnError)
  306. {
  307. if( OnError )
  308. {
  309. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  310. try
  311. {
  312. #endif
  313. dynamic_load_(err_id, std::forward<E>(e));
  314. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  315. }
  316. catch(...)
  317. {
  318. }
  319. #endif
  320. }
  321. else
  322. dynamic_load_(err_id, std::forward<E>(e));
  323. }
  324. template <bool OnError, class E, class F>
  325. inline void dynamic_load_accumulate( int err_id, F && f ) noexcept(OnError)
  326. {
  327. if( OnError )
  328. {
  329. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  330. try
  331. {
  332. #endif
  333. dynamic_accumulate_<E>(err_id, std::forward<F>(f));
  334. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  335. }
  336. catch(...)
  337. {
  338. }
  339. #endif
  340. }
  341. else
  342. dynamic_accumulate_<E>(err_id, std::forward<F>(f));
  343. }
  344. }
  345. #endif
  346. ////////////////////////////////////////
  347. namespace leaf_detail
  348. {
  349. template <class E>
  350. inline void slot<E>::unload( int err_id ) noexcept(!BOOST_LEAF_CFG_CAPTURE)
  351. {
  352. BOOST_LEAF_ASSERT(err_id);
  353. if( this->key()!=err_id )
  354. return;
  355. if( impl * p = tls::read_ptr<slot<E>>() )
  356. {
  357. if( !p->has_value(err_id) )
  358. *p = std::move(*this);
  359. }
  360. #if BOOST_LEAF_CFG_CAPTURE
  361. else
  362. dynamic_load<false>(err_id, std::move(*this).value(err_id));
  363. #endif
  364. }
  365. template <bool OnError, class E>
  366. BOOST_LEAF_CONSTEXPR inline int load_slot( int err_id, E && e ) noexcept(OnError)
  367. {
  368. using T = typename std::decay<E>::type;
  369. static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
  370. static_assert(!std::is_same<T, error_id>::value, "Error objects of type error_id are not allowed");
  371. BOOST_LEAF_ASSERT((err_id&3)==1);
  372. if( slot<T> * p = tls::read_ptr<slot<T>>() )
  373. {
  374. if( !OnError || !p->has_value(err_id) )
  375. (void) p->load(err_id, std::forward<E>(e));
  376. }
  377. #if BOOST_LEAF_CFG_CAPTURE
  378. else
  379. dynamic_load<OnError>(err_id, std::forward<E>(e));
  380. #endif
  381. return 0;
  382. }
  383. template <bool OnError, class F>
  384. BOOST_LEAF_CONSTEXPR inline int load_slot_deferred( int err_id, F && f ) noexcept(OnError)
  385. {
  386. using E = typename function_traits<F>::return_type;
  387. using T = typename std::decay<E>::type;
  388. static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
  389. static_assert(!std::is_same<T, error_id>::value, "Error objects of type error_id are not allowed");
  390. BOOST_LEAF_ASSERT((err_id&3)==1);
  391. if( slot<T> * p = tls::read_ptr<slot<T>>() )
  392. {
  393. if( !OnError || !p->has_value(err_id) )
  394. (void) p->load(err_id, std::forward<F>(f)());
  395. }
  396. #if BOOST_LEAF_CFG_CAPTURE
  397. else
  398. dynamic_load<OnError>(err_id, std::forward<F>(f)());
  399. #endif
  400. return 0;
  401. }
  402. template <bool OnError, class F>
  403. BOOST_LEAF_CONSTEXPR inline int load_slot_accumulate( int err_id, F && f ) noexcept(OnError)
  404. {
  405. static_assert(function_traits<F>::arity==1, "Lambdas passed to accumulate must take a single e-type argument by reference");
  406. using E = typename std::decay<fn_arg_type<F,0>>::type;
  407. static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
  408. BOOST_LEAF_ASSERT((err_id&3)==1);
  409. if( auto sl = tls::read_ptr<slot<E>>() )
  410. {
  411. if( auto v = sl->has_value(err_id) )
  412. (void) std::forward<F>(f)(*v);
  413. else
  414. (void) std::forward<F>(f)(sl->load(err_id,E()));
  415. }
  416. #if BOOST_LEAF_CFG_CAPTURE
  417. else
  418. dynamic_load_accumulate<OnError, E>(err_id, std::forward<F>(f));
  419. #endif
  420. return 0;
  421. }
  422. }
  423. ////////////////////////////////////////
  424. namespace leaf_detail
  425. {
  426. template <class T, int Arity = function_traits<T>::arity>
  427. struct load_item
  428. {
  429. static_assert(Arity==0 || Arity==1, "If a functions is passed to new_error or load, it must take zero or one argument");
  430. };
  431. template <class E>
  432. struct load_item<E, -1>
  433. {
  434. BOOST_LEAF_CONSTEXPR static int load_( int err_id, E && e ) noexcept
  435. {
  436. return load_slot<false>(err_id, std::forward<E>(e));
  437. }
  438. };
  439. template <class F>
  440. struct load_item<F, 0>
  441. {
  442. BOOST_LEAF_CONSTEXPR static int load_( int err_id, F && f ) noexcept
  443. {
  444. return load_slot_deferred<false>(err_id, std::forward<F>(f));
  445. }
  446. };
  447. template <class F>
  448. struct load_item<F, 1>
  449. {
  450. BOOST_LEAF_CONSTEXPR static int load_( int err_id, F && f ) noexcept
  451. {
  452. return load_slot_accumulate<false>(err_id, std::forward<F>(f));
  453. }
  454. };
  455. }
  456. ////////////////////////////////////////
  457. namespace leaf_detail
  458. {
  459. struct BOOST_LEAF_SYMBOL_VISIBLE tls_tag_id_factory_current_id;
  460. template <class=void>
  461. struct BOOST_LEAF_SYMBOL_VISIBLE id_factory
  462. {
  463. static atomic_unsigned_int counter;
  464. BOOST_LEAF_CONSTEXPR static unsigned generate_next_id() noexcept
  465. {
  466. auto id = (counter+=4);
  467. BOOST_LEAF_ASSERT((id&3)==1);
  468. return id;
  469. }
  470. };
  471. template <class T>
  472. atomic_unsigned_int id_factory<T>::counter(unsigned(-3));
  473. inline int current_id() noexcept
  474. {
  475. unsigned id = tls::read_uint<tls_tag_id_factory_current_id>();
  476. BOOST_LEAF_ASSERT(id==0 || (id&3)==1);
  477. return int(id);
  478. }
  479. inline int new_id() noexcept
  480. {
  481. unsigned id = id_factory<>::generate_next_id();
  482. tls::write_uint<tls_tag_id_factory_current_id>(id);
  483. return int(id);
  484. }
  485. struct inject_loc
  486. {
  487. char const * const file;
  488. int const line;
  489. char const * const fn;
  490. template <class T>
  491. friend T operator+( inject_loc loc, T && x ) noexcept
  492. {
  493. x.load_source_location_(loc.file, loc.line, loc.fn);
  494. return std::move(x);
  495. }
  496. };
  497. }
  498. #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
  499. namespace leaf_detail
  500. {
  501. class leaf_category final: public std::error_category
  502. {
  503. bool equivalent( int, std::error_condition const & ) const noexcept final override { return false; }
  504. bool equivalent( std::error_code const &, int ) const noexcept final override { return false; }
  505. char const * name() const noexcept final override { return "LEAF error"; }
  506. std::string message( int ) const final override { return name(); }
  507. public:
  508. ~leaf_category() noexcept final override { }
  509. };
  510. template <class=void>
  511. struct get_error_category
  512. {
  513. static leaf_category cat;
  514. };
  515. template <class T>
  516. leaf_category get_error_category<T>::cat;
  517. inline int import_error_code( std::error_code const & ec ) noexcept
  518. {
  519. if( int err_id = ec.value() )
  520. {
  521. std::error_category const & cat = get_error_category<>::cat;
  522. if( &ec.category() == &cat )
  523. {
  524. BOOST_LEAF_ASSERT((err_id&3)==1);
  525. return (err_id&~3)|1;
  526. }
  527. else
  528. {
  529. err_id = new_id();
  530. (void) load_slot<false>(err_id, ec);
  531. return (err_id&~3)|1;
  532. }
  533. }
  534. else
  535. return 0;
  536. }
  537. }
  538. inline bool is_error_id( std::error_code const & ec ) noexcept
  539. {
  540. bool res = (&ec.category() == &leaf_detail::get_error_category<>::cat);
  541. BOOST_LEAF_ASSERT(!res || !ec.value() || ((ec.value()&3)==1));
  542. return res;
  543. }
  544. #endif
  545. ////////////////////////////////////////
  546. namespace leaf_detail
  547. {
  548. BOOST_LEAF_CONSTEXPR error_id make_error_id(int) noexcept;
  549. }
  550. class BOOST_LEAF_SYMBOL_VISIBLE error_id
  551. {
  552. friend error_id BOOST_LEAF_CONSTEXPR leaf_detail::make_error_id(int) noexcept;
  553. int value_;
  554. BOOST_LEAF_CONSTEXPR explicit error_id( int value ) noexcept:
  555. value_(value)
  556. {
  557. BOOST_LEAF_ASSERT(value_==0 || ((value_&3)==1));
  558. }
  559. public:
  560. BOOST_LEAF_CONSTEXPR error_id() noexcept:
  561. value_(0)
  562. {
  563. }
  564. #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
  565. error_id( std::error_code const & ec ) noexcept:
  566. value_(leaf_detail::import_error_code(ec))
  567. {
  568. BOOST_LEAF_ASSERT(!value_ || ((value_&3)==1));
  569. }
  570. template <class Enum>
  571. error_id( Enum e, typename std::enable_if<std::is_error_code_enum<Enum>::value, Enum>::type * = 0 ) noexcept:
  572. value_(leaf_detail::import_error_code(e))
  573. {
  574. }
  575. operator std::error_code() const noexcept
  576. {
  577. return std::error_code(value_, leaf_detail::get_error_category<>::cat);
  578. }
  579. #endif
  580. BOOST_LEAF_CONSTEXPR error_id load() const noexcept
  581. {
  582. return *this;
  583. }
  584. template <class Item>
  585. BOOST_LEAF_CONSTEXPR error_id load(Item && item) const noexcept
  586. {
  587. if (int err_id = value())
  588. {
  589. int const unused[] = { 42, leaf_detail::load_item<Item>::load_(err_id, std::forward<Item>(item)) };
  590. (void)unused;
  591. }
  592. return *this;
  593. }
  594. template <class... Item>
  595. BOOST_LEAF_CONSTEXPR error_id load( Item && ... item ) const noexcept
  596. {
  597. if( int err_id = value() )
  598. {
  599. int const unused[] = { 42, leaf_detail::load_item<Item>::load_(err_id, std::forward<Item>(item))... };
  600. (void) unused;
  601. }
  602. return *this;
  603. }
  604. BOOST_LEAF_CONSTEXPR int value() const noexcept
  605. {
  606. BOOST_LEAF_ASSERT(value_==0 || ((value_&3)==1));
  607. return value_;
  608. }
  609. BOOST_LEAF_CONSTEXPR explicit operator bool() const noexcept
  610. {
  611. return value_ != 0;
  612. }
  613. BOOST_LEAF_CONSTEXPR friend bool operator==( error_id a, error_id b ) noexcept
  614. {
  615. return a.value_ == b.value_;
  616. }
  617. BOOST_LEAF_CONSTEXPR friend bool operator!=( error_id a, error_id b ) noexcept
  618. {
  619. return !(a == b);
  620. }
  621. BOOST_LEAF_CONSTEXPR friend bool operator<( error_id a, error_id b ) noexcept
  622. {
  623. return a.value_ < b.value_;
  624. }
  625. template <class CharT, class Traits>
  626. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, error_id x )
  627. {
  628. return os << x.value_;
  629. }
  630. BOOST_LEAF_CONSTEXPR void load_source_location_( char const * file, int line, char const * function ) const noexcept
  631. {
  632. BOOST_LEAF_ASSERT(file&&*file);
  633. BOOST_LEAF_ASSERT(line>0);
  634. BOOST_LEAF_ASSERT(function&&*function);
  635. BOOST_LEAF_ASSERT(value_);
  636. (void) load(e_source_location {file,line,function});
  637. }
  638. };
  639. namespace leaf_detail
  640. {
  641. BOOST_LEAF_CONSTEXPR inline error_id make_error_id( int err_id ) noexcept
  642. {
  643. BOOST_LEAF_ASSERT(err_id==0 || (err_id&3)==1);
  644. return error_id((err_id&~3)|1);
  645. }
  646. }
  647. inline error_id new_error() noexcept
  648. {
  649. return leaf_detail::make_error_id(leaf_detail::new_id());
  650. }
  651. template <class... Item>
  652. inline error_id new_error( Item && ... item ) noexcept
  653. {
  654. return leaf_detail::make_error_id(leaf_detail::new_id()).load(std::forward<Item>(item)...);
  655. }
  656. inline error_id current_error() noexcept
  657. {
  658. return leaf_detail::make_error_id(leaf_detail::current_id());
  659. }
  660. ////////////////////////////////////////////
  661. class polymorphic_context
  662. {
  663. };
  664. #if BOOST_LEAF_CFG_CAPTURE
  665. using context_ptr = std::shared_ptr<polymorphic_context>;
  666. #endif
  667. ////////////////////////////////////////////
  668. template <class Ctx>
  669. class context_activator
  670. {
  671. context_activator( context_activator const & ) = delete;
  672. context_activator & operator=( context_activator const & ) = delete;
  673. Ctx * ctx_;
  674. public:
  675. explicit BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator(Ctx & ctx) noexcept:
  676. ctx_(ctx.is_active() ? nullptr : &ctx)
  677. {
  678. if( ctx_ )
  679. ctx_->activate();
  680. }
  681. BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator( context_activator && x ) noexcept:
  682. ctx_(x.ctx_)
  683. {
  684. x.ctx_ = nullptr;
  685. }
  686. BOOST_LEAF_ALWAYS_INLINE ~context_activator() noexcept
  687. {
  688. if( ctx_ && ctx_->is_active() )
  689. ctx_->deactivate();
  690. }
  691. };
  692. ////////////////////////////////////////////
  693. template <class R>
  694. struct is_result_type: std::false_type
  695. {
  696. };
  697. template <class R>
  698. struct is_result_type<R const>: is_result_type<R>
  699. {
  700. };
  701. } }
  702. #endif