From f2cfa3e1c4e1e30b3f221b35d6efee02cd3b8d86 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Mon, 13 Jun 2022 21:25:27 +0200
Subject: [PATCH] net/SocketError: add SocketErrorCategory()

---
 src/net/SocketError.hxx | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/net/SocketError.hxx b/src/net/SocketError.hxx
index 5c7ab6949..cca051c8e 100644
--- a/src/net/SocketError.hxx
+++ b/src/net/SocketError.hxx
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015-2021 Max Kellermann <max.kellermann@gmail.com>
+ * Copyright 2015-2022 Max Kellermann <max.kellermann@gmail.com>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -27,8 +27,7 @@
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef SOCKET_ERROR_HXX
-#define SOCKET_ERROR_HXX
+#pragma once
 
 #include "system/Error.hxx"
 
@@ -168,6 +167,20 @@ public:
 	}
 };
 
+/**
+ * Returns the error_category to be used to wrap socket errors.
+ */
+[[gnu::const]]
+static inline const std::error_category &
+SocketErrorCategory() noexcept
+{
+#ifdef _WIN32
+	return LastErrorCategory();
+#else
+	return ErrnoCategory();
+#endif
+}
+
 [[gnu::pure]]
 static inline auto
 MakeSocketError(socket_error_t code, const char *msg) noexcept
@@ -185,5 +198,3 @@ MakeSocketError(const char *msg) noexcept
 {
 	return MakeSocketError(GetSocketError(), msg);
 }
-
-#endif