-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautounion.hh
84 lines (64 loc) · 1.59 KB
/
autounion.hh
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
#ifndef __YAAL_TYPES__AUTOUNION__
#define __YAAL_TYPES__AUTOUNION__ 1
#include "../requirements.hh"
#ifdef __YAAL__
/* yaal/types/autounion.hh
*/
namespace yaal {
template<typename T, bool bigendian = false>
class autounion {
union data_u {
T value;
uint8_t bytes[sizeof(T)];
data_u(T const& t): value(t) {}
} data;
public:
autounion(): data(T()) {}
autounion(T const& t): data(t) {}
T value() const {
return data.value;
}
operator T () const {
return value();
}
T& value() {
return data.value;
}
operator T& () {
return value();
}
uint8_t& operator[] (uint8_t const i) {
if (bigendian)
return data.bytes[i];
else
return data.bytes[size - 1 - i];
}
uint8_t operator[] (uint8_t const i) const {
if (bigendian)
return data.bytes[i];
else
return data.bytes[size - 1 - i];
}
static const uint8_t size = sizeof(T);
};
}
#if 0
// Example usage.
void fancy(autounion<uint16_t> value) {
value -= 10;
std::cout << value << ":" << std::endl;
for (int i = 0; i < value.size; ++i) {
std::cout << (int)value[i] << std::endl;
}
}
int main() {
fancy(0x00ff);
std::cout << std::endl;
autounion<uint16_t, false> foo = 0x00ff;
for (int i = 0; i < foo.size; ++i) {
std::cout << (int)foo[i] << std::endl;
}
}
#endif
#endif
#endif