error.hpp 24 KB

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