From 0c2c20254bf1f409c39522681ee001fcc9c54764 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Mon, 13 Jun 2022 21:19:27 +0200
Subject: [PATCH] system/Error: add IsLastError()

---
 src/system/Error.hxx | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/system/Error.hxx b/src/system/Error.hxx
index e58b1bd94..d629e0a08 100644
--- a/src/system/Error.hxx
+++ b/src/system/Error.hxx
@@ -64,6 +64,14 @@ LastErrorCategory() noexcept
 	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
 MakeLastError(DWORD code, const char *msg) noexcept
 {
@@ -187,8 +195,7 @@ static inline bool
 IsFileNotFound(const std::system_error &e) noexcept
 {
 #ifdef _WIN32
-	return e.code().category() == LastErrorCategory() &&
-		e.code().value() == ERROR_FILE_NOT_FOUND;
+	return IsLastError(e, ERROR_FILE_NOT_FOUND);
 #else
 	return IsErrno(e, ENOENT);
 #endif
@@ -199,8 +206,7 @@ static inline bool
 IsPathNotFound(const std::system_error &e) noexcept
 {
 #ifdef _WIN32
-	return e.code().category() == LastErrorCategory() &&
-		e.code().value() == ERROR_PATH_NOT_FOUND;
+	return IsLastError(e, ERROR_PATH_NOT_FOUND);
 #else
 	return IsErrno(e, ENOTDIR);
 #endif
@@ -211,8 +217,7 @@ static inline bool
 IsAccessDenied(const std::system_error &e) noexcept
 {
 #ifdef _WIN32
-	return e.code().category() == LastErrorCategory() &&
-		e.code().value() == ERROR_ACCESS_DENIED;
+	return IsLastError(e, ERROR_ACCESS_DENIED);
 #else
 	return IsErrno(e, EACCES);
 #endif