error.hpp 22 KB

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