diff --git a/include/sp/common/ByteSwapping.h b/include/sp/common/ByteSwapping.h index f8754c7..907c215 100644 --- a/include/sp/common/ByteSwapping.h +++ b/include/sp/common/ByteSwapping.h @@ -1,26 +1,10 @@ #pragma once -#ifdef _WIN32 -#include -#else -#include -#endif - #include namespace sp { -template -void SwapBytes(T& value) { - char* ptr = reinterpret_cast(&value); - std::reverse(ptr, ptr + sizeof(T)); -} - -static bool IsSystemBigEndian() { - static constexpr std::uint16_t test = 10; - static const bool isBigEndian = reinterpret_cast(&test)[1] == 10; - return isBigEndian; -} +bool IsSystemBigEndian(); /** * \brief Serialize value to (network byte order) big endian @@ -29,21 +13,13 @@ template void ToNetwork(T& value) {} template <> -void ToNetwork(std::uint16_t& value) { - value = htons(value); -} +void ToNetwork(std::uint16_t& value); template <> -void ToNetwork(std::uint32_t& value) { - value = htonl(value); -} +void ToNetwork(std::uint32_t& value); template <> -void ToNetwork(std::uint64_t& value) { - if (IsSystemBigEndian()) - return; - SwapBytes(value); -} +void ToNetwork(std::uint64_t& value); /** * \brief Deserialize value from (network byte order) big endian @@ -52,21 +28,13 @@ template void FromNetwork(T& value) {} template <> -void FromNetwork(std::uint16_t& value) { - value = ntohs(value); -} +void FromNetwork(std::uint16_t& value); template <> -void FromNetwork(std::uint32_t& value) { - value = ntohl(value); -} +void FromNetwork(std::uint32_t& value); template <> -void FromNetwork(std::uint64_t& value) { - if (IsSystemBigEndian()) - return; - SwapBytes(value); -} +void FromNetwork(std::uint64_t& value); /** * \brief Swap bytes if the value is any kind of integer @@ -75,18 +43,12 @@ template void TrySwapBytes(T& value) {} template <> -void TrySwapBytes(std::uint16_t& value) { - SwapBytes(value); -} +void TrySwapBytes(std::uint16_t& value); template <> -void TrySwapBytes(std::uint32_t& value) { - SwapBytes(value); -} +void TrySwapBytes(std::uint32_t& value); template <> -void TrySwapBytes(std::uint64_t& value) { - SwapBytes(value); -} +void TrySwapBytes(std::uint64_t& value); } // namespace sp diff --git a/src/sp/common/ByteSwapping.cpp b/src/sp/common/ByteSwapping.cpp new file mode 100644 index 0000000..b6f15a2 --- /dev/null +++ b/src/sp/common/ByteSwapping.cpp @@ -0,0 +1,74 @@ +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +#include + +namespace sp { + +template +void SwapBytes(T& value) { + char* ptr = reinterpret_cast(&value); + std::reverse(ptr, ptr + sizeof(T)); +} + +bool IsSystemBigEndian() { + static constexpr std::uint16_t test = 10; + static const bool isBigEndian = reinterpret_cast(&test)[1] == 10; + return isBigEndian; +} + +template <> +void ToNetwork(std::uint16_t& value) { + value = htons(value); +} + +template <> +void ToNetwork(std::uint32_t& value) { + value = htonl(value); +} + +template <> +void ToNetwork(std::uint64_t& value) { + if (IsSystemBigEndian()) + return; + SwapBytes(value); +} + +template <> +void FromNetwork(std::uint16_t& value) { + value = ntohs(value); +} + +template <> +void FromNetwork(std::uint32_t& value) { + value = ntohl(value); +} + +template <> +void FromNetwork(std::uint64_t& value) { + if (IsSystemBigEndian()) + return; + SwapBytes(value); +} + +template <> +void TrySwapBytes(std::uint16_t& value) { + SwapBytes(value); +} + +template <> +void TrySwapBytes(std::uint32_t& value) { + SwapBytes(value); +} + +template <> +void TrySwapBytes(std::uint64_t& value) { + SwapBytes(value); +} + +} // namespace sp