-
Notifications
You must be signed in to change notification settings - Fork 13
/
test3.cpp
147 lines (129 loc) · 2.96 KB
/
test3.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//------------------------------- test3.cpp ------------------------------------
//
// This software is in the public domain. The only restriction on its use is
// that no one can remove it from the public domain by claiming ownership of it,
// including the original authors.
//
// There is no warranty of correctness on the software contained herein. Use
// at your own risk.
//
//------------------------------------------------------------------------------
#include "hash_append.h"
#include <cassert>
#include <iostream>
struct X
{
static constexpr xstd::endian desired_endian = xstd::endian::big;
};
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <type_traits>
#if 1
#ifdef htonl
#undef htonl
#undef htons
#undef ntohl
#undef ntohs
#endif
template <class T>
constexpr
inline
T
reverse_bytes(T t)
{
unsigned char* bytes = static_cast<unsigned char*>
(std::memmove(std::addressof(t), std::addressof(t), sizeof(T)));
for (unsigned i = 0; i < sizeof(T)/2; ++i)
std::swap(bytes[i], bytes[sizeof(T)-1-i]);
return t;
}
template <class T>
constexpr
inline
T
reverse_bytes_if(T t, std::true_type)
{
return reverse_bytes(t);
}
template <class T>
constexpr
inline
T
reverse_bytes_if(T t, std::false_type)
{
return t;
}
std::uint32_t
constexpr
inline
htonl(std::uint32_t x)
{
return reverse_bytes_if(x, std::integral_constant<bool,
xstd::endian::native == xstd::endian::little>{});
}
std::uint16_t
constexpr
inline
htons(std::uint16_t x)
{
return reverse_bytes_if(x, std::integral_constant<bool,
xstd::endian::native == xstd::endian::little>{});
}
std::uint32_t
constexpr
inline
ntohl(std::uint32_t x)
{
return reverse_bytes_if(x, std::integral_constant<bool,
xstd::endian::native == xstd::endian::little>{});
}
std::uint16_t
constexpr
inline
ntohs(std::uint16_t x)
{
return reverse_bytes_if(x, std::integral_constant<bool,
xstd::endian::native == xstd::endian::little>{});
}
template <class T,
class = std::enable_if_t<std::is_integral<T>{}>
>
constexpr
inline
T
hton(T t)
{
return reverse_bytes_if(t, std::integral_constant<bool,
xstd::endian::native == xstd::endian::little>{});
}
template <class T,
class = std::enable_if_t<std::is_integral<T>{}>
>
constexpr
inline
T
ntoh(T t)
{
return reverse_bytes_if(t, std::integral_constant<bool,
xstd::endian::native == xstd::endian::little>{});
}
#endif
int
test(int x)
{
return htonl(x);
}
// int
// main()
// {
// using namespace xstd;
// if (endian::native == endian::little)
// std::cout << "little endian\n";
// else if (endian::native == endian::big)
// std::cout << "little endian\n";
// else
// assert(false);
// if (X::desired_endian != endian::native)
// std::cout << "X needs work\n";
// }