mirror of
https://github.com/boostorg/system.git
synced 2025-07-30 20:47:14 +02:00
Add error_condition::to_string
This commit is contained in:
32
include/boost/system/detail/append_int.hpp
Normal file
32
include/boost/system/detail/append_int.hpp
Normal file
@ -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 <boost/system/detail/snprintf.hpp>
|
||||
#include <string>
|
||||
|
||||
//
|
||||
|
||||
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
|
@ -18,6 +18,7 @@
|
||||
#include <boost/system/detail/interop_category.hpp>
|
||||
#include <boost/system/detail/enable_if.hpp>
|
||||
#include <boost/system/detail/is_same.hpp>
|
||||
#include <boost/system/detail/append_int.hpp>
|
||||
#include <boost/system/detail/snprintf.hpp>
|
||||
#include <boost/system/detail/config.hpp>
|
||||
#include <boost/assert/source_location.hpp>
|
||||
@ -549,18 +550,18 @@ public:
|
||||
{
|
||||
std::error_code const& e2 = *reinterpret_cast<std::error_code const*>( 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include <boost/system/detail/enable_if.hpp>
|
||||
#include <boost/system/detail/is_same.hpp>
|
||||
#include <boost/system/detail/errc.hpp>
|
||||
#include <boost/system/detail/append_int.hpp>
|
||||
#include <boost/system/is_error_condition_enum.hpp>
|
||||
#include <boost/system/detail/config.hpp>
|
||||
#include <boost/config.hpp>
|
||||
@ -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<class Ch, class Tr>
|
||||
inline friend std::basic_ostream<Ch, Tr>&
|
||||
operator<< (std::basic_ostream<Ch, Tr>& os, error_condition const & en)
|
||||
{
|
||||
{
|
||||
os << "cond:" << en.category().name() << ':' << en.value();
|
||||
}
|
||||
|
||||
os << en.to_string();
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user