throw_exception.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef BOOST_THROW_EXCEPTION_HPP_INCLUDED
  2. #define BOOST_THROW_EXCEPTION_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // boost/throw_exception.hpp
  8. //
  9. // Copyright (c) 2002, 2018-2022 Peter Dimov
  10. // Copyright (c) 2008-2009 Emil Dotchevski and Reverge Studios, Inc.
  11. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. // http://www.boost.org/libs/throw_exception
  17. #include <boost/exception/exception.hpp>
  18. #include <boost/assert/source_location.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/config/workaround.hpp>
  21. #include <exception>
  22. #include <utility>
  23. #include <cstddef>
  24. #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
  25. #include <type_traits>
  26. #endif
  27. #if !defined( BOOST_EXCEPTION_DISABLE ) && defined( BOOST_BORLANDC ) && BOOST_WORKAROUND( BOOST_BORLANDC, BOOST_TESTED_AT(0x593) )
  28. # define BOOST_EXCEPTION_DISABLE
  29. #endif
  30. namespace boost
  31. {
  32. #if defined( BOOST_NO_EXCEPTIONS )
  33. BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
  34. BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined
  35. #endif
  36. // boost::wrapexcept<E>
  37. namespace detail
  38. {
  39. typedef char (&wrapexcept_s1)[ 1 ];
  40. typedef char (&wrapexcept_s2)[ 2 ];
  41. template<class T> wrapexcept_s1 wrapexcept_is_convertible( T* );
  42. template<class T> wrapexcept_s2 wrapexcept_is_convertible( void* );
  43. template<class E, class B, std::size_t I = sizeof( wrapexcept_is_convertible<B>( static_cast< E* >( BOOST_NULLPTR ) ) ) > struct wrapexcept_add_base;
  44. template<class E, class B> struct wrapexcept_add_base<E, B, 1>
  45. {
  46. struct type {};
  47. };
  48. template<class E, class B> struct wrapexcept_add_base<E, B, 2>
  49. {
  50. typedef B type;
  51. };
  52. } // namespace detail
  53. template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept:
  54. public detail::wrapexcept_add_base<E, boost::exception_detail::clone_base>::type,
  55. public E,
  56. public detail::wrapexcept_add_base<E, boost::exception>::type
  57. {
  58. private:
  59. struct deleter
  60. {
  61. wrapexcept * p_;
  62. ~deleter() { delete p_; }
  63. };
  64. private:
  65. void copy_from( void const* )
  66. {
  67. }
  68. void copy_from( boost::exception const* p )
  69. {
  70. static_cast<boost::exception&>( *this ) = *p;
  71. }
  72. public:
  73. explicit wrapexcept( E const & e ): E( e )
  74. {
  75. copy_from( &e );
  76. }
  77. explicit wrapexcept( E const & e, boost::source_location const & loc ): E( e )
  78. {
  79. copy_from( &e );
  80. set_info( *this, throw_file( loc.file_name() ) );
  81. set_info( *this, throw_line( static_cast<int>( loc.line() ) ) );
  82. set_info( *this, throw_function( loc.function_name() ) );
  83. set_info( *this, throw_column( static_cast<int>( loc.column() ) ) );
  84. }
  85. virtual boost::exception_detail::clone_base const * clone() const BOOST_OVERRIDE
  86. {
  87. wrapexcept * p = new wrapexcept( *this );
  88. deleter del = { p };
  89. boost::exception_detail::copy_boost_exception( p, this );
  90. del.p_ = BOOST_NULLPTR;
  91. return p;
  92. }
  93. virtual void rethrow() const BOOST_OVERRIDE
  94. {
  95. #if defined( BOOST_NO_EXCEPTIONS )
  96. boost::throw_exception( *this );
  97. #else
  98. throw *this;
  99. #endif
  100. }
  101. };
  102. // All boost exceptions are required to derive from std::exception,
  103. // to ensure compatibility with BOOST_NO_EXCEPTIONS.
  104. inline void throw_exception_assert_compatibility( std::exception const & ) {}
  105. // boost::throw_exception
  106. #if !defined( BOOST_NO_EXCEPTIONS )
  107. #if defined( BOOST_EXCEPTION_DISABLE )
  108. template<class E> BOOST_NORETURN void throw_exception( E const & e )
  109. {
  110. throw_exception_assert_compatibility( e );
  111. throw e;
  112. }
  113. template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & )
  114. {
  115. throw_exception_assert_compatibility( e );
  116. throw e;
  117. }
  118. #else // defined( BOOST_EXCEPTION_DISABLE )
  119. template<class E> BOOST_NORETURN void throw_exception( E const & e )
  120. {
  121. throw_exception_assert_compatibility( e );
  122. throw wrapexcept<E>( e );
  123. }
  124. template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc )
  125. {
  126. throw_exception_assert_compatibility( e );
  127. throw wrapexcept<E>( e, loc );
  128. }
  129. #endif // defined( BOOST_EXCEPTION_DISABLE )
  130. #endif // !defined( BOOST_NO_EXCEPTIONS )
  131. } // namespace boost
  132. // BOOST_THROW_EXCEPTION
  133. #define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x, BOOST_CURRENT_LOCATION)
  134. namespace boost
  135. {
  136. // throw_with_location
  137. namespace detail
  138. {
  139. struct BOOST_SYMBOL_VISIBLE throw_location
  140. {
  141. boost::source_location location_;
  142. explicit throw_location( boost::source_location const & loc ): location_( loc )
  143. {
  144. }
  145. };
  146. template<class E> class BOOST_SYMBOL_VISIBLE with_throw_location: public E, public throw_location
  147. {
  148. public:
  149. with_throw_location( E const & e, boost::source_location const & loc ): E( e ), throw_location( loc )
  150. {
  151. }
  152. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  153. with_throw_location( E && e, boost::source_location const & loc ): E( std::move( e ) ), throw_location( loc )
  154. {
  155. }
  156. #endif
  157. };
  158. } // namespace detail
  159. #if !defined(BOOST_NO_EXCEPTIONS)
  160. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
  161. template<class E> BOOST_NORETURN void throw_with_location( E && e, boost::source_location const & loc = BOOST_CURRENT_LOCATION )
  162. {
  163. throw_exception_assert_compatibility( e );
  164. throw detail::with_throw_location<typename std::decay<E>::type>( std::forward<E>( e ), loc );
  165. }
  166. #else
  167. template<class E> BOOST_NORETURN void throw_with_location( E const & e, boost::source_location const & loc = BOOST_CURRENT_LOCATION )
  168. {
  169. throw_exception_assert_compatibility( e );
  170. throw detail::with_throw_location<E>( e, loc );
  171. }
  172. #endif
  173. #else
  174. template<class E> BOOST_NORETURN void throw_with_location( E const & e, boost::source_location const & loc = BOOST_CURRENT_LOCATION )
  175. {
  176. boost::throw_exception( e, loc );
  177. }
  178. #endif
  179. // get_throw_location
  180. template<class E> boost::source_location get_throw_location( E const & e )
  181. {
  182. #if defined(BOOST_NO_RTTI)
  183. (void)e;
  184. return boost::source_location();
  185. #else
  186. if( detail::throw_location const* pl = dynamic_cast< detail::throw_location const* >( &e ) )
  187. {
  188. return pl->location_;
  189. }
  190. else if( boost::exception const* px = dynamic_cast< boost::exception const* >( &e ) )
  191. {
  192. return exception_detail::get_exception_throw_location( *px );
  193. }
  194. else
  195. {
  196. return boost::source_location();
  197. }
  198. #endif
  199. }
  200. } // namespace boost
  201. #endif // #ifndef BOOST_THROW_EXCEPTION_HPP_INCLUDED