Add conversions to std:: counterparts when <system_error> is available

This commit is contained in:
Peter Dimov
2017-05-20 06:05:39 +03:00
parent ebda81008e
commit 6010be4144

View File

@ -28,6 +28,10 @@
# error BOOST_POSIX_API or BOOST_WINDOWS_API must be defined
#endif
#ifndef BOOST_NO_CXX11_HDR_SYSTEM_ERROR
#include <system_error>
#endif
#include <boost/config/abi_prefix.hpp> // must be the last #include
#ifndef BOOST_SYSTEM_NOEXCEPT
@ -181,28 +185,7 @@ namespace boost
// ----------------------------------------------------------------------//
// class error_category ------------------------------------------------//
class error_category : public noncopyable
{
public:
virtual ~error_category(){}
virtual const char * name() const BOOST_SYSTEM_NOEXCEPT = 0;
virtual std::string message( int ev ) const = 0;
inline virtual error_condition default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT;
inline virtual bool equivalent( int code,
const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT;
inline virtual bool equivalent( const error_code & code,
int condition ) const BOOST_SYSTEM_NOEXCEPT;
bool operator==(const error_category & rhs) const BOOST_SYSTEM_NOEXCEPT { return this == &rhs; }
bool operator!=(const error_category & rhs) const BOOST_SYSTEM_NOEXCEPT { return this != &rhs; }
bool operator<( const error_category & rhs ) const BOOST_SYSTEM_NOEXCEPT
{
return std::less<const error_category*>()( this, &rhs );
}
};
class error_category;
// predefined error categories -----------------------------------------//
@ -224,6 +207,76 @@ namespace boost
static const error_category & native_ecat BOOST_ATTRIBUTE_UNUSED = system_category();
#endif
// class error_category ------------------------------------------------//
class error_category : public noncopyable
{
#ifndef BOOST_NO_CXX11_HDR_SYSTEM_ERROR
private:
class std_category: public std::error_category
{
private:
boost::system::error_category const * pc_;
public:
explicit std_category( boost::system::error_category const * pc ): pc_( pc )
{
}
virtual const char * name() const BOOST_NOEXCEPT
{
return pc_->name();
}
virtual std::string message( int ev ) const
{
return pc_->message( ev );
}
virtual std::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT;
};
std_category std_cat_;
public:
error_category() BOOST_SYSTEM_NOEXCEPT: std_cat_( this ) {}
operator std::error_category const & () const BOOST_SYSTEM_NOEXCEPT
{
if( this == &generic_category() )
{
return std::generic_category();
}
return std_cat_;
}
#endif
public:
virtual ~error_category(){}
virtual const char * name() const BOOST_SYSTEM_NOEXCEPT = 0;
virtual std::string message( int ev ) const = 0;
inline virtual error_condition default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT;
inline virtual bool equivalent( int code,
const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT;
inline virtual bool equivalent( const error_code & code,
int condition ) const BOOST_SYSTEM_NOEXCEPT;
bool operator==(const error_category & rhs) const BOOST_SYSTEM_NOEXCEPT { return this == &rhs; }
bool operator!=(const error_category & rhs) const BOOST_SYSTEM_NOEXCEPT { return this != &rhs; }
bool operator<( const error_category & rhs ) const BOOST_SYSTEM_NOEXCEPT
{
return std::less<const error_category*>()( this, &rhs );
}
};
// class error_condition -----------------------------------------------//
// error_conditions are portable, error_codes are system or library specific
@ -308,12 +361,30 @@ namespace boost
|| (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
}
#ifndef BOOST_NO_CXX11_HDR_SYSTEM_ERROR
operator std::error_condition () const BOOST_SYSTEM_NOEXCEPT
{
return std::error_condition( value(), category() );
}
#endif
private:
int m_val;
const error_category * m_cat;
};
#ifndef BOOST_NO_CXX11_HDR_SYSTEM_ERROR
inline std::error_condition error_category::std_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
{
return pc_->default_error_condition( ev );
}
#endif
// class error_code ----------------------------------------------------//
// We want error_code to be a value type that can be copied without slicing
@ -402,6 +473,15 @@ namespace boost
|| (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
}
#ifndef BOOST_NO_CXX11_HDR_SYSTEM_ERROR
operator std::error_code () const BOOST_SYSTEM_NOEXCEPT
{
return std::error_code( value(), category() );
}
#endif
private:
int m_val;
const error_category * m_cat;