mirror of
https://github.com/boostorg/system.git
synced 2025-07-29 12:07:13 +02:00
Extract generic_error_category into its own header
This commit is contained in:
143
include/boost/system/detail/generic_category.hpp
Normal file
143
include/boost/system/detail/generic_category.hpp
Normal file
@ -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 <boost/system/detail/error_category.hpp>
|
||||
#include <boost/system/detail/config.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <cstring>
|
||||
|
||||
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
|
@ -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
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include <boost/system/is_error_condition_enum.hpp>
|
||||
#include <boost/system/detail/errc.hpp>
|
||||
#include <boost/system/detail/error_category.hpp>
|
||||
#include <boost/system/detail/generic_category.hpp>
|
||||
#include <boost/system/api_config.hpp>
|
||||
#include <boost/system/detail/config.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
@ -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 <boost/system/detail/generic_category_impl.hpp>
|
||||
|
||||
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)
|
||||
|
Reference in New Issue
Block a user