exception.hpp 13 KB

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