throw_exception.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //
  8. // boost/throw_exception.hpp
  9. //
  10. // Copyright (c) 2002, 2018, 2019 Peter Dimov
  11. // Copyright (c) 2008-2009 Emil Dotchevski and Reverge Studios, Inc.
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. // http://www.boost.org/libs/throw_exception
  18. //
  19. #include <boost/assert/source_location.hpp>
  20. #include <boost/config.hpp>
  21. #include <boost/config/workaround.hpp>
  22. #include <exception>
  23. #if !defined( BOOST_EXCEPTION_DISABLE ) && defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x593) )
  24. # define BOOST_EXCEPTION_DISABLE
  25. #endif
  26. namespace boost
  27. {
  28. // All boost exceptions are required to derive from std::exception,
  29. // to ensure compatibility with BOOST_NO_EXCEPTIONS.
  30. inline void throw_exception_assert_compatibility( std::exception const & ) {}
  31. } // namespace boost
  32. #if defined( BOOST_NO_EXCEPTIONS )
  33. namespace boost
  34. {
  35. BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
  36. BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined
  37. } // namespace boost
  38. #elif defined( BOOST_EXCEPTION_DISABLE )
  39. namespace boost
  40. {
  41. template<class E> BOOST_NORETURN void throw_exception( E const & e )
  42. {
  43. throw_exception_assert_compatibility( e );
  44. throw e;
  45. }
  46. template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & )
  47. {
  48. throw_exception_assert_compatibility( e );
  49. throw e;
  50. }
  51. } // namespace boost
  52. #else // !defined( BOOST_NO_EXCEPTIONS ) && !defined( BOOST_EXCEPTION_DISABLE )
  53. #include <boost/exception/exception.hpp>
  54. namespace boost
  55. {
  56. // boost::wrapexcept<E>
  57. namespace detail
  58. {
  59. typedef char (&wrapexcept_s1)[ 1 ];
  60. typedef char (&wrapexcept_s2)[ 2 ];
  61. template<class T> wrapexcept_s1 wrapexcept_is_convertible( T* );
  62. template<class T> wrapexcept_s2 wrapexcept_is_convertible( void* );
  63. template<class E, class B, int I = sizeof( wrapexcept_is_convertible<B>( static_cast< E* >( 0 ) ) ) > struct wrapexcept_add_base;
  64. template<class E, class B> struct wrapexcept_add_base<E, B, 1>
  65. {
  66. struct type {};
  67. };
  68. template<class E, class B> struct wrapexcept_add_base<E, B, 2>
  69. {
  70. typedef B type;
  71. };
  72. } // namespace detail
  73. template<class E> struct BOOST_SYMBOL_VISIBLE wrapexcept:
  74. public detail::wrapexcept_add_base<E, boost::exception_detail::clone_base>::type,
  75. public E,
  76. public detail::wrapexcept_add_base<E, boost::exception>::type
  77. {
  78. private:
  79. struct deleter
  80. {
  81. wrapexcept * p_;
  82. ~deleter() { delete p_; }
  83. };
  84. private:
  85. void copy_from( void const* )
  86. {
  87. }
  88. void copy_from( boost::exception const* p )
  89. {
  90. static_cast<boost::exception&>( *this ) = *p;
  91. }
  92. public:
  93. explicit wrapexcept( E const & e ): E( e )
  94. {
  95. copy_from( &e );
  96. }
  97. explicit wrapexcept( E const & e, boost::source_location const & loc ): E( e )
  98. {
  99. copy_from( &e );
  100. set_info( *this, throw_file( loc.file_name() ) );
  101. set_info( *this, throw_line( loc.line() ) );
  102. set_info( *this, throw_function( loc.function_name() ) );
  103. }
  104. virtual boost::exception_detail::clone_base const * clone() const
  105. {
  106. wrapexcept * p = new wrapexcept( *this );
  107. deleter del = { p };
  108. boost::exception_detail::copy_boost_exception( p, this );
  109. del.p_ = 0;
  110. return p;
  111. }
  112. virtual void rethrow() const
  113. {
  114. throw *this;
  115. }
  116. };
  117. // boost::throw_exception
  118. template<class E> BOOST_NORETURN void throw_exception( E const & e )
  119. {
  120. throw_exception_assert_compatibility( e );
  121. throw wrapexcept<E>( e );
  122. }
  123. template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc )
  124. {
  125. throw_exception_assert_compatibility( e );
  126. throw wrapexcept<E>( e, loc );
  127. }
  128. } // namespace boost
  129. #endif
  130. // BOOST_THROW_EXCEPTION
  131. #define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x, BOOST_CURRENT_LOCATION)
  132. #endif // #ifndef BOOST_THROW_EXCEPTION_HPP_INCLUDED