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

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

Restruction de Sophya en modules plus petit (TArray , SkyMap, HiStats, ...)

Reza 2/3/2000

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