-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathxregStreams.h
374 lines (317 loc) · 11.5 KB
/
xregStreams.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* MIT License
*
* Copyright (c) 2020 Robert Grupp
*
* 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.
*/
/**
* @file
* @brief Utilities for handling streams of data.
**/
#ifndef XREGSTREAMS_H_
#define XREGSTREAMS_H_
#include <cstddef>
#include <string>
#include <vector>
#include <type_traits>
#include "xregEndianUtils.h"
#include "xregSizedTypeUtils.h"
#include "xregObjectUtils.h"
namespace xreg
{
class Stream
{
public:
using size_type = size_t;
using int8 = sized_types::int8;
using int16 = sized_types::int16;
using int32 = sized_types::int32;
using uint8 = sized_types::uint8;
using uint16 = sized_types::uint16;
using uint32 = sized_types::uint32;
using int64 = sized_types::int64;
using uint64 = sized_types::uint64;
using float32 = sized_types::float32;
using float64 = sized_types::float64;
Stream() = default;
Stream(const Stream&) = delete;
Stream& operator=(const Stream&) = delete;
ByteOrder byte_order() const
{
return byte_order_;
}
void set_byte_order(const ByteOrder bo)
{
byte_order_ = bo;
}
void switch_byte_order();
protected:
bool need_to_swap() const;
ByteOrder byte_order_ = kNATIVE_ENDIAN;
size_type num_bytes_ = 0;
private:
static_assert(sizeof(int8) == 1, "8-bit signed int wrong size!");
static_assert(sizeof(uint8) == 1, "8-bit unsigned int wrong size!");
static_assert(sizeof(int16) == 2, "16-bit signed int wrong size!");
static_assert(sizeof(uint16) == 2, "16-bit unsigned int wrong size!");
static_assert(sizeof(int32) == 4, "32-bit signed int wrong size!");
static_assert(sizeof(uint32) == 4, "32-bit unsigned int wrong size!");
static_assert(sizeof(int64) == 8, "64-bit signed int wrong size!");
static_assert(sizeof(uint64) == 8, "64-bit unsigned int wrong size!");
static_assert(sizeof(float32) == 4, "32 bit float wrong size!");
static_assert(sizeof(float64) == 8, "64 bit float wrong size!");
};
template <class T>
struct IsBasicSerializableType
{
enum { value = 0 };
};
#define XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(T) \
template <> \
struct IsBasicSerializableType<T> \
{ \
enum { value = 1 }; \
}
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::uint8);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::int8);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::uint16);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::int16);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::uint32);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::int32);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::uint64);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::int64);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::float32);
XREG_MAKE_BASIC_SERIALIZABLE_TRAIT(Stream::float64);
#undef XREG_MAKE_BASIC_SERIALIZABLE_TRAIT
template <class T>
struct IsSerializable
{
enum { value = 0 };
};
#define XREG_MAKE_SERIALIZABLE_TRAIT(T) \
template <> \
struct IsSerializable<T> \
{ \
enum { value = 1 }; \
}
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::uint8);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::int8);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::uint16);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::int16);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::uint32);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::int32);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::uint64);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::int64);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::float32);
XREG_MAKE_SERIALIZABLE_TRAIT(Stream::float64);
namespace detail
{
template <class T>
void ByteSwapArray(T* arr, const Stream::size_type len)
{
for (Stream::size_type i = 0; i < len; ++i)
{
SwapByteOrder(arr + i);
}
}
// For 1-byte types this is a noop
inline void ByteSwapArray(Stream::int8*, const Stream::size_type)
{ }
inline void ByteSwapArray(Stream::uint8*, const Stream::size_type)
{ }
} // detail
class InputStream : public Stream
{
public:
virtual ~InputStream() { }
void read(Stream::int8* x, const size_type len = 1);
void read(Stream::int16* x, const size_type len = 1);
void read(Stream::int32* x, const size_type len = 1);
void read(Stream::int64* x, const size_type len = 1);
void read(Stream::uint8* x, const size_type len = 1);
void read(Stream::uint16* x, const size_type len = 1);
void read(Stream::uint32* x, const size_type len = 1);
void read(Stream::uint64* x, const size_type len = 1);
void read(Stream::float32* x, const size_type len = 1);
void read(Stream::float64* x, const size_type len = 1);
InputStream& operator>>(Stream::int8& x);
InputStream& operator>>(Stream::int16& x);
InputStream& operator>>(Stream::int32& x);
InputStream& operator>>(Stream::int64& x);
InputStream& operator>>(Stream::uint8& x);
InputStream& operator>>(Stream::uint16& x);
InputStream& operator>>(Stream::uint32& x);
InputStream& operator>>(Stream::uint64& x);
InputStream& operator>>(Stream::float32& x);
InputStream& operator>>(Stream::float64& x);
/// \brief Reads bytes, and treats as ascii characters, until
/// a UNIX new line character is reached.
///
/// This does not attempt to deal with Windows style new lines.
/// This is not efficient, and should only be used when minimal
/// text processing is needed on a stream.
std::string read_ascii_line();
/// \brief Reads bytes, and treats as ascii characters, returning
/// the next white-space delimited token.
///
/// This is not efficient, and should only be used when minimal
/// text processing is needed on a stream.
std::string read_ascii_token();
/**
* @brief Reads a buffer and casts/copies it to a
* buffer of different type.
*
* This is meant to be called in the following manner:
* <code>
int x[10];
in.read_and_cast<int8>(x, 10);
</code>
* In the above example, the stream has data written to
* it in 8-bit integers, but the user wishes to store
* the data in an array of int's (which is most likely
* and array of int32's).
* @param ptr The final buffer to be populated, does not
* need to be a serializable type, but a
* conversion must be well-defined.
* @param len The length of the final buffer and number
* of items to be read from the stream
**/
template <class U, class T>
void read_and_cast(T* ptr, const size_type len = 1)
{
static_assert(IsBasicSerializableType<U>::value, "type must be serializable");
if (std::is_same<T,U>::value)
{
read(ptr, len);
}
else
{
// really inefficient for small lengths, but I'm anticipating
// using this with large buffers, so let's roll with it for now
// TODO: implement with 2 branches, one with a static buffer,
// the other with a dynamic buffer
std::vector<U> tmp_buf(len);
read(&tmp_buf[0], len);
CopyObjectArray(ptr, &tmp_buf[0], len);
}
}
size_type num_bytes_read() const;
/// \brief Reads in bytes and discards them; to advance the stream.
///
/// Stream objects with more knowledge about the stream source may implement
/// this more efficiently (e.g. a file input stream, can seek through the file)
virtual void skip(const size_type num_bytes);
private:
template <class T>
void read_helper(T* ptr, const size_type len)
{
const size_type nbytes = len * sizeof(T);
raw_read(ptr, nbytes);
num_bytes_ += nbytes;
if (need_to_swap())
{
detail::ByteSwapArray(ptr, len);
}
}
virtual void raw_read(void* ptr, const size_type num_bytes) = 0;
};
class OutputStream : public Stream
{
public:
virtual ~OutputStream() { }
void write(const Stream::int8& x);
void write(const Stream::int16& x);
void write(const Stream::int32& x);
void write(const Stream::int64& x);
void write(const Stream::uint8& x);
void write(const Stream::uint16& x);
void write(const Stream::uint32& x);
void write(const Stream::uint64& x);
void write(const Stream::float32& x);
void write(const Stream::float64& x);
void write(const Stream::int8* x, const size_type len);
void write(const Stream::int16* x, const size_type len);
void write(const Stream::int32* x, const size_type len);
void write(const Stream::int64* x, const size_type len);
void write(const Stream::uint8* x, const size_type len);
void write(const Stream::uint16* x, const size_type len);
void write(const Stream::uint32* x, const size_type len);
void write(const Stream::uint64* x, const size_type len);
void write(const Stream::float32* x, const size_type len);
void write(const Stream::float64* x, const size_type len);
OutputStream& operator<<(const Stream::int8& x);
OutputStream& operator<<(const Stream::int16& x);
OutputStream& operator<<(const Stream::int32& x);
OutputStream& operator<<(const Stream::int64& x);
OutputStream& operator<<(const Stream::uint8& x);
OutputStream& operator<<(const Stream::uint16& x);
OutputStream& operator<<(const Stream::uint32& x);
OutputStream& operator<<(const Stream::uint64& x);
OutputStream& operator<<(const Stream::float32& x);
OutputStream& operator<<(const Stream::float64& x);
/// \brief Writes an ascii string
void write_ascii(const std::string& s);
/// \brief Writes an ascii string AND a new line character.
///
/// The input string is written, and a new line character is
/// then separately written.
void write_ascii_line(const std::string& s);
/// Write a single UNIX new line character.
void write_ascii_new_line_char();
size_type num_bytes_written() const;
private:
template <class T>
void write_helper(const T* ptr, const size_type len)
{
if ((sizeof(T) == 1) || !need_to_swap())
{
const size_type nbytes = len * sizeof(T);
raw_write(ptr, nbytes);
num_bytes_ += nbytes;
}
else
{
for (size_type i = 0; i < len; ++i)
{
for (size_type j = 0; j < sizeof(T); ++j, ++num_bytes_)
{
raw_write(reinterpret_cast<const Stream::uint8*>(ptr + i) + sizeof(T) - 1 - j, 1);
}
}
}
}
virtual void raw_write(const void* ptr, const size_type num_bytes) = 0;
};
#define XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(T) \
void Serialize(const T& x, OutputStream& out); \
void DeSerialize(T* x, InputStream& in)
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::uint8);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::int8);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::uint16);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::int16);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::uint32);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::int32);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::uint64);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::int64);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::float32);
XREG_MAKE_BASIC_SERIALIZE_FNS_DEC(Stream::float64);
#undef XREG_MAKE_BASIC_SERIALIZE_FNS_DEC
} // xreg
#endif