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

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

Gestion typeid/typename. Gestion simultanee tag+reference

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