mirror of
https://github.com/boostorg/system.git
synced 2025-12-25 16:28:05 +01:00
Compare commits
35 Commits
feature/he
...
feature/us
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78a1813c13 | ||
|
|
584f9731ad | ||
|
|
f821d5e74a | ||
|
|
91aeb86f57 | ||
|
|
e7c1079c4f | ||
|
|
be972baaa3 | ||
|
|
f48cc5aec6 | ||
|
|
349fb30e8e | ||
|
|
08dc402e77 | ||
|
|
a627662e63 | ||
|
|
1d4de72899 | ||
|
|
d966de437e | ||
|
|
b300fdeef4 | ||
|
|
24179e73c9 | ||
|
|
c182707dfe | ||
|
|
d79e38e5d5 | ||
|
|
73de31c541 | ||
|
|
7b401cef93 | ||
|
|
e180bfe37e | ||
|
|
ec93d058fe | ||
|
|
a4e700ecf4 | ||
|
|
70d16a61e6 | ||
|
|
3a41aaabad | ||
|
|
46d383b3bd | ||
|
|
9afd678532 | ||
|
|
68c89304f2 | ||
|
|
0272ea0ea5 | ||
|
|
c4626e0413 | ||
|
|
ff7116404b | ||
|
|
ca68b08511 | ||
|
|
bbd0a3766d | ||
|
|
a9909bb82b | ||
|
|
3bdea5dfa3 | ||
|
|
02ea086173 | ||
|
|
4b7018de85 |
39
appveyor.yml
39
appveyor.yml
@@ -1,4 +1,4 @@
|
||||
# Copyright 2016, 2017 Peter Dimov
|
||||
# Copyright 2016-2018 Peter Dimov
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
|
||||
@@ -15,48 +15,30 @@ branches:
|
||||
environment:
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
TOOLSET: msvc-9.0
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
TOOLSET: msvc-10.0
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
TOOLSET: msvc-11.0
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
TOOLSET: msvc-12.0
|
||||
TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0,msvc-12.0
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
TOOLSET: msvc-14.0
|
||||
ADDRMD: 32,64
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
TOOLSET: msvc-14.1
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
TOOLSET: msvc-14.1
|
||||
CXXSTD: 17
|
||||
CXXSTD: 14,17
|
||||
ADDRMD: 32,64
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\cygwin\bin;
|
||||
TOOLSET: gcc
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\cygwin\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11
|
||||
CXXSTD: 03,11,14,1z
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\cygwin64\bin;
|
||||
TOOLSET: gcc
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\cygwin64\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11
|
||||
CXXSTD: 03,11,14,1z
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\mingw\bin;
|
||||
TOOLSET: gcc
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\mingw\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11
|
||||
CXXSTD: 03,11,14,1z
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin;
|
||||
TOOLSET: gcc
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
|
||||
ADDPATH: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11
|
||||
CXXSTD: 03,11,14,1z
|
||||
|
||||
install:
|
||||
- set BOOST_BRANCH=develop
|
||||
@@ -77,4 +59,5 @@ build: off
|
||||
test_script:
|
||||
- PATH=%ADDPATH%%PATH%
|
||||
- if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD%
|
||||
- b2 -j3 libs/system/test toolset=%TOOLSET% variant=debug,release %CXXSTD%
|
||||
- if not "%ADDRMD%" == "" set ADDRMD=address-model=%ADDRMD%
|
||||
- b2 -j3 libs/system/test toolset=%TOOLSET% %CXXSTD% %ADDRMD% variant=debug,release
|
||||
|
||||
@@ -33,6 +33,10 @@
|
||||
# define BOOST_SYSTEM_HAS_CONSTEXPR
|
||||
#endif
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_GCC, < 60000)
|
||||
# undef BOOST_SYSTEM_HAS_CONSTEXPR
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_SYSTEM_HAS_CONSTEXPR)
|
||||
# define BOOST_SYSTEM_CONSTEXPR constexpr
|
||||
#else
|
||||
|
||||
@@ -20,45 +20,71 @@ 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)
|
||||
// glibc has two incompatible strerror_r definitions
|
||||
|
||||
inline char const * strerror_r_helper( char const * r, char const * )
|
||||
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 )
|
||||
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 strerror_r_helper( strerror_r( ev, buffer, sizeof( buffer ) ), buffer );
|
||||
return generic_error_category_message( ev, buffer, sizeof( 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
|
||||
|
||||
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 )
|
||||
# endif
|
||||
|
||||
return m? m: "Unknown error";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <boost/winapi/error_codes.hpp>
|
||||
#include <boost/winapi/error_handling.hpp>
|
||||
#include <boost/winapi/character_code_conversion.hpp>
|
||||
#include <boost/winapi/local_memory.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
//
|
||||
|
||||
@@ -24,66 +26,184 @@ namespace system
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )
|
||||
|
||||
inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len )
|
||||
{
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( push )
|
||||
# pragma warning( disable: 4996 )
|
||||
# endif
|
||||
|
||||
_snprintf( buffer, len - 1, "Unknown error (%d)", ev );
|
||||
|
||||
buffer[ len - 1 ] = 0;
|
||||
return buffer;
|
||||
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( pop )
|
||||
# endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len )
|
||||
{
|
||||
std::snprintf( buffer, len, "Unknown error (%d)", ev );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline boost::winapi::UINT_ message_cp_win32()
|
||||
{
|
||||
#if defined(BOOST_SYSTEM_USE_UTF8)
|
||||
|
||||
return boost::winapi::CP_UTF8_;
|
||||
|
||||
#else
|
||||
|
||||
return boost::winapi::CP_ACP_;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
inline char const * system_category_message_win32( int ev, char * buffer, std::size_t len ) BOOST_NOEXCEPT
|
||||
{
|
||||
if( len == 0 )
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
if( len == 1 )
|
||||
{
|
||||
buffer[0] = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#if defined(__GNUC__)
|
||||
# define BOOST_SYSTEM_ALLOCA __builtin_alloca
|
||||
#else
|
||||
# define BOOST_SYSTEM_ALLOCA _alloca
|
||||
#endif
|
||||
|
||||
wchar_t * wbuffer = static_cast<wchar_t*>( BOOST_SYSTEM_ALLOCA( len * sizeof( wchar_t ) ) );
|
||||
|
||||
#undef BOOST_SYSTEM_ALLOCA
|
||||
|
||||
using namespace boost::winapi;
|
||||
|
||||
DWORD_ retval = boost::winapi::FormatMessageW(
|
||||
FORMAT_MESSAGE_FROM_SYSTEM_ | FORMAT_MESSAGE_IGNORE_INSERTS_,
|
||||
NULL,
|
||||
ev,
|
||||
MAKELANGID_( LANG_NEUTRAL_, SUBLANG_DEFAULT_ ), // Default language
|
||||
wbuffer,
|
||||
static_cast<DWORD_>( len ),
|
||||
NULL
|
||||
);
|
||||
|
||||
if( retval == 0 )
|
||||
{
|
||||
return unknown_message_win32( ev, buffer, len );
|
||||
}
|
||||
|
||||
UINT_ const code_page = message_cp_win32();
|
||||
|
||||
int r = boost::winapi::WideCharToMultiByte( code_page, 0, wbuffer, -1, buffer, static_cast<int>( len ), NULL, NULL );
|
||||
|
||||
if( r == 0 )
|
||||
{
|
||||
return unknown_message_win32( ev, buffer, len );
|
||||
}
|
||||
|
||||
--r; // exclude null terminator
|
||||
|
||||
while( r > 0 && ( buffer[ r-1 ] == '\n' || buffer[ r-1 ] == '\r' ) )
|
||||
{
|
||||
buffer[ --r ] = 0;
|
||||
}
|
||||
|
||||
if( r > 0 && buffer[ r-1 ] == '.' )
|
||||
{
|
||||
buffer[ --r ] = 0;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
struct local_free
|
||||
{
|
||||
void * p_;
|
||||
|
||||
~local_free()
|
||||
{
|
||||
boost::winapi::LocalFree( p_ );
|
||||
}
|
||||
};
|
||||
|
||||
inline std::string unknown_message_win32( int ev )
|
||||
{
|
||||
char buffer[ 38 ];
|
||||
return unknown_message_win32( ev, buffer, sizeof( buffer ) );
|
||||
}
|
||||
|
||||
inline std::string system_category_message_win32( int ev )
|
||||
{
|
||||
using namespace boost::winapi;
|
||||
|
||||
std::wstring buf( 128, wchar_t() );
|
||||
wchar_t * lpMsgBuf = 0;
|
||||
|
||||
for( ;; )
|
||||
DWORD_ retval = boost::winapi::FormatMessageW(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER_ | FORMAT_MESSAGE_FROM_SYSTEM_ | FORMAT_MESSAGE_IGNORE_INSERTS_,
|
||||
NULL,
|
||||
ev,
|
||||
MAKELANGID_( LANG_NEUTRAL_, SUBLANG_DEFAULT_ ), // Default language
|
||||
(LPWSTR_) &lpMsgBuf,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
|
||||
if( retval == 0 )
|
||||
{
|
||||
DWORD_ retval = boost::winapi::FormatMessageW(
|
||||
FORMAT_MESSAGE_FROM_SYSTEM_ | FORMAT_MESSAGE_IGNORE_INSERTS_,
|
||||
NULL,
|
||||
ev,
|
||||
MAKELANGID_( LANG_NEUTRAL_, SUBLANG_DEFAULT_ ), // Default language
|
||||
&buf[0],
|
||||
static_cast<DWORD_>( buf.size() ),
|
||||
NULL
|
||||
);
|
||||
|
||||
if( retval > 0 )
|
||||
{
|
||||
buf.resize(retval);
|
||||
break;
|
||||
}
|
||||
else if( boost::winapi::GetLastError() != ERROR_INSUFFICIENT_BUFFER_ )
|
||||
{
|
||||
return "Unknown error";
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.resize( buf.size() + buf.size() / 2 );
|
||||
}
|
||||
return unknown_message_win32( ev );
|
||||
}
|
||||
|
||||
int num_chars = static_cast<int>( buf.size() + 1 ) * 2;
|
||||
local_free lf_ = { lpMsgBuf };
|
||||
|
||||
boost::winapi::LPSTR_ narrow_buffer =
|
||||
#if defined(__GNUC__)
|
||||
(boost::winapi::LPSTR_)__builtin_alloca( num_chars );
|
||||
#else
|
||||
(boost::winapi::LPSTR_)_alloca( num_chars );
|
||||
#endif
|
||||
UINT_ const code_page = message_cp_win32();
|
||||
|
||||
if( boost::winapi::WideCharToMultiByte( CP_ACP_, 0, buf.c_str(), -1, narrow_buffer, num_chars, NULL, NULL ) == 0 )
|
||||
int r = boost::winapi::WideCharToMultiByte( code_page, 0, lpMsgBuf, -1, 0, 0, NULL, NULL );
|
||||
|
||||
if( r == 0 )
|
||||
{
|
||||
return "Unknown error";
|
||||
return unknown_message_win32( ev );
|
||||
}
|
||||
|
||||
std::string str( narrow_buffer );
|
||||
std::string buffer( r, char() );
|
||||
|
||||
while( !str.empty() && ( str[ str.size() - 1 ] == '\n' || str[ str.size() - 1 ] == '\r' ) )
|
||||
r = boost::winapi::WideCharToMultiByte( code_page, 0, lpMsgBuf, -1, &buffer[0], r, NULL, NULL );
|
||||
|
||||
if( r == 0 )
|
||||
{
|
||||
str.erase( str.size() - 1 );
|
||||
return unknown_message_win32( ev );
|
||||
}
|
||||
|
||||
if( str.size() && str[ str.size() - 1 ] == '.' )
|
||||
--r; // exclude null terminator
|
||||
|
||||
while( r > 0 && ( buffer[ r-1 ] == '\n' || buffer[ r-1 ] == '\r' ) )
|
||||
{
|
||||
str.erase( str.size() - 1 );
|
||||
--r;
|
||||
}
|
||||
|
||||
return str;
|
||||
if( r > 0 && buffer[ r-1 ] == '.' )
|
||||
{
|
||||
--r;
|
||||
}
|
||||
|
||||
buffer.resize( r );
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
inline error_condition system_category_default_error_condition_win32( int ev ) BOOST_NOEXCEPT
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <cstring>
|
||||
|
||||
// TODO: undef these macros if not already defined
|
||||
#include <boost/cerrno.hpp>
|
||||
@@ -195,9 +196,12 @@ protected:
|
||||
|
||||
#else
|
||||
|
||||
~error_category()
|
||||
{
|
||||
}
|
||||
// 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
|
||||
|
||||
@@ -213,12 +217,15 @@ public:
|
||||
|
||||
virtual const char * name() const BOOST_NOEXCEPT = 0;
|
||||
|
||||
virtual std::string message( int ev ) const = 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_;
|
||||
@@ -281,6 +288,7 @@ public:
|
||||
}
|
||||
|
||||
std::string message( int ev ) const;
|
||||
char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT;
|
||||
};
|
||||
|
||||
class BOOST_SYMBOL_VISIBLE system_error_category: public error_category
|
||||
@@ -296,8 +304,10 @@ public:
|
||||
return "system";
|
||||
}
|
||||
|
||||
std::string message( int ev ) const;
|
||||
error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT;
|
||||
|
||||
std::string message( int ev ) const;
|
||||
char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
@@ -373,6 +383,31 @@ template<class T> struct enable_if<false, T>
|
||||
{
|
||||
};
|
||||
|
||||
// failed_impl
|
||||
|
||||
#if !defined(BOOST_SYSTEM_HAS_CONSTEXPR)
|
||||
|
||||
inline bool failed_impl( int ev, error_category const & cat )
|
||||
{
|
||||
return cat.failed( ev );
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR inline bool failed_impl( int ev, error_category const & cat )
|
||||
{
|
||||
if( cat == system_category() || cat == generic_category() )
|
||||
{
|
||||
return ev != 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return cat.failed( ev );
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// class error_condition
|
||||
@@ -383,18 +418,21 @@ class error_condition
|
||||
{
|
||||
private:
|
||||
|
||||
int m_val;
|
||||
error_category const * m_cat;
|
||||
int val_;
|
||||
bool failed_;
|
||||
error_category const * cat_;
|
||||
|
||||
public:
|
||||
|
||||
// constructors:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR error_condition() BOOST_NOEXCEPT: m_val( 0 ), m_cat( &generic_category() )
|
||||
BOOST_SYSTEM_CONSTEXPR error_condition() BOOST_NOEXCEPT:
|
||||
val_( 0 ), failed_( false ), cat_( &generic_category() )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) BOOST_NOEXCEPT: m_val( val ), m_cat( &cat )
|
||||
BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) BOOST_NOEXCEPT:
|
||||
val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -408,8 +446,9 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
|
||||
{
|
||||
m_val = val;
|
||||
m_cat = &cat;
|
||||
val_ = val;
|
||||
failed_ = detail::failed_impl( val, cat );
|
||||
cat_ = &cat;
|
||||
}
|
||||
|
||||
template<typename ErrorConditionEnum>
|
||||
@@ -422,32 +461,43 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
|
||||
{
|
||||
m_val = 0;
|
||||
m_cat = &generic_category();
|
||||
val_ = 0;
|
||||
failed_ = false;
|
||||
cat_ = &generic_category();
|
||||
}
|
||||
|
||||
// observers:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
|
||||
{
|
||||
return m_val;
|
||||
return val_;
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
|
||||
{
|
||||
return *m_cat;
|
||||
return *cat_;
|
||||
}
|
||||
|
||||
std::string message() const
|
||||
{
|
||||
return m_cat->message( value() );
|
||||
return cat_->message( value() );
|
||||
}
|
||||
|
||||
char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return cat_->message( value(), buffer, len );
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
|
||||
{
|
||||
return failed_;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
|
||||
{
|
||||
return m_val != 0;
|
||||
return failed_;
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -457,12 +507,12 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
|
||||
{
|
||||
return m_val == 0? 0 : unspecified_bool_true;
|
||||
return failed_? unspecified_bool_true: 0;
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
|
||||
{
|
||||
return m_val == 0;
|
||||
return !failed_;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -473,17 +523,17 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
|
||||
{
|
||||
return lhs.m_val == rhs.m_val && *lhs.m_cat == *rhs.m_cat;
|
||||
return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
|
||||
{
|
||||
return *lhs.m_cat < *rhs.m_cat || ( *lhs.m_cat == *rhs.m_cat && lhs.m_val < rhs.m_val );
|
||||
return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
|
||||
}
|
||||
|
||||
#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
|
||||
|
||||
operator std::error_condition () const BOOST_NOEXCEPT
|
||||
operator std::error_condition () const
|
||||
{
|
||||
return std::error_condition( value(), category() );
|
||||
}
|
||||
@@ -504,18 +554,21 @@ class error_code
|
||||
{
|
||||
private:
|
||||
|
||||
int m_val;
|
||||
const error_category * m_cat;
|
||||
int val_;
|
||||
bool failed_;
|
||||
const error_category * cat_;
|
||||
|
||||
public:
|
||||
|
||||
// constructors:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR error_code() BOOST_NOEXCEPT: m_val( 0 ), m_cat( &system_category() )
|
||||
BOOST_SYSTEM_CONSTEXPR error_code() BOOST_NOEXCEPT:
|
||||
val_( 0 ), failed_( false ), cat_( &system_category() )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR error_code( int val, const error_category & cat ) BOOST_NOEXCEPT: m_val( val ), m_cat( &cat )
|
||||
BOOST_SYSTEM_CONSTEXPR error_code( int val, const error_category & cat ) BOOST_NOEXCEPT:
|
||||
val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -529,8 +582,9 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
|
||||
{
|
||||
m_val = val;
|
||||
m_cat = &cat;
|
||||
val_ = val;
|
||||
failed_ = detail::failed_impl( val, cat );
|
||||
cat_ = &cat;
|
||||
}
|
||||
|
||||
template<typename ErrorCodeEnum>
|
||||
@@ -543,37 +597,48 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
|
||||
{
|
||||
m_val = 0;
|
||||
m_cat = &system_category();
|
||||
val_ = 0;
|
||||
failed_ = false;
|
||||
cat_ = &system_category();
|
||||
}
|
||||
|
||||
// observers:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
|
||||
{
|
||||
return m_val;
|
||||
return val_;
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
|
||||
{
|
||||
return *m_cat;
|
||||
return *cat_;
|
||||
}
|
||||
|
||||
error_condition default_error_condition() const BOOST_NOEXCEPT
|
||||
{
|
||||
return m_cat->default_error_condition( value() );
|
||||
return cat_->default_error_condition( value() );
|
||||
}
|
||||
|
||||
std::string message() const
|
||||
{
|
||||
return m_cat->message( value() );
|
||||
return cat_->message( value() );
|
||||
}
|
||||
|
||||
char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return cat_->message( value(), buffer, len );
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
|
||||
{
|
||||
return failed_;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
|
||||
{
|
||||
return m_val != 0;
|
||||
return failed_;
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -583,12 +648,12 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
|
||||
{
|
||||
return m_val == 0? 0 : unspecified_bool_true;
|
||||
return failed_? unspecified_bool_true: 0;
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
|
||||
{
|
||||
return m_val == 0;
|
||||
return !failed_;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -600,17 +665,17 @@ public:
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
|
||||
{
|
||||
return lhs.m_val == rhs.m_val && *lhs.m_cat == *rhs.m_cat;
|
||||
return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
|
||||
}
|
||||
|
||||
BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
|
||||
{
|
||||
return *lhs.m_cat < *rhs.m_cat || ( *lhs.m_cat == *rhs.m_cat && lhs.m_val < rhs.m_val );
|
||||
return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
|
||||
}
|
||||
|
||||
#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
|
||||
|
||||
operator std::error_code () const BOOST_NOEXCEPT
|
||||
operator std::error_code () const
|
||||
{
|
||||
return std::error_code( value(), category() );
|
||||
}
|
||||
@@ -760,6 +825,52 @@ inline bool error_category::equivalent( const error_code & code, int condition )
|
||||
return *this == code.category() && code.value() == condition;
|
||||
}
|
||||
|
||||
inline char const * 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;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_EXCEPTIONS)
|
||||
try
|
||||
#endif
|
||||
{
|
||||
std::string m = this->message( ev );
|
||||
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( push )
|
||||
# pragma warning( disable: 4996 )
|
||||
# endif
|
||||
|
||||
std::strncpy( buffer, m.c_str(), len - 1 );
|
||||
buffer[ len-1 ] = 0;
|
||||
|
||||
# if defined( BOOST_MSVC )
|
||||
# pragma warning( pop )
|
||||
# endif
|
||||
|
||||
return buffer;
|
||||
}
|
||||
#if !defined(BOOST_NO_EXCEPTIONS)
|
||||
catch( ... )
|
||||
{
|
||||
return "Message text unavailable";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
inline bool error_category::failed( int ev ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return ev != 0;
|
||||
}
|
||||
|
||||
} // namespace system
|
||||
|
||||
} // namespace boost
|
||||
@@ -773,34 +884,49 @@ inline std::string boost::system::detail::generic_error_category::message( int e
|
||||
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)
|
||||
|
||||
#include <boost/system/detail/system_category_win32.hpp>
|
||||
|
||||
inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return system_category_default_error_condition_win32( ev );
|
||||
}
|
||||
|
||||
inline std::string boost::system::detail::system_error_category::message( int ev ) const
|
||||
{
|
||||
return system_category_message_win32( ev );
|
||||
}
|
||||
|
||||
inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
|
||||
inline char const * boost::system::detail::system_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return system_category_default_error_condition_win32( ev );
|
||||
return system_category_message_win32( ev, buffer, len );
|
||||
}
|
||||
|
||||
#else // #if defined(BOOST_WINDOWS_API)
|
||||
|
||||
#include <boost/system/detail/system_category_posix.hpp>
|
||||
|
||||
inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return system_category_default_error_condition_posix( ev );
|
||||
}
|
||||
|
||||
inline std::string boost::system::detail::system_error_category::message( int ev ) const
|
||||
{
|
||||
return generic_error_category_message( ev );
|
||||
}
|
||||
|
||||
inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
|
||||
inline char const * boost::system::detail::system_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return system_category_default_error_condition_posix( ev );
|
||||
return generic_error_category_message( ev, buffer, len );
|
||||
}
|
||||
|
||||
#endif // #if defined(BOOST_WINDOWS_API)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# Boost System Library test Jamfile
|
||||
|
||||
# Copyright Beman Dawes 2003, 2006
|
||||
# Copyright 2017, 2018 Peter Dimov
|
||||
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt
|
||||
@@ -13,7 +14,6 @@ import os ;
|
||||
project
|
||||
: requirements
|
||||
<library>/boost/system//boost_system
|
||||
<toolset>msvc:<asynch-exceptions>on
|
||||
;
|
||||
|
||||
lib throw_test : throw_test.cpp : <link>shared:<define>THROW_DYN_LINK=1 ;
|
||||
@@ -98,33 +98,37 @@ else
|
||||
}
|
||||
}
|
||||
|
||||
test-suite "system"
|
||||
: [ system-run error_code_test.cpp ]
|
||||
[ system-run error_code_user_test.cpp ]
|
||||
[ system-run system_error_test.cpp ]
|
||||
[ run dynamic_link_test.cpp throw_test
|
||||
: : : <link>shared : throw_test_shared
|
||||
]
|
||||
[ system-run initialization_test.cpp ]
|
||||
[ run header_only_test.cpp
|
||||
: : : -<library>/boost/system//boost_system
|
||||
]
|
||||
[ run header_only_test.cpp
|
||||
: : : -<library>/boost/system//boost_system <define>BOOST_NO_ANSI_APIS : header_only_test_no_ansi
|
||||
]
|
||||
[ run config_test.cpp
|
||||
: : : <test-info>always_show_run_output
|
||||
]
|
||||
[ system-run- std_interop_test.cpp ]
|
||||
[ system-run std_mismatch_test.cpp ]
|
||||
[ system-run single_instance_test.cpp single_instance_1.cpp single_instance_2.cpp ]
|
||||
[ run single_instance_test.cpp single_instance_lib1 single_instance_lib2 : : : <link>static : single_instance_lib_static ]
|
||||
[ run single_instance_test.cpp single_instance_lib1 single_instance_lib2 : : : <link>shared : single_instance_lib_shared ]
|
||||
[ system-run before_main_test.cpp ]
|
||||
[ run-fail throws_assign_fail.cpp ]
|
||||
[ system-run- constexpr_test.cpp ]
|
||||
[ system-run win32_hresult_test.cpp ]
|
||||
;
|
||||
system-run error_code_test.cpp ;
|
||||
system-run error_code_user_test.cpp ;
|
||||
system-run system_error_test.cpp ;
|
||||
|
||||
run dynamic_link_test.cpp throw_test : : : <link>shared : throw_test_shared ;
|
||||
|
||||
system-run initialization_test.cpp ;
|
||||
|
||||
run header_only_test.cpp : : : -<library>/boost/system//boost_system ;
|
||||
run header_only_test.cpp : : : -<library>/boost/system//boost_system <define>BOOST_NO_ANSI_APIS : header_only_test_no_ansi ;
|
||||
|
||||
run config_test.cpp : : : <test-info>always_show_run_output ;
|
||||
|
||||
system-run- std_interop_test.cpp ;
|
||||
system-run std_mismatch_test.cpp ;
|
||||
|
||||
system-run single_instance_test.cpp single_instance_1.cpp single_instance_2.cpp ;
|
||||
run single_instance_test.cpp single_instance_lib1 single_instance_lib2 : : : <link>static : single_instance_lib_static ;
|
||||
run single_instance_test.cpp single_instance_lib1 single_instance_lib2 : : : <link>shared : single_instance_lib_shared ;
|
||||
|
||||
system-run before_main_test.cpp ;
|
||||
run-fail throws_assign_fail.cpp ;
|
||||
system-run- constexpr_test.cpp ;
|
||||
system-run win32_hresult_test.cpp ;
|
||||
|
||||
system-run error_category_test.cpp ;
|
||||
system-run generic_category_test.cpp ;
|
||||
system-run system_category_test.cpp ;
|
||||
system-run after_main_test.cpp ;
|
||||
system-run failed_test.cpp ;
|
||||
system-run- failed_constexpr_test.cpp ;
|
||||
|
||||
# Quick (CI) test
|
||||
run quick.cpp ;
|
||||
|
||||
30
test/after_main_test.cpp
Normal file
30
test/after_main_test.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
// Copyright 2018 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/core/quick_exit.hpp>
|
||||
#include <cerrno>
|
||||
|
||||
using namespace boost::system;
|
||||
|
||||
struct Z
|
||||
{
|
||||
~Z()
|
||||
{
|
||||
BOOST_TEST_CSTR_EQ( generic_category().name(), "generic" );
|
||||
BOOST_TEST_CSTR_EQ( system_category().name(), "system" );
|
||||
|
||||
boost::quick_exit( boost::report_errors() );
|
||||
}
|
||||
};
|
||||
|
||||
static Z z;
|
||||
|
||||
static error_code e1( 1, system_category() );
|
||||
static error_code e2( ENOENT, generic_category() );
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
||||
82
test/error_category_test.cpp
Normal file
82
test/error_category_test.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
|
||||
// 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
|
||||
|
||||
// Avoid spurious VC++ warnings
|
||||
# define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
//
|
||||
|
||||
namespace sys = boost::system;
|
||||
|
||||
class user_category: public sys::error_category
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char * name() const BOOST_NOEXCEPT
|
||||
{
|
||||
return "user";
|
||||
}
|
||||
|
||||
virtual std::string message( int ev ) const
|
||||
{
|
||||
char buffer[ 256 ];
|
||||
std::sprintf( buffer, "user message %d", ev );
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
using sys::error_category::message;
|
||||
};
|
||||
|
||||
static user_category s_cat_1;
|
||||
static user_category s_cat_2;
|
||||
|
||||
int main()
|
||||
{
|
||||
// default_error_condition
|
||||
|
||||
BOOST_TEST( s_cat_1.default_error_condition( 1 ) == sys::error_condition( 1, s_cat_1 ) );
|
||||
BOOST_TEST( s_cat_2.default_error_condition( 2 ) == sys::error_condition( 2, s_cat_2 ) );
|
||||
|
||||
// equivalent
|
||||
|
||||
BOOST_TEST( s_cat_1.equivalent( 1, sys::error_condition( 1, s_cat_1 ) ) );
|
||||
BOOST_TEST( !s_cat_1.equivalent( 1, sys::error_condition( 2, s_cat_1 ) ) );
|
||||
BOOST_TEST( !s_cat_1.equivalent( 1, sys::error_condition( 2, s_cat_2 ) ) );
|
||||
|
||||
// the other equivalent
|
||||
|
||||
BOOST_TEST( s_cat_1.equivalent( sys::error_code( 1, s_cat_1 ), 1 ) );
|
||||
BOOST_TEST( !s_cat_1.equivalent( sys::error_code( 1, s_cat_1 ), 2 ) );
|
||||
BOOST_TEST( !s_cat_1.equivalent( sys::error_code( 1, s_cat_2 ), 1 ) );
|
||||
|
||||
// message
|
||||
|
||||
{
|
||||
char buffer[ 256 ];
|
||||
BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), s_cat_1.message( 1 ).c_str() );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 4 ];
|
||||
BOOST_TEST_CSTR_EQ( s_cat_1.message( 1, buffer, sizeof( buffer ) ), "use" );
|
||||
}
|
||||
|
||||
// ==
|
||||
|
||||
BOOST_TEST_NOT( s_cat_1 == s_cat_2 );
|
||||
BOOST_TEST( s_cat_1 != s_cat_2 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
58
test/failed_constexpr_test.cpp
Normal file
58
test/failed_constexpr_test.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
// Copyright 2018 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#if !defined(BOOST_SYSTEM_HAS_CONSTEXPR)
|
||||
|
||||
BOOST_PRAGMA_MESSAGE("Skipping constexpr test, BOOST_SYSTEM_HAS_CONSTEXPR isn't defined")
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
using namespace boost::system;
|
||||
|
||||
constexpr error_code ec1( 1, system_category() );
|
||||
|
||||
BOOST_STATIC_ASSERT( ec1.failed() );
|
||||
BOOST_STATIC_ASSERT( ec1 );
|
||||
BOOST_STATIC_ASSERT( !!ec1 );
|
||||
|
||||
constexpr error_code ec2( 2, generic_category() );
|
||||
|
||||
BOOST_STATIC_ASSERT( ec2.failed() );
|
||||
BOOST_STATIC_ASSERT( ec2 );
|
||||
BOOST_STATIC_ASSERT( !!ec2 );
|
||||
|
||||
constexpr error_code ec3;
|
||||
|
||||
BOOST_STATIC_ASSERT( !ec3.failed() );
|
||||
BOOST_STATIC_ASSERT( ec3? false: true );
|
||||
BOOST_STATIC_ASSERT( !ec3 );
|
||||
|
||||
constexpr error_condition en1( 1, system_category() );
|
||||
|
||||
BOOST_STATIC_ASSERT( en1.failed() );
|
||||
BOOST_STATIC_ASSERT( en1 );
|
||||
BOOST_STATIC_ASSERT( !!en1 );
|
||||
|
||||
constexpr error_condition en2( 2, generic_category() );
|
||||
|
||||
BOOST_STATIC_ASSERT( en2.failed() );
|
||||
BOOST_STATIC_ASSERT( en2 );
|
||||
BOOST_STATIC_ASSERT( !!en2 );
|
||||
|
||||
constexpr error_condition en3;
|
||||
|
||||
BOOST_STATIC_ASSERT( !en3.failed() );
|
||||
BOOST_STATIC_ASSERT( en3? false: true );
|
||||
BOOST_STATIC_ASSERT( !en3 );
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
185
test/failed_test.cpp
Normal file
185
test/failed_test.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
|
||||
// Copyright 2018 Peter Dimov.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
|
||||
// Avoid spurious VC++ warnings
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
using namespace boost::system;
|
||||
|
||||
struct http_category_impl: public error_category
|
||||
{
|
||||
// clang++ 3.8 and below: initialization of const object
|
||||
// requires a user-provided default constructor
|
||||
BOOST_SYSTEM_CONSTEXPR http_category_impl() BOOST_NOEXCEPT
|
||||
{
|
||||
}
|
||||
|
||||
char const * name() const BOOST_NOEXCEPT
|
||||
{
|
||||
return "http";
|
||||
}
|
||||
|
||||
std::string message( int ev ) const
|
||||
{
|
||||
char buffer[ 32 ];
|
||||
|
||||
std::sprintf( buffer, "HTTP/1.0 %d", ev );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool failed( int ev ) const BOOST_NOEXCEPT
|
||||
{
|
||||
return !( ev >= 200 && ev < 300 );
|
||||
}
|
||||
};
|
||||
|
||||
error_category const & http_category()
|
||||
{
|
||||
static const http_category_impl instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
#define TEST_NOT_FAILED(ec) BOOST_TEST( !ec.failed() ); BOOST_TEST( ec? false: true ); BOOST_TEST( !ec );
|
||||
#define TEST_FAILED(ec) BOOST_TEST( ec.failed() ); BOOST_TEST( ec ); BOOST_TEST( !!ec );
|
||||
|
||||
template<class Ec> void test()
|
||||
{
|
||||
{
|
||||
Ec ec;
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec.assign( 1, generic_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec.clear();
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec = Ec( 1, generic_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec = Ec();
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
Ec ec;
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec.assign( 1, system_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec.clear();
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec = Ec( 1, system_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec = Ec();
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
Ec ec( 0, generic_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec.assign( 1, system_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec = Ec( 0, system_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
Ec ec( 1, generic_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec.assign( 0, system_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
Ec ec( 0, system_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec.assign( 1, generic_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec = Ec( 0, generic_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
Ec ec( 1, system_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec.assign( 0, generic_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
Ec ec( 0, http_category() );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec.assign( 200, http_category() );
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec = Ec( 404, http_category() );
|
||||
TEST_FAILED( ec );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( !generic_category().failed( 0 ) );
|
||||
BOOST_TEST( generic_category().failed( 7 ) );
|
||||
|
||||
BOOST_TEST( !system_category().failed( 0 ) );
|
||||
BOOST_TEST( system_category().failed( 7 ) );
|
||||
|
||||
BOOST_TEST( http_category().failed( 0 ) );
|
||||
BOOST_TEST( !http_category().failed( 200 ) );
|
||||
BOOST_TEST( http_category().failed( 404 ) );
|
||||
|
||||
test<error_code>();
|
||||
test<error_condition>();
|
||||
|
||||
{
|
||||
error_condition ec( errc::success );
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec = errc::address_family_not_supported;
|
||||
TEST_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
error_condition ec( errc::address_family_not_supported );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec = errc::success;
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
error_code ec( make_error_code( errc::success ) );
|
||||
TEST_NOT_FAILED( ec );
|
||||
|
||||
ec = make_error_code( errc::address_family_not_supported );
|
||||
TEST_FAILED( ec );
|
||||
}
|
||||
|
||||
{
|
||||
error_code ec( make_error_code( errc::address_family_not_supported ) );
|
||||
TEST_FAILED( ec );
|
||||
|
||||
ec = make_error_code( errc::success );
|
||||
TEST_NOT_FAILED( ec );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
41
test/generic_category_test.cpp
Normal file
41
test/generic_category_test.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
// 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
|
||||
|
||||
// Avoid spurious VC++ warnings
|
||||
# define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <cstring>
|
||||
|
||||
//
|
||||
|
||||
namespace sys = boost::system;
|
||||
|
||||
int main()
|
||||
{
|
||||
sys::error_category const & cat = sys::generic_category();
|
||||
|
||||
// message
|
||||
|
||||
for( int i = -2; i < 1024; ++i )
|
||||
{
|
||||
{
|
||||
BOOST_TEST_CSTR_EQ( cat.message( i ).c_str(), std::strerror( i ) );
|
||||
}
|
||||
|
||||
{
|
||||
char buffer[ 256 ];
|
||||
BOOST_TEST_CSTR_EQ( cat.message( i, buffer, sizeof( buffer ) ), std::strerror( i ) );
|
||||
}
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
116
test/system_category_test.cpp
Normal file
116
test/system_category_test.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
|
||||
// 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
|
||||
|
||||
// Avoid spurious VC++ warnings
|
||||
# define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
|
||||
//
|
||||
|
||||
#if defined(BOOST_WINDOWS_API)
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
std::string sys_strerror( int ev )
|
||||
{
|
||||
void * lpMsgBuf = 0;
|
||||
|
||||
DWORD retval = FormatMessageA(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL,
|
||||
ev,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPSTR) &lpMsgBuf,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
|
||||
struct local_free
|
||||
{
|
||||
void * p_;
|
||||
|
||||
~local_free()
|
||||
{
|
||||
LocalFree( p_ );
|
||||
}
|
||||
};
|
||||
|
||||
local_free lf_ = { lpMsgBuf };
|
||||
|
||||
if( retval == 0 )
|
||||
{
|
||||
char buffer[ 38 ];
|
||||
|
||||
std::sprintf( buffer, "Unknown error (%d)", ev );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string str( static_cast<char const*>( lpMsgBuf ) );
|
||||
|
||||
while( !str.empty() && (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
|
||||
{
|
||||
str.erase( str.size()-1 );
|
||||
}
|
||||
|
||||
if( !str.empty() && str[str.size()-1] == '.' )
|
||||
{
|
||||
str.erase( str.size()-1 );
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
std::string sys_strerror( int ev )
|
||||
{
|
||||
return std::strerror( ev );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//
|
||||
|
||||
namespace sys = boost::system;
|
||||
|
||||
static void test_message( sys::error_category const & cat, int ev )
|
||||
{
|
||||
BOOST_TEST_EQ( cat.message( ev ), sys_strerror( ev ) );
|
||||
|
||||
char buffer[ 2048 ]; // yes, really
|
||||
BOOST_TEST_CSTR_EQ( cat.message( ev, buffer, sizeof( buffer ) ), sys_strerror( ev ).c_str() );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
sys::error_category const & cat = sys::system_category();
|
||||
|
||||
// message
|
||||
|
||||
for( int i = -2; i < 1024; ++i )
|
||||
{
|
||||
test_message( cat, i );
|
||||
}
|
||||
|
||||
test_message( cat, 5810 );
|
||||
|
||||
for( int i = 10000; i < 11032; ++i )
|
||||
{
|
||||
test_message( cat, i );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user