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

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

Corr. bug et ajout Methodes PInPersist::GetTagName - Reza 16/11/99

File size: 11.3 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 string GetTagName(int itag); // 0..NbTags-1
117 vector<string> const & GetTagNames();
118
119 void GetByte (char& c);
120 void GetBytes(void* ptr, size_t bytes);
121 void GetR4 (r_4&);
122 void GetR4s (r_4*, size_t);
123 void GetR8 (r_8&);
124 void GetR8s (r_8*, size_t);
125 void GetI2 (int_2&);
126 void GetI2s (int_2*, size_t);
127 void GetU2 (uint_2&);
128 void GetU2s (uint_2*, size_t);
129 void GetI4 (int_4&);
130 void GetI4s (int_4*, size_t);
131 void GetU4 (uint_4&);
132 void GetU4s (uint_4*, size_t);
133 void GetI8 (int_8&);
134 void GetI8s (int_8*, size_t);
135 void GetU8 (uint_8&);
136 void GetU8s (uint_8*, size_t);
137 void GetLine (char* ptr, size_t len);
138 void GetStr (string&);
139
140 void Get(char& c) {GetByte(c);}
141 void Get(r_4& x) {GetR4(x);}
142 void Get(r_8& x) {GetR8(x);}
143 void Get(uint_2& x) {GetU2(x);}
144 void Get(int_2& x) {GetI2(x);}
145 void Get(uint_4& x) {GetU4(x);}
146 void Get(int_4& x) {GetI4(x);}
147 void Get(uint_8& x) {GetU8(x);}
148 void Get(int_8& x) {GetI8(x);}
149 void Get(r_4* x, size_t n) {GetR4s(x,n);}
150 void Get(r_8* x, size_t n) {GetR8s(x,n);}
151 void Get(uint_2* x, size_t n) {GetU2s(x,n);}
152 void Get(int_2* x, size_t n) {GetI2s(x,n);}
153 void Get(uint_4* x, size_t n) {GetU4s(x,n);}
154 void Get(int_4* x, size_t n) {GetI4s(x,n);}
155 void Get(uint_8* x, size_t n) {GetU8s(x,n);}
156 void Get(int_8* x, size_t n) {GetI8s(x,n);}
157 void Get(string& x) {GetStr(x);}
158
159 PPersist* ReadObject();
160
161
162 int Version() {return version;}
163 time_t CreationDate() { return creationdate; }
164
165 protected:
166 void CheckTag (short datasz);
167 void CheckArrayTag(short datasz, size_t sz);
168 void GetRawByte (char& c);
169 void GetRawBytes(void* ptr, size_t bytes);
170 void GetRawI2 (int_2&);
171 void GetRawI4 (int_4&);
172 void GetRawI8 (int_8&);
173 void GetRawU8 (uint_8&);
174 void GetObject(PPersist*); // Object has been allocated with correct type
175 int_4 assignObjectId(PPersist* x);
176 void Scan();
177 char* GetCStr(uint_2 l);
178
179 istream* s;
180
181 bool bigEndian;
182 int version;
183
184 time_t creationdate;
185
186 // already read objects, id = order in array
187 typedef vector<PPersist*> ObjList;
188 ObjList objList;
189
190 friend class PPersist;
191 };
192
193 class POutPersist : public PIOPersist {
194 public:
195 POutPersist(string const& flnm, int endianness = PPS_NATIVE);
196 ~POutPersist();
197
198 void WriteTag(string const& name);
199
200 void PutByte (char c);
201 void PutBytes(void const* ptr, size_t bytes);
202 void PutR4 (r_4);
203 void PutR4s (r_4 const*, size_t);
204 void PutR8 (r_8);
205 void PutR8s (r_8 const*, size_t);
206 void PutI2 (int_2);
207 void PutI2s (int_2 const*, size_t);
208 void PutU2 (uint_2);
209 void PutU2s (uint_2 const*, size_t);
210 void PutI4 (int_4);
211 void PutI4s (int_4 const*, size_t);
212 void PutU4 (uint_4);
213 void PutU4s (uint_4 const*, size_t);
214 void PutI8 (int_8);
215 void PutI8s (int_8 const*, size_t);
216 void PutU8 (uint_8);
217 void PutU8s (uint_8 const*, size_t);
218 void PutLine (char const* ptr, size_t len=0); // deprecated ?
219 void PutStr (string const&);
220 void PutObject (PPersist const*); // Like doing Write(stream) on object
221
222 void Put(char c) {PutByte(c);}
223 void Put(r_4 x) {PutR4(x);}
224 void Put(r_8 x) {PutR8(x);}
225 void Put(uint_2 x) {PutU2(x);}
226 void Put(int_2 x) {PutI2(x);}
227 void Put(uint_4 x) {PutU4(x);}
228 void Put(int_4 x) {PutI4(x);}
229 void Put(uint_8 x) {PutU8(x);}
230 void Put(int_8 x) {PutI8(x);}
231 void Put(r_4 const* x, size_t n) {PutR4s(x,n);}
232 void Put(r_8 const* x, size_t n) {PutR8s(x,n);}
233 void Put(uint_2 const* x, size_t n) {PutU2s(x,n);}
234 void Put(int_2 const* x, size_t n) {PutI2s(x,n);}
235 void Put(uint_4 const* x, size_t n) {PutU4s(x,n);}
236 void Put(int_4 const* x, size_t n) {PutI4s(x,n);}
237 void Put(uint_8 const* x, size_t n) {PutU8s(x,n);}
238 void Put(int_8 const* x, size_t n) {PutI8s(x,n);}
239 void Put(string const& s) {PutStr(s);}
240 void Put(PPersist const* x) {PutObject(x);}
241
242
243 protected:
244 ostream* s;
245 bool bigEndian;
246
247 void PutArrayTag(short datasz, size_t sz);
248 void PutRawByte (char);
249 void PutRawI2 (int_2);
250 void PutRawI4 (int_4);
251 void PutRawI8 (int_8);
252 void PutRawU8 (uint_8);
253 void PutRawBytes(void const* ptr, size_t bytes);
254 bool serializeNullAndRepeat(PPersist const* x);
255 int_4 findObjectId(PPersist const* x);
256 int_4 assignObjectId(PPersist const* x);
257
258 // already serialized objects
259 typedef map<PPersist const*, int_4, less<PPersist const*> > ObjList;
260 ObjList objList;
261 };
262
263
264#define RAWPERSISTIO(_Type_,_xtyp_) \
265 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
266 { \
267 c.Put##_xtyp_(data); \
268 return c; \
269 } \
270 \
271 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
272 { \
273 c.Get##_xtyp_(data); \
274 return c; \
275 }
276
277 RAWPERSISTIO(int_4,I4);
278 RAWPERSISTIO(uint_4,U4);
279 RAWPERSISTIO(int_2,I2);
280 RAWPERSISTIO(uint_2,U2);
281 RAWPERSISTIO(char,Byte);
282 RAWPERSISTIO(r_4,R4);
283 RAWPERSISTIO(r_8,R8);
284
285#if 0
286#define STRUCTPERSISTIO(_Type_, _field_, _size_) \
287 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
288 { \
289 c.PutBytes(&data._field_, _size_); \
290 return c; \
291 } \
292 \
293 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
294 { \
295 c.GetBytes(&data._field_, _size_); \
296 return c; \
297 }
298
299#endif
300
301 inline POutPersist& operator << (POutPersist& c, PPersist const& obj)
302 {
303 obj.Write(c);
304 return c;
305 }
306
307 inline PInPersist& operator >> (PInPersist& c, PPersist& obj)
308 {
309 obj.Read(c);
310 return c;
311 }
312
313 // Utility class to
314 // - compute the class ID from a MD5 hash of the class name
315 // - register classes with PIOPersist, through PPRegister macro
316
317 template <class T>
318 class PPersistRegistrar {
319 public:
320 static PPersist* Create() {return new T();}
321 static void Register(string id) {PIOPersist::RegisterClass(Hash(id),Create);}
322 static uint_8 Hash(string id) {
323 return PIOPersist::Hash(id);
324 }
325 };
326
327#define PPRegister(className) PPersistRegistrar<className>::Register(#className);
328
329} // namespace
330
331#endif
Note: See TracBrowser for help on using the repository browser.