blob: df1334b8c7cba883e3a1855799a541e23ed57a24 (
plain)
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
|
#pragma once
#include "buffer.h"
#include "error.h"
#include <cstdint>
#include <cstring>
namespace saw {
/**
* Helper class to encode/decode any primtive type into/from litte endian.
* The shift class does this by shifting bytes. This type of procedure is
* platform independent. So it does not matter if the memory layout is
* little endian or big endian
*/
template <typename T, size_t N> class ShiftStreamValue {
public:
inline static error_or<void> decode(typename native_data_type<schema::Primitive<T,N>>::type &val, buffer &buff) {
if (buff.read_composite_length() < sizeof(T)) {
return make_error<err::buffer_exhausted>();
}
typename native_data_type<schema::Primitive<schema::UnsignedInteger,N>>::type raw = 0;
for (size_t i = 0; i < sizeof(T); ++i) {
raw |= (static_cast<uint64_t>(buff.read(i)) << (i * 8));
}
memcpy(&val, &raw, sizeof(T));
buff.read_advance(sizeof(T));
return void_t{};
}
inline static error_or<void> encode(const typename native_data_type<schema::Primitive<T,N>>::type &val, buffer &buff) {
error err = buff.write_require_length(sizeof(T));
if (err.failed()) {
return err;
}
typename native_data_type<schema::Primitive<schema::UnsignedInteger,N>>::type raw{};
memcpy(&raw, &val, sizeof(T));
for (size_t i = 0; i < sizeof(T); ++i) {
buffer.write(i) = raw >> (i * 8);
}
buffer.write_advance(sizeof(T));
return void_t{};
}
inline static size_t size() const { return N; }
};
template <typename T> using stream_value = shift_stream_value<T>;
} // namespace saw
|