Add error_code(error_code const& ec, source_location const* loc) and a corresponding assign

This commit is contained in:
Peter Dimov
2022-05-05 05:02:37 +03:00
parent 5debb8a041
commit 256fe92dbb
5 changed files with 205 additions and 0 deletions

View File

@ -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<boost::uintptr_t>( loc ): 2 ) | ( ec.lc_flags_ & 1 );
}
}
template<class ErrorCodeEnum> error_code( ErrorCodeEnum e, source_location const * loc,
typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::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<typename ErrorCodeEnum>
BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value, error_code>::type &
operator=( ErrorCodeEnum val ) BOOST_NOEXCEPT

View File

@ -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)

View File

@ -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 ;

View File

@ -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 <boost/system.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cerrno>
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();
}

View File

@ -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 <boost/system.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cerrno>
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();
}