From 0ccf08509b6bbb415ecff9a266d34d77b71cd253 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 1 Oct 2021 23:04:04 +0300 Subject: [PATCH] Add a constructor taking ErrorCodeEnum and a source location --- include/boost/system/detail/error_code.hpp | 23 ++ test/CMakeLists.txt | 2 + test/Jamfile.v2 | 2 + test/ec_location_test2.cpp | 287 +++++++++++++++++++++ 4 files changed, 314 insertions(+) create mode 100644 test/ec_location_test2.cpp diff --git a/include/boost/system/detail/error_code.hpp b/include/boost/system/detail/error_code.hpp index 847c895..fb79150 100644 --- a/include/boost/system/detail/error_code.hpp +++ b/include/boost/system/detail/error_code.hpp @@ -113,6 +113,22 @@ public: *this = make_error_code( e ); } + template error_code( ErrorCodeEnum e, source_location const * loc, + typename detail::enable_if::value>::type* = 0 ) BOOST_NOEXCEPT: + d1_(), lc_flags_( 0 ) + { + error_code e2 = make_error_code( e ); + + if( e2.lc_flags_ == 0 || e2.lc_flags_ == 1 ) + { + *this = e2; + } + else + { + *this = error_code( e2.d1_.val_, *e2.d1_.cat_, loc ); + } + } + #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) error_code( std::error_code const& ec ) BOOST_NOEXCEPT: @@ -143,6 +159,13 @@ public: return *this; } + template + typename detail::enable_if::value, void>::type + assign( ErrorCodeEnum val, source_location const * loc ) BOOST_NOEXCEPT + { + *this = error_code( val, loc ); + } + BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT { *this = error_code(); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3b38593..6f31850 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -106,6 +106,8 @@ boost_test(TYPE run SOURCES error_code_test2.cpp) boost_test(TYPE run SOURCES system_error_test2.cpp) boost_test(TYPE run SOURCES std_interop_test10.cpp) +boost_test(TYPE run SOURCES ec_location_test2.cpp) + # result set(BOOST_TEST_COMPILE_FEATURES cxx_std_11) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f491bca..33dec94 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -128,6 +128,8 @@ run error_code_test2.cpp ; run system_error_test2.cpp ; run std_interop_test10.cpp ; +run ec_location_test2.cpp ; + # result import ../../config/checks/config : requires ; diff --git a/test/ec_location_test2.cpp b/test/ec_location_test2.cpp new file mode 100644 index 0000000..e60bb7a --- /dev/null +++ b/test/ec_location_test2.cpp @@ -0,0 +1,287 @@ +// Copyright 2021 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +namespace sys = boost::system; + +enum E +{ + none = 0, + einval = EINVAL +}; + +namespace boost +{ +namespace system +{ + +template<> struct is_error_code_enum< ::E > +{ + static const bool value = true; +}; + +} // namespace system +} // namespace boost + +sys::error_code make_error_code( E e ) +{ + return e == 0? sys::error_code(): sys::error_code( e, sys::generic_category() ); +} + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) + +enum E2 +{ + e2inval = EINVAL +}; + +namespace boost +{ +namespace system +{ + +template<> struct is_error_code_enum< ::E2 > +{ + static const bool value = true; +}; + +} // namespace system +} // namespace boost + +std::error_code make_error_code( E2 e ) +{ + return std::error_code( e, std::generic_category() ); +} + +#endif + +int main() +{ + { + sys::error_code ec( einval ); + + BOOST_TEST_EQ( ec.value(), EINVAL ); + BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + sys::error_code ec( einval, &loc ); + + BOOST_TEST_EQ( ec.value(), EINVAL ); + BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 77 ); + } + + { + sys::error_code ec( none ); + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + sys::error_code ec( none, &loc ); + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec = sys::error_code( einval, &loc ); + + BOOST_TEST_EQ( ec.value(), EINVAL ); + BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 127 ); + } + + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec = sys::error_code( none, &loc ); + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec.assign( einval, &loc ); + + BOOST_TEST_EQ( ec.value(), EINVAL ); + BOOST_TEST_EQ( &ec.category(), &sys::generic_category() ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 175 ); + } + + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec.assign( none, &loc ); + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) + + { + sys::error_code ec( e2inval ); + + BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + sys::error_code ec( e2inval, &loc ); + + BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec = sys::error_code( e2inval, &loc ); + + BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + + { + sys::error_code ec; + + BOOST_TEST_EQ( ec.value(), 0 ); + BOOST_TEST_EQ( &ec.category(), &sys::system_category() ); + + BOOST_TEST( !ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + + BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION; + + ec.assign( e2inval, &loc ); + + BOOST_TEST_EQ( ec, std::error_code( EINVAL, std::generic_category() ) ); + + BOOST_TEST( ec.failed() ); + + BOOST_TEST( !ec.has_location() ); + BOOST_TEST_EQ( ec.location().line(), 0 ); + } + +#endif + + return boost::report_errors(); +}