/Src/Dependencies/Boost/boost/exception/exception.hpp

http://hadesmem.googlecode.com/ · C++ Header · 456 lines · 374 code · 74 blank · 8 comment · 26 complexity · f0899e9a11a4713badcc81449e1bb4f0 MD5 · raw file

  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. #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. template <class T>
  262. struct
  263. error_info_injector:
  264. public T,
  265. public exception
  266. {
  267. explicit
  268. error_info_injector( T const & x ):
  269. T(x)
  270. {
  271. }
  272. ~error_info_injector() throw()
  273. {
  274. }
  275. };
  276. struct large_size { char c[256]; };
  277. large_size dispatch_boost_exception( exception const * );
  278. struct small_size { };
  279. small_size dispatch_boost_exception( void const * );
  280. template <class,int>
  281. struct enable_error_info_helper;
  282. template <class T>
  283. struct
  284. enable_error_info_helper<T,sizeof(large_size)>
  285. {
  286. typedef T type;
  287. };
  288. template <class T>
  289. struct
  290. enable_error_info_helper<T,sizeof(small_size)>
  291. {
  292. typedef error_info_injector<T> type;
  293. };
  294. template <class T>
  295. struct
  296. enable_error_info_return_type
  297. {
  298. typedef typename enable_error_info_helper<T,sizeof(exception_detail::dispatch_boost_exception(static_cast<T *>(0)))>::type type;
  299. };
  300. }
  301. template <class T>
  302. inline
  303. typename
  304. exception_detail::enable_error_info_return_type<T>::type
  305. enable_error_info( T const & x )
  306. {
  307. typedef typename exception_detail::enable_error_info_return_type<T>::type rt;
  308. return rt(x);
  309. }
  310. ////////////////////////////////////////////////////////////////////////
  311. namespace
  312. exception_detail
  313. {
  314. class
  315. clone_base
  316. {
  317. public:
  318. virtual clone_base const * clone() const = 0;
  319. virtual void rethrow() const = 0;
  320. virtual
  321. ~clone_base() throw()
  322. {
  323. }
  324. };
  325. inline
  326. void
  327. copy_boost_exception( exception * a, exception const * b )
  328. {
  329. refcount_ptr<error_info_container> data;
  330. if( error_info_container * d=b->data_.get() )
  331. data = d->clone();
  332. a->throw_file_ = b->throw_file_;
  333. a->throw_line_ = b->throw_line_;
  334. a->throw_function_ = b->throw_function_;
  335. a->data_ = data;
  336. }
  337. inline
  338. void
  339. copy_boost_exception( void *, void const * )
  340. {
  341. }
  342. template <class T>
  343. class
  344. clone_impl:
  345. public T,
  346. public clone_base
  347. {
  348. public:
  349. explicit
  350. clone_impl( T const & x ):
  351. T(x)
  352. {
  353. copy_boost_exception(this,&x);
  354. }
  355. ~clone_impl() throw()
  356. {
  357. }
  358. private:
  359. clone_base const *
  360. clone() const
  361. {
  362. return new clone_impl(*this);
  363. }
  364. void
  365. rethrow() const
  366. {
  367. throw*this;
  368. }
  369. };
  370. }
  371. template <class T>
  372. inline
  373. exception_detail::clone_impl<T>
  374. enable_current_exception( T const & x )
  375. {
  376. return exception_detail::clone_impl<T>(x);
  377. }
  378. }
  379. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  380. #pragma warning(pop)
  381. #endif
  382. #endif