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

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

new md5

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