blob: 103f6fa04b2425a41794136f9da35e7630cf6b6d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#pragma once
#include "base/base.hpp"
#include "std/type_traits.hpp"
#include <cstddef>
// #define ENDIAN_IS_BIG
inline bool IsBigEndian()
{
#ifdef ENDIAN_IS_BIG
return true;
#else
return false;
#endif
}
template <typename T> T ReverseByteOrder(T t)
{
static_assert(is_integral<T>::value, "Only integral types are supported.");
T res;
char const * a = reinterpret_cast<char const *>(&t);
char * b = reinterpret_cast<char *>(&res);
for (size_t i = 0; i < sizeof(T); ++i)
b[i] = a[sizeof(T) - 1 - i];
return res;
}
template <typename T> inline T SwapIfBigEndian(T t)
{
#ifdef ENDIAN_IS_BIG
return ReverseByteOrder(t);
#else
return t;
#endif
}
|