-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastream.h
227 lines (192 loc) · 5.74 KB
/
datastream.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#include <string>
#include <vector>
#include <type_traits>
#include <assert.h>
#ifdef __GNUC__
#include <byteswap.h>
#endif
#ifdef MSVC
#include <stdlib.h>
#endif
#include "dis6_global.h"
namespace DIS6
{
enum ByteOrder {
BigEndian,
LittleEndian
};
template <typename...>
struct ForceIgnoreStaticAssertUntilCalled : std::false_type
{
};
constexpr ByteOrder systemByteOrder()
{
union {
uint16_t shortData;
uint8_t byteData;
} num {1};
return num.byteData ? LittleEndian : BigEndian;
}
class DIS6_EXPORT DataStream
{
public:
DataStream();
DataStream(const std::vector<unsigned char>& data);
DataStream(unsigned char* data, size_t size);
~DataStream();
void resetPosition();
bool setPosition(size_t pos);
size_t position() const;
size_t size() const;
std::vector<unsigned char> data() const;
void setData(const std::vector<unsigned char>& data);
void setData(unsigned char* data, size_t size);
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
bool append(const T& data)
{
auto copyData = data;
swapByteOrder(copyData);
writeData(copyData);
return m_errorState;
}
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
bool read(T& data)
{
readData(data);
swapByteOrder(data);
return m_errorState;
}
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
bool operator<<(const T& data)
{
auto copyData = data;
swapByteOrder(copyData);
writeData(copyData);
return m_errorState;
}
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
bool operator>>(T& data)
{
readData(data);
swapByteOrder(data);
return m_errorState;
}
//for custom implementations
//req: Custom types must impelement << and >> operators with RHS being DataStream
//, LHS being T, and return type being bool
//example: bool operator>>(const EntityId& lhs, DataStream& rhs);
template <typename T>
auto append(const T& data) -> decltype(data >> *this)
{
return data >> *this;
}
template <typename T>
auto read(T& data) -> decltype(data << *this)
{
return data << *this;
}
template <typename T>
auto operator<<(const T& data) -> decltype(data >> *this)
{
return data >> *this;
}
template <typename T>
auto operator>>(T& data) -> decltype(data << *this)
{
return data << *this;
}
//catch all with error message.
//this is a neat trick. Signatures of "..." are guaranteed to be last priority for overload resolution
//we declare that our n args are of type T, and have a struct that requires n template args before it can be
//evaluated, but actually it just derives from std::false_type. This way the static_assert isn't evaluated until
//somebody actually calls it with at least one arg.
template <typename... T>
bool append(...)
{
//force evaluatoin and make always false.
static_assert(ForceIgnoreStaticAssertUntilCalled<T...>::value, "This type hasn't been impelmented by DataStream yet!");
return false;
}
ByteOrder byteOrder() const;
void setByteOrder(const ByteOrder &byteOrder);
bool errorState() const;
std::string error() const;
private:
//private for now - this might be stupid
DataStream(const DataStream& copy);
template <typename T, std::enable_if_t<std::is_same<int16_t, T>::value || std::is_same<uint16_t, T>::value>* = nullptr>
void swapByteOrder(T& data) const
{
if(systemByteOrder() == m_byteOrder)
return;
#ifdef __GNUC__
data = bswap_16(data);
#elif MSVC
data = _byteswap_ushort(data);
#endif
}
template <typename T, std::enable_if_t<std::is_same<int32_t, T>::value || std::is_same<uint32_t, T>::value || std::is_same<float, T>::value>* = nullptr>
void swapByteOrder(T& data) const
{
if(systemByteOrder() == m_byteOrder)
return;
#ifdef __GNUC__
data = bswap_32(data);
#elif MSVC
data = _byteswap_ulong(data);
#endif
}
template <typename T, std::enable_if_t<std::is_same<int64_t, T>::value || std::is_same<uint64_t, T>::value || std::is_same<double, T>::value>* = nullptr>
void swapByteOrder(T& data) const
{
if(systemByteOrder() == m_byteOrder)
return;
#ifdef __GNUC__
data = bswap_64(data);
#elif MSVC
data = _byteswap_uint64(data);
#endif
}
void swapByteOrder(...) const
{
//do nothing
}
template <typename T>
void writeData(const T& data)
{
//access data in same memory address in two different ways.
auto dataAddress = reinterpret_cast<const unsigned char*>( (&data) );
for(size_t i = 0; i < sizeof(T); ++i)
{
m_pos = m_data.insert(m_pos, (*dataAddress));
++dataAddress;
++m_pos;
}
}
template <typename T>
void readData(T& data)
{
auto dataAddress = reinterpret_cast<unsigned char*>( (&data) );
for(size_t i = 0; i < sizeof(T); ++i)
{
assert(m_pos != m_data.end());
if(m_pos == m_data.end())
{
//ERROR Overrun
m_error = "DataStream buffer overrun.";
m_errorState = true;
}
(*dataAddress) = (*m_pos);
++dataAddress;
++m_pos;
}
}
std::vector<unsigned char> m_data;
std::vector<unsigned char>::iterator m_pos;
ByteOrder m_byteOrder; //Default BigEndian
bool m_doSwap;
bool m_errorState;
std::string m_error;
};
}