From ade40806b660cebdf9eae2bceeea4414528b31aa Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sat, 17 May 2025 15:38:26 -0500 Subject: [PATCH] AWIPS message byte swap cleanup --- wxdata/include/scwx/awips/message.hpp | 205 ++++++++++++++++---------- 1 file changed, 126 insertions(+), 79 deletions(-) diff --git a/wxdata/include/scwx/awips/message.hpp b/wxdata/include/scwx/awips/message.hpp index 486e7f06..12ec226c 100644 --- a/wxdata/include/scwx/awips/message.hpp +++ b/wxdata/include/scwx/awips/message.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -52,113 +53,159 @@ public: static float SwapFloat(float f) { - std::uint32_t temp; - std::memcpy(&temp, &f, sizeof(std::uint32_t)); - temp = ntohl(temp); - std::memcpy(&f, &temp, sizeof(float)); + if constexpr (std::endian::native == std::endian::little) + { + // Variable is initialized by memcpy + // NOLINTNEXTLINE(cppcoreguidelines-init-variables) + std::uint32_t temp; + std::memcpy(&temp, &f, sizeof(std::uint32_t)); + temp = ntohl(temp); + std::memcpy(&f, &temp, sizeof(float)); + } return f; } static double SwapDouble(double d) { - std::uint64_t temp; - std::memcpy(&temp, &d, sizeof(std::uint64_t)); - temp = ntohll(temp); - std::memcpy(&d, &temp, sizeof(float)); + if constexpr (std::endian::native == std::endian::little) + { + // Variable is initialized by memcpy + // NOLINTNEXTLINE(cppcoreguidelines-init-variables) + std::uint64_t temp; + std::memcpy(&temp, &d, sizeof(std::uint64_t)); + temp = Swap64(temp); + std::memcpy(&d, &temp, sizeof(float)); + } return d; } - template - static void SwapArray(std::array& arr, - std::size_t size = _Size) + static std::uint64_t Swap64(std::uint64_t value) { - std::transform(std::execution::par_unseq, - arr.begin(), - arr.begin() + size, - arr.begin(), - [](float f) { return SwapFloat(f); }); + if constexpr (std::endian::native == std::endian::little) + { + // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers) + std::uint32_t high = ntohl(static_cast(value >> 32)); + std::uint32_t low = + ntohl(static_cast(value & 0xFFFFFFFFULL)); + return (static_cast(low) << 32) | high; + // NOLINTEND(cppcoreguidelines-avoid-magic-numbers) + } + else + { + return value; + } } - template - static void SwapArray(std::array& arr, - std::size_t size = _Size) + template + static void SwapArray(std::array& arr, + std::size_t size = kSize) { - std::transform(std::execution::par_unseq, - arr.begin(), - arr.begin() + size, - arr.begin(), - [](std::int16_t u) { return ntohs(u); }); + if constexpr (std::endian::native == std::endian::little) + { + std::transform(std::execution::par_unseq, + arr.begin(), + arr.begin() + size, + arr.begin(), + [](float f) { return SwapFloat(f); }); + } } - template - static void SwapArray(std::array& arr, - std::size_t size = _Size) + template + static void SwapArray(std::array& arr, + std::size_t size = kSize) { - std::transform(std::execution::par_unseq, - arr.begin(), - arr.begin() + size, - arr.begin(), - [](std::uint16_t u) { return ntohs(u); }); + if constexpr (std::endian::native == std::endian::little) + { + std::transform(std::execution::par_unseq, + arr.begin(), + arr.begin() + size, + arr.begin(), + [](std::int16_t u) { return ntohs(u); }); + } } - template - static void SwapArray(std::array& arr, - std::size_t size = _Size) + template + static void SwapArray(std::array& arr, + std::size_t size = kSize) { - std::transform(std::execution::par_unseq, - arr.begin(), - arr.begin() + size, - arr.begin(), - [](std::uint32_t u) { return ntohl(u); }); + if constexpr (std::endian::native == std::endian::little) + { + std::transform(std::execution::par_unseq, + arr.begin(), + arr.begin() + size, + arr.begin(), + [](std::uint16_t u) { return ntohs(u); }); + } + } + + template + static void SwapArray(std::array& arr, + std::size_t size = kSize) + { + if constexpr (std::endian::native == std::endian::little) + { + std::transform(std::execution::par_unseq, + arr.begin(), + arr.begin() + size, + arr.begin(), + [](std::uint32_t u) { return ntohl(u); }); + } } template static void SwapMap(std::map& m) { - std::for_each(std::execution::par_unseq, - m.begin(), - m.end(), - [](auto& p) { p.second = SwapFloat(p.second); }); + if constexpr (std::endian::native == std::endian::little) + { + std::for_each(std::execution::par_unseq, + m.begin(), + m.end(), + [](auto& p) { p.second = SwapFloat(p.second); }); + } } template static void SwapVector(std::vector& v) { - std::transform(std::execution::par_unseq, - v.begin(), - v.end(), - v.begin(), - [](T u) - { - if constexpr (std::is_same_v || - std::is_same_v) - { - return static_cast(ntohs(u)); - } - else if constexpr (std::is_same_v || - std::is_same_v) - { - return static_cast(ntohl(u)); - } - else if constexpr (std::is_same_v || - std::is_same_v) - { - return static_cast(ntohll(u)); - } - else if constexpr (std::is_same_v) - { - return SwapFloat(u); - } - else if constexpr (std::is_same_v) - { - return SwapDouble(u); - } - else - { - static_assert(std::is_same_v, - "Unsupported type for SwapVector"); - } - }); + if constexpr (std::endian::native == std::endian::little) + { + std::transform( + std::execution::par_unseq, + v.begin(), + v.end(), + v.begin(), + [](T u) + { + if constexpr (std::is_same_v || + std::is_same_v) + { + return static_cast(ntohs(u)); + } + else if constexpr (std::is_same_v || + std::is_same_v) + { + return static_cast(ntohl(u)); + } + else if constexpr (std::is_same_v || + std::is_same_v) + { + return static_cast(Swap64(u)); + } + else if constexpr (std::is_same_v) + { + return SwapFloat(u); + } + else if constexpr (std::is_same_v) + { + return SwapDouble(u); + } + else + { + static_assert(std::is_same_v, + "Unsupported type for SwapVector"); + } + }); + } } private: