exception.hpp 13 KB

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