exception_ptr.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Copyright (c) 2019 Dario Menendez, Banco Santander
  3. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_EXCEPTION_618474C2DE1511DEB74A388C56D89593
  6. #define BOOST_EXCEPTION_618474C2DE1511DEB74A388C56D89593
  7. #include <boost/exception/detail/requires_cxx11.hpp>
  8. #include <boost/exception/exception.hpp>
  9. #include <boost/exception/info.hpp>
  10. #include <boost/exception/diagnostic_information.hpp>
  11. #ifndef BOOST_NO_EXCEPTIONS
  12. # include <boost/exception/detail/clone_current_exception.hpp>
  13. #endif
  14. #include <boost/exception/detail/type_info.hpp>
  15. #ifndef BOOST_NO_RTTI
  16. #include <boost/core/demangle.hpp>
  17. #endif
  18. #include <boost/shared_ptr.hpp>
  19. #include <boost/make_shared.hpp>
  20. #include <stdexcept>
  21. #include <new>
  22. #include <ios>
  23. #include <stdlib.h>
  24. #ifndef BOOST_EXCEPTION_ENABLE_WARNINGS
  25. #if defined(__GNUC__) && __GNUC__*100+__GNUC_MINOR__>301
  26. #pragma GCC system_header
  27. #endif
  28. #ifdef __clang__
  29. #pragma clang system_header
  30. #endif
  31. #ifdef _MSC_VER
  32. #pragma warning(push,1)
  33. #endif
  34. #endif
  35. namespace
  36. boost
  37. {
  38. namespace
  39. exception_detail
  40. {
  41. #ifndef BOOST_NO_CXX11_HDR_EXCEPTION
  42. struct
  43. std_exception_ptr_wrapper:
  44. std::exception
  45. {
  46. std::exception_ptr p;
  47. explicit std_exception_ptr_wrapper( std::exception_ptr const & ptr ) BOOST_NOEXCEPT:
  48. p(ptr)
  49. {
  50. }
  51. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  52. explicit std_exception_ptr_wrapper( std::exception_ptr && ptr ) BOOST_NOEXCEPT:
  53. p(static_cast<std::exception_ptr &&>(ptr))
  54. {
  55. }
  56. #endif
  57. };
  58. shared_ptr<exception_detail::clone_base const>
  59. inline
  60. wrap_exception_ptr( std::exception_ptr const & e )
  61. {
  62. exception_detail::clone_base const & base = boost::enable_current_exception(std_exception_ptr_wrapper(e));
  63. return shared_ptr<exception_detail::clone_base const>(base.clone());
  64. }
  65. #endif
  66. }
  67. class exception_ptr;
  68. namespace exception_detail { void rethrow_exception_( exception_ptr const & ); }
  69. class
  70. exception_ptr
  71. {
  72. typedef boost::shared_ptr<exception_detail::clone_base const> impl;
  73. impl ptr_;
  74. friend void exception_detail::rethrow_exception_( exception_ptr const & );
  75. typedef exception_detail::clone_base const * (impl::*unspecified_bool_type)() const;
  76. public:
  77. exception_ptr()
  78. {
  79. }
  80. #ifndef BOOST_NO_CXX11_HDR_EXCEPTION
  81. exception_ptr( std::exception_ptr const & e ):
  82. ptr_(exception_detail::wrap_exception_ptr(e))
  83. {
  84. }
  85. #endif
  86. explicit
  87. exception_ptr( impl const & ptr ):
  88. ptr_(ptr)
  89. {
  90. }
  91. bool
  92. operator==( exception_ptr const & other ) const
  93. {
  94. return ptr_==other.ptr_;
  95. }
  96. bool
  97. operator!=( exception_ptr const & other ) const
  98. {
  99. return ptr_!=other.ptr_;
  100. }
  101. operator unspecified_bool_type() const
  102. {
  103. return ptr_?&impl::get:0;
  104. }
  105. };
  106. namespace
  107. exception_detail
  108. {
  109. template <class E>
  110. inline
  111. exception_ptr
  112. copy_exception_impl( E const & e )
  113. {
  114. return exception_ptr(boost::make_shared<E>(e));
  115. }
  116. }
  117. template <class E>
  118. inline
  119. exception_ptr
  120. copy_exception( E const & e )
  121. {
  122. return exception_detail::copy_exception_impl(boost::enable_current_exception(e));
  123. }
  124. template <class T>
  125. inline
  126. exception_ptr
  127. make_exception_ptr( T const & e )
  128. {
  129. return boost::copy_exception(e);
  130. }
  131. #ifndef BOOST_NO_RTTI
  132. typedef error_info<struct tag_original_exception_type,std::type_info const *> original_exception_type;
  133. inline
  134. std::string
  135. to_string( original_exception_type const & x )
  136. {
  137. return core::demangle(x.value()->name());
  138. }
  139. #endif
  140. #ifndef BOOST_NO_EXCEPTIONS
  141. namespace
  142. exception_detail
  143. {
  144. struct
  145. bad_alloc_:
  146. boost::exception,
  147. std::bad_alloc
  148. {
  149. ~bad_alloc_() BOOST_NOEXCEPT_OR_NOTHROW { }
  150. };
  151. struct
  152. bad_exception_:
  153. boost::exception,
  154. std::bad_exception
  155. {
  156. ~bad_exception_() BOOST_NOEXCEPT_OR_NOTHROW { }
  157. };
  158. template <class Exception>
  159. exception_ptr
  160. get_static_exception_object()
  161. {
  162. Exception ba;
  163. exception_detail::clone_impl<Exception> c(ba);
  164. #ifndef BOOST_EXCEPTION_DISABLE
  165. c <<
  166. throw_function(BOOST_CURRENT_FUNCTION) <<
  167. throw_file(__FILE__) <<
  168. throw_line(__LINE__);
  169. #endif
  170. static exception_ptr ep(shared_ptr<exception_detail::clone_base const>(new exception_detail::clone_impl<Exception>(c)));
  171. return ep;
  172. }
  173. template <class Exception>
  174. struct
  175. exception_ptr_static_exception_object
  176. {
  177. static exception_ptr const e;
  178. };
  179. template <class Exception>
  180. exception_ptr const
  181. exception_ptr_static_exception_object<Exception>::
  182. e = get_static_exception_object<Exception>();
  183. }
  184. #if defined(__GNUC__)
  185. # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  186. # pragma GCC visibility push (default)
  187. # endif
  188. #endif
  189. class
  190. unknown_exception:
  191. public boost::exception,
  192. public std::exception
  193. {
  194. public:
  195. unknown_exception()
  196. {
  197. }
  198. explicit
  199. unknown_exception( std::exception const & e )
  200. {
  201. add_original_type(e);
  202. }
  203. explicit
  204. unknown_exception( boost::exception const & e ):
  205. boost::exception(e)
  206. {
  207. add_original_type(e);
  208. }
  209. ~unknown_exception() BOOST_NOEXCEPT_OR_NOTHROW
  210. {
  211. }
  212. private:
  213. template <class E>
  214. void
  215. add_original_type( E const & e )
  216. {
  217. #ifndef BOOST_NO_RTTI
  218. (*this) << original_exception_type(&typeid(e));
  219. #endif
  220. }
  221. };
  222. #if defined(__GNUC__)
  223. # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  224. # pragma GCC visibility pop
  225. # endif
  226. #endif
  227. namespace
  228. exception_detail
  229. {
  230. template <class T>
  231. class
  232. current_exception_std_exception_wrapper:
  233. public T,
  234. public boost::exception
  235. {
  236. public:
  237. explicit
  238. current_exception_std_exception_wrapper( T const & e1 ):
  239. T(e1)
  240. {
  241. add_original_type(e1);
  242. }
  243. current_exception_std_exception_wrapper( T const & e1, boost::exception const & e2 ):
  244. T(e1),
  245. boost::exception(e2)
  246. {
  247. add_original_type(e1);
  248. }
  249. ~current_exception_std_exception_wrapper() BOOST_NOEXCEPT_OR_NOTHROW
  250. {
  251. }
  252. private:
  253. template <class E>
  254. void
  255. add_original_type( E const & e )
  256. {
  257. #ifndef BOOST_NO_RTTI
  258. (*this) << original_exception_type(&typeid(e));
  259. #endif
  260. }
  261. };
  262. #ifdef BOOST_NO_RTTI
  263. template <class T>
  264. boost::exception const *
  265. get_boost_exception( T const * )
  266. {
  267. try
  268. {
  269. throw;
  270. }
  271. catch(
  272. boost::exception & x )
  273. {
  274. return &x;
  275. }
  276. catch(...)
  277. {
  278. return 0;
  279. }
  280. }
  281. #else
  282. template <class T>
  283. boost::exception const *
  284. get_boost_exception( T const * x )
  285. {
  286. return dynamic_cast<boost::exception const *>(x);
  287. }
  288. #endif
  289. template <class T>
  290. inline
  291. exception_ptr
  292. current_exception_std_exception( T const & e1 )
  293. {
  294. if( boost::exception const * e2 = get_boost_exception(&e1) )
  295. return boost::copy_exception(current_exception_std_exception_wrapper<T>(e1,*e2));
  296. else
  297. return boost::copy_exception(current_exception_std_exception_wrapper<T>(e1));
  298. }
  299. inline
  300. exception_ptr
  301. current_exception_unknown_exception()
  302. {
  303. return boost::copy_exception(unknown_exception());
  304. }
  305. inline
  306. exception_ptr
  307. current_exception_unknown_boost_exception( boost::exception const & e )
  308. {
  309. return boost::copy_exception(unknown_exception(e));
  310. }
  311. inline
  312. exception_ptr
  313. current_exception_unknown_std_exception( std::exception const & e )
  314. {
  315. if( boost::exception const * be = get_boost_exception(&e) )
  316. return current_exception_unknown_boost_exception(*be);
  317. else
  318. return boost::copy_exception(unknown_exception(e));
  319. }
  320. inline
  321. exception_ptr
  322. current_exception_impl()
  323. {
  324. exception_detail::clone_base const * e=0;
  325. switch(
  326. exception_detail::clone_current_exception(e) )
  327. {
  328. case exception_detail::clone_current_exception_result::
  329. success:
  330. {
  331. BOOST_ASSERT(e!=0);
  332. return exception_ptr(shared_ptr<exception_detail::clone_base const>(e));
  333. }
  334. case exception_detail::clone_current_exception_result::
  335. bad_alloc:
  336. {
  337. BOOST_ASSERT(!e);
  338. return exception_detail::exception_ptr_static_exception_object<bad_alloc_>::e;
  339. }
  340. case exception_detail::clone_current_exception_result::
  341. bad_exception:
  342. {
  343. BOOST_ASSERT(!e);
  344. return exception_detail::exception_ptr_static_exception_object<bad_exception_>::e;
  345. }
  346. default:
  347. BOOST_ASSERT(0);
  348. case exception_detail::clone_current_exception_result::
  349. not_supported:
  350. {
  351. BOOST_ASSERT(!e);
  352. try
  353. {
  354. throw;
  355. }
  356. catch(
  357. exception_detail::clone_base & e )
  358. {
  359. return exception_ptr(shared_ptr<exception_detail::clone_base const>(e.clone()));
  360. }
  361. #ifdef BOOST_NO_CXX11_HDR_EXCEPTION
  362. catch(
  363. std::domain_error & e )
  364. {
  365. return exception_detail::current_exception_std_exception(e);
  366. }
  367. catch(
  368. std::invalid_argument & e )
  369. {
  370. return exception_detail::current_exception_std_exception(e);
  371. }
  372. catch(
  373. std::length_error & e )
  374. {
  375. return exception_detail::current_exception_std_exception(e);
  376. }
  377. catch(
  378. std::out_of_range & e )
  379. {
  380. return exception_detail::current_exception_std_exception(e);
  381. }
  382. catch(
  383. std::logic_error & e )
  384. {
  385. return exception_detail::current_exception_std_exception(e);
  386. }
  387. catch(
  388. std::range_error & e )
  389. {
  390. return exception_detail::current_exception_std_exception(e);
  391. }
  392. catch(
  393. std::overflow_error & e )
  394. {
  395. return exception_detail::current_exception_std_exception(e);
  396. }
  397. catch(
  398. std::underflow_error & e )
  399. {
  400. return exception_detail::current_exception_std_exception(e);
  401. }
  402. catch(
  403. std::ios_base::failure & e )
  404. {
  405. return exception_detail::current_exception_std_exception(e);
  406. }
  407. catch(
  408. std::runtime_error & e )
  409. {
  410. return exception_detail::current_exception_std_exception(e);
  411. }
  412. catch(
  413. std::bad_alloc & e )
  414. {
  415. return exception_detail::current_exception_std_exception(e);
  416. }
  417. #ifndef BOOST_NO_TYPEID
  418. catch(
  419. std::bad_cast & e )
  420. {
  421. return exception_detail::current_exception_std_exception(e);
  422. }
  423. catch(
  424. std::bad_typeid & e )
  425. {
  426. return exception_detail::current_exception_std_exception(e);
  427. }
  428. #endif
  429. catch(
  430. std::bad_exception & e )
  431. {
  432. return exception_detail::current_exception_std_exception(e);
  433. }
  434. catch(
  435. std::exception & e )
  436. {
  437. return exception_detail::current_exception_unknown_std_exception(e);
  438. }
  439. catch(
  440. boost::exception & e )
  441. {
  442. return exception_detail::current_exception_unknown_boost_exception(e);
  443. }
  444. #endif // #ifdef BOOST_NO_CXX11_HDR_EXCEPTION
  445. catch(
  446. ... )
  447. {
  448. #ifndef BOOST_NO_CXX11_HDR_EXCEPTION
  449. try
  450. {
  451. return exception_ptr(std::current_exception());
  452. }
  453. catch(
  454. ...)
  455. {
  456. return exception_detail::current_exception_unknown_exception();
  457. }
  458. #else
  459. return exception_detail::current_exception_unknown_exception();
  460. #endif
  461. }
  462. }
  463. }
  464. }
  465. }
  466. inline
  467. exception_ptr
  468. current_exception()
  469. {
  470. exception_ptr ret;
  471. try
  472. {
  473. ret=exception_detail::current_exception_impl();
  474. }
  475. catch(
  476. std::bad_alloc & )
  477. {
  478. ret=exception_detail::exception_ptr_static_exception_object<exception_detail::bad_alloc_>::e;
  479. }
  480. catch(
  481. ... )
  482. {
  483. ret=exception_detail::exception_ptr_static_exception_object<exception_detail::bad_exception_>::e;
  484. }
  485. BOOST_ASSERT(ret);
  486. return ret;
  487. }
  488. #endif // ifndef BOOST_NO_EXCEPTIONS
  489. namespace
  490. exception_detail
  491. {
  492. inline
  493. void
  494. rethrow_exception_( exception_ptr const & p )
  495. {
  496. BOOST_ASSERT(p);
  497. #if defined( BOOST_NO_CXX11_HDR_EXCEPTION ) || defined( BOOST_NO_EXCEPTIONS )
  498. p.ptr_->rethrow();
  499. #else
  500. try
  501. {
  502. p.ptr_->rethrow();
  503. }
  504. catch(
  505. std_exception_ptr_wrapper const & wrp)
  506. {
  507. // if an std::exception_ptr was wrapped above then rethrow it
  508. std::rethrow_exception(wrp.p);
  509. }
  510. #endif
  511. }
  512. }
  513. BOOST_NORETURN
  514. inline
  515. void
  516. rethrow_exception( exception_ptr const & p )
  517. {
  518. exception_detail::rethrow_exception_(p);
  519. BOOST_ASSERT(0);
  520. #if defined(UNDER_CE)
  521. // some CE platforms don't define ::abort()
  522. exit(-1);
  523. #else
  524. abort();
  525. #endif
  526. }
  527. inline
  528. std::string
  529. diagnostic_information( exception_ptr const & p, bool verbose=true )
  530. {
  531. if( p )
  532. #ifdef BOOST_NO_EXCEPTIONS
  533. return "<unavailable> due to BOOST_NO_EXCEPTIONS";
  534. #else
  535. try
  536. {
  537. rethrow_exception(p);
  538. }
  539. catch(
  540. ... )
  541. {
  542. return current_exception_diagnostic_information(verbose);
  543. }
  544. #endif
  545. return "<empty>";
  546. }
  547. inline
  548. std::string
  549. to_string( exception_ptr const & p )
  550. {
  551. std::string s='\n'+diagnostic_information(p);
  552. std::string padding(" ");
  553. std::string r;
  554. bool f=false;
  555. for( std::string::const_iterator i=s.begin(),e=s.end(); i!=e; ++i )
  556. {
  557. if( f )
  558. r+=padding;
  559. char c=*i;
  560. r+=c;
  561. f=(c=='\n');
  562. }
  563. return r;
  564. }
  565. }
  566. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  567. #pragma warning(pop)
  568. #endif
  569. #endif