net/SocketError: add SocketErrorCategory()

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

View File

@ -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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,8 +27,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef SOCKET_ERROR_HXX #pragma once
#define SOCKET_ERROR_HXX
#include "system/Error.hxx" #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]] [[gnu::pure]]
static inline auto static inline auto
MakeSocketError(socket_error_t code, const char *msg) noexcept MakeSocketError(socket_error_t code, const char *msg) noexcept
@ -185,5 +198,3 @@ MakeSocketError(const char *msg) noexcept
{ {
return MakeSocketError(GetSocketError(), msg); return MakeSocketError(GetSocketError(), msg);
} }
#endif