exception.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 defined(__GNUC__) && !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. class exception;
  116. template <class T>
  117. class shared_ptr;
  118. namespace
  119. exception_detail
  120. {
  121. class error_info_base;
  122. struct type_info_;
  123. struct
  124. error_info_container
  125. {
  126. virtual char const * diagnostic_information( char const * ) const = 0;
  127. virtual shared_ptr<error_info_base> get( type_info_ const & ) const = 0;
  128. virtual void set( shared_ptr<error_info_base> const &, type_info_ const & ) = 0;
  129. virtual void add_ref() const = 0;
  130. virtual bool release() const = 0;
  131. virtual refcount_ptr<exception_detail::error_info_container> clone() const = 0;
  132. protected:
  133. ~error_info_container() throw()
  134. {
  135. }
  136. };
  137. template <class>
  138. struct get_info;
  139. template <>
  140. struct get_info<throw_function>;
  141. template <>
  142. struct get_info<throw_file>;
  143. template <>
  144. struct get_info<throw_line>;
  145. char const * get_diagnostic_information( exception const &, char const * );
  146. void copy_boost_exception( exception *, exception const * );
  147. template <class E,class Tag,class T>
  148. E const & set_info( E const &, error_info<Tag,T> const & );
  149. template <class E>
  150. E const & set_info( E const &, throw_function const & );
  151. template <class E>
  152. E const & set_info( E const &, throw_file const & );
  153. template <class E>
  154. E const & set_info( E const &, throw_line const & );
  155. }
  156. class
  157. exception
  158. {
  159. protected:
  160. exception():
  161. throw_function_(0),
  162. throw_file_(0),
  163. throw_line_(-1)
  164. {
  165. }
  166. #ifdef __HP_aCC
  167. //On HP aCC, this protected copy constructor prevents throwing boost::exception.
  168. //On all other platforms, the same effect is achieved by the pure virtual destructor.
  169. exception( exception const & x ) throw():
  170. data_(x.data_),
  171. throw_function_(x.throw_function_),
  172. throw_file_(x.throw_file_),
  173. throw_line_(x.throw_line_)
  174. {
  175. }
  176. #endif
  177. virtual ~exception() throw()
  178. #ifndef __HP_aCC
  179. = 0 //Workaround for HP aCC, =0 incorrectly leads to link errors.
  180. #endif
  181. ;
  182. #if (defined(__MWERKS__) && __MWERKS__<=0x3207) || (defined(_MSC_VER) && _MSC_VER<=1310)
  183. public:
  184. #else
  185. private:
  186. template <class E>
  187. friend E const & exception_detail::set_info( E const &, throw_function const & );
  188. template <class E>
  189. friend E const & exception_detail::set_info( E const &, throw_file const & );
  190. template <class E>
  191. friend E const & exception_detail::set_info( E const &, throw_line const & );
  192. template <class E,class Tag,class T>
  193. friend E const & exception_detail::set_info( E const &, error_info<Tag,T> const & );
  194. friend char const * exception_detail::get_diagnostic_information( exception const &, char const * );
  195. template <class>
  196. friend struct exception_detail::get_info;
  197. friend struct exception_detail::get_info<throw_function>;
  198. friend struct exception_detail::get_info<throw_file>;
  199. friend struct exception_detail::get_info<throw_line>;
  200. friend void exception_detail::copy_boost_exception( exception *, exception const * );
  201. #endif
  202. mutable exception_detail::refcount_ptr<exception_detail::error_info_container> data_;
  203. mutable char const * throw_function_;
  204. mutable char const * throw_file_;
  205. mutable int throw_line_;
  206. };
  207. inline
  208. exception::
  209. ~exception() throw()
  210. {
  211. }
  212. namespace
  213. exception_detail
  214. {
  215. template <class E>
  216. E const &
  217. set_info( E const & x, throw_function const & y )
  218. {
  219. x.throw_function_=y.v_;
  220. return x;
  221. }
  222. template <class E>
  223. E const &
  224. set_info( E const & x, throw_file const & y )
  225. {
  226. x.throw_file_=y.v_;
  227. return x;
  228. }
  229. template <class E>
  230. E const &
  231. set_info( E const & x, throw_line const & y )
  232. {
  233. x.throw_line_=y.v_;
  234. return x;
  235. }
  236. }
  237. ////////////////////////////////////////////////////////////////////////
  238. namespace
  239. exception_detail
  240. {
  241. template <class T>
  242. struct
  243. error_info_injector:
  244. public T,
  245. public exception
  246. {
  247. explicit
  248. error_info_injector( T const & x ):
  249. T(x)
  250. {
  251. }
  252. ~error_info_injector() throw()
  253. {
  254. }
  255. };
  256. struct large_size { char c[256]; };
  257. large_size dispatch_boost_exception( exception const * );
  258. struct small_size { };
  259. small_size dispatch_boost_exception( void const * );
  260. template <class,int>
  261. struct enable_error_info_helper;
  262. template <class T>
  263. struct
  264. enable_error_info_helper<T,sizeof(large_size)>
  265. {
  266. typedef T type;
  267. };
  268. template <class T>
  269. struct
  270. enable_error_info_helper<T,sizeof(small_size)>
  271. {
  272. typedef error_info_injector<T> type;
  273. };
  274. template <class T>
  275. struct
  276. enable_error_info_return_type
  277. {
  278. typedef typename enable_error_info_helper<T,sizeof(exception_detail::dispatch_boost_exception((T*)0))>::type type;
  279. };
  280. }
  281. template <class T>
  282. inline
  283. typename
  284. exception_detail::enable_error_info_return_type<T>::type
  285. enable_error_info( T const & x )
  286. {
  287. typedef typename exception_detail::enable_error_info_return_type<T>::type rt;
  288. return rt(x);
  289. }
  290. ////////////////////////////////////////////////////////////////////////
  291. namespace
  292. exception_detail
  293. {
  294. class
  295. clone_base
  296. {
  297. public:
  298. virtual clone_base const * clone() const = 0;
  299. virtual void rethrow() const = 0;
  300. virtual
  301. ~clone_base() throw()
  302. {
  303. }
  304. };
  305. inline
  306. void
  307. copy_boost_exception( exception * a, exception const * b )
  308. {
  309. refcount_ptr<error_info_container> data;
  310. if( error_info_container * d=b->data_.get() )
  311. data = d->clone();
  312. a->throw_file_ = b->throw_file_;
  313. a->throw_line_ = b->throw_line_;
  314. a->throw_function_ = b->throw_function_;
  315. a->data_ = data;
  316. }
  317. inline
  318. void
  319. copy_boost_exception( void *, void const * )
  320. {
  321. }
  322. template <class T>
  323. class
  324. clone_impl:
  325. public T,
  326. public clone_base
  327. {
  328. public:
  329. explicit
  330. clone_impl( T const & x ):
  331. T(x)
  332. {
  333. copy_boost_exception(this,&x);
  334. }
  335. ~clone_impl() throw()
  336. {
  337. }
  338. private:
  339. clone_base const *
  340. clone() const
  341. {
  342. return new clone_impl(*this);
  343. }
  344. void
  345. rethrow() const
  346. {
  347. throw*this;
  348. }
  349. };
  350. }
  351. template <class T>
  352. inline
  353. exception_detail::clone_impl<T>
  354. enable_current_exception( T const & x )
  355. {
  356. return exception_detail::clone_impl<T>(x);
  357. }
  358. }
  359. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  360. #pragma warning(pop)
  361. #endif
  362. #endif