exception.hpp 12 KB

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