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