/
Serialization.cpp
209 lines (167 loc) · 3.91 KB
/
Serialization.cpp
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
#include "pch.h"
#include "Serialization.h"
#include "SerializationImpl.h"
namespace sg {
// type_table
struct type_record
{
const char* name;
creator_t creator;
};
class type_table
{
public:
static type_table& getInstance();
void append(const char* name, creator_t c);
type_record* find(const char* name);
private:
type_table();
void sort();
private:
std::vector<type_record> m_records;
bool m_dirty = false;
};
type_table& type_table::getInstance()
{
static type_table s_inst;
return s_inst;
}
type_table::type_table()
{
}
void type_table::append(const char* name, creator_t c)
{
// if the type is already registered, update the record.
// this is a common occurrence when hot-reloading dlls.
if (auto* rec = find(name)) {
rec->creator = c;
}
else {
m_records.push_back({ name, c });
m_dirty = true;
}
}
type_record* type_table::find(const char* name)
{
sort();
auto it = std::lower_bound(m_records.begin(), m_records.end(), name, [](type_record& a, const char* name) {
return std::strcmp(a.name, name) < 0;
});
if (it != m_records.end() && std::strcmp(it->name, name) == 0)
return &*it;
else
return nullptr;
}
void type_table::sort()
{
if (!m_dirty)
return;
m_dirty = false;
std::sort(m_records.begin(), m_records.end(), [](type_record& a, type_record& b) {
return std::strcmp(a.name, b.name) < 0;
});
}
void register_type(const char* name, creator_t c)
{
type_table::getInstance().append(name, c);
}
void* create_instance_(const char* name)
{
if (type_record* rec = type_table::getInstance().find(name))
return rec->creator();
char message[512];
snprintf(message, std::size(message), "create_instance_(): type \"%s\" is not registered. maybe forgot sgRegisterType()?", name);
throw std::runtime_error(message);
return nullptr;
}
// serializer
struct serializer::impl
{
std::ostream& stream;
std::map<pointer_t, uint32_t> pointer_records;
impl(std::ostream& s) : stream(s) {}
};
serializer::serializer(std::ostream& s)
: m_impl(std::make_unique<impl>(s))
{
}
serializer::~serializer()
{
}
void serializer::write(const void* v, size_t size)
{
m_impl->stream.write((char*)v, size);
}
std::ostream& serializer::getStream()
{
return m_impl->stream;
}
hptr serializer::getHandle(pointer_t v)
{
if (!v)
return { 0 };
hptr ret;
uint32_t& index = m_impl->pointer_records[v];
if (index == 0) {
index = (uint32_t)m_impl->pointer_records.size();
ret = { index | hptr::kFleshFlag };
}
else {
ret = { index };
}
return ret;
}
// deserializer
struct deserializer::impl
{
std::istream& stream;
std::vector<Record> pointer_records;
impl(std::istream& s) : stream(s) {}
};
deserializer::deserializer(std::istream& s)
: m_impl(std::make_unique<impl>(s))
{
}
deserializer::~deserializer()
{
}
void deserializer::read(void* v, size_t size)
{
m_impl->stream.read((char*)v, size);
}
std::istream& deserializer::getStream()
{
return m_impl->stream;
}
void deserializer::setPointer(hptr h, pointer_t v)
{
uint32_t index = h.getIndex();
auto& records = m_impl->pointer_records;
while (records.size() <= index)
records.resize(std::max<size_t>(256, records.size() * 2));
records[index].pointer = v;
}
deserializer::Record& deserializer::getRecord(hptr h)
{
return m_impl->pointer_records[h.getIndex()];
}
bool deserializer::getPointer_(hptr h, pointer_t& v)
{
if (h.isNull()) {
v = nullptr;
return true;
}
uint32_t index = h.getIndex();
auto& records = m_impl->pointer_records;
#ifdef sgDebug
if (index >= records.size()) {
// should not be here
mu::DbgBreak();
v = nullptr;
return false;
}
#endif
v = records[index].pointer;
return true;
}
} // namespace sg