From e3f198e52cb35caf1feed6c7abe334c84554083b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 19 Sep 2021 15:37:49 +0300 Subject: [PATCH] Add error_condition::to_string --- include/boost/system/detail/append_int.hpp | 32 +++++++++++++++++++ include/boost/system/detail/error_code.hpp | 15 +++++---- .../boost/system/detail/error_condition.hpp | 16 +++++++--- test/error_condition_test3.cpp | 10 ++++++ 4 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 include/boost/system/detail/append_int.hpp diff --git a/include/boost/system/detail/append_int.hpp b/include/boost/system/detail/append_int.hpp new file mode 100644 index 0000000..d927376 --- /dev/null +++ b/include/boost/system/detail/append_int.hpp @@ -0,0 +1,32 @@ +#ifndef BOOST_SYSTEM_DETAIL_APPEND_INT_HPP_INCLUDED +#define BOOST_SYSTEM_DETAIL_APPEND_INT_HPP_INCLUDED + +// Copyright 2021 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +// + +namespace boost +{ +namespace system +{ +namespace detail +{ + +inline void append_int( std::string& s, int v ) +{ + char buffer[ 32 ]; + detail::snprintf( buffer, sizeof( buffer ), ":%d", v ); + + s += buffer; +} + +} // namespace detail +} // namespace system +} // namespace boost + +#endif // #ifndef BOOST_SYSTEM_DETAIL_APPEND_INT_HPP_INCLUDED diff --git a/include/boost/system/detail/error_code.hpp b/include/boost/system/detail/error_code.hpp index 0984be7..6f4adef 100644 --- a/include/boost/system/detail/error_code.hpp +++ b/include/boost/system/detail/error_code.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -549,18 +550,18 @@ public: { std::error_code const& e2 = *reinterpret_cast( d2_ ); - char buffer[ 32 ]; - detail::snprintf( buffer, sizeof( buffer ), "%d", e2.value() ); + std::string r( "std:" ); + r += e2.category().name(); + detail::append_int( r, e2.value() ); - return std::string( "std:" ) + e2.category().name() + ":" + buffer; + return r; } else #endif { - char buffer[ 32 ]; - detail::snprintf( buffer, sizeof( buffer ), "%d", value() ); - - return std::string( category().name() ) + ":" + buffer; + std::string r = category().name(); + detail::append_int( r, value() ); + return r; } } diff --git a/include/boost/system/detail/error_condition.hpp b/include/boost/system/detail/error_condition.hpp index c52d495..a16ce57 100644 --- a/include/boost/system/detail/error_condition.hpp +++ b/include/boost/system/detail/error_condition.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -246,14 +247,21 @@ public: #endif + std::string to_string() const + { + std::string r( "cond:" ); + + r += category().name(); + detail::append_int( r, value() ); + + return r; + } + template inline friend std::basic_ostream& operator<< (std::basic_ostream& os, error_condition const & en) { - { - os << "cond:" << en.category().name() << ':' << en.value(); - } - + os << en.to_string(); return os; } }; diff --git a/test/error_condition_test3.cpp b/test/error_condition_test3.cpp index 4d3c58d..567ac8f 100644 --- a/test/error_condition_test3.cpp +++ b/test/error_condition_test3.cpp @@ -22,6 +22,8 @@ int main() BOOST_TEST( !en.failed() ); BOOST_TEST( !en ); + BOOST_TEST_EQ( en.to_string(), std::string( "cond:generic:0" ) ); + { sys::error_condition en2( en ); @@ -34,6 +36,8 @@ int main() BOOST_TEST_EQ( en, en2 ); BOOST_TEST_NOT( en != en2 ); + + BOOST_TEST_EQ( en2.to_string(), std::string( "cond:generic:0" ) ); } { @@ -48,6 +52,8 @@ int main() BOOST_TEST_EQ( en, en2 ); BOOST_TEST_NOT( en != en2 ); + + BOOST_TEST_EQ( en2.to_string(), std::string( "cond:generic:0" ) ); } { @@ -63,6 +69,8 @@ int main() BOOST_TEST_NE( en, en2 ); BOOST_TEST( en != en2 ); + + BOOST_TEST_EQ( en2.to_string(), std::string( "cond:generic:5" ) ); } { @@ -78,6 +86,8 @@ int main() BOOST_TEST_NE( en, en2 ); BOOST_TEST( en != en2 ); + + BOOST_TEST_EQ( en2.to_string(), std::string( "cond:system:5" ) ); } return boost::report_errors();