diff --git a/include/boost/system/detail/generic_category.hpp b/include/boost/system/detail/generic_category.hpp new file mode 100644 index 0000000..cba4a2e --- /dev/null +++ b/include/boost/system/detail/generic_category.hpp @@ -0,0 +1,69 @@ +// Implementation details of generic_error_category +// +// Copyright 2018 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See library home page at http://www.boost.org/libs/system + +#include + +namespace boost +{ + +namespace system +{ + +namespace detail +{ + +#if defined(__GLIBC__) + +// std::strerror is not thread-safe on glibc (for no reason) +// glibc also has two incompatible strerror_r definitions (for no reason) + +inline char const * strerror_r_helper( char const * r, char const * ) +{ + return r; +} + +inline char const * strerror_r_helper( int r, char const * buffer ) +{ + return r == 0? buffer: "Unknown error"; +} + +inline std::string generic_error_category_message( int ev ) +{ + char buffer[ 128 ]; + return strerror_r_helper( strerror_r( ev, buffer, sizeof( buffer ) ), buffer ); +} + +#else + +// std::strerror is thread-safe on everything else, incl. Windows + +inline std::string generic_error_category_message( int ev ) +{ + +# if defined( BOOST_MSVC ) +# pragma warning( push ) +# pragma warning( disable: 4996 ) +# endif + + char const * m = std::strerror( ev ); + +# if defined( BOOST_MSVC ) +# pragma warning( pop ) +# endif + + return m? m: "Unknown error"; + +#endif +} + +} // namespace detail + +} // namespace system + +} // namespace boost diff --git a/include/boost/system/error_code.hpp b/include/boost/system/error_code.hpp index c3b51f1..684cd47 100644 --- a/include/boost/system/error_code.hpp +++ b/include/boost/system/error_code.hpp @@ -17,7 +17,6 @@ #include #include #include -#include // TODO: undef these macros if not already defined #include @@ -761,38 +760,19 @@ inline bool error_category::equivalent( const error_code & code, int condition ) return *this == code.category() && code.value() == condition; } -// generic_error_category implementation - -namespace detail -{ - -inline char const * generic_error_category_message( int ev ) -{ -#ifdef BOOST_MSVC -#pragma warning( push ) -#pragma warning( disable: 4996 ) -#endif - - char const * m = std::strerror( ev ); - -#ifdef BOOST_MSVC -#pragma warning( pop ) -#endif - - return m? m: "Unknown error"; -} - -inline std::string generic_error_category::message( int ev ) const -{ - return generic_error_category_message( ev ); -} - -} // namespace detail - } // namespace system } // namespace boost +// generic_error_category implementation + +#include + +inline std::string boost::system::detail::generic_error_category::message( int ev ) const +{ + return generic_error_category_message( ev ); +} + // system_error_category implementation #if defined(BOOST_WINDOWS_API)