system/Error: add IsLastError()

This commit is contained in:
Max Kellermann 2022-06-13 21:19:27 +02:00
parent 193d6a4fd4
commit 0c2c20254b

View File

@ -64,6 +64,14 @@ LastErrorCategory() noexcept
return std::system_category(); return std::system_category();
} }
[[gnu::pure]]
inline bool
IsLastError(const std::system_error &e, DWORD code) noexcept
{
return e.code().category() == LastErrorCategory() &&
(DWORD)e.code().value() == code;
}
static inline std::system_error static inline std::system_error
MakeLastError(DWORD code, const char *msg) noexcept MakeLastError(DWORD code, const char *msg) noexcept
{ {
@ -187,8 +195,7 @@ static inline bool
IsFileNotFound(const std::system_error &e) noexcept IsFileNotFound(const std::system_error &e) noexcept
{ {
#ifdef _WIN32 #ifdef _WIN32
return e.code().category() == LastErrorCategory() && return IsLastError(e, ERROR_FILE_NOT_FOUND);
e.code().value() == ERROR_FILE_NOT_FOUND;
#else #else
return IsErrno(e, ENOENT); return IsErrno(e, ENOENT);
#endif #endif
@ -199,8 +206,7 @@ static inline bool
IsPathNotFound(const std::system_error &e) noexcept IsPathNotFound(const std::system_error &e) noexcept
{ {
#ifdef _WIN32 #ifdef _WIN32
return e.code().category() == LastErrorCategory() && return IsLastError(e, ERROR_PATH_NOT_FOUND);
e.code().value() == ERROR_PATH_NOT_FOUND;
#else #else
return IsErrno(e, ENOTDIR); return IsErrno(e, ENOTDIR);
#endif #endif
@ -211,8 +217,7 @@ static inline bool
IsAccessDenied(const std::system_error &e) noexcept IsAccessDenied(const std::system_error &e) noexcept
{ {
#ifdef _WIN32 #ifdef _WIN32
return e.code().category() == LastErrorCategory() && return IsLastError(e, ERROR_ACCESS_DENIED);
e.code().value() == ERROR_ACCESS_DENIED;
#else #else
return IsErrno(e, EACCES); return IsErrno(e, EACCES);
#endif #endif