From 256fe92dbba4cbcf9bf15b773ca369dd412e78eb Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 5 May 2022 05:02:37 +0300 Subject: [PATCH] Add error_code(error_code const& ec, source_location const* loc) and a corresponding assign --- include/boost/system/detail/error_code.hpp | 16 ++++ test/CMakeLists.txt | 3 + test/Jamfile.v2 | 3 + test/ec_location_test3.cpp | 90 +++++++++++++++++++++ test/ec_location_test4.cpp | 93 ++++++++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 test/ec_location_test3.cpp create mode 100644 test/ec_location_test4.cpp diff --git a/include/boost/system/detail/error_code.hpp b/include/boost/system/detail/error_code.hpp index 1a643fc..5840d0f 100644 --- a/include/boost/system/detail/error_code.hpp +++ b/include/boost/system/detail/error_code.hpp @@ -144,6 +144,17 @@ public: *this = make_error_code( e ); } + error_code( error_code const& ec, source_location const * loc ) BOOST_NOEXCEPT: + d1_(), lc_flags_( 0 ) + { + *this = ec; + + if( ec.lc_flags_ != 0 && ec.lc_flags_ != 1 ) + { + lc_flags_ = ( loc? reinterpret_cast( loc ): 2 ) | ( ec.lc_flags_ & 1 ); + } + } + template error_code( ErrorCodeEnum e, source_location const * loc, typename detail::enable_if::value>::type* = 0 ) BOOST_NOEXCEPT: d1_(), lc_flags_( 0 ) @@ -194,6 +205,11 @@ public: *this = error_code( val, cat, loc ); } + void assign( error_code const& ec, source_location const * loc ) BOOST_NOEXCEPT + { + *this = error_code( ec, loc ); + } + template BOOST_SYSTEM_CONSTEXPR typename detail::enable_if::value, error_code>::type & operator=( ErrorCodeEnum val ) BOOST_NOEXCEPT diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7215e86..9c09d7d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -121,6 +121,9 @@ boost_test(TYPE run SOURCES errc_test4.cpp) boost_test(TYPE run SOURCES std_interop_test13.cpp) boost_test(TYPE run SOURCES std_interop_test14.cpp) +boost_test(TYPE run SOURCES ec_location_test3.cpp) +boost_test(TYPE run SOURCES ec_location_test4.cpp) + # result set(BOOST_TEST_COMPILE_FEATURES cxx_std_11) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f2e77dc..684992d 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -145,6 +145,9 @@ run errc_test4.cpp ; run std_interop_test13.cpp ; run std_interop_test14.cpp ; +run ec_location_test3.cpp ; +run ec_location_test4.cpp ; + # result import ../../config/checks/config : requires ; diff --git a/test/ec_location_test3.cpp b/test/ec_location_test3.cpp new file mode 100644 index 0000000..4dcffb7 --- /dev/null +++ b/test/ec_location_test3.cpp @@ -0,0 +1,90 @@ +// Copyright 2021, 2022 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +int main() +{ + int const val = ENOENT; + boost::system::error_category const & cat = boost::system::generic_category(); + + { + boost::system::error_code ec; + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + boost::system::error_code ec2( ec, &loc ); + + BOOST_TEST_EQ( ec2, ec ); + + BOOST_TEST( !ec2.has_location() ); + BOOST_TEST_EQ( ec2.location(), boost::source_location() ); + + boost::system::error_code ec3( ec2, 0 ); + + BOOST_TEST_EQ( ec3, ec2 ); + + BOOST_TEST( !ec3.has_location() ); + BOOST_TEST_EQ( ec3.location(), boost::source_location() ); + } + + { + boost::system::error_code ec( val, cat ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + boost::system::error_code ec2( ec, &loc ); + + BOOST_TEST_EQ( ec2, ec ); + + BOOST_TEST( ec2.has_location() ); + BOOST_TEST_EQ( ec2.location(), loc ); + + boost::system::error_code ec3( ec2, 0 ); + + BOOST_TEST_EQ( ec3, ec2 ); + + BOOST_TEST( !ec3.has_location() ); + BOOST_TEST_EQ( ec3.location(), boost::source_location() ); + } + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) + + { + std::error_code e2( val, std::generic_category() ); + + boost::system::error_code ec( e2 ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + boost::system::error_code ec2( ec, &loc ); + + BOOST_TEST_EQ( ec2, ec ); + + BOOST_TEST( !ec2.has_location() ); + BOOST_TEST_EQ( ec2.location(), boost::source_location() ); + + boost::system::error_code ec3( ec2, 0 ); + + BOOST_TEST_EQ( ec3, ec2 ); + + BOOST_TEST( !ec3.has_location() ); + BOOST_TEST_EQ( ec3.location(), boost::source_location() ); + } + +#endif + + return boost::report_errors(); +} diff --git a/test/ec_location_test4.cpp b/test/ec_location_test4.cpp new file mode 100644 index 0000000..4c60113 --- /dev/null +++ b/test/ec_location_test4.cpp @@ -0,0 +1,93 @@ +// Copyright 2021, 2022 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +int main() +{ + int const val = ENOENT; + boost::system::error_category const & cat = boost::system::generic_category(); + + { + boost::system::error_code ec; + boost::system::error_code ec2( ec ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec.assign( ec, &loc ); + + BOOST_TEST_EQ( ec, ec2 ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + ec.assign( ec, 0 ); + + BOOST_TEST_EQ( ec, ec2 ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + } + + { + boost::system::error_code ec( val, cat ); + boost::system::error_code ec2( ec ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec.assign( ec, &loc ); + + BOOST_TEST_EQ( ec, ec2 ); + + BOOST_TEST( ec.has_location() ); + BOOST_TEST_EQ( ec.location(), loc ); + + ec.assign( ec, 0 ); + + BOOST_TEST_EQ( ec, ec2 ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + } + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) + + { + std::error_code e2( val, std::generic_category() ); + + boost::system::error_code ec( e2 ); + boost::system::error_code ec2( ec ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec.assign( ec, &loc ); + + BOOST_TEST_EQ( ec, ec2 ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + + ec.assign( ec, 0 ); + + BOOST_TEST_EQ( ec, ec2 ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location(), boost::source_location() ); + } + +#endif + + return boost::report_errors(); +}