-
Notifications
You must be signed in to change notification settings - Fork 0
/
StdInt.hpp
219 lines (175 loc) · 5.26 KB
/
StdInt.hpp
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
212
213
214
215
216
217
218
// handy big/little endian integers
// they can be treated as normal vars, but will be stored in memory in their
// asigned endianess
#pragma once
#include <iostream>
#include <stdint.h>
#include "endian.hpp"
namespace jdd{
// forwad declarations
class BigEndian;
class LilEndian;
template<class NativeType, class Encoding> union StdInt;
// Big Endian
typedef StdInt<uint8_t, BigEndian> uint8_be;
typedef StdInt<uint16_t, BigEndian> uint16_be;
typedef StdInt<uint32_t, BigEndian> uint32_be;
typedef StdInt<uint64_t, BigEndian> uint64_be;
typedef StdInt<int8_t, BigEndian> int8_be;
typedef StdInt<int16_t, BigEndian> int16_be;
typedef StdInt<int32_t, BigEndian> int32_be;
typedef StdInt<int64_t, BigEndian> int64_be;
// Little Endian
typedef StdInt<uint8_t, LilEndian> uint8_le;
typedef StdInt<uint16_t, LilEndian> uint16_le;
typedef StdInt<uint32_t, LilEndian> uint32_le;
typedef StdInt<uint64_t, LilEndian> uint64_le;
typedef StdInt<int8_t, LilEndian> int8_le;
typedef StdInt<int16_t, LilEndian> int16_le;
typedef StdInt<int32_t, LilEndian> int32_le;
typedef StdInt<int64_t, LilEndian> int64_le;
//==============================================================================
template<class NativeType, class Encoding>
union StdInt
{
public:
// default constructor
StdInt()
{
}
// construct from native type
StdInt(const NativeType& rhv):
data(Encoding::encode(rhv))
{
}
// copy constructor
StdInt(const StdInt& rhv):
data(rhv.data)
{
}
// assignment from native type
NativeType operator = (const NativeType& rhv)
{
data = Encoding::encode(rhv);
return rhv;
}
// cast to native type
operator NativeType () const
{
return Encoding::decode(data);
}
// access individual bytes that makup the StdInt
uint8_t& byte(size_t i)
{
return buffer[i];
}
// access const ref to individual bytes that makup the StdInt
const uint8_t& byte(size_t i) const
{
return buffer[i];
}
// assignment from another StdInt
const StdInt& operator = (const StdInt& rhv)
{
data = rhv.data;
return rhv;
}
// bitwise not
StdInt operator ~ () const
{
StdInt rtn;
rtn.data = ~data;
return rtn;
}
// equal to
template<typename T, typename E> friend
bool operator == (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv);
// not equal too
template<typename T, typename E> friend
bool operator != (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv);
// bitwise or
template<typename T, typename E> friend
StdInt<T, E> operator | (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv);
// bitwise and
template<typename T, typename E> friend
StdInt<T, E> operator & (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv);
// bitwise xor
template<typename T, typename E> friend
StdInt<T, E> operator ^ (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv);
//TODO operator a &= b
//TODO operator a |= b
//TODO operator a ^= b
private:
NativeType data;
uint8_t buffer[];
};
//==============================================================================
// stream operators for StdInts
template<typename T1, typename T2>
std::istream& operator >> (std::istream &is, StdInt<T1, T2> &i)
{
is.read( (char*)&i, sizeof(i) );
return is;
}
template<typename T1, typename T2>
std::ostream& operator << (std::ostream &os, const StdInt<T1, T2> &i)
{
os.write( (const char*)&i, sizeof(i) );
return os;
}
//==============================================================================
// operator overloads for StdInts (these need to be defined as non member
// functions or else compiler has trouble deciding wether to use these or the
// bultin ones that are for the native int type that the StdInts can be cast
// too if doing something like "uint32_be(0) == 0")
template<typename T, typename E>
bool operator == (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv)
{
return lhv.data == rhv.data;
}
template<typename T, typename E>
bool operator != (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv)
{
return lhv.data != rhv.data;
}
template<typename T, typename E>
StdInt<T, E> operator | (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv)
{
StdInt<T, E> rtn;
rtn.data = lhv.data | rhv.data;
return rtn;
}
template<typename T, typename E>
StdInt<T, E> operator & (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv)
{
StdInt<T, E> rtn;
rtn.data = lhv.data & rhv.data;
return rtn;
}
template<typename T, typename E>
StdInt<T, E> operator ^ (const StdInt<T, E>& lhv, const StdInt<T, E>& rhv)
{
StdInt<T, E> rtn;
rtn.data = lhv.data ^ rhv.data;
return rtn;
}
//==============================================================================
//encodings
class BigEndian
{
public:
template<typename T>
static inline T encode(T t) {return htobe(t);}
template<typename T>
static inline T decode(T t) {return betoh(t);}
};
//------------------------------------------------------------------------------
class LilEndian
{
public:
template<typename T>
static inline T encode(T t) {return htole(t);}
template<typename T>
static inline T decode(T t) {return letoh(t);}
};
} // end namespace