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