From 7b56fd9acf0a190ff3f6aad263f961b4d09a582c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 27 Aug 2020 00:47:48 +0300 Subject: [PATCH] Extract error_category into its own header --- .../boost/system/detail/error_category.hpp | 147 ++++++++++++++++++ include/boost/system/error_code.hpp | 117 +------------- 2 files changed, 150 insertions(+), 114 deletions(-) create mode 100644 include/boost/system/detail/error_category.hpp diff --git a/include/boost/system/detail/error_category.hpp b/include/boost/system/detail/error_category.hpp new file mode 100644 index 0000000..3fd0ed6 --- /dev/null +++ b/include/boost/system/detail/error_category.hpp @@ -0,0 +1,147 @@ +#ifndef BOOST_SYSTEM_DETAIL_ERROR_CATEGORY_HPP_INCLUDED +#define BOOST_SYSTEM_DETAIL_ERROR_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 +#include + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) +# include +#endif + +namespace boost +{ + +namespace system +{ + +class error_code; +class error_condition; + +#if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG ) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif + +std::size_t hash_value( error_code const & ec ); + +class BOOST_SYMBOL_VISIBLE error_category +{ +private: + + friend std::size_t hash_value( error_code const & ec ); + +#if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) +public: + + error_category( error_category const & ) = delete; + error_category& operator=( error_category const & ) = delete; + +#else +private: + + error_category( error_category const & ); + error_category& operator=( error_category const & ); + +#endif + +private: + + boost::ulong_long_type id_; + +protected: + +#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && !defined(BOOST_NO_CXX11_NON_PUBLIC_DEFAULTED_FUNCTIONS) + + ~error_category() = default; + +#else + + // We'd like to make the destructor protected, to make code that deletes + // an error_category* not compile; unfortunately, doing the below makes + // the destructor user-provided and hence breaks use after main, as the + // categories may get destroyed before code that uses them + + // ~error_category() {} + +#endif + + BOOST_SYSTEM_CONSTEXPR error_category() BOOST_NOEXCEPT: id_( 0 ) + { + } + + explicit BOOST_SYSTEM_CONSTEXPR error_category( boost::ulong_long_type id ) BOOST_NOEXCEPT: id_( id ) + { + } + +public: + + virtual const char * name() const BOOST_NOEXCEPT = 0; + + virtual error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT; + virtual bool equivalent( int code, const error_condition & condition ) const BOOST_NOEXCEPT; + virtual bool equivalent( const error_code & code, int condition ) const BOOST_NOEXCEPT; + + virtual std::string message( int ev ) const = 0; + virtual char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT; + + virtual bool failed( int ev ) const BOOST_NOEXCEPT; + + BOOST_SYSTEM_CONSTEXPR bool operator==( const error_category & rhs ) const BOOST_NOEXCEPT + { + return rhs.id_ == 0? this == &rhs: id_ == rhs.id_; + } + + BOOST_SYSTEM_CONSTEXPR bool operator!=( const error_category & rhs ) const BOOST_NOEXCEPT + { + return !( *this == rhs ); + } + + BOOST_SYSTEM_CONSTEXPR bool operator<( const error_category & rhs ) const BOOST_NOEXCEPT + { + if( id_ < rhs.id_ ) + { + return true; + } + + if( id_ > rhs.id_ ) + { + return false; + } + + if( rhs.id_ != 0 ) + { + return false; // equal + } + + return std::less()( this, &rhs ); + } + +#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) + + operator std::error_category const & () const; + +#endif +}; + +#if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG ) +#pragma GCC diagnostic pop +#endif + +} // namespace system + +} // namespace boost + +#endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CATEGORY_HPP_INCLUDED diff --git a/include/boost/system/error_code.hpp b/include/boost/system/error_code.hpp index 332d34c..7ba24dc 100644 --- a/include/boost/system/error_code.hpp +++ b/include/boost/system/error_code.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -40,125 +41,13 @@ class error_code; // values defined by the operating system class error_condition; // portable generic values defined below, but ultimately // based on the POSIX standard -// class error_category +// predefined error categories + #if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG ) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" #endif -#ifdef BOOST_MSVC -#pragma warning( push ) -// 'this' : used in base member initializer list -#pragma warning( disable: 4355 ) -#endif - -std::size_t hash_value( error_code const & ec ); - -class BOOST_SYMBOL_VISIBLE error_category -{ -private: - - friend std::size_t hash_value( error_code const & ec ); - -#if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) -public: - - error_category( error_category const & ) = delete; - error_category& operator=( error_category const & ) = delete; - -#else -private: - - error_category( error_category const & ); - error_category& operator=( error_category const & ); - -#endif - -private: - - boost::ulong_long_type id_; - -protected: - -#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && !defined(BOOST_NO_CXX11_NON_PUBLIC_DEFAULTED_FUNCTIONS) - - ~error_category() = default; - -#else - - // We'd like to make the destructor protected, to make code that deletes - // an error_category* not compile; unfortunately, doing the below makes - // the destructor user-provided and hence breaks use after main, as the - // categories may get destroyed before code that uses them - - // ~error_category() {} - -#endif - - BOOST_SYSTEM_CONSTEXPR error_category() BOOST_NOEXCEPT: id_( 0 ) - { - } - - explicit BOOST_SYSTEM_CONSTEXPR error_category( boost::ulong_long_type id ) BOOST_NOEXCEPT: id_( id ) - { - } - -public: - - virtual const char * name() const BOOST_NOEXCEPT = 0; - - virtual error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT; - virtual bool equivalent( int code, const error_condition & condition ) const BOOST_NOEXCEPT; - virtual bool equivalent( const error_code & code, int condition ) const BOOST_NOEXCEPT; - - virtual std::string message( int ev ) const = 0; - virtual char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT; - - virtual bool failed( int ev ) const BOOST_NOEXCEPT; - - BOOST_SYSTEM_CONSTEXPR bool operator==( const error_category & rhs ) const BOOST_NOEXCEPT - { - return rhs.id_ == 0? this == &rhs: id_ == rhs.id_; - } - - BOOST_SYSTEM_CONSTEXPR bool operator!=( const error_category & rhs ) const BOOST_NOEXCEPT - { - return !( *this == rhs ); - } - - BOOST_SYSTEM_CONSTEXPR bool operator<( const error_category & rhs ) const BOOST_NOEXCEPT - { - if( id_ < rhs.id_ ) - { - return true; - } - - if( id_ > rhs.id_ ) - { - return false; - } - - if( rhs.id_ != 0 ) - { - return false; // equal - } - - return std::less()( this, &rhs ); - } - -#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) - - operator std::error_category const & () const; - -#endif -}; - -#ifdef BOOST_MSVC -#pragma warning( pop ) -#endif - -// predefined error categories - namespace detail {