From 6d7a57a970363841a3a7d6a631fc3fb0166758bf Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 18 Feb 2022 02:55:34 +0200 Subject: [PATCH] Add throw_exception_from_error overload for std::exception_ptr --- include/boost/system/result.hpp | 13 +++++++ test/result_value_access.cpp | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/include/boost/system/result.hpp b/include/boost/system/result.hpp index 6b0edbb..26baa6d 100644 --- a/include/boost/system/result.hpp +++ b/include/boost/system/result.hpp @@ -18,6 +18,7 @@ #include #include #include +#include // @@ -53,6 +54,18 @@ BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( std::errc boost::throw_with_location( std::system_error( make_error_code( e ) ), loc ); } +BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( std::exception_ptr const & p, boost::source_location const& loc ) +{ + if( p ) + { + std::rethrow_exception( p ); + } + else + { + boost::throw_with_location( std::bad_exception(), loc ); + } +} + #if defined(__GNUC__) && __GNUC__ >= 7 && __GNUC__ <= 8 # pragma GCC diagnostic pop #endif diff --git a/test/result_value_access.cpp b/test/result_value_access.cpp index 1f045c4..6093942 100644 --- a/test/result_value_access.cpp +++ b/test/result_value_access.cpp @@ -32,6 +32,10 @@ BOOST_NORETURN void throw_exception_from_error( Y const &, boost::source_locatio throw E(); } +struct E2 +{ +}; + int main() { { @@ -210,6 +214,34 @@ int main() BOOST_TEST_EQ( r.operator->(), static_cast(0) ); } + { + result const r( std::make_exception_ptr( E2() ) ); + + BOOST_TEST( !r.has_value() ); + BOOST_TEST( r.has_error() ); + + BOOST_TEST_NOT( r ); + BOOST_TEST( !r ); + + BOOST_TEST_THROWS( r.value(), E2 ); + + BOOST_TEST_EQ( r.operator->(), static_cast(0) ); + } + + { + result const r( in_place_error ); + + BOOST_TEST( !r.has_value() ); + BOOST_TEST( r.has_error() ); + + BOOST_TEST_NOT( r ); + BOOST_TEST( !r ); + + BOOST_TEST_THROWS( r.value(), std::bad_exception ); + + BOOST_TEST_EQ( r.operator->(), static_cast(0) ); + } + { result r( 1 ); @@ -430,5 +462,33 @@ int main() BOOST_TEST_EQ( r.operator->(), static_cast(0) ); } + { + result const r( std::make_exception_ptr( E2() ) ); + + BOOST_TEST( !r.has_value() ); + BOOST_TEST( r.has_error() ); + + BOOST_TEST_NOT( r ); + BOOST_TEST( !r ); + + BOOST_TEST_THROWS( r.value(), E2 ); + + BOOST_TEST_EQ( r.operator->(), static_cast(0) ); + } + + { + result const r( in_place_error ); + + BOOST_TEST( !r.has_value() ); + BOOST_TEST( r.has_error() ); + + BOOST_TEST_NOT( r ); + BOOST_TEST( !r ); + + BOOST_TEST_THROWS( r.value(), std::bad_exception ); + + BOOST_TEST_EQ( r.operator->(), static_cast(0) ); + } + return boost::report_errors(); }