From ddd679e6d77b702cdcb71930ddd08b02d3b3b19c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 27 Aug 2020 01:06:07 +0300 Subject: [PATCH] Extract generic_error_category into its own header --- .../boost/system/detail/generic_category.hpp | 143 ++++++++++++++++++ .../system/detail/generic_category_impl.hpp | 76 ---------- include/boost/system/error_code.hpp | 35 +---- 3 files changed, 144 insertions(+), 110 deletions(-) create mode 100644 include/boost/system/detail/generic_category.hpp diff --git a/include/boost/system/detail/generic_category.hpp b/include/boost/system/detail/generic_category.hpp new file mode 100644 index 0000000..4341c9e --- /dev/null +++ b/include/boost/system/detail/generic_category.hpp @@ -0,0 +1,143 @@ +#ifndef BOOST_SYSTEM_DETAIL_GENERIC_CATEGORY_HPP_INCLUDED +#define BOOST_SYSTEM_DETAIL_GENERIC_CATEGORY_HPP_INCLUDED + +// Copyright Beman Dawes 2006, 2007 +// Copyright Christoper Kohlhoff 2007 +// Copyright Peter Dimov 2017, 2018 +// +// 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 +#include +#include +#include +#include + +namespace boost +{ + +namespace system +{ + +namespace detail +{ + +// generic_error_category + +#if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG ) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif + +class BOOST_SYMBOL_VISIBLE generic_error_category: public error_category +{ +public: + + // clang++ 3.8 and below: initialization of const object + // requires a user-provided default constructor + BOOST_SYSTEM_CONSTEXPR generic_error_category() BOOST_NOEXCEPT: + error_category( ( boost::ulong_long_type( 0xB2AB117A ) << 32 ) + 0x257EDF0D ) + { + } + + const char * name() const BOOST_NOEXCEPT BOOST_OVERRIDE + { + return "generic"; + } + + std::string message( int ev ) const BOOST_OVERRIDE; + char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT BOOST_OVERRIDE; +}; + +#if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG ) +#pragma GCC diagnostic pop +#endif + +// generic_error_category::message + +#if defined(__GLIBC__) + +// glibc has two incompatible strerror_r definitions + +inline char const * strerror_r_helper( char const * r, char const * ) BOOST_NOEXCEPT +{ + return r; +} + +inline char const * strerror_r_helper( int r, char const * buffer ) BOOST_NOEXCEPT +{ + return r == 0? buffer: "Unknown error"; +} + +inline char const * generic_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT +{ + return strerror_r_helper( strerror_r( ev, buffer, len ), buffer ); +} + +inline std::string generic_error_category::message( int ev ) const +{ + char buffer[ 128 ]; + return generic_error_category_message( ev, buffer, sizeof( buffer ) ); +} + +#else // #if defined(__GLIBC__) + +// std::strerror is thread-safe on everything else, incl. Windows + +# if defined( BOOST_MSVC ) +# pragma warning( push ) +# pragma warning( disable: 4996 ) +# elif defined(__clang__) && defined(__has_warning) +# pragma clang diagnostic push +# if __has_warning("-Wdeprecated-declarations") +# pragma clang diagnostic ignored "-Wdeprecated-declarations" +# endif +# endif + +inline std::string generic_error_category::message( int ev ) const +{ + char const * m = std::strerror( ev ); + return m? m: "Unknown error"; +} + +inline char const * generic_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT +{ + if( len == 0 ) + { + return buffer; + } + + if( len == 1 ) + { + buffer[0] = 0; + return buffer; + } + + char const * m = std::strerror( ev ); + + if( m == 0 ) return "Unknown error"; + + std::strncpy( buffer, m, len - 1 ); + buffer[ len-1 ] = 0; + + return buffer; +} + +# if defined( BOOST_MSVC ) +# pragma warning( pop ) +# elif defined(__clang__) && defined(__has_warning) +# pragma clang diagnostic pop +# endif + +#endif // #if defined(__GLIBC__) + +} // namespace detail + +} // namespace system + +} // namespace boost + +#endif // #ifndef BOOST_SYSTEM_DETAIL_GENERIC_CATEGORY_HPP_INCLUDED diff --git a/include/boost/system/detail/generic_category_impl.hpp b/include/boost/system/detail/generic_category_impl.hpp index 35175f8..17bec40 100644 --- a/include/boost/system/detail/generic_category_impl.hpp +++ b/include/boost/system/detail/generic_category_impl.hpp @@ -18,82 +18,6 @@ namespace system namespace detail { -#if defined(__GLIBC__) - -// glibc has two incompatible strerror_r definitions - -inline char const * strerror_r_helper( char const * r, char const * ) BOOST_NOEXCEPT -{ - return r; -} - -inline char const * strerror_r_helper( int r, char const * buffer ) BOOST_NOEXCEPT -{ - return r == 0? buffer: "Unknown error"; -} - -inline char const * generic_error_category_message( int ev, char * buffer, std::size_t len ) BOOST_NOEXCEPT -{ - return strerror_r_helper( strerror_r( ev, buffer, len ), buffer ); -} - -inline std::string generic_error_category_message( int ev ) -{ - char buffer[ 128 ]; - return generic_error_category_message( ev, buffer, sizeof( buffer ) ); -} - -#else - -// std::strerror is thread-safe on everything else, incl. Windows - -# if defined( BOOST_MSVC ) -# pragma warning( push ) -# pragma warning( disable: 4996 ) -# elif defined(__clang__) && defined(__has_warning) -# pragma clang diagnostic push -# if __has_warning("-Wdeprecated-declarations") -# pragma clang diagnostic ignored "-Wdeprecated-declarations" -# endif -# endif - -inline std::string generic_error_category_message( int ev ) -{ - char const * m = std::strerror( ev ); - return m? m: "Unknown error"; -} - -inline char const * generic_error_category_message( int ev, char * buffer, std::size_t len ) BOOST_NOEXCEPT -{ - if( len == 0 ) - { - return buffer; - } - - if( len == 1 ) - { - buffer[0] = 0; - return buffer; - } - - char const * m = std::strerror( ev ); - - if( m == 0 ) return "Unknown error"; - - std::strncpy( buffer, m, len - 1 ); - buffer[ len-1 ] = 0; - - return buffer; -} - -# if defined( BOOST_MSVC ) -# pragma warning( pop ) -# elif defined(__clang__) && defined(__has_warning) -# pragma clang diagnostic pop -# endif - -#endif - } // namespace detail } // namespace system diff --git a/include/boost/system/error_code.hpp b/include/boost/system/error_code.hpp index 33971cd..82fbce5 100644 --- a/include/boost/system/error_code.hpp +++ b/include/boost/system/error_code.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -51,26 +52,6 @@ class error_condition; // portable generic values defined below, but ultimate namespace detail { -class BOOST_SYMBOL_VISIBLE generic_error_category: public error_category -{ -public: - - // clang++ 3.8 and below: initialization of const object - // requires a user-provided default constructor - BOOST_SYSTEM_CONSTEXPR generic_error_category() BOOST_NOEXCEPT: - error_category( ( boost::ulong_long_type( 0xB2AB117A ) << 32 ) + 0x257EDF0D ) - { - } - - const char * name() const BOOST_NOEXCEPT BOOST_OVERRIDE - { - return "generic"; - } - - std::string message( int ev ) const BOOST_OVERRIDE; - char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT BOOST_OVERRIDE; -}; - class BOOST_SYMBOL_VISIBLE system_error_category: public error_category { public: @@ -675,20 +656,6 @@ inline bool error_category::failed( int ev ) const BOOST_NOEXCEPT } // 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 ); -} - -inline char const * boost::system::detail::generic_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT -{ - return generic_error_category_message( ev, buffer, len ); -} - // system_error_category implementation #if defined(BOOST_WINDOWS_API)