refactor bitfield io
This commit is contained in:
@@ -1,49 +1,21 @@
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <winsock2.h>
|
||||
|
||||
#else
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <endian.h>
|
||||
|
||||
#define htonll htobe64
|
||||
#define ntohll be64toh
|
||||
|
||||
#endif
|
||||
#include <algorithm>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <>
|
||||
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||
value = htons(value);
|
||||
bool IsLittleEndian() {
|
||||
#ifdef SP_BIG_ENDIAN
|
||||
return false;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <>
|
||||
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||
value = htonl(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||
value = htonll(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) {
|
||||
value = ntohll(value);
|
||||
void SwapBytes(std::uint8_t* begin, std::uint8_t* end) {
|
||||
if (IsLittleEndian()) {
|
||||
std::reverse(begin, end);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
|
||||
Reference in New Issue
Block a user