diff options
Diffstat (limited to 'src/old/serializer.cpp')
-rw-r--r-- | src/old/serializer.cpp | 338 |
1 files changed, 338 insertions, 0 deletions
diff --git a/src/old/serializer.cpp b/src/old/serializer.cpp new file mode 100644 index 0000000..636224e --- /dev/null +++ b/src/old/serializer.cpp | |||
@@ -0,0 +1,338 @@ | |||
1 | #include "serializer.h" | ||
2 | #include "serializable.h" | ||
3 | #include <list> | ||
4 | |||
5 | Serializer::Serializer(bool bLoading): | ||
6 | bLoading(bLoading) | ||
7 | { | ||
8 | } | ||
9 | Serializer::~Serializer() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | bool Serializer::isLoading() | ||
14 | { | ||
15 | return bLoading; | ||
16 | } | ||
17 | Serializer &Serializer::operator<<(bool p) | ||
18 | { | ||
19 | write( &p, sizeof(p) ); | ||
20 | return *this; | ||
21 | } | ||
22 | Serializer &Serializer::operator<<(int8_t p) | ||
23 | { | ||
24 | write( &p, sizeof(p) ); | ||
25 | return *this; | ||
26 | } | ||
27 | Serializer &Serializer::operator<<(int16_t p) | ||
28 | { | ||
29 | write( &p, sizeof(p) ); | ||
30 | return *this; | ||
31 | } | ||
32 | Serializer &Serializer::operator<<(int32_t p) | ||
33 | { | ||
34 | write( &p, sizeof(p) ); | ||
35 | return *this; | ||
36 | } | ||
37 | Serializer &Serializer::operator<<(int64_t p) | ||
38 | { | ||
39 | write( &p, sizeof(p) ); | ||
40 | return *this; | ||
41 | } | ||
42 | Serializer &Serializer::operator<<(uint8_t p) | ||
43 | { | ||
44 | write( &p, sizeof(p) ); | ||
45 | return *this; | ||
46 | } | ||
47 | Serializer &Serializer::operator<<(uint16_t p) | ||
48 | { | ||
49 | write( &p, sizeof(p) ); | ||
50 | return *this; | ||
51 | } | ||
52 | Serializer &Serializer::operator<<(uint32_t p) | ||
53 | { | ||
54 | write( &p, sizeof(p) ); | ||
55 | return *this; | ||
56 | } | ||
57 | Serializer &Serializer::operator<<(uint64_t p) | ||
58 | { | ||
59 | write( &p, sizeof(p) ); | ||
60 | return *this; | ||
61 | } | ||
62 | Serializer &Serializer::operator<<(long p) | ||
63 | { | ||
64 | write( &p, sizeof(p) ); | ||
65 | return *this; | ||
66 | } | ||
67 | Serializer &Serializer::operator<<(float p) | ||
68 | { | ||
69 | write( &p, sizeof(p) ); | ||
70 | return *this; | ||
71 | } | ||
72 | Serializer &Serializer::operator<<(double p) | ||
73 | { | ||
74 | write( &p, sizeof(p) ); | ||
75 | return *this; | ||
76 | } | ||
77 | Serializer &Serializer::operator<<(long double p) | ||
78 | { | ||
79 | write( &p, sizeof(p) ); | ||
80 | return *this; | ||
81 | } | ||
82 | |||
83 | Serializer &Serializer::operator>>(bool &p) | ||
84 | { | ||
85 | read( &p, sizeof(p) ); | ||
86 | return *this; | ||
87 | } | ||
88 | Serializer &Serializer::operator>>(int8_t &p) | ||
89 | { | ||
90 | read( &p, sizeof(p) ); | ||
91 | return *this; | ||
92 | } | ||
93 | Serializer &Serializer::operator>>(int16_t &p) | ||
94 | { | ||
95 | read( &p, sizeof(p) ); | ||
96 | return *this; | ||
97 | } | ||
98 | Serializer &Serializer::operator>>(int32_t &p) | ||
99 | { | ||
100 | read( &p, sizeof(p) ); | ||
101 | return *this; | ||
102 | } | ||
103 | Serializer &Serializer::operator>>(int64_t &p) | ||
104 | { | ||
105 | read( &p, sizeof(p) ); | ||
106 | return *this; | ||
107 | } | ||
108 | Serializer &Serializer::operator>>(uint8_t &p) | ||
109 | { | ||
110 | read( &p, sizeof(p) ); | ||
111 | return *this; | ||
112 | } | ||
113 | Serializer &Serializer::operator>>(uint16_t &p) | ||
114 | { | ||
115 | read( &p, sizeof(p) ); | ||
116 | return *this; | ||
117 | } | ||
118 | Serializer &Serializer::operator>>(uint32_t &p) | ||
119 | { | ||
120 | read( &p, sizeof(p) ); | ||
121 | return *this; | ||
122 | } | ||
123 | Serializer &Serializer::operator>>(uint64_t &p) | ||
124 | { | ||
125 | read( &p, sizeof(p) ); | ||
126 | return *this; | ||
127 | } | ||
128 | Serializer &Serializer::operator>>(long &p) | ||
129 | { | ||
130 | read( &p, sizeof(p) ); | ||
131 | return *this; | ||
132 | } | ||
133 | Serializer &Serializer::operator>>(float &p) | ||
134 | { | ||
135 | read( &p, sizeof(p) ); | ||
136 | return *this; | ||
137 | } | ||
138 | Serializer &Serializer::operator>>(double &p) | ||
139 | { | ||
140 | read( &p, sizeof(p) ); | ||
141 | return *this; | ||
142 | } | ||
143 | Serializer &Serializer::operator>>(long double &p) | ||
144 | { | ||
145 | read( &p, sizeof(p) ); | ||
146 | return *this; | ||
147 | } | ||
148 | |||
149 | Serializer &Serializer::operator&&(bool &p) | ||
150 | { | ||
151 | if (bLoading) | ||
152 | { | ||
153 | return *this >> p; | ||
154 | } | ||
155 | else | ||
156 | { | ||
157 | return *this << p; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | Serializer &Serializer::operator&&(int8_t &p) | ||
162 | { | ||
163 | if (bLoading) | ||
164 | { | ||
165 | return *this >> p; | ||
166 | } | ||
167 | else | ||
168 | { | ||
169 | return *this << p; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | Serializer &Serializer::operator&&(int16_t &p) | ||
174 | { | ||
175 | if (bLoading) | ||
176 | { | ||
177 | return *this >> p; | ||
178 | } | ||
179 | else | ||
180 | { | ||
181 | return *this << p; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | Serializer &Serializer::operator&&(int32_t &p) | ||
186 | { | ||
187 | if (bLoading) | ||
188 | { | ||
189 | return *this >> p; | ||
190 | } | ||
191 | else | ||
192 | { | ||
193 | return *this << p; | ||
194 | } | ||
195 | } | ||
196 | |||
197 | Serializer &Serializer::operator&&(int64_t &p) | ||
198 | { | ||
199 | if (bLoading) | ||
200 | { | ||
201 | return *this >> p; | ||
202 | } | ||
203 | else | ||
204 | { | ||
205 | return *this << p; | ||
206 | } | ||
207 | } | ||
208 | |||
209 | Serializer &Serializer::operator&&(uint8_t &p) | ||
210 | { | ||
211 | if (bLoading) | ||
212 | { | ||
213 | return *this >> p; | ||
214 | } | ||
215 | else | ||
216 | { | ||
217 | return *this << p; | ||
218 | } | ||
219 | } | ||
220 | |||
221 | Serializer &Serializer::operator&&(uint16_t &p) | ||
222 | { | ||
223 | if (bLoading) | ||
224 | { | ||
225 | return *this >> p; | ||
226 | } | ||
227 | else | ||
228 | { | ||
229 | return *this << p; | ||
230 | } | ||
231 | } | ||
232 | |||
233 | Serializer &Serializer::operator&&(uint32_t &p) | ||
234 | { | ||
235 | if (bLoading) | ||
236 | { | ||
237 | return *this >> p; | ||
238 | } | ||
239 | else | ||
240 | { | ||
241 | return *this << p; | ||
242 | } | ||
243 | } | ||
244 | |||
245 | Serializer &Serializer::operator&&(uint64_t &p) | ||
246 | { | ||
247 | if (bLoading) | ||
248 | { | ||
249 | return *this >> p; | ||
250 | } | ||
251 | else | ||
252 | { | ||
253 | return *this << p; | ||
254 | } | ||
255 | } | ||
256 | |||
257 | Serializer &Serializer::operator&&(float &p) | ||
258 | { | ||
259 | if (bLoading) | ||
260 | { | ||
261 | return *this >> p; | ||
262 | } | ||
263 | else | ||
264 | { | ||
265 | return *this << p; | ||
266 | } | ||
267 | } | ||
268 | |||
269 | Serializer &Serializer::operator&&(double &p) | ||
270 | { | ||
271 | if (bLoading) | ||
272 | { | ||
273 | return *this >> p; | ||
274 | } | ||
275 | else | ||
276 | { | ||
277 | return *this << p; | ||
278 | } | ||
279 | } | ||
280 | |||
281 | Serializer &Serializer::operator&&(long double &p) | ||
282 | { | ||
283 | if (bLoading) | ||
284 | { | ||
285 | return *this >> p; | ||
286 | } | ||
287 | else | ||
288 | { | ||
289 | return *this << p; | ||
290 | } | ||
291 | } | ||
292 | |||
293 | |||
294 | Serializer &operator<<(Serializer &s, Serializable &p) | ||
295 | { | ||
296 | p.serialize( s ); | ||
297 | return s; | ||
298 | } | ||
299 | |||
300 | Serializer &operator>>(Serializer &s, Serializable &p) | ||
301 | { | ||
302 | p.serialize( s ); | ||
303 | return s; | ||
304 | } | ||
305 | |||
306 | Serializer &operator&&(Serializer &s, Serializable &p) | ||
307 | { | ||
308 | if (s.isLoading()) | ||
309 | { | ||
310 | return s >> p; | ||
311 | } | ||
312 | else | ||
313 | { | ||
314 | return s << p; | ||
315 | } | ||
316 | } | ||
317 | |||
318 | Serializer &operator<<( Serializer &ar, std::string &s ) | ||
319 | { | ||
320 | ar << (uint32_t)s.length(); | ||
321 | ar.write( s.c_str(), s.length() ); | ||
322 | |||
323 | return ar; | ||
324 | } | ||
325 | |||
326 | Serializer &operator>>( Serializer &ar, std::string &s ) | ||
327 | { | ||
328 | uint32_t l; | ||
329 | ar >> l; | ||
330 | char *tmp = new char[l+1]; | ||
331 | tmp[l] = '\0'; | ||
332 | ar.read( tmp, l ); | ||
333 | s = tmp; | ||
334 | delete[] tmp; | ||
335 | |||
336 | return ar; | ||
337 | } | ||
338 | |||