-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeze.h
230 lines (189 loc) · 6.61 KB
/
freeze.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
#pragma once
#include <string>
#include <vector>
#include <fstream>
#include <cassert>
#include <map>
namespace freeze {
constexpr char blockStart = '(';
constexpr char blockEnd = ')';
constexpr char arrayStart = '[';
constexpr char arrayEnd = ']';
constexpr char stringIndicator = '"';
constexpr char intIndicator = 'i';
constexpr char doubleIndicator = 'd';
constexpr char boolIndicator = 'b';
void escapeReservedChars(std::string &s, const std::vector<char> &reservedCharacters);
void unescapeReservedChars(std::string &s, const std::vector<char> &reservedCharacters);
class IceBlock {
private:
std::string frozenData;
std::string thawNewData();
template<typename T>
T meltItem() {
assert(frozenData[0] == blockStart);
return T(IceBlock(thawNewData()));
}
template<>
bool meltItem<bool>() {
assert(frozenData[0] == boolIndicator);
return thawNewData()[0] == 't';
}
template<>
int meltItem<int>() {
assert(frozenData[0] == intIndicator);
return atoi(thawNewData().c_str());
}
// TODO see if above can be merged since literally same code
template<>
unsigned int meltItem<unsigned int>() {
assert(frozenData[0] == intIndicator);
return atoi(thawNewData().c_str());
}
template<>
double meltItem<double>() {
assert(frozenData[0] == doubleIndicator);
return atof(thawNewData().c_str());
}
template<>
std::string meltItem<std::string>() {
assert(frozenData[0] == stringIndicator);
std::string data = thawNewData();
unescapeReservedChars(data, {stringIndicator});
return data;
}
template<typename T>
std::vector<T> meltVector() {
assert(frozenData[0] == arrayStart);
IceBlock arrayBlock = IceBlock(thawNewData());
std::vector<T> toReturn;
while (!arrayBlock.empty())
toReturn.push_back(arrayBlock.melt<T>());
return toReturn;
}
template<typename T, size_t size>
std::array<T, size> meltArray() {
assert(frozenData[0] == arrayStart);
IceBlock arrayBlock = IceBlock(thawNewData());
std::array<T, size> toReturn;
for (unsigned int i = 0; i < size; i++)
toReturn.at(i) = arrayBlock.melt<T>();
return toReturn;
}
template<typename T, typename U>
std::pair<T, U> meltPair() {
assert(frozenData[0] == arrayStart);
IceBlock arrayBlock = IceBlock(thawNewData());
return std::pair<T, U>{arrayBlock.melt<T>(), arrayBlock.melt<U>()};
}
template<typename T, typename U>
std::map<T, U> meltMap() {
assert(frozenData[0] = arrayStart);
IceBlock mapBlock = IceBlock(thawNewData());
std::map<T, U> toReturn;
while (!mapBlock.empty())
toReturn.insert(meltPair<T, U>());
return toReturn;
}
public:
static IceBlock fromFile(const std::string &path);
IceBlock(std::string frozenData);
IceBlock();
bool empty();
std::string getFrozenData();
// Yeah yeah this feels hella jank but C++ doesn't support partial template specialization so this is the next best thing
template<typename T>
T melt() {
return melt_impl(static_cast<T *>(0));
}
template<typename T>
T melt_impl(T *) {
return meltItem<T>();
}
template<typename T>
std::vector<T> melt_impl(std::vector<T> *) {
return meltVector<T>();
}
template<typename T, size_t size>
std::array<T, size> melt_impl(std::array<T, size> *) {
return meltArray<T, size>();
}
template<typename T, typename U>
std::pair<T, U> melt_impl(std::pair<T, U> *) {
return meltPair<T, U>();
}
template<typename T, typename U>
std::map<T, U> melt_impl(std::map<T, U> *) {
return meltMap<T, U>();
}
// See above
template<typename T>
void freeze(const T &item) {
frozenData += blockStart;
frozenData += item.freeze().frozenData;
frozenData += blockEnd;
}
template<>
void freeze<std::string>(const std::string &item) {
frozenData += stringIndicator;
std::string dup = item;
escapeReservedChars(dup, {stringIndicator});
frozenData += dup;
frozenData += stringIndicator;
}
template<>
void freeze<double>(const double &item) {
frozenData += doubleIndicator;
frozenData += std::to_string(item);
frozenData += doubleIndicator;
}
template<>
void freeze<unsigned int>(const unsigned int &item) {
frozenData += intIndicator;
frozenData += std::to_string(item);
frozenData += intIndicator;
}
template<>
void freeze<int>(const int &item) {
frozenData += intIndicator;
frozenData += std::to_string(item);
frozenData += intIndicator;
}
template<>
void freeze<bool>(const bool &item) {
frozenData += boolIndicator;
frozenData += (item ? 't' : 'f');
frozenData += boolIndicator;
}
// Works bc overloading
template<typename T>
void freeze(const std::vector<T> &from) {
frozenData += arrayStart;
for (const T &item : from)
freeze(item);
frozenData += arrayEnd;
}
template<typename T, size_t size>
void freeze(const std::array<T, size> &from) {
frozenData += arrayStart;
for (const T &item : from)
freeze(item);
frozenData += arrayEnd;
}
template<typename T, typename U>
void freeze(const std::pair<T, U> &item) {
frozenData += arrayStart;
freeze(item.first);
freeze(item.second);
frozenData += arrayEnd;
}
template<typename T, typename U>
void freeze(const std::map<T, U> &from) {
frozenData += arrayStart;
for (const std::pair<T, U> &pair : from)
freeze(pair);
frozenData += arrayEnd;
}
void save(const std::string &path);
};
}