From a5d68e52e6d65b4942a6cdfefeac61517f4fc67b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 3 Feb 2022 18:35:42 +0200 Subject: [PATCH] Add throw_exception_from_error overload for std::error_code --- include/boost/system/result.hpp | 6 ++++++ test/result_value_access.cpp | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/include/boost/system/result.hpp b/include/boost/system/result.hpp index 4e809b1..4872c7e 100644 --- a/include/boost/system/result.hpp +++ b/include/boost/system/result.hpp @@ -16,6 +16,7 @@ #include #include #include +#include // @@ -31,6 +32,11 @@ BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( error_code boost::throw_exception( system_error( e ) ); } +BOOST_NORETURN BOOST_NOINLINE inline void throw_exception_from_error( std::error_code const & e ) +{ + boost::throw_exception( std::system_error( e ) ); +} + // in_place_* using in_place_value_t = variant2::in_place_index_t<0>; diff --git a/test/result_value_access.cpp b/test/result_value_access.cpp index fdd92ac..02eccb7 100644 --- a/test/result_value_access.cpp +++ b/test/result_value_access.cpp @@ -1,10 +1,11 @@ -// Copyright 2017, 2021 Peter Dimov. +// Copyright 2017, 2021, 2022 Peter Dimov. // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #include #include #include +#include using namespace boost::system; @@ -165,6 +166,22 @@ int main() BOOST_TEST_EQ( result( ec ).operator->(), static_cast(0) ); } + { + auto ec = make_error_code( std::errc::invalid_argument ); + + result const r( ec ); + + BOOST_TEST( !r.has_value() ); + BOOST_TEST( r.has_error() ); + + BOOST_TEST_NOT( r ); + BOOST_TEST( !r ); + + BOOST_TEST_THROWS( r.value(), std::system_error ); + + BOOST_TEST_EQ( r.operator->(), static_cast(0) ); + } + { result r( 1 ); @@ -341,5 +358,21 @@ int main() BOOST_TEST_EQ( result( ec ).operator->(), static_cast(0) ); } + { + auto ec = make_error_code( std::errc::invalid_argument ); + + result const r( ec ); + + BOOST_TEST( !r.has_value() ); + BOOST_TEST( r.has_error() ); + + BOOST_TEST_NOT( r ); + BOOST_TEST( !r ); + + BOOST_TEST_THROWS( r.value(), std::system_error ); + + BOOST_TEST_EQ( r.operator->(), static_cast(0) ); + } + return boost::report_errors(); }