source: Sophya/trunk/SophyaLib/BaseTools/ppersist.h@ 576

Last change on this file since 576 was 576, checked in by ansari, 26 years ago

PPersist version SOPHYA

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