1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 |
|
---|
3 | #ifndef PPERSIST_H_SEEN
|
---|
4 | #define PPERSIST_H_SEEN
|
---|
5 |
|
---|
6 | // Flat file persistance, similar to Java serialization
|
---|
7 | //
|
---|
8 | // E. Aubourg CEA DAPNIA/SPP 1999
|
---|
9 |
|
---|
10 |
|
---|
11 | #include "machdefs.h"
|
---|
12 | #include "pexceptions.h"
|
---|
13 | #include "md5.h"
|
---|
14 |
|
---|
15 | #include <time.h>
|
---|
16 |
|
---|
17 | #include <string>
|
---|
18 | #include <map>
|
---|
19 | #include <vector>
|
---|
20 | #include <typeinfo>
|
---|
21 |
|
---|
22 | #if defined(__KCC__)
|
---|
23 | using std::string ;
|
---|
24 | #include <list.h>
|
---|
25 | #include <map.h>
|
---|
26 | #include <functional.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | // Classe de base pour les objets qui peuvent devenir PPersist
|
---|
30 |
|
---|
31 | namespace PlanckDPC {
|
---|
32 |
|
---|
33 | class AnyDataObj;
|
---|
34 |
|
---|
35 | class PIOPersist;
|
---|
36 | class PInPersist;
|
---|
37 | class POutPersist;
|
---|
38 | class PPersist;
|
---|
39 |
|
---|
40 | /* Persistant (delegate or mixin) object */
|
---|
41 |
|
---|
42 | class PPersist {
|
---|
43 | public:
|
---|
44 | virtual ~PPersist() {}
|
---|
45 | // J'ajoute cette fonction pour assurer la compatibilite
|
---|
46 | // avec l'ancien PPersist d'Eros (Reza 23/04/99)
|
---|
47 | virtual int_4 ClassId() const { return(0); }
|
---|
48 |
|
---|
49 | void Write(string const& fn) const;
|
---|
50 | void Read(string const& fn);
|
---|
51 |
|
---|
52 | virtual void Write(POutPersist&) const;
|
---|
53 | void Read(PInPersist& s); // Reads the type tag and the object
|
---|
54 | void Write(POutPersist&, string const& tag) const;
|
---|
55 | void ReadAtTag(PInPersist& s, string const& tag);
|
---|
56 |
|
---|
57 | virtual AnyDataObj* DataObj() // Retourne l'objet reel $CHECK$ - Reza
|
---|
58 | { return(NULL); } // Devrait etre virtuelle pure
|
---|
59 | protected:
|
---|
60 | virtual void ReadSelf(PInPersist&)=0;
|
---|
61 | virtual void WriteSelf(POutPersist&) const=0;
|
---|
62 |
|
---|
63 | friend class PInPersist;
|
---|
64 | friend class POutPersist;
|
---|
65 | };
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | // Ancestor for PInPersist and POutPersist
|
---|
70 | // Handles (statically) the registration of classes.
|
---|
71 |
|
---|
72 | class PIOPersist {
|
---|
73 | public:
|
---|
74 | enum {PPS_NATIVE = -1, PPS_LITTLE_ENDIAN = 0, PPS_BIG_ENDIAN = 1};
|
---|
75 | typedef PPersist* (*ClassCreatorFunc)();
|
---|
76 |
|
---|
77 | static void RegisterClass(uint_8 classId, ClassCreatorFunc f);
|
---|
78 | static ClassCreatorFunc FindCreatorFunc(uint_8 classId);
|
---|
79 | static uint_8 Hash(string const& typname) {
|
---|
80 | MD5Init(&ctx);
|
---|
81 | MD5Update(&ctx, (unsigned char*) typname.c_str(), typname.size());
|
---|
82 | MD5Final(&ctx);
|
---|
83 | return ( *((uint_8*) ctx.digest) + *((uint_8*) (ctx.digest+8)));
|
---|
84 | }
|
---|
85 | static MD5_CTX ctx;
|
---|
86 |
|
---|
87 | static void Initialize(); // Pour initialiser classList
|
---|
88 | private:
|
---|
89 |
|
---|
90 | typedef map<uint_8, ClassCreatorFunc, less<uint_8> > ClassList;
|
---|
91 | // Pas de createur appele pour objets statiques sur Linux - $CHECK$ Reza 26/04/99
|
---|
92 | static ClassList * classList;
|
---|
93 |
|
---|
94 | protected:
|
---|
95 | enum {PPS_NULL = 0, // this is a null object
|
---|
96 | PPS_STRING = 1, // string, length (2b) + data
|
---|
97 | PPS_OBJECT = 2, // classId, data...
|
---|
98 | PPS_REFERENCE = 3, // objectId
|
---|
99 | PPS_TAG = 4, // tag entries
|
---|
100 | PPS_EOF = 5, // Just before tag infomation, offset to PPS_TAG
|
---|
101 | PPS_LINE = 6, // '\n'-terminated, deprecated ?
|
---|
102 | PPS_SIMPLE = 16, // 16 + number of bytes, up to 8 bytes
|
---|
103 | PPS_SIMPLE_ARRAY = 32, // 32 + number of bytes, up to 8 bytes, then 2 bytes of length
|
---|
104 | PPS_SIMPLE_ARRAY4 = 64, // 64 + number of bytes, up to 8 bytes, then 4 bytes of length
|
---|
105 | PPS_SIMPLE_ARRAY8 = 128 // 64 + number of bytes, up to 8 bytes, then 8 bytes of length
|
---|
106 | };
|
---|
107 |
|
---|
108 | map<string, int_8> tags;
|
---|
109 | };
|
---|
110 |
|
---|
111 |
|
---|
112 | // TBD : use hash tables instead of maps. Check hashtbl status in STL.
|
---|
113 |
|
---|
114 | class PInPersist : public PIOPersist {
|
---|
115 | public:
|
---|
116 | PInPersist(string const& flnm, bool scan=true);
|
---|
117 | ~PInPersist();
|
---|
118 |
|
---|
119 | bool GotoTag(string const& name);
|
---|
120 | int NbTags();
|
---|
121 | bool GotoTagNum(int itag); // 0..NbTags-1
|
---|
122 |
|
---|
123 | void GetByte (char& c);
|
---|
124 | void GetBytes(void* ptr, size_t bytes);
|
---|
125 | void GetR4 (r_4&);
|
---|
126 | void GetR4s (r_4*, size_t);
|
---|
127 | void GetR8 (r_8&);
|
---|
128 | void GetR8s (r_8*, size_t);
|
---|
129 | void GetI2 (int_2&);
|
---|
130 | void GetI2s (int_2*, size_t);
|
---|
131 | void GetU2 (uint_2&);
|
---|
132 | void GetU2s (uint_2*, size_t);
|
---|
133 | void GetI4 (int_4&);
|
---|
134 | void GetI4s (int_4*, size_t);
|
---|
135 | void GetU4 (uint_4&);
|
---|
136 | void GetU4s (uint_4*, size_t);
|
---|
137 | void GetI8 (int_8&);
|
---|
138 | void GetI8s (int_8*, size_t);
|
---|
139 | void GetU8 (uint_8&);
|
---|
140 | void GetU8s (uint_8*, size_t);
|
---|
141 | void GetLine (char* ptr, size_t len);
|
---|
142 | void GetStr (string&);
|
---|
143 |
|
---|
144 | void Get(char& c) {GetByte(c);}
|
---|
145 | void Get(r_4& x) {GetR4(x);}
|
---|
146 | void Get(r_8& x) {GetR8(x);}
|
---|
147 | void Get(uint_2& x) {GetU2(x);}
|
---|
148 | void Get(int_2& x) {GetI2(x);}
|
---|
149 | void Get(uint_4& x) {GetU4(x);}
|
---|
150 | void Get(int_4& x) {GetI4(x);}
|
---|
151 | void Get(uint_8& x) {GetU8(x);}
|
---|
152 | void Get(int_8& x) {GetI8(x);}
|
---|
153 | void Get(r_4* x, size_t n) {GetR4s(x,n);}
|
---|
154 | void Get(r_8* x, size_t n) {GetR8s(x,n);}
|
---|
155 | void Get(uint_2* x, size_t n) {GetU2s(x,n);}
|
---|
156 | void Get(int_2* x, size_t n) {GetI2s(x,n);}
|
---|
157 | void Get(uint_4* x, size_t n) {GetU4s(x,n);}
|
---|
158 | void Get(int_4* x, size_t n) {GetI4s(x,n);}
|
---|
159 | void Get(uint_8* x, size_t n) {GetU8s(x,n);}
|
---|
160 | void Get(int_8* x, size_t n) {GetI8s(x,n);}
|
---|
161 | void Get(string& x) {GetStr(x);}
|
---|
162 |
|
---|
163 | PPersist* ReadObject();
|
---|
164 |
|
---|
165 |
|
---|
166 | int Version() {return version;}
|
---|
167 | time_t CreationDate() { return creationdate; }
|
---|
168 |
|
---|
169 | protected:
|
---|
170 | void CheckTag (short datasz);
|
---|
171 | void CheckArrayTag(short datasz, size_t sz);
|
---|
172 | void GetRawByte (char& c);
|
---|
173 | void GetRawBytes(void* ptr, size_t bytes);
|
---|
174 | void GetRawI2 (int_2&);
|
---|
175 | void GetRawI4 (int_4&);
|
---|
176 | void GetRawI8 (int_8&);
|
---|
177 | void GetRawU8 (uint_8&);
|
---|
178 | void GetObject(PPersist*); // Object has been allocated with correct type
|
---|
179 | int_4 assignObjectId(PPersist* x);
|
---|
180 | void Scan();
|
---|
181 | char* GetCStr(uint_2 l);
|
---|
182 |
|
---|
183 | istream* s;
|
---|
184 |
|
---|
185 | bool bigEndian;
|
---|
186 | int version;
|
---|
187 |
|
---|
188 | time_t creationdate;
|
---|
189 |
|
---|
190 | // already read objects, id = order in array
|
---|
191 | typedef vector<PPersist*> ObjList;
|
---|
192 | ObjList objList;
|
---|
193 |
|
---|
194 | friend class PPersist;
|
---|
195 | };
|
---|
196 |
|
---|
197 | class POutPersist : public PIOPersist {
|
---|
198 | public:
|
---|
199 | POutPersist(string const& flnm, int endianness = PPS_NATIVE);
|
---|
200 | ~POutPersist();
|
---|
201 |
|
---|
202 | void WriteTag(string const& name);
|
---|
203 |
|
---|
204 | void PutByte (char c);
|
---|
205 | void PutBytes(void const* ptr, size_t bytes);
|
---|
206 | void PutR4 (r_4);
|
---|
207 | void PutR4s (r_4 const*, size_t);
|
---|
208 | void PutR8 (r_8);
|
---|
209 | void PutR8s (r_8 const*, size_t);
|
---|
210 | void PutI2 (int_2);
|
---|
211 | void PutI2s (int_2 const*, size_t);
|
---|
212 | void PutU2 (uint_2);
|
---|
213 | void PutU2s (uint_2 const*, size_t);
|
---|
214 | void PutI4 (int_4);
|
---|
215 | void PutI4s (int_4 const*, size_t);
|
---|
216 | void PutU4 (uint_4);
|
---|
217 | void PutU4s (uint_4 const*, size_t);
|
---|
218 | void PutI8 (int_8);
|
---|
219 | void PutI8s (int_8 const*, size_t);
|
---|
220 | void PutU8 (uint_8);
|
---|
221 | void PutU8s (uint_8 const*, size_t);
|
---|
222 | void PutLine (char const* ptr, size_t len=0); // deprecated ?
|
---|
223 | void PutStr (string const&);
|
---|
224 | void PutObject (PPersist const*); // Like doing Write(stream) on object
|
---|
225 |
|
---|
226 | void Put(char c) {PutByte(c);}
|
---|
227 | void Put(r_4 x) {PutR4(x);}
|
---|
228 | void Put(r_8 x) {PutR8(x);}
|
---|
229 | void Put(uint_2 x) {PutU2(x);}
|
---|
230 | void Put(int_2 x) {PutI2(x);}
|
---|
231 | void Put(uint_4 x) {PutU4(x);}
|
---|
232 | void Put(int_4 x) {PutI4(x);}
|
---|
233 | void Put(uint_8 x) {PutU8(x);}
|
---|
234 | void Put(int_8 x) {PutI8(x);}
|
---|
235 | void Put(r_4 const* x, size_t n) {PutR4s(x,n);}
|
---|
236 | void Put(r_8 const* x, size_t n) {PutR8s(x,n);}
|
---|
237 | void Put(uint_2 const* x, size_t n) {PutU2s(x,n);}
|
---|
238 | void Put(int_2 const* x, size_t n) {PutI2s(x,n);}
|
---|
239 | void Put(uint_4 const* x, size_t n) {PutU4s(x,n);}
|
---|
240 | void Put(int_4 const* x, size_t n) {PutI4s(x,n);}
|
---|
241 | void Put(uint_8 const* x, size_t n) {PutU8s(x,n);}
|
---|
242 | void Put(int_8 const* x, size_t n) {PutI8s(x,n);}
|
---|
243 | void Put(string const& s) {PutStr(s);}
|
---|
244 | void Put(PPersist const* x) {PutObject(x);}
|
---|
245 |
|
---|
246 |
|
---|
247 | protected:
|
---|
248 | ostream* s;
|
---|
249 | bool bigEndian;
|
---|
250 |
|
---|
251 | void PutArrayTag(short datasz, size_t sz);
|
---|
252 | void PutRawByte (char);
|
---|
253 | void PutRawI2 (int_2);
|
---|
254 | void PutRawI4 (int_4);
|
---|
255 | void PutRawI8 (int_8);
|
---|
256 | void PutRawU8 (uint_8);
|
---|
257 | void PutRawBytes(void const* ptr, size_t bytes);
|
---|
258 | bool serializeNullAndRepeat(PPersist const* x);
|
---|
259 | int_4 findObjectId(PPersist const* x);
|
---|
260 | int_4 assignObjectId(PPersist const* x);
|
---|
261 |
|
---|
262 | // already serialized objects
|
---|
263 | typedef map<PPersist const*, int_4, less<PPersist const*> > ObjList;
|
---|
264 | ObjList objList;
|
---|
265 | };
|
---|
266 |
|
---|
267 |
|
---|
268 | #define RAWPERSISTIO(_Type_,_xtyp_) \
|
---|
269 | inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
|
---|
270 | { \
|
---|
271 | c.Put##_xtyp_(data); \
|
---|
272 | return c; \
|
---|
273 | } \
|
---|
274 | \
|
---|
275 | inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
|
---|
276 | { \
|
---|
277 | c.Get##_xtyp_(data); \
|
---|
278 | return c; \
|
---|
279 | }
|
---|
280 |
|
---|
281 | RAWPERSISTIO(int_4,I4);
|
---|
282 | RAWPERSISTIO(uint_4,U4);
|
---|
283 | RAWPERSISTIO(int_2,I2);
|
---|
284 | RAWPERSISTIO(uint_2,U2);
|
---|
285 | RAWPERSISTIO(char,Byte);
|
---|
286 | RAWPERSISTIO(r_4,R4);
|
---|
287 | RAWPERSISTIO(r_8,R8);
|
---|
288 |
|
---|
289 | #if 0
|
---|
290 | #define STRUCTPERSISTIO(_Type_, _field_, _size_) \
|
---|
291 | inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
|
---|
292 | { \
|
---|
293 | c.PutBytes(&data._field_, _size_); \
|
---|
294 | return c; \
|
---|
295 | } \
|
---|
296 | \
|
---|
297 | inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
|
---|
298 | { \
|
---|
299 | c.GetBytes(&data._field_, _size_); \
|
---|
300 | return c; \
|
---|
301 | }
|
---|
302 |
|
---|
303 | #endif
|
---|
304 |
|
---|
305 | inline POutPersist& operator << (POutPersist& c, PPersist const& obj)
|
---|
306 | {
|
---|
307 | obj.Write(c);
|
---|
308 | return c;
|
---|
309 | }
|
---|
310 |
|
---|
311 | inline PInPersist& operator >> (PInPersist& c, PPersist& obj)
|
---|
312 | {
|
---|
313 | obj.Read(c);
|
---|
314 | return c;
|
---|
315 | }
|
---|
316 |
|
---|
317 | // Utility class to
|
---|
318 | // - compute the class ID from a MD5 hash of the class name
|
---|
319 | // - register classes with PIOPersist, through PPRegister macro
|
---|
320 |
|
---|
321 | template <class T>
|
---|
322 | class PPersistRegistrar {
|
---|
323 | public:
|
---|
324 | static PPersist* Create() {return new T();}
|
---|
325 | static void Register() {PIOPersist::RegisterClass(Hash(),Create);}
|
---|
326 | static uint_8 Hash() {
|
---|
327 | return PIOPersist::Hash(typeid(T).name());
|
---|
328 | }
|
---|
329 | };
|
---|
330 |
|
---|
331 | #define PPRegister(className) PPersistRegistrar<className>::Register();
|
---|
332 |
|
---|
333 | } // namespace
|
---|
334 |
|
---|
335 | #endif
|
---|