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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright (C) 2007 Xagasoft, All rights reserved.
*
* This file is part of the libbu++ library and is released under the
* terms of the license contained in the file LICENSE.
*/
#ifndef BU_ARCHIVE_H
#define BU_ARCHIVE_H
#include <stdint.h>
#include <string>
#include "bu/archival.h"
#include "bu/stream.h"
#include <list>
#include "bu/hash.h"
#include "bu/list.h"
#include "bu/set.h"
namespace Bu
{
/**
* Provides a framework for serialization of objects and primitives. The
* archive will handle any basic primitive, a few special types, like char *
* strings, as well as STL classes and anything that inherits from the
* Archival class. Each Archive operates on a Stream, so you can send the
* data using an Archive almost anywhere.
*
* In order to use an Archive to store something to a file, try something
* like:
*@code
* File sOut("output", "wb"); // This is a stream subclass
* Archive ar( sOut, Archive::save );
* ar << myClass;
@endcode
* In this example myClass is any class that inherits from Archival. When
* the storage operator is called, the Archival::archive() function in the
* myClass object is called with a reference to the Archive. This can be
* handled in one of two ways:
*@code
* void MyClass::archive( Archive &ar )
* {
* ar && sName && nAge && sJob;
* }
@endcode
* Here we don't worry about weather we're loading or saving by using the
* smart && operator. This allows us to write very consistent, very simple
* archive functions that really do a lot of work. If we wanted to do
* something different in the case of loading or saving we would do:
*@code
* void MyClass::archive( Archive &ar )
* {
* if( ar.isLoading() )
* {
* ar >> sName >> nAge >> sJob;
* } else
* {
* ar << sName << nAge << sJob;
* }
* }
@endcode
* Archive currently does not provide facility to make fully portable
* archives. For example, it will not convert between endianness for you,
* nor will it take into account differences between primitive sizes on
* different platforms. This, at the moment, is up to the user to ensure.
* One way of dealing with the latter problem is to make sure and use
* explicit primitive types from the stdint.h header, i.e. int32_t.
*/
class Archive
{
private:
bool bLoading;
public:
bool isLoading();
enum
{
load = true,
save = false
};
Archive( Stream &rStream, bool bLoading );
virtual ~Archive();
virtual void close();
virtual void write(const void *, int32_t);
virtual void read(void *, int32_t);
virtual Archive &operator<<(bool);
virtual Archive &operator<<(int8_t);
virtual Archive &operator<<(int16_t);
virtual Archive &operator<<(int32_t);
virtual Archive &operator<<(int64_t);
virtual Archive &operator<<(uint8_t);
virtual Archive &operator<<(uint16_t);
virtual Archive &operator<<(uint32_t);
virtual Archive &operator<<(uint64_t);
virtual Archive &operator<<(long);
virtual Archive &operator<<(float);
virtual Archive &operator<<(double);
virtual Archive &operator<<(long double);
virtual Archive &operator>>(bool &);
virtual Archive &operator>>(int8_t &);
virtual Archive &operator>>(int16_t &);
virtual Archive &operator>>(int32_t &);
virtual Archive &operator>>(int64_t &);
virtual Archive &operator>>(uint8_t &);
virtual Archive &operator>>(uint16_t &);
virtual Archive &operator>>(uint32_t &);
virtual Archive &operator>>(uint64_t &);
virtual Archive &operator>>(long &);
virtual Archive &operator>>(float &);
virtual Archive &operator>>(double &);
virtual Archive &operator>>(long double &);
virtual Archive &operator&&(bool &);
virtual Archive &operator&&(int8_t &);
virtual Archive &operator&&(int16_t &);
virtual Archive &operator&&(int32_t &);
virtual Archive &operator&&(int64_t &);
virtual Archive &operator&&(uint8_t &);
virtual Archive &operator&&(uint16_t &);
virtual Archive &operator&&(uint32_t &);
virtual Archive &operator&&(uint64_t &);
virtual Archive &operator&&(float &);
virtual Archive &operator&&(double &);
virtual Archive &operator&&(long double &);
/**
* For storage, get an ID for the pointer to the object you're going to
* write.
*/
uint32_t getID( const void *ptr );
/**
* For loading. Assosiates an empty pointer with an id. When you wind
* up loading an id reference to a pointer for an object that may or
* may not have loaded yet, call this with the id, if it has been loaded
* already, you'll immediately get a pointer, if not, it will write one
* for you when the time comes.
*/
void assocPtrID( void **ptr, uint32_t id );
/**
* For loading. Call this when you load an object that other things may
* have pointers to. It will assosiate every pointer that's been
* registered with assocPtrID to the pointer passed in, and id passed
* in. It will also set things up so future calls to assocPtrID will
* automatically succeed immediately.
*/
void readID( const void *ptr, uint32_t id );
private:
Stream &rStream;
uint32_t nNextID;
Hash<uint32_t,uint32_t> hPtrID;
Hash<uint32_t,List<void **> > hPtrDest;
};
Archive &operator<<(Archive &, class Bu::Archival &);
Archive &operator>>(Archive &, class Bu::Archival &);
//Archive &operator&&(Archive &s, class Bu::Archival &p);
Archive &operator<<(Archive &, std::string &);
Archive &operator>>(Archive &, std::string &);
//Archive &operator&&(Archive &, std::string &);
template<typename T> Archive &operator&&( Archive &ar, T &dat )
{
if( ar.isLoading() )
{
return ar >> dat;
}
else
{
return ar << dat;
}
}
template<typename T> Archive &operator<<( Archive &ar, std::list<T> &l )
{
typename std::list<T>::size_type num = l.getSize();
ar << num;
for( typename std::list<T>::const_iterator i = l.begin(); i != l.end();
i++ )
{
ar << *i;
}
return ar;
}
template<typename T> Archive &operator>>( Archive &ar, std::list<T> &l )
{
typename std::list<T>::size_type num;
ar >> num;
l.resize( num );
for( typename std::list<T>::const_iterator i = l.begin();
i != l.end(); i++ )
{
ar >> *i;
}
return ar;
}
Archive &operator<<(Archive &, class Bu::Archival *p);
Archive &operator>>(Archive &, class Bu::Archival *p);
template<typename key, typename value>
Archive &operator<<( Archive &ar, Hash<key,value> &h )
{
ar << h.getSize();
for( typename Hash<key,value>::iterator i = h.begin(); i != h.end(); i++ )
{
//std::pair<key,value> p = *i;
ar << (i.getKey()) << (i.getValue());
}
return ar;
}
template<typename key, typename value>
Archive &operator>>( Archive &ar, Hash<key,value> &h )
{
h.clear();
uint32_t nSize;
ar >> nSize;
for( uint32_t j = 0; j < nSize; j++ )
{
key k; value v;
ar >> k >> v;
h.insert( k, v );
}
return ar;
}
template<typename value>
Archive &operator<<( Archive &ar, List<value> &h )
{
ar << h.getSize();
for( typename List<value>::iterator i = h.begin(); i != h.end(); i++ )
{
ar << (*i);
}
return ar;
}
template<typename value>
Archive &operator>>( Archive &ar, List<value> &h )
{
h.clear();
uint32_t nSize;
ar >> nSize;
for( uint32_t j = 0; j < nSize; j++ )
{
value v;
ar >> v;
h.append( v );
}
return ar;
}
template<typename value>
Archive &operator<<( Archive &ar, Set<value> &h )
{
ar << h.getSize();
for( typename Set<value>::iterator i = h.begin(); i != h.end(); i++ )
{
ar << (*i);
}
return ar;
}
template<typename value>
Archive &operator>>( Archive &ar, Set<value> &h )
{
h.clear();
uint32_t nSize;
ar >> nSize;
for( uint32_t j = 0; j < nSize; j++ )
{
value v;
ar >> v;
h.insert( v );
}
return ar;
}
}
#endif
|