exception.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593
  5. #define BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593
  6. #include <boost/assert/source_location.hpp>
  7. #include <boost/config.hpp>
  8. #include <exception>
  9. #ifdef BOOST_EXCEPTION_MINI_BOOST
  10. #include <memory>
  11. namespace boost { namespace exception_detail { using std::shared_ptr; } }
  12. #else
  13. namespace boost { template <class T> class shared_ptr; }
  14. namespace boost { namespace exception_detail { using boost::shared_ptr; } }
  15. #endif
  16. #if !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  17. #if defined(__GNUC__) && __GNUC__*100+__GNUC_MINOR__>301
  18. #pragma GCC system_header
  19. #endif
  20. #ifdef __clang__
  21. #pragma clang system_header
  22. #endif
  23. #ifdef _MSC_VER
  24. #pragma warning(push,1)
  25. #pragma warning(disable: 4265)
  26. #endif
  27. #endif
  28. namespace
  29. boost
  30. {
  31. namespace
  32. exception_detail
  33. {
  34. template <class T>
  35. class
  36. refcount_ptr
  37. {
  38. public:
  39. refcount_ptr():
  40. px_(0)
  41. {
  42. }
  43. ~refcount_ptr()
  44. {
  45. release();
  46. }
  47. refcount_ptr( refcount_ptr const & x ):
  48. px_(x.px_)
  49. {
  50. add_ref();
  51. }
  52. refcount_ptr &
  53. operator=( refcount_ptr const & x )
  54. {
  55. adopt(x.px_);
  56. return *this;
  57. }
  58. void
  59. adopt( T * px )
  60. {
  61. release();
  62. px_=px;
  63. add_ref();
  64. }
  65. T *
  66. get() const
  67. {
  68. return px_;
  69. }
  70. private:
  71. T * px_;
  72. void
  73. add_ref()
  74. {
  75. if( px_ )
  76. px_->add_ref();
  77. }
  78. void
  79. release()
  80. {
  81. if( px_ && px_->release() )
  82. px_=0;
  83. }
  84. };
  85. }
  86. ////////////////////////////////////////////////////////////////////////
  87. template <class Tag,class T>
  88. class error_info;
  89. typedef error_info<struct throw_function_,char const *> throw_function;
  90. typedef error_info<struct throw_file_,char const *> throw_file;
  91. typedef error_info<struct throw_line_,int> throw_line;
  92. typedef error_info<struct throw_column_,int> throw_column;
  93. template <>
  94. class
  95. error_info<throw_function_,char const *>
  96. {
  97. public:
  98. typedef char const * value_type;
  99. value_type v_;
  100. explicit
  101. error_info( value_type v ):
  102. v_(v)
  103. {
  104. }
  105. };
  106. template <>
  107. class
  108. error_info<throw_file_,char const *>
  109. {
  110. public:
  111. typedef char const * value_type;
  112. value_type v_;
  113. explicit
  114. error_info( value_type v ):
  115. v_(v)
  116. {
  117. }
  118. };
  119. template <>
  120. class
  121. error_info<throw_line_,int>
  122. {
  123. public:
  124. typedef int value_type;
  125. value_type v_;
  126. explicit
  127. error_info( value_type v ):
  128. v_(v)
  129. {
  130. }
  131. };
  132. template <>
  133. class
  134. error_info<throw_column_,int>
  135. {
  136. public:
  137. typedef int value_type;
  138. value_type v_;
  139. explicit
  140. error_info( value_type v ):
  141. v_(v)
  142. {
  143. }
  144. };
  145. class
  146. BOOST_SYMBOL_VISIBLE
  147. exception;
  148. namespace
  149. exception_detail
  150. {
  151. class error_info_base;
  152. struct type_info_;
  153. struct
  154. error_info_container
  155. {
  156. virtual char const * diagnostic_information( char const * ) const = 0;
  157. virtual shared_ptr<error_info_base> get( type_info_ const & ) const = 0;
  158. virtual void set( shared_ptr<error_info_base> const &, type_info_ const & ) = 0;
  159. virtual void add_ref() const = 0;
  160. virtual bool release() const = 0;
  161. virtual refcount_ptr<exception_detail::error_info_container> clone() const = 0;
  162. protected:
  163. ~error_info_container() BOOST_NOEXCEPT_OR_NOTHROW
  164. {
  165. }
  166. };
  167. template <class>
  168. struct get_info;
  169. template <>
  170. struct get_info<throw_function>;
  171. template <>
  172. struct get_info<throw_file>;
  173. template <>
  174. struct get_info<throw_line>;
  175. template <>
  176. struct get_info<throw_column>;
  177. template <class>
  178. struct set_info_rv;
  179. template <>
  180. struct set_info_rv<throw_function>;
  181. template <>
  182. struct set_info_rv<throw_file>;
  183. template <>
  184. struct set_info_rv<throw_line>;
  185. template <>
  186. struct set_info_rv<throw_column>;
  187. char const * get_diagnostic_information( exception const &, char const * );
  188. void copy_boost_exception( exception *, exception const * );
  189. template <class E,class Tag,class T>
  190. E const & set_info( E const &, error_info<Tag,T> const & );
  191. template <class E>
  192. E const & set_info( E const &, throw_function const & );
  193. template <class E>
  194. E const & set_info( E const &, throw_file const & );
  195. template <class E>
  196. E const & set_info( E const &, throw_line const & );
  197. template <class E>
  198. E const & set_info( E const &, throw_column const & );
  199. boost::source_location get_exception_throw_location( exception const & );
  200. }
  201. class
  202. BOOST_SYMBOL_VISIBLE
  203. exception
  204. {
  205. //<N3757>
  206. public:
  207. template <class Tag> void set( typename Tag::type const & );
  208. template <class Tag> typename Tag::type const * get() const;
  209. //</N3757>
  210. protected:
  211. exception():
  212. throw_function_(0),
  213. throw_file_(0),
  214. throw_line_(-1),
  215. throw_column_(-1)
  216. {
  217. }
  218. #ifdef __HP_aCC
  219. //On HP aCC, this protected copy constructor prevents throwing boost::exception.
  220. //On all other platforms, the same effect is achieved by the pure virtual destructor.
  221. exception( exception const & x ) BOOST_NOEXCEPT_OR_NOTHROW:
  222. data_(x.data_),
  223. throw_function_(x.throw_function_),
  224. throw_file_(x.throw_file_),
  225. throw_line_(x.throw_line_),
  226. throw_column_(x.throw_column_)
  227. {
  228. }
  229. #endif
  230. virtual ~exception() BOOST_NOEXCEPT_OR_NOTHROW
  231. #ifndef __HP_aCC
  232. = 0 //Workaround for HP aCC, =0 incorrectly leads to link errors.
  233. #endif
  234. ;
  235. #if (defined(__MWERKS__) && __MWERKS__<=0x3207) || (defined(_MSC_VER) && _MSC_VER<=1310)
  236. public:
  237. #else
  238. private:
  239. template <class E>
  240. friend E const & exception_detail::set_info( E const &, throw_function const & );
  241. template <class E>
  242. friend E const & exception_detail::set_info( E const &, throw_file const & );
  243. template <class E>
  244. friend E const & exception_detail::set_info( E const &, throw_line const & );
  245. template <class E>
  246. friend E const & exception_detail::set_info( E const &, throw_column const & );
  247. template <class E,class Tag,class T>
  248. friend E const & exception_detail::set_info( E const &, error_info<Tag,T> const & );
  249. friend char const * exception_detail::get_diagnostic_information( exception const &, char const * );
  250. friend boost::source_location exception_detail::get_exception_throw_location( exception const & );
  251. template <class>
  252. friend struct exception_detail::get_info;
  253. friend struct exception_detail::get_info<throw_function>;
  254. friend struct exception_detail::get_info<throw_file>;
  255. friend struct exception_detail::get_info<throw_line>;
  256. friend struct exception_detail::get_info<throw_column>;
  257. template <class>
  258. friend struct exception_detail::set_info_rv;
  259. friend struct exception_detail::set_info_rv<throw_function>;
  260. friend struct exception_detail::set_info_rv<throw_file>;
  261. friend struct exception_detail::set_info_rv<throw_line>;
  262. friend struct exception_detail::set_info_rv<throw_column>;
  263. friend void exception_detail::copy_boost_exception( exception *, exception const * );
  264. #endif
  265. mutable exception_detail::refcount_ptr<exception_detail::error_info_container> data_;
  266. mutable char const * throw_function_;
  267. mutable char const * throw_file_;
  268. mutable int throw_line_;
  269. mutable int throw_column_;
  270. };
  271. inline
  272. exception::
  273. ~exception() BOOST_NOEXCEPT_OR_NOTHROW
  274. {
  275. }
  276. namespace
  277. exception_detail
  278. {
  279. template <class E>
  280. E const &
  281. set_info( E const & x, throw_function const & y )
  282. {
  283. x.throw_function_=y.v_;
  284. return x;
  285. }
  286. template <class E>
  287. E const &
  288. set_info( E const & x, throw_file const & y )
  289. {
  290. x.throw_file_=y.v_;
  291. return x;
  292. }
  293. template <class E>
  294. E const &
  295. set_info( E const & x, throw_line const & y )
  296. {
  297. x.throw_line_=y.v_;
  298. return x;
  299. }
  300. template <class E>
  301. E const &
  302. set_info( E const & x, throw_column const & y )
  303. {
  304. x.throw_column_=y.v_;
  305. return x;
  306. }
  307. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  308. template <>
  309. struct
  310. set_info_rv<throw_column>
  311. {
  312. template <class E>
  313. static
  314. E const &
  315. set( E const & x, throw_column && y )
  316. {
  317. x.throw_column_=y.v_;
  318. return x;
  319. }
  320. };
  321. #endif
  322. inline boost::source_location get_exception_throw_location( exception const & x )
  323. {
  324. return boost::source_location(
  325. x.throw_file_? x.throw_file_: "",
  326. x.throw_line_ >= 0? x.throw_line_: 0,
  327. x.throw_function_? x.throw_function_: "",
  328. x.throw_column_ >= 0? x.throw_column_: 0
  329. );
  330. }
  331. }
  332. ////////////////////////////////////////////////////////////////////////
  333. namespace
  334. exception_detail
  335. {
  336. template <class T>
  337. struct
  338. BOOST_SYMBOL_VISIBLE
  339. error_info_injector:
  340. public T,
  341. public exception
  342. {
  343. explicit
  344. error_info_injector( T const & x ):
  345. T(x)
  346. {
  347. }
  348. ~error_info_injector() BOOST_NOEXCEPT_OR_NOTHROW
  349. {
  350. }
  351. };
  352. struct large_size { char c[256]; };
  353. large_size dispatch_boost_exception( exception const * );
  354. struct small_size { };
  355. small_size dispatch_boost_exception( void const * );
  356. template <class,int>
  357. struct enable_error_info_helper;
  358. template <class T>
  359. struct
  360. enable_error_info_helper<T,sizeof(large_size)>
  361. {
  362. typedef T type;
  363. };
  364. template <class T>
  365. struct
  366. enable_error_info_helper<T,sizeof(small_size)>
  367. {
  368. typedef error_info_injector<T> type;
  369. };
  370. template <class T>
  371. struct
  372. enable_error_info_return_type
  373. {
  374. typedef typename enable_error_info_helper<T,sizeof(exception_detail::dispatch_boost_exception(static_cast<T *>(0)))>::type type;
  375. };
  376. }
  377. template <class T>
  378. inline
  379. typename
  380. exception_detail::enable_error_info_return_type<T>::type
  381. enable_error_info( T const & x )
  382. {
  383. typedef typename exception_detail::enable_error_info_return_type<T>::type rt;
  384. return rt(x);
  385. }
  386. ////////////////////////////////////////////////////////////////////////
  387. #if defined(BOOST_NO_EXCEPTIONS)
  388. BOOST_NORETURN void throw_exception(std::exception const & e); // user defined
  389. #endif
  390. namespace
  391. exception_detail
  392. {
  393. class
  394. BOOST_SYMBOL_VISIBLE
  395. clone_base
  396. {
  397. public:
  398. virtual clone_base const * clone() const = 0;
  399. virtual void rethrow() const = 0;
  400. virtual
  401. ~clone_base() BOOST_NOEXCEPT_OR_NOTHROW
  402. {
  403. }
  404. };
  405. inline
  406. void
  407. copy_boost_exception( exception * a, exception const * b )
  408. {
  409. refcount_ptr<error_info_container> data;
  410. if( error_info_container * d=b->data_.get() )
  411. data = d->clone();
  412. a->throw_file_ = b->throw_file_;
  413. a->throw_line_ = b->throw_line_;
  414. a->throw_function_ = b->throw_function_;
  415. a->throw_column_ = b->throw_column_;
  416. a->data_ = data;
  417. }
  418. inline
  419. void
  420. copy_boost_exception( void *, void const * )
  421. {
  422. }
  423. template <class T>
  424. class
  425. BOOST_SYMBOL_VISIBLE
  426. clone_impl:
  427. public T,
  428. public virtual clone_base
  429. {
  430. struct clone_tag { };
  431. clone_impl( clone_impl const & x, clone_tag ):
  432. T(x)
  433. {
  434. copy_boost_exception(this,&x);
  435. }
  436. public:
  437. explicit
  438. clone_impl( T const & x ):
  439. T(x)
  440. {
  441. copy_boost_exception(this,&x);
  442. }
  443. ~clone_impl() BOOST_NOEXCEPT_OR_NOTHROW
  444. {
  445. }
  446. private:
  447. clone_base const *
  448. clone() const
  449. {
  450. return new clone_impl(*this,clone_tag());
  451. }
  452. void
  453. rethrow() const
  454. {
  455. #if defined(BOOST_NO_EXCEPTIONS)
  456. boost::throw_exception(*this);
  457. #else
  458. throw*this;
  459. #endif
  460. }
  461. };
  462. }
  463. template <class T>
  464. inline
  465. exception_detail::clone_impl<T>
  466. enable_current_exception( T const & x )
  467. {
  468. return exception_detail::clone_impl<T>(x);
  469. }
  470. }
  471. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  472. #pragma warning(pop)
  473. #endif
  474. #endif // #ifndef BOOST_EXCEPTION_274DA366004E11DCB1DDFE2E56D89593