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

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

reorg exc, ppersist tags...

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