From e1fd6bb1f81c405dddf30cbb8c7b500e1b0c3c6c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 25 Jan 2022 22:04:40 +0200 Subject: [PATCH] Update wrapexcept to not include boost::exception or clone_base under C++11 --- include/boost/throw_exception.hpp | 95 +++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/include/boost/throw_exception.hpp b/include/boost/throw_exception.hpp index b8a2e49..bb0e2f7 100644 --- a/include/boost/throw_exception.hpp +++ b/include/boost/throw_exception.hpp @@ -41,7 +41,14 @@ BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_loc #endif -// boost::wrapexcept +// has_source_location + +struct has_source_location +{ + virtual boost::source_location location() const BOOST_NOEXCEPT = 0; +}; + +// wrapexcept namespace detail { @@ -66,7 +73,19 @@ template struct wrapexcept_add_base } // namespace detail -template struct BOOST_SYMBOL_VISIBLE wrapexcept: +#if !defined(BOOST_NO_CXX11_HDR_EXCEPTION) + +template struct BOOST_SYMBOL_VISIBLE wrapexcept; + +#else + +template struct BOOST_SYMBOL_VISIBLE wrapexcept; + +#endif + +// C++98 version for backward compatibility + +template struct BOOST_SYMBOL_VISIBLE wrapexcept: public detail::wrapexcept_add_base::type, public E, public detail::wrapexcept_add_base::type @@ -131,6 +150,58 @@ public: } }; +// C++11 version + +template struct BOOST_SYMBOL_VISIBLE wrapexcept: + public E, + public has_source_location +{ +private: + + boost::source_location loc_; + +private: + + void copy_from( void const* ) + { + } + + void copy_from( boost::exception const* p ) + { + static_cast( *this ) = *p; + } + + template void set_info_impl( void*, T const& ) + { + } + + template void set_info_impl( boost::exception * p, T const& t ) + { + exception_detail::set_info( *p, t ); + } + +public: + + explicit wrapexcept( E const & e ): E( e ) + { + copy_from( &e ); + } + + explicit wrapexcept( E const & e, boost::source_location const & loc ): E( e ), loc_( loc ) + { + copy_from( &e ); + + set_info_impl( this, throw_file( loc.file_name() ) ); + set_info_impl( this, throw_line( loc.line() ) ); + set_info_impl( this, throw_function( loc.function_name() ) ); + } + + virtual boost::source_location location() const BOOST_NOEXCEPT + { + return loc_; + } +}; + // All boost exceptions are required to derive from std::exception, // to ensure compatibility with BOOST_NO_EXCEPTIONS. @@ -154,18 +225,32 @@ template BOOST_NORETURN void throw_exception( E const & e, boost::sourc throw e; } -#else // defined( BOOST_EXCEPTION_DISABLE ) +#elif !defined(BOOST_NO_CXX11_HDR_EXCEPTION) template BOOST_NORETURN void throw_exception( E const & e ) { throw_exception_assert_compatibility( e ); - throw wrapexcept( e ); + throw e; } template BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc ) { throw_exception_assert_compatibility( e ); - throw wrapexcept( e, loc ); + throw wrapexcept( e, loc ); +} + +#else // C++98 + +template BOOST_NORETURN void throw_exception( E const & e ) +{ + throw_exception_assert_compatibility( e ); + throw wrapexcept( e ); +} + +template BOOST_NORETURN void throw_exception( E const & e, boost::source_location const & loc ) +{ + throw_exception_assert_compatibility( e ); + throw wrapexcept( e, loc ); } #endif // defined( BOOST_EXCEPTION_DISABLE )