-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.hpp
179 lines (148 loc) · 4.29 KB
/
serializer.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
#pragma once
//serializer: a class designed to save and restore the state of classes.
//
//benefits:
//- data() will be portable in size (it is not necessary to specify type sizes.)
//- data() will be portable in endianness (always stored internally as little-endian.)
//- one serialize function can both save and restore class states.
//
//caveats:
//- only plain-old-data can be stored. complex classes must provide serialize(serializer&);
//- floating-point usage is not portable across different implementations
#include <nall/array.hpp>
#include <nall/bit.hpp>
#include <nall/range.hpp>
#include <nall/stdint.hpp>
#include <nall/traits.hpp>
#include <nall/utility.hpp>
namespace nall {
struct serializer;
template<typename T>
struct has_serialize {
template<typename C> static auto test(decltype(std::declval<C>().serialize(std::declval<serializer&>()))*) -> char;
template<typename C> static auto test(...) -> long;
static constexpr bool value = sizeof(test<T>(0)) == sizeof(char);
};
template<typename T> constexpr bool has_serialize_v = has_serialize<T>::value;
struct serializer {
explicit operator bool() const {
return _size;
}
auto reading() const -> bool {
return _mode == 0;
}
auto writing() const -> bool {
return _mode == 1;
}
auto setReading() -> void {
_mode = 0;
_size = 0;
}
auto setWriting() -> void {
_mode = 1;
_size = 0;
}
auto data() const -> const u8* {
return _data;
}
auto size() const -> u32 {
return _size;
}
auto capacity() const -> u32 {
return _capacity;
}
auto reserve(u32 size) -> void {
if(size > _capacity) {
auto data = new u8[bit::round(size)]();
memory::copy(data, _data, _capacity);
delete[] _data;
_data = data;
_capacity = bit::round(size);
}
}
template<typename T> auto operator()(T& value) -> serializer& {
static_assert(has_serialize_v<T> || is_integral_v<T> || is_floating_point_v<T>);
if constexpr(has_serialize_v<T>) {
value.serialize(*this);
} else if constexpr(is_integral_v<T>) {
integer(value);
} else if constexpr(is_floating_point_v<T>) {
real(value);
}
return *this;
}
template<typename T, s32 N> auto operator()(T (&array)[N]) -> serializer& {
for(auto& value : array) operator()(value);
return *this;
}
template<typename T> auto operator()(array_span<T> array) -> serializer& {
for(auto& value : array) operator()(value);
return *this;
}
auto operator=(const serializer& s) -> serializer& {
if(_data) delete[] _data;
_mode = s._mode;
_data = new u8[s._capacity];
_size = s._size;
_capacity = s._capacity;
memory::copy(_data, s._data, s._capacity);
return *this;
}
auto operator=(serializer&& s) -> serializer& {
if(_data) delete[] _data;
_mode = s._mode;
_data = s._data;
_size = s._size;
_capacity = s._capacity;
s._data = nullptr;
return *this;
}
serializer(const serializer& s) { operator=(s); }
serializer(serializer&& s) { operator=(move(s)); }
serializer() {
setWriting();
_data = new u8[1024 * 1024]();
_size = 0;
_capacity = 1024 * 1024;
}
serializer(const u8* data, u32 capacity) {
setReading();
_data = new u8[capacity]();
_size = 0;
_capacity = capacity;
memory::copy(_data, data, capacity);
}
~serializer() {
if(_data) delete[] _data;
}
private:
template<typename T> auto integer(T& value) -> serializer& {
enum : u32 { size = std::is_same<bool, T>::value ? 1 : sizeof(T) };
reserve(_size + size);
if(writing()) {
for(u32 n : range(size)) _data[_size++] = value >> (n << 3);
} else if(reading()) {
value = 0;
for(u32 n : range(size)) value |= (T)_data[_size++] << (n << 3);
}
return *this;
}
template<typename T> auto real(T& value) -> serializer& {
enum : u32 { size = sizeof(T) };
reserve(_size + size);
//this is rather dangerous, and not cross-platform safe;
//but there is no standardized way to export floating point values
auto p = (u8*)&value;
if(writing()) {
for(u32 n : range(size)) _data[_size++] = p[n];
} else if(reading()) {
for(u32 n : range(size)) p[n] = _data[_size++];
}
return *this;
}
bool _mode = 0;
u8* _data = nullptr;
u32 _size = 0;
u32 _capacity = 0;
};
}