-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtEndian.h
211 lines (180 loc) · 8.75 KB
/
tEndian.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Endian Template by Terence J. Grant ([email protected])
Work with big endian and little endian types in C++ without ever calling a
conversion function.
The MIT License (MIT)
Copyright (c) 2012-03-09 Terence J. Grant ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions: The above copyright notice and this
permission notice shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cassert>
#include <cstdint>
#include <iostream>
//Both tLittleEndian and tBigEndian are manufactured primitives
//that are essentially immutable except for the assignment operators.
//All math logic happens outside of the class in the native format
//and an internal set/swap method is used to store the data.
template<typename T>
class tEndianBase
{
protected:
#if _MSC_VER
#pragma warning ( push )
#pragma warning ( disable : 4701 )
#pragma warning ( disable : 4789 )
#endif
static T Swap(const T& b)
{
T n;
switch(sizeof(T))
{
case 8: //64-bit
((uint8_t*)&n)[0] = ((uint8_t*)&b)[7];
((uint8_t*)&n)[1] = ((uint8_t*)&b)[6];
((uint8_t*)&n)[2] = ((uint8_t*)&b)[5];
((uint8_t*)&n)[3] = ((uint8_t*)&b)[4];
((uint8_t*)&n)[4] = ((uint8_t*)&b)[3];
((uint8_t*)&n)[5] = ((uint8_t*)&b)[2];
((uint8_t*)&n)[6] = ((uint8_t*)&b)[1];
((uint8_t*)&n)[7] = ((uint8_t*)&b)[0];
break;
case 4: //32-bit
((uint8_t*)&n)[0] = ((uint8_t*)&b)[3];
((uint8_t*)&n)[1] = ((uint8_t*)&b)[2];
((uint8_t*)&n)[2] = ((uint8_t*)&b)[1];
((uint8_t*)&n)[3] = ((uint8_t*)&b)[0];
break;
case 2: //16-bit
((uint8_t*)&n)[0] = ((uint8_t*)&b)[1];
((uint8_t*)&n)[1] = ((uint8_t*)&b)[0];
break;
default:
assert(0); //Endian swap is only defined for 2, 4, and 8-byte types
break;
}
return n;
}
#if _MSC_VER
#pragma warning ( pop )
#endif
};
template<typename T>
class tLittleEndian
: public tEndianBase<T>
{
protected:
T mData;
protected:
static T Transform(const T& b)
{
const uint16_t i = 1;
return (reinterpret_cast<const char&>(i) == 1) ? b : tEndianBase<T>::Swap(b);
}
public:
// Constructors
tLittleEndian() = default;
//If we endian swap, it happens in two places:
//1. Set an OE from a PE
//2. Get a PE from an OE
// Storage in
tLittleEndian(const T& b) : mData(Transform(b)) { }
template <typename U> explicit tLittleEndian(U const & b) : mData(Transform(T(b))) { }
// Storage out
template <typename U> operator U() const { return U(Transform(mData)); }
operator T() const { return Transform(mData); }
template <typename U> bool operator ==(U const& o) { return U(*this) == o; }
template <typename U> bool operator !=(U const& o) { return U(*this) != o; }
//Arithmetic assignment operators
tLittleEndian& operator ++() /* prefix */ { *this = T(*this) + T(1); return *this; }
tLittleEndian operator ++(int) /* suffix */ { tLittleEndian t(*this); *this = T(*this) + T(1); return t; }
tLittleEndian& operator --() /* prefix */ { *this = T(*this) - T(1); return *this; }
tLittleEndian operator --(int) /* suffix */ { tLittleEndian t(*this); *this = T(*this) - T(1); return t; }
//Compound assignment operators
tLittleEndian& operator +=(const T& b) { *this = T(*this) + b; return *this; }
tLittleEndian& operator -=(const T& b) { *this = T(*this) - b; return *this; }
tLittleEndian& operator *=(const T& b) { *this = T(*this) * b; return *this; }
tLittleEndian& operator /=(const T& b) { *this = T(*this) / b; return *this; }
tLittleEndian& operator %=(const T& b) { *this = T(*this) % b; return *this; }
tLittleEndian& operator &=(const T& b) { *this = T(*this) & b; return *this; }
tLittleEndian& operator |=(const T& b) { *this = T(*this) | b; return *this; }
tLittleEndian& operator ^=(const T& b) { *this = T(*this) ^ b; return *this; }
tLittleEndian& operator <<=(const T& b) { *this = T(T(*this) << b); return *this; }
tLittleEndian& operator >>=(const T& b) { *this = T(T(*this) >> b); return *this; }
friend std::ostream& operator <<(std::ostream &out, const tLittleEndian b) { out << T(b); return out; }
friend std::istream& operator >>(std::istream &in, tLittleEndian &b) { T val; in >> val; b = val; return in; }
};
template<typename T>
class tBigEndian
: public tEndianBase<T>
{
protected:
T mData;
protected:
static T Transform(const T& b)
{
const uint16_t i = 1;
return (reinterpret_cast<const char&>(i) == 1) ? tEndianBase<T>::Swap(b) : b;
}
public:
// Constructors
tBigEndian() = default;
//If we endian swap, it happens in two places:
//1. Set an OE from a PE
//2. Get a PE from an OE
// Storage in
tBigEndian(const T& b) : mData(Transform(b)) { }
template <typename U> explicit tBigEndian(U const & b) : mData(Transform(T(b))) { }
// Storage out
template <typename U> operator U() const { return U(Transform(mData)); }
operator T() const { return Transform(mData); }
template <typename U> bool operator ==(U const& o) { return U(*this) == o; }
template <typename U> bool operator !=(U const& o) { return U(*this) != o; }
//Arithmetic assignment operators
tBigEndian& operator ++() /* prefix */ { *this = T(*this) + 1; return *this; }
tBigEndian operator ++(int) /* suffix */ { tBigEndian t(*this); *this = T(*this) + 1; return t; }
tBigEndian& operator --() /* prefix */ { *this = T(*this) - 1; return *this; }
tBigEndian operator --(int) /* suffix */ { tBigEndian t(*this); *this = T(*this) - 1; return t; }
//Compound assignment operators
tBigEndian& operator +=(const T& b) { *this = T(*this) + b; return *this; }
tBigEndian& operator -=(const T& b) { *this = T(*this) - b; return *this; }
tBigEndian& operator *=(const T& b) { *this = T(*this) * b; return *this; }
tBigEndian& operator /=(const T& b) { *this = T(*this) / b; return *this; }
tBigEndian& operator %=(const T& b) { *this = T(*this) % b; return *this; }
tBigEndian& operator &=(const T& b) { *this = T(*this) & b; return *this; }
tBigEndian& operator |=(const T& b) { *this = T(*this) | b; return *this; }
tBigEndian& operator ^=(const T& b) { *this = T(*this) ^ b; return *this; }
tBigEndian& operator <<=(const T& b) { *this = T(T(*this) << b); return *this; }
tBigEndian& operator >>=(const T& b) { *this = T(T(*this) >> b); return *this; }
friend std::ostream& operator <<(std::ostream &out, const tBigEndian b) { out << T(b); return out; }
friend std::istream& operator >>(std::istream &in, tBigEndian &b) { T val; in >> val; b = val; return in; }
};
typedef tLittleEndian<int16_t> leint16;
typedef tLittleEndian<int32_t> leint32;
typedef tLittleEndian<int64_t> leint64;
typedef tLittleEndian<uint16_t> leuint16;
typedef tLittleEndian<uint32_t> leuint32;
typedef tLittleEndian<uint64_t> leuint64;
typedef tLittleEndian<float> lefloat32;
typedef tLittleEndian<double> lefloat64;
typedef tBigEndian<int16_t> beint16;
typedef tBigEndian<int32_t> beint32;
typedef tBigEndian<int64_t> beint64;
typedef tBigEndian<uint16_t> beuint16;
typedef tBigEndian<uint32_t> beuint32;
typedef tBigEndian<uint64_t> beuint64;
typedef tBigEndian<float> befloat32;
typedef tBigEndian<double> befloat64;