From 0368a1b39183a074ab24852db278e9667b4fb54c Mon Sep 17 00:00:00 2001 From: Ralph Sennhauser Date: Thu, 31 Jul 2025 19:19:06 +0200 Subject: [PATCH] Make requirements sufficient in StunClient While std::is_pod is required it is not sufficient so use std::is_integral as condition which is. Further replace the static_assert with a requires and use the new endian support to avoid use preprocessor. Signed-off-by: Ralph Sennhauser --- source/network/StunClient.cpp | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/source/network/StunClient.cpp b/source/network/StunClient.cpp index 9d3ce821d5..c2cc98d8ca 100644 --- a/source/network/StunClient.cpp +++ b/source/network/StunClient.cpp @@ -27,8 +27,10 @@ #include "ps/CStr.h" #include "ps/ConfigDB.h" +#include #include #include +#include #include #include #include @@ -82,43 +84,41 @@ ENetAddress m_StunServer; ENetAddress m_PublicAddress; /** - * Push POD data to a network-byte-order buffer. - * TODO: this should be optimised & moved to byte_order.h + * Push integral type to a network-byte-order buffer. */ -template +template void AddToBuffer(std::vector& buffer, const T value) { - static_assert(std::is_standard_layout_v && std::is_trivial_v, "T must be POD"); buffer.reserve(buffer.size() + n); // std::byte* can alias anything so this is legal. const std::byte* ptr = reinterpret_cast(&value); for (size_t a = 0; a < n; ++a) -#if BYTE_ORDER == LITTLE_ENDIAN - buffer.push_back(static_cast(*(ptr + n - 1 - a))); -#else - buffer.push_back(static_cast(*(ptr + a))); -#endif + { + if constexpr (std::endian::native == std::endian::little) + buffer.push_back(static_cast(*(ptr + n - 1 - a))); + else + buffer.push_back(static_cast(*(ptr + a))); + } } /** - * Read POD data from a network-byte-order buffer. - * TODO: this should be optimised & moved to byte_order.h + * Read integral type from a network-byte-order buffer. */ -template +template bool GetFromBuffer(const std::vector& buffer, u32& offset, T& result) { - static_assert(std::is_standard_layout_v && std::is_trivial_v, "T must be POD"); if (offset + n > buffer.size()) return false; // std::byte* can alias anything so this is legal. std::byte* ptr = reinterpret_cast(&result); for (size_t a = 0; a < n; ++a) -#if BYTE_ORDER == LITTLE_ENDIAN - *ptr++ = static_cast(buffer[offset + n - 1 - a]); -#else - *ptr++ = static_cast(buffer[offset + a]); -#endif + { + if constexpr (std::endian::native == std::endian::little) + *ptr++ = static_cast(buffer[offset + n - 1 - a]); + else + *ptr++ = static_cast(buffer[offset + a]); + } offset += n; return true;