exception.hpp 12 KB

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