byteswapping: move declaration into source file
This commit is contained in:
@@ -1,26 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#else
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
template <typename T>
|
bool IsSystemBigEndian();
|
||||||
void SwapBytes(T& value) {
|
|
||||||
char* ptr = reinterpret_cast<char*>(&value);
|
|
||||||
std::reverse(ptr, ptr + sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool IsSystemBigEndian() {
|
|
||||||
static constexpr std::uint16_t test = 10;
|
|
||||||
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
|
|
||||||
return isBigEndian;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Serialize value to (network byte order) big endian
|
* \brief Serialize value to (network byte order) big endian
|
||||||
@@ -29,21 +13,13 @@ template <typename T>
|
|||||||
void ToNetwork(T& value) {}
|
void ToNetwork(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
void ToNetwork<std::uint16_t>(std::uint16_t& value);
|
||||||
value = htons(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
void ToNetwork<std::uint32_t>(std::uint32_t& value);
|
||||||
value = htonl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
void ToNetwork<std::uint64_t>(std::uint64_t& value);
|
||||||
if (IsSystemBigEndian())
|
|
||||||
return;
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Deserialize value from (network byte order) big endian
|
* \brief Deserialize value from (network byte order) big endian
|
||||||
@@ -52,21 +28,13 @@ template <typename T>
|
|||||||
void FromNetwork(T& value) {}
|
void FromNetwork(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
|
void FromNetwork<std::uint16_t>(std::uint16_t& value);
|
||||||
value = ntohs(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
|
void FromNetwork<std::uint32_t>(std::uint32_t& value);
|
||||||
value = ntohl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
|
void FromNetwork<std::uint64_t>(std::uint64_t& value);
|
||||||
if (IsSystemBigEndian())
|
|
||||||
return;
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Swap bytes if the value is any kind of integer
|
* \brief Swap bytes if the value is any kind of integer
|
||||||
@@ -75,18 +43,12 @@ template <typename T>
|
|||||||
void TrySwapBytes(T& value) {}
|
void TrySwapBytes(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
|
void TrySwapBytes<std::uint16_t>(std::uint16_t& value);
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
|
void TrySwapBytes<std::uint32_t>(std::uint32_t& value);
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
|
void TrySwapBytes<std::uint64_t>(std::uint64_t& value);
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
74
src/sp/common/ByteSwapping.cpp
Normal file
74
src/sp/common/ByteSwapping.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
#include <sp/common/ByteSwapping.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void SwapBytes(T& value) {
|
||||||
|
char* ptr = reinterpret_cast<char*>(&value);
|
||||||
|
std::reverse(ptr, ptr + sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsSystemBigEndian() {
|
||||||
|
static constexpr std::uint16_t test = 10;
|
||||||
|
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
|
||||||
|
return isBigEndian;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
value = htons(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
value = htonl(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
if (IsSystemBigEndian())
|
||||||
|
return;
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
value = ntohs(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
value = ntohl(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
if (IsSystemBigEndian())
|
||||||
|
return;
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sp
|
||||||
Reference in New Issue
Block a user