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