exception.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. namespace
  7. boost
  8. {
  9. namespace
  10. exception_detail
  11. {
  12. template <class T>
  13. class
  14. refcount_ptr
  15. {
  16. public:
  17. refcount_ptr():
  18. px_(0)
  19. {
  20. }
  21. ~refcount_ptr()
  22. {
  23. release();
  24. }
  25. refcount_ptr( refcount_ptr const & x ):
  26. px_(x.px_)
  27. {
  28. add_ref();
  29. }
  30. refcount_ptr &
  31. operator=( refcount_ptr const & x )
  32. {
  33. adopt(x.px_);
  34. return *this;
  35. }
  36. void
  37. adopt( T * px )
  38. {
  39. release();
  40. px_=px;
  41. add_ref();
  42. }
  43. T *
  44. get() const
  45. {
  46. return px_;
  47. }
  48. private:
  49. T * px_;
  50. void
  51. add_ref()
  52. {
  53. if( px_ )
  54. px_->add_ref();
  55. }
  56. void
  57. release()
  58. {
  59. if( px_ )
  60. px_->release();
  61. }
  62. };
  63. }
  64. ////////////////////////////////////////////////////////////////////////
  65. template <class Tag,class T>
  66. class error_info;
  67. typedef error_info<struct throw_function_,char const *> throw_function;
  68. typedef error_info<struct throw_file_,char const *> throw_file;
  69. typedef error_info<struct throw_line_,int> throw_line;
  70. template <>
  71. class
  72. error_info<throw_function_,char const *>
  73. {
  74. public:
  75. typedef char const * value_type;
  76. value_type v_;
  77. explicit
  78. error_info( value_type v ):
  79. v_(v)
  80. {
  81. }
  82. };
  83. template <>
  84. class
  85. error_info<throw_file_,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_line_,int>
  99. {
  100. public:
  101. typedef int value_type;
  102. value_type v_;
  103. explicit
  104. error_info( value_type v ):
  105. v_(v)
  106. {
  107. }
  108. };
  109. template <class E,class Tag,class T>
  110. E const & operator<<( E const &, error_info<Tag,T> const & );
  111. class exception;
  112. template <class>
  113. class shared_ptr;
  114. namespace
  115. exception_detail
  116. {
  117. class error_info_base;
  118. struct type_info_;
  119. struct
  120. error_info_container
  121. {
  122. virtual char const * diagnostic_information() const = 0;
  123. virtual shared_ptr<error_info_base const> get( type_info_ const & ) const = 0;
  124. virtual void set( shared_ptr<error_info_base const> const &, type_info_ const & ) = 0;
  125. virtual void add_ref() const = 0;
  126. virtual void release() const = 0;
  127. protected:
  128. virtual
  129. ~error_info_container() throw()
  130. {
  131. }
  132. };
  133. template <class>
  134. struct get_info;
  135. template <>
  136. struct get_info<throw_function>;
  137. template <>
  138. struct get_info<throw_file>;
  139. template <>
  140. struct get_info<throw_line>;
  141. char const * get_diagnostic_information( exception const & );
  142. }
  143. class
  144. exception
  145. {
  146. protected:
  147. exception():
  148. throw_function_(0),
  149. throw_file_(0),
  150. throw_line_(-1)
  151. {
  152. }
  153. #ifdef __HP_aCC
  154. //On HP aCC, this protected copy constructor prevents throwing boost::exception.
  155. //On all other platforms, the same effect is achieved by the pure virtual destructor.
  156. exception( exception const & x ) throw():
  157. data_(x.data_),
  158. throw_function_(x.throw_function_),
  159. throw_file_(x.throw_file_),
  160. throw_line_(x.throw_line_)
  161. {
  162. }
  163. #endif
  164. virtual ~exception() throw()
  165. #ifndef __HP_aCC
  166. = 0 //Workaround for HP aCC, =0 incorrectly leads to link errors.
  167. #endif
  168. ;
  169. private:
  170. template <class E>
  171. friend
  172. E const &
  173. operator<<( E const & x, throw_function const & y )
  174. {
  175. x.throw_function_=y.v_;
  176. return x;
  177. }
  178. template <class E>
  179. friend
  180. E const &
  181. operator<<( E const & x, throw_file const & y )
  182. {
  183. x.throw_file_=y.v_;
  184. return x;
  185. }
  186. template <class E>
  187. friend
  188. E const &
  189. operator<<( E const & x, throw_line const & y )
  190. {
  191. x.throw_line_=y.v_;
  192. return x;
  193. }
  194. friend char const * exception_detail::get_diagnostic_information( exception const & );
  195. template <class E,class Tag,class T>
  196. friend E const & operator<<( E const &, error_info<Tag,T> const & );
  197. template <class>
  198. friend struct exception_detail::get_info;
  199. friend struct exception_detail::get_info<throw_function>;
  200. friend struct exception_detail::get_info<throw_file>;
  201. friend struct exception_detail::get_info<throw_line>;
  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. ////////////////////////////////////////////////////////////////////////
  213. namespace
  214. exception_detail
  215. {
  216. template <class T>
  217. struct
  218. error_info_injector:
  219. public T,
  220. public exception
  221. {
  222. explicit
  223. error_info_injector( T const & x ):
  224. T(x)
  225. {
  226. }
  227. ~error_info_injector() throw()
  228. {
  229. }
  230. };
  231. struct large_size { char c[256]; };
  232. large_size dispatch( exception * );
  233. struct small_size { };
  234. small_size dispatch( void * );
  235. template <class,int>
  236. struct enable_error_info_helper;
  237. template <class T>
  238. struct
  239. enable_error_info_helper<T,sizeof(large_size)>
  240. {
  241. typedef T type;
  242. };
  243. template <class T>
  244. struct
  245. enable_error_info_helper<T,sizeof(small_size)>
  246. {
  247. typedef error_info_injector<T> type;
  248. };
  249. template <class T>
  250. struct
  251. enable_error_info_return_type
  252. {
  253. typedef typename enable_error_info_helper<T,sizeof(dispatch((T*)0))>::type type;
  254. };
  255. }
  256. template <class T>
  257. inline
  258. typename
  259. exception_detail::enable_error_info_return_type<T>::type
  260. enable_error_info( T const & x )
  261. {
  262. typedef typename exception_detail::enable_error_info_return_type<T>::type rt;
  263. return rt(x);
  264. }
  265. ////////////////////////////////////////////////////////////////////////
  266. namespace
  267. exception_detail
  268. {
  269. class
  270. clone_base
  271. {
  272. public:
  273. virtual clone_base const * clone() const = 0;
  274. virtual void rethrow() const = 0;
  275. virtual
  276. ~clone_base() throw()
  277. {
  278. }
  279. };
  280. inline
  281. void
  282. copy_boost_exception( exception * a, exception const * b )
  283. {
  284. *a = *b;
  285. }
  286. inline
  287. void
  288. copy_boost_exception( void *, void const * )
  289. {
  290. }
  291. template <class T>
  292. class
  293. clone_impl:
  294. public T,
  295. public clone_base
  296. {
  297. public:
  298. explicit
  299. clone_impl( T const & x ):
  300. T(x)
  301. {
  302. copy_boost_exception(this,&x);
  303. }
  304. ~clone_impl() throw()
  305. {
  306. }
  307. private:
  308. clone_base const *
  309. clone() const
  310. {
  311. return new clone_impl(*this);
  312. }
  313. void
  314. rethrow() const
  315. {
  316. throw*this;
  317. }
  318. };
  319. }
  320. template <class T>
  321. inline
  322. exception_detail::clone_impl<T>
  323. enable_current_exception( T const & x )
  324. {
  325. return exception_detail::clone_impl<T>(x);
  326. }
  327. }
  328. #endif