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