error.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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/print.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. #if BOOST_LEAF_CFG_CAPTURE
  17. # include <memory>
  18. #endif
  19. #define BOOST_LEAF_TOKEN_PASTE(x, y) x ## y
  20. #define BOOST_LEAF_TOKEN_PASTE2(x, y) BOOST_LEAF_TOKEN_PASTE(x, y)
  21. #define BOOST_LEAF_TMP BOOST_LEAF_TOKEN_PASTE2(boost_leaf_tmp_, __LINE__)
  22. #define BOOST_LEAF_ASSIGN(v,r)\
  23. auto && BOOST_LEAF_TMP = r;\
  24. static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(BOOST_LEAF_TMP)>::type>::value,\
  25. "BOOST_LEAF_ASSIGN/BOOST_LEAF_AUTO requires a result object as the second argument (see is_result_type)");\
  26. if( !BOOST_LEAF_TMP )\
  27. return BOOST_LEAF_TMP.error();\
  28. v = std::forward<decltype(BOOST_LEAF_TMP)>(BOOST_LEAF_TMP).value()
  29. #define BOOST_LEAF_AUTO(v, r)\
  30. BOOST_LEAF_ASSIGN(auto v, r)
  31. #if BOOST_LEAF_CFG_GNUC_STMTEXPR
  32. #define BOOST_LEAF_CHECK(r)\
  33. ({\
  34. auto && BOOST_LEAF_TMP = (r);\
  35. static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(BOOST_LEAF_TMP)>::type>::value,\
  36. "BOOST_LEAF_CHECK requires a result object (see is_result_type)");\
  37. if( !BOOST_LEAF_TMP )\
  38. return BOOST_LEAF_TMP.error();\
  39. std::move(BOOST_LEAF_TMP);\
  40. }).value()
  41. #else
  42. #define BOOST_LEAF_CHECK(r)\
  43. {\
  44. auto && BOOST_LEAF_TMP = (r);\
  45. static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(BOOST_LEAF_TMP)>::type>::value,\
  46. "BOOST_LEAF_CHECK requires a result object (see is_result_type)");\
  47. if( !BOOST_LEAF_TMP )\
  48. return BOOST_LEAF_TMP.error();\
  49. }
  50. #endif
  51. #define BOOST_LEAF_NEW_ERROR ::boost::leaf::leaf_detail::inject_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::new_error
  52. namespace boost { namespace leaf {
  53. class error_id;
  54. namespace leaf_detail
  55. {
  56. struct BOOST_LEAF_SYMBOL_VISIBLE tls_tag_unexpected_enabled_counter;
  57. struct BOOST_LEAF_SYMBOL_VISIBLE tls_tag_id_factory_current_id;
  58. struct inject_loc
  59. {
  60. char const * const file;
  61. int const line;
  62. char const * const fn;
  63. template <class T>
  64. friend T operator+( inject_loc loc, T && x ) noexcept
  65. {
  66. x.load_source_location_(loc.file, loc.line, loc.fn);
  67. return std::move(x);
  68. }
  69. };
  70. }
  71. } }
  72. ////////////////////////////////////////
  73. namespace boost { namespace leaf {
  74. #if BOOST_LEAF_CFG_DIAGNOSTICS
  75. namespace leaf_detail
  76. {
  77. class BOOST_LEAF_SYMBOL_VISIBLE e_unexpected_count
  78. {
  79. public:
  80. char const * (*first_type)();
  81. int count;
  82. BOOST_LEAF_CONSTEXPR explicit e_unexpected_count(char const * (*ft)()) noexcept:
  83. first_type(ft),
  84. count(1)
  85. {
  86. }
  87. template <class CharT, class Traits>
  88. void print( std::basic_ostream<CharT, Traits> & os ) const
  89. {
  90. BOOST_LEAF_ASSERT(first_type != nullptr);
  91. BOOST_LEAF_ASSERT(count>0);
  92. os << "Detected ";
  93. if( count==1 )
  94. os << "1 attempt to communicate an unexpected error object";
  95. else
  96. os << count << " attempts to communicate unexpected error objects, the first one";
  97. (os << " of type " << first_type() << '\n').flush();
  98. }
  99. };
  100. template <>
  101. struct diagnostic<e_unexpected_count, false, false>
  102. {
  103. static constexpr bool is_invisible = true;
  104. template <class CharT, class Traits>
  105. BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, e_unexpected_count const &) noexcept { }
  106. };
  107. template <class E>
  108. class BOOST_LEAF_SYMBOL_VISIBLE slot;
  109. class BOOST_LEAF_SYMBOL_VISIBLE e_unexpected_info
  110. {
  111. e_unexpected_info( e_unexpected_info const & ) = delete;
  112. e_unexpected_info & operator=( e_unexpected_info const & ) = delete;
  113. struct slot_base
  114. {
  115. public:
  116. slot_base * next_;
  117. virtual void deactivate() noexcept = 0;
  118. virtual void propagate( int err_id ) noexcept = 0;
  119. virtual void print( std::ostream &, int key_to_print ) const = 0;
  120. virtual ~slot_base() noexcept { };
  121. protected:
  122. BOOST_LEAF_CONSTEXPR slot_base():
  123. next_(nullptr)
  124. {
  125. }
  126. };
  127. template <class E>
  128. class slot_store: public slot_base, public slot<E>
  129. {
  130. slot_store( slot_store const & ) = delete;
  131. slot_store & operator=( slot_store const & ) = delete;
  132. void deactivate() noexcept final override
  133. {
  134. slot<E>::deactivate();
  135. }
  136. void propagate( int err_id ) noexcept final override
  137. {
  138. slot<E>::propagate(err_id);
  139. }
  140. void print( std::ostream & os, int key_to_print ) const final override
  141. {
  142. slot<E>::print(os, key_to_print);
  143. }
  144. public:
  145. template <class T>
  146. BOOST_LEAF_CONSTEXPR slot_store( int err_id, T && e )
  147. {
  148. slot<E>::put(err_id, std::forward<T>(e));
  149. }
  150. };
  151. slot_base * first_;
  152. slot_base * * last_;
  153. public:
  154. BOOST_LEAF_CONSTEXPR e_unexpected_info() noexcept:
  155. first_(nullptr),
  156. last_(&first_)
  157. {
  158. }
  159. BOOST_LEAF_CONSTEXPR e_unexpected_info( e_unexpected_info && other ) noexcept:
  160. first_(other.first_),
  161. last_(other.last_ == &other.first_? &first_ : other.last_)
  162. {
  163. BOOST_LEAF_ASSERT(last_ != nullptr);
  164. BOOST_LEAF_ASSERT(*last_ == nullptr);
  165. other.first_ = nullptr;
  166. other.last_ = nullptr;
  167. }
  168. ~e_unexpected_info() noexcept
  169. {
  170. for( slot_base const * p = first_; p; )
  171. {
  172. slot_base const * n = p -> next_;
  173. delete p;
  174. p = n;
  175. }
  176. }
  177. template <class E>
  178. BOOST_LEAF_CONSTEXPR typename std::decay<E>::type & put(int err_id, E && e)
  179. {
  180. using T = typename std::decay<E>::type;
  181. BOOST_LEAF_ASSERT(last_ != nullptr);
  182. BOOST_LEAF_ASSERT(*last_ == nullptr);
  183. BOOST_LEAF_ASSERT(tls::read_ptr<slot<T>>() == nullptr);
  184. slot_store<T> * ss = new slot_store<T>(err_id, std::forward<E>(e));
  185. *last_ = ss;
  186. last_ = &ss->next_;
  187. ss->activate();
  188. return ss->value(err_id);
  189. }
  190. void deactivate() noexcept
  191. {
  192. for( slot_base * p=first_; p; p=p->next_ )
  193. p->deactivate();
  194. }
  195. void propagate( int err_id ) noexcept
  196. {
  197. for( slot_base * p=first_; p; p=p->next_ )
  198. p->propagate(err_id);
  199. }
  200. template <class CharT, class Traits>
  201. void print( std::basic_ostream<CharT, Traits> & os, int key_to_print ) const
  202. {
  203. os << "Unhandled error objects:\n";
  204. for( slot_base const * p=first_; p; p=p->next_ )
  205. p->print(os, key_to_print);
  206. }
  207. };
  208. template <>
  209. struct diagnostic<e_unexpected_info, false, false>
  210. {
  211. static constexpr bool is_invisible = true;
  212. template <class CharT, class Traits>
  213. BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, e_unexpected_info const &) noexcept { }
  214. };
  215. }
  216. #endif
  217. } }
  218. ////////////////////////////////////////
  219. namespace boost { namespace leaf {
  220. struct BOOST_LEAF_SYMBOL_VISIBLE e_source_location
  221. {
  222. char const * file;
  223. int line;
  224. char const * function;
  225. template <class CharT, class Traits>
  226. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, e_source_location const & x )
  227. {
  228. return os << leaf::type<e_source_location>() << ": " << x.file << '(' << x.line << ") in function " << x.function;
  229. }
  230. };
  231. ////////////////////////////////////////
  232. namespace leaf_detail
  233. {
  234. template <class E>
  235. class BOOST_LEAF_SYMBOL_VISIBLE slot:
  236. optional<E>
  237. {
  238. slot( slot const & ) = delete;
  239. slot & operator=( slot const & ) = delete;
  240. using impl = optional<E>;
  241. slot<E> * prev_;
  242. public:
  243. BOOST_LEAF_CONSTEXPR slot() noexcept:
  244. prev_(nullptr)
  245. {
  246. }
  247. BOOST_LEAF_CONSTEXPR slot( slot && x ) noexcept:
  248. optional<E>(std::move(x)),
  249. prev_(nullptr)
  250. {
  251. BOOST_LEAF_ASSERT(x.prev_==nullptr);
  252. }
  253. void activate() noexcept
  254. {
  255. prev_ = tls::read_ptr<slot<E>>();
  256. tls::write_ptr<slot<E>>(this);
  257. }
  258. void deactivate() noexcept
  259. {
  260. tls::write_ptr<slot<E>>(prev_);
  261. }
  262. void propagate( int err_id ) noexcept(!BOOST_LEAF_CFG_DIAGNOSTICS);
  263. template <class CharT, class Traits>
  264. void print( std::basic_ostream<CharT, Traits> & os, int key_to_print ) const
  265. {
  266. #if BOOST_LEAF_CFG_DIAGNOSTICS
  267. if( !diagnostic<E>::is_invisible )
  268. if( int k = this->key() )
  269. {
  270. if( key_to_print )
  271. {
  272. if( key_to_print!=k )
  273. return;
  274. }
  275. else
  276. os << '[' << k << "] ";
  277. diagnostic<E>::print(os, value(k));
  278. (os << '\n').flush();
  279. }
  280. #else
  281. (void) os;
  282. (void) key_to_print;
  283. #endif
  284. }
  285. using impl::put;
  286. using impl::has_value;
  287. using impl::value;
  288. };
  289. #if BOOST_LEAF_CFG_DIAGNOSTICS
  290. template <>
  291. inline void slot<e_unexpected_info>::deactivate() noexcept
  292. {
  293. if( int const err_id = this->key() )
  294. if( e_unexpected_info * info = this->has_value(err_id) )
  295. info->deactivate();
  296. tls::write_ptr<slot<e_unexpected_info>>(prev_);
  297. }
  298. template <>
  299. inline void slot<e_unexpected_info>::propagate( int err_id )
  300. {
  301. if( e_unexpected_info * info = this->has_value(err_id) )
  302. info->propagate(err_id);
  303. }
  304. template <class E>
  305. BOOST_LEAF_CONSTEXPR inline void load_unexpected_count( int err_id ) noexcept
  306. {
  307. if( slot<e_unexpected_count> * sl = tls::read_ptr<slot<e_unexpected_count>>() )
  308. {
  309. if( e_unexpected_count * unx = sl->has_value(err_id) )
  310. ++unx->count;
  311. else
  312. sl->put(err_id, e_unexpected_count(&type<E>));
  313. }
  314. }
  315. template <class E>
  316. BOOST_LEAF_CONSTEXPR inline void load_unexpected_info( int err_id, E && e )
  317. {
  318. if( slot<e_unexpected_info> * sl = tls::read_ptr<slot<e_unexpected_info>>() )
  319. {
  320. if( e_unexpected_info * unx = sl->has_value(err_id) )
  321. (void) unx->put(err_id, std::forward<E>(e));
  322. else
  323. (void) sl->put(err_id).put(err_id, std::forward<E>(e));
  324. }
  325. }
  326. template <class E, class F>
  327. BOOST_LEAF_CONSTEXPR inline void accumulate_unexpected_info( int err_id, F && f )
  328. {
  329. if( slot<e_unexpected_info> * sl = tls::read_ptr<slot<e_unexpected_info>>() )
  330. {
  331. if( e_unexpected_info * unx = sl->has_value(err_id) )
  332. (void) std::forward<F>(f)(unx->put(err_id, E{}));
  333. else
  334. (void) std::forward<F>(f)(sl->put(err_id).put(err_id, E{}));
  335. }
  336. }
  337. template <bool OnError, class E>
  338. inline void load_unexpected( int err_id, E && e ) noexcept(OnError)
  339. {
  340. load_unexpected_count<E>(err_id);
  341. if( OnError )
  342. {
  343. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  344. try
  345. {
  346. #endif
  347. load_unexpected_info(err_id, std::forward<E>(e));
  348. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  349. }
  350. catch(...)
  351. {
  352. }
  353. #endif
  354. }
  355. else
  356. load_unexpected_info(err_id, std::forward<E>(e));
  357. }
  358. template <bool OnError, class E, class F>
  359. inline void accumulate_unexpected( int err_id, F && f ) noexcept(OnError)
  360. {
  361. load_unexpected_count<E>(err_id);
  362. if( OnError )
  363. {
  364. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  365. try
  366. {
  367. #endif
  368. accumulate_unexpected_info<E>(err_id, std::forward<F>(f));
  369. #ifndef BOOST_LEAF_NO_EXCEPTIONS
  370. }
  371. catch(...)
  372. {
  373. }
  374. #endif
  375. }
  376. else
  377. accumulate_unexpected_info<E>(err_id, std::forward<F>(f));
  378. }
  379. #endif
  380. template <class E>
  381. inline void slot<E>::propagate( int err_id ) noexcept(!BOOST_LEAF_CFG_DIAGNOSTICS)
  382. {
  383. if( this->key()!=err_id && err_id!=0 )
  384. return;
  385. if( impl * p = tls::read_ptr<slot<E>>() )
  386. *p = std::move(*this);
  387. #if BOOST_LEAF_CFG_DIAGNOSTICS
  388. else
  389. {
  390. int c = int(tls::read_uint<tls_tag_unexpected_enabled_counter>());
  391. BOOST_LEAF_ASSERT(c>=0);
  392. if( c )
  393. load_unexpected<false>(err_id, std::move(*this).value(err_id));
  394. }
  395. #endif
  396. }
  397. template <bool OnError, class E>
  398. BOOST_LEAF_CONSTEXPR inline int load_slot( int err_id, E && e ) noexcept(OnError)
  399. {
  400. static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
  401. static_assert(!std::is_same<typename std::decay<E>::type, error_id>::value, "Error objects of type error_id are not allowed");
  402. using T = typename std::decay<E>::type;
  403. BOOST_LEAF_ASSERT((err_id&3)==1);
  404. if( slot<T> * p = tls::read_ptr<slot<T>>() )
  405. {
  406. if( !OnError || !p->has_value(err_id) )
  407. (void) p->put(err_id, std::forward<E>(e));
  408. }
  409. #if BOOST_LEAF_CFG_DIAGNOSTICS
  410. else
  411. {
  412. int c = int(tls::read_uint<tls_tag_unexpected_enabled_counter>());
  413. BOOST_LEAF_ASSERT(c>=0);
  414. if( c )
  415. load_unexpected<OnError>(err_id, std::forward<E>(e));
  416. }
  417. #endif
  418. return 0;
  419. }
  420. template <bool OnError, class F>
  421. BOOST_LEAF_CONSTEXPR inline int accumulate_slot( int err_id, F && f ) noexcept(OnError)
  422. {
  423. static_assert(function_traits<F>::arity==1, "Lambdas passed to accumulate must take a single e-type argument by reference");
  424. using E = typename std::decay<fn_arg_type<F,0>>::type;
  425. static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
  426. BOOST_LEAF_ASSERT((err_id&3)==1);
  427. if( auto sl = tls::read_ptr<slot<E>>() )
  428. {
  429. if( auto v = sl->has_value(err_id) )
  430. (void) std::forward<F>(f)(*v);
  431. else
  432. (void) std::forward<F>(f)(sl->put(err_id,E()));
  433. }
  434. #if BOOST_LEAF_CFG_DIAGNOSTICS
  435. else
  436. {
  437. int c = int(tls::read_uint<tls_tag_unexpected_enabled_counter>());
  438. BOOST_LEAF_ASSERT(c>=0);
  439. if( c )
  440. accumulate_unexpected<OnError, E>(err_id, std::forward<F>(f));
  441. }
  442. #endif
  443. return 0;
  444. }
  445. }
  446. ////////////////////////////////////////
  447. namespace leaf_detail
  448. {
  449. template <class=void>
  450. struct BOOST_LEAF_SYMBOL_VISIBLE id_factory
  451. {
  452. static atomic_unsigned_int counter;
  453. BOOST_LEAF_CONSTEXPR static unsigned generate_next_id() noexcept
  454. {
  455. auto id = (counter+=4);
  456. BOOST_LEAF_ASSERT((id&3)==1);
  457. return id;
  458. }
  459. };
  460. template <class T>
  461. atomic_unsigned_int id_factory<T>::counter(unsigned(-3));
  462. inline int current_id() noexcept
  463. {
  464. unsigned id = tls::read_uint<tls_tag_id_factory_current_id>();
  465. BOOST_LEAF_ASSERT(id==0 || (id&3)==1);
  466. return int(id);
  467. }
  468. inline int new_id() noexcept
  469. {
  470. unsigned id = id_factory<>::generate_next_id();
  471. tls::write_uint<tls_tag_id_factory_current_id>(id);
  472. return int(id);
  473. }
  474. }
  475. ////////////////////////////////////////
  476. namespace leaf_detail
  477. {
  478. template <class T, int Arity = function_traits<T>::arity>
  479. struct load_item
  480. {
  481. static_assert(Arity==0 || Arity==1, "If a functions is passed to new_error or load, it must take zero or one argument");
  482. };
  483. template <class E>
  484. struct load_item<E, -1>
  485. {
  486. BOOST_LEAF_CONSTEXPR static int load_( int err_id, E && e ) noexcept
  487. {
  488. return load_slot<false>(err_id, std::forward<E>(e));
  489. }
  490. };
  491. template <class F>
  492. struct load_item<F, 0>
  493. {
  494. BOOST_LEAF_CONSTEXPR static int load_( int err_id, F && f ) noexcept
  495. {
  496. return load_slot<false>(err_id, std::forward<F>(f)());
  497. }
  498. };
  499. template <class F>
  500. struct load_item<F, 1>
  501. {
  502. BOOST_LEAF_CONSTEXPR static int load_( int err_id, F && f ) noexcept
  503. {
  504. return accumulate_slot<false>(err_id, std::forward<F>(f));
  505. }
  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. std::error_code to_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. protected:
  675. polymorphic_context() noexcept = default;
  676. ~polymorphic_context() noexcept = default;
  677. public:
  678. virtual error_id propagate_captured_errors() noexcept = 0;
  679. virtual void activate() noexcept = 0;
  680. virtual void deactivate() noexcept = 0;
  681. virtual void propagate( error_id ) noexcept = 0;
  682. virtual bool is_active() const noexcept = 0;
  683. virtual void print( std::ostream & ) const { };
  684. error_id captured_id_;
  685. template <class CharT, class Traits>
  686. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, polymorphic_context const & ctx )
  687. {
  688. ctx.print(os);
  689. return os;
  690. }
  691. };
  692. #if BOOST_LEAF_CFG_CAPTURE
  693. using context_ptr = std::shared_ptr<polymorphic_context>;
  694. #endif
  695. ////////////////////////////////////////////
  696. template <class Ctx>
  697. class context_activator
  698. {
  699. context_activator( context_activator const & ) = delete;
  700. context_activator & operator=( context_activator const & ) = delete;
  701. #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
  702. int const uncaught_exceptions_;
  703. #endif
  704. Ctx * ctx_;
  705. public:
  706. explicit BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator(Ctx & ctx) noexcept:
  707. #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
  708. uncaught_exceptions_(std::uncaught_exceptions()),
  709. #endif
  710. ctx_(ctx.is_active() ? nullptr : &ctx)
  711. {
  712. if( ctx_ )
  713. ctx_->activate();
  714. }
  715. BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator( context_activator && x ) noexcept:
  716. #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
  717. uncaught_exceptions_(x.uncaught_exceptions_),
  718. #endif
  719. ctx_(x.ctx_)
  720. {
  721. x.ctx_ = nullptr;
  722. }
  723. BOOST_LEAF_ALWAYS_INLINE ~context_activator() noexcept
  724. {
  725. if( ctx_ && ctx_->is_active() )
  726. ctx_->deactivate();
  727. }
  728. };
  729. template <class Ctx>
  730. BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator<Ctx> activate_context(Ctx & ctx) noexcept
  731. {
  732. return context_activator<Ctx>(ctx);
  733. }
  734. ////////////////////////////////////////////
  735. template <class R>
  736. struct is_result_type: std::false_type
  737. {
  738. };
  739. template <class R>
  740. struct is_result_type<R const>: is_result_type<R>
  741. {
  742. };
  743. } }
  744. #endif