mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 01:40:05 +00:00
AWIPS message byte swap cleanup
This commit is contained in:
parent
2a5068c4bb
commit
ade40806b6
1 changed files with 126 additions and 79 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cstring>
|
||||
#include <execution>
|
||||
#include <istream>
|
||||
|
|
@ -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<std::size_t _Size>
|
||||
static void SwapArray(std::array<float, _Size>& 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<std::uint32_t>(value >> 32));
|
||||
std::uint32_t low =
|
||||
ntohl(static_cast<std::uint32_t>(value & 0xFFFFFFFFULL));
|
||||
return (static_cast<std::uint64_t>(low) << 32) | high;
|
||||
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers)
|
||||
}
|
||||
else
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
template<std::size_t _Size>
|
||||
static void SwapArray(std::array<std::int16_t, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
template<std::size_t kSize>
|
||||
static void SwapArray(std::array<float, kSize>& 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<std::size_t _Size>
|
||||
static void SwapArray(std::array<std::uint16_t, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
template<std::size_t kSize>
|
||||
static void SwapArray(std::array<std::int16_t, kSize>& 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<std::size_t _Size>
|
||||
static void SwapArray(std::array<std::uint32_t, _Size>& arr,
|
||||
std::size_t size = _Size)
|
||||
template<std::size_t kSize>
|
||||
static void SwapArray(std::array<std::uint16_t, kSize>& 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<std::size_t kSize>
|
||||
static void SwapArray(std::array<std::uint32_t, kSize>& 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<typename T>
|
||||
static void SwapMap(std::map<T, float>& 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<typename T>
|
||||
static void SwapVector(std::vector<T>& v)
|
||||
{
|
||||
std::transform(std::execution::par_unseq,
|
||||
v.begin(),
|
||||
v.end(),
|
||||
v.begin(),
|
||||
[](T u)
|
||||
{
|
||||
if constexpr (std::is_same_v<T, std::uint16_t> ||
|
||||
std::is_same_v<T, std::int16_t>)
|
||||
{
|
||||
return static_cast<T>(ntohs(u));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::uint32_t> ||
|
||||
std::is_same_v<T, std::int32_t>)
|
||||
{
|
||||
return static_cast<T>(ntohl(u));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::uint64_t> ||
|
||||
std::is_same_v<T, std::int64_t>)
|
||||
{
|
||||
return static_cast<T>(ntohll(u));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, float>)
|
||||
{
|
||||
return SwapFloat(u);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, double>)
|
||||
{
|
||||
return SwapDouble(u);
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(std::is_same_v<T, void>,
|
||||
"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<T, std::uint16_t> ||
|
||||
std::is_same_v<T, std::int16_t>)
|
||||
{
|
||||
return static_cast<T>(ntohs(u));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::uint32_t> ||
|
||||
std::is_same_v<T, std::int32_t>)
|
||||
{
|
||||
return static_cast<T>(ntohl(u));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::uint64_t> ||
|
||||
std::is_same_v<T, std::int64_t>)
|
||||
{
|
||||
return static_cast<T>(Swap64(u));
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, float>)
|
||||
{
|
||||
return SwapFloat(u);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, double>)
|
||||
{
|
||||
return SwapDouble(u);
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(std::is_same_v<T, void>,
|
||||
"Unsupported type for SwapVector");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue