From 059955a48c69099c2eb768e2c38e61b88a6b4ab7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 31 May 2022 13:50:07 +0200 Subject: [PATCH] net/SocketAddress: add #ifdefs for std::span --- src/net/AllocatedSocketAddress.cxx | 1 - src/net/SocketAddress.cxx | 10 +++++++++- src/net/SocketAddress.hxx | 21 +++++++++------------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/net/AllocatedSocketAddress.cxx b/src/net/AllocatedSocketAddress.cxx index 3deb78909..d38f6ed4b 100644 --- a/src/net/AllocatedSocketAddress.cxx +++ b/src/net/AllocatedSocketAddress.cxx @@ -30,7 +30,6 @@ #include "AllocatedSocketAddress.hxx" #include "IPv4Address.hxx" #include "IPv6Address.hxx" -#include "util/StringView.hxx" #include diff --git a/src/net/SocketAddress.cxx b/src/net/SocketAddress.cxx index 6963c50b7..94ce4b8bb 100644 --- a/src/net/SocketAddress.cxx +++ b/src/net/SocketAddress.cxx @@ -131,6 +131,8 @@ SocketAddress::GetPort() const noexcept } } +#ifdef __cpp_lib_span + static std::span GetSteadyPart(const struct sockaddr_in &address) noexcept { @@ -149,7 +151,11 @@ GetSteadyPart(const struct sockaddr_in6 &address) noexcept }; } -#endif +#endif // __cpp_lib_span + +#endif // HAVE_TCP + +#ifdef __cpp_lib_span std::span SocketAddress::GetSteadyPart() const noexcept @@ -175,3 +181,5 @@ SocketAddress::GetSteadyPart() const noexcept return {}; } } + +#endif diff --git a/src/net/SocketAddress.hxx b/src/net/SocketAddress.hxx index 420d32939..f7a0d4e39 100644 --- a/src/net/SocketAddress.hxx +++ b/src/net/SocketAddress.hxx @@ -39,7 +39,14 @@ #endif #include + +#if __cplusplus >= 202002 || (defined(__GNUC__) && __GNUC__ >= 10) +#include +#endif + +#ifdef __cpp_lib_span #include +#endif #ifdef HAVE_UN #include @@ -72,10 +79,6 @@ public: size_type _size) noexcept :address(_address), size(_size) {} - explicit SocketAddress(std::span src) noexcept - :address((const struct sockaddr *)(const void *)src.data()), - size(src.size()) {} - static constexpr SocketAddress Null() noexcept { return nullptr; } @@ -161,14 +164,7 @@ public: unsigned GetPort() const noexcept; #endif - operator std::span() const noexcept { - const void *q = reinterpret_cast(address); - return { - (const std::byte *)q, - (std::size_t)size, - }; - } - +#ifdef __cpp_lib_span /** * Return a buffer pointing to the "steady" portion of the * address, i.e. without volatile parts like the port number. @@ -178,6 +174,7 @@ public: */ [[gnu::pure]] std::span GetSteadyPart() const noexcept; +#endif [[gnu::pure]] bool operator==(const SocketAddress other) const noexcept;