[219] | 1 | #ifndef PPERSIST_H_SEEN
|
---|
| 2 | #define PPERSIST_H_SEEN
|
---|
| 3 |
|
---|
| 4 | // Classe mixin pour implementer une persistance pas trop bete.
|
---|
| 5 |
|
---|
| 6 | #include "defs.h"
|
---|
| 7 | #include "machine.h"
|
---|
| 8 | #include "perrors.h"
|
---|
| 9 | #include "pclassids.h"
|
---|
| 10 |
|
---|
| 11 | #include <string>
|
---|
| 12 | #include <list>
|
---|
| 13 | #include <map>
|
---|
| 14 | #include <functional>
|
---|
| 15 |
|
---|
| 16 | #if defined(__KCC__)
|
---|
| 17 | using std::string ;
|
---|
| 18 | #include <list.h>
|
---|
| 19 | #include <map.h>
|
---|
| 20 | #include <functional.h>
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
| 23 | #ifdef RFIO
|
---|
| 24 | #include "erostream.h"
|
---|
| 25 | #else
|
---|
| 26 | #include <iostream.h>
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | class PPersistMgr;
|
---|
| 31 | class PPersist;
|
---|
| 32 | class PShPersist;
|
---|
| 33 | class PInPersist;
|
---|
| 34 | class POutPersist;
|
---|
| 35 |
|
---|
| 36 | typedef less<int_4> Int4Compare;
|
---|
| 37 |
|
---|
| 38 | class PPersistMgr {
|
---|
| 39 | public:
|
---|
| 40 | typedef PPersist* (*ClassCreatorFunc)();
|
---|
| 41 |
|
---|
| 42 | static void RegisterClass(int_4 classId, ClassCreatorFunc f,
|
---|
| 43 | bool shared=false);
|
---|
| 44 | static PPersist* ReadObject(PInPersist&);
|
---|
| 45 |
|
---|
| 46 | static ClassCreatorFunc FindCreatorFunc(int_4 classId);
|
---|
| 47 | static ClassCreatorFunc FindCreatorFunc(int_4 classId, bool& isShared);
|
---|
| 48 |
|
---|
| 49 | static void Reset(); // Nouveau fichier, on oublie les objets partages.
|
---|
| 50 |
|
---|
| 51 | #ifndef __DECCXX
|
---|
| 52 | private:
|
---|
| 53 | #endif
|
---|
| 54 | struct ClassListEntry {
|
---|
| 55 | bool shared;
|
---|
| 56 | ClassCreatorFunc f;
|
---|
| 57 | };
|
---|
| 58 | #ifdef __DECCXX
|
---|
| 59 | private:
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
| 62 | typedef map<int_4, ClassListEntry, Int4Compare> ClassList;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | static ClassList* classList;
|
---|
| 66 | friend class PeidaInitiator;
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | class PPersist EXC_AWARE {
|
---|
| 72 | public:
|
---|
| 73 | enum {PPS_NATIVE = -1, PPS_LITTLE_ENDIAN = 0, PPS_BIG_ENDIAN = 1};
|
---|
| 74 | virtual ~PPersist() {}
|
---|
| 75 | virtual int_4 ClassId() const=0;
|
---|
| 76 |
|
---|
| 77 | void Write(string const& fn) const;
|
---|
| 78 | void Read(string const& fn);
|
---|
| 79 |
|
---|
| 80 | virtual void Write(POutPersist&) const;
|
---|
| 81 | void Read(PInPersist& s); // Se lit, y compris le tag de type.
|
---|
| 82 | int_4 Write(POutPersist&, int_4 key) const;
|
---|
| 83 | int_4 Write(POutPersist&, int_4 key, string& nom) const;
|
---|
| 84 | void ReadAtTag(PInPersist& s, int_4 tagid);
|
---|
| 85 | void ReadAtKey(PInPersist& s, int_4 key);
|
---|
| 86 | protected:
|
---|
| 87 | virtual void ReadSelf(PInPersist&)=0; // A redefinir...
|
---|
| 88 | virtual void WriteSelf(POutPersist&) const=0; // A redefinir...
|
---|
| 89 |
|
---|
| 90 | friend class PPersistMgr;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | class PShPersist : public PPersist{
|
---|
| 94 | public:
|
---|
| 95 | PShPersist() :mObjectID(0) {}
|
---|
| 96 | virtual ~PShPersist();
|
---|
| 97 | virtual void Write(POutPersist&) const;
|
---|
| 98 | static PShPersist* FindObject(int_4 objId);
|
---|
| 99 |
|
---|
| 100 | private:
|
---|
| 101 | typedef map<int_4, PShPersist*, Int4Compare> ObjList;
|
---|
| 102 |
|
---|
| 103 | static ObjList* objList;
|
---|
| 104 | /*mutable*/ int_4 mObjectID;
|
---|
| 105 | friend class PPersistMgr;
|
---|
| 106 | friend class PeidaInitiator;
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | class PInPersist {
|
---|
| 112 | public:
|
---|
| 113 | PInPersist(string const& flnm, bool scan=true);
|
---|
| 114 | ~PInPersist();
|
---|
| 115 |
|
---|
| 116 | bool GotoTag(int_4 num);
|
---|
| 117 | bool GotoKey(int_4 key, int rang=0);
|
---|
| 118 | int NbKey(int_4 key);
|
---|
| 119 | void ListTags();
|
---|
| 120 | inline int_4 NbTags() { return(mNTags); }
|
---|
| 121 | int_4 TagKey(int_4 num, int_4& cid, int_4& ln);
|
---|
| 122 | string TagName(int_4 num);
|
---|
| 123 |
|
---|
| 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 |
|
---|
| 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 |
|
---|
| 162 | int Version() {return version;}
|
---|
| 163 | string CreationDate() { return creationdate; }
|
---|
| 164 |
|
---|
| 165 | protected:
|
---|
| 166 | void Scan();
|
---|
| 167 | char* GetCStr(uint_2 l);
|
---|
| 168 | #ifdef RFIO
|
---|
| 169 | erosifstream *s;
|
---|
| 170 | #else
|
---|
| 171 | istream* s;
|
---|
| 172 | #endif
|
---|
| 173 | int bigEndian;
|
---|
| 174 | int version;
|
---|
| 175 |
|
---|
| 176 | string fName;
|
---|
| 177 | string creationdate;
|
---|
| 178 |
|
---|
| 179 | struct PPFTag {
|
---|
| 180 | int_8 popos;
|
---|
| 181 | int_4 cid;
|
---|
| 182 | int_4 key;
|
---|
| 183 | uint_2 lnom;
|
---|
| 184 | char* nom;
|
---|
| 185 | };
|
---|
| 186 | PPFTag* mTags;
|
---|
| 187 | int_4 mNTags;
|
---|
| 188 | list<char*> mSbuffs;
|
---|
| 189 | char* mSbuf;
|
---|
| 190 | int mSbsz;
|
---|
| 191 | };
|
---|
| 192 |
|
---|
| 193 | class POutPersist {
|
---|
| 194 | public:
|
---|
| 195 | POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE);
|
---|
| 196 | ~POutPersist();
|
---|
| 197 |
|
---|
| 198 | int_4 WriteTag(int_4 key, char const * name=NULL);
|
---|
| 199 | void PutByte (char c);
|
---|
| 200 | void PutBytes(void const* ptr, size_t bytes);
|
---|
| 201 | void PutR4 (r_4);
|
---|
| 202 | void PutR4s (r_4 const*, size_t);
|
---|
| 203 | void PutR8 (r_8);
|
---|
| 204 | void PutR8s (r_8 const*, size_t);
|
---|
| 205 | void PutI2 (int_2);
|
---|
| 206 | void PutI2s (int_2 const*, size_t);
|
---|
| 207 | void PutU2 (uint_2);
|
---|
| 208 | void PutU2s (uint_2 const*, size_t);
|
---|
| 209 | void PutI4 (int_4);
|
---|
| 210 | void PutI4s (int_4 const*, size_t);
|
---|
| 211 | void PutU4 (uint_4);
|
---|
| 212 | void PutU4s (uint_4 const*, size_t);
|
---|
| 213 | void PutI8 (int_8);
|
---|
| 214 | void PutI8s (int_8 const*, size_t);
|
---|
| 215 | void PutU8 (uint_8);
|
---|
| 216 | void PutU8s (uint_8 const*, size_t);
|
---|
| 217 | void PutLine (char const* ptr, size_t len=0);
|
---|
| 218 |
|
---|
| 219 | void Put(char c) {PutByte(c);}
|
---|
| 220 | void Put(r_4 x) {PutR4(x);}
|
---|
| 221 | void Put(r_8 x) {PutR8(x);}
|
---|
| 222 | void Put(uint_2 x) {PutU2(x);}
|
---|
| 223 | void Put(int_2 x) {PutI2(x);}
|
---|
| 224 | void Put(uint_4 x) {PutU4(x);}
|
---|
| 225 | void Put(int_4 x) {PutI4(x);}
|
---|
| 226 | void Put(uint_8 x) {PutU8(x);}
|
---|
| 227 | void Put(int_8 x) {PutI8(x);}
|
---|
| 228 | void Put(r_4 const* x, size_t n) {PutR4s(x,n);}
|
---|
| 229 | void Put(r_8 const* x, size_t n) {PutR8s(x,n);}
|
---|
| 230 | void Put(uint_2 const* x, size_t n) {PutU2s(x,n);}
|
---|
| 231 | void Put(int_2 const* x, size_t n) {PutI2s(x,n);}
|
---|
| 232 | void Put(uint_4 const* x, size_t n) {PutU4s(x,n);}
|
---|
| 233 | void Put(int_4 const* x, size_t n) {PutI4s(x,n);}
|
---|
| 234 | void Put(uint_8 const* x, size_t n) {PutU8s(x,n);}
|
---|
| 235 | void Put(int_8 const* x, size_t n) {PutI8s(x,n);}
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | protected:
|
---|
| 239 | #ifdef RFIO
|
---|
| 240 | erosofstream *s;
|
---|
| 241 | #else
|
---|
| 242 | ostream* s;
|
---|
| 243 | #endif
|
---|
| 244 | int bigEndian;
|
---|
| 245 | int_4 numTag;
|
---|
| 246 | int_8 previous;
|
---|
| 247 | };
|
---|
| 248 |
|
---|
| 249 | /*
|
---|
| 250 | // Ceci est dangereux car un template a priorite sur un changement de type, meme trivial...
|
---|
| 251 |
|
---|
| 252 | template <class T>
|
---|
| 253 | POutPersist& operator << (POutPersist& c, T const& data)
|
---|
| 254 | {
|
---|
| 255 | c.PutBytes(&data, sizeof(T));
|
---|
| 256 | return c;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | template <class T>
|
---|
| 260 | PInPersist& operator >> (PInPersist& c, T& data)
|
---|
| 261 | {
|
---|
| 262 | c.GetBytes(&data, sizeof(T));
|
---|
| 263 | return c;
|
---|
| 264 | }
|
---|
| 265 | */
|
---|
| 266 |
|
---|
| 267 | #define RAWPERSISTIO(_Type_,_xtyp_) \
|
---|
| 268 | inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
|
---|
| 269 | { \
|
---|
| 270 | c.Put##_xtyp_(data); \
|
---|
| 271 | return c; \
|
---|
| 272 | } \
|
---|
| 273 | \
|
---|
| 274 | inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
|
---|
| 275 | { \
|
---|
| 276 | c.Get##_xtyp_(data); \
|
---|
| 277 | return c; \
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | RAWPERSISTIO(int_4,I4)
|
---|
| 281 | RAWPERSISTIO(uint_4,U4)
|
---|
| 282 | RAWPERSISTIO(int_2,I2)
|
---|
| 283 | RAWPERSISTIO(uint_2,U2)
|
---|
| 284 | RAWPERSISTIO(char,Byte)
|
---|
| 285 | RAWPERSISTIO(r_4,R4)
|
---|
| 286 | RAWPERSISTIO(r_8,R8)
|
---|
| 287 |
|
---|
| 288 | #if 0
|
---|
| 289 | #define STRUCTPERSISTIO(_Type_, _field_, _size_) \
|
---|
| 290 | inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
|
---|
| 291 | { \
|
---|
| 292 | c.PutBytes(&data._field_, _size_); \
|
---|
| 293 | return c; \
|
---|
| 294 | } \
|
---|
| 295 | \
|
---|
| 296 | inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
|
---|
| 297 | { \
|
---|
| 298 | c.GetBytes(&data._field_, _size_); \
|
---|
| 299 | return c; \
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | #endif
|
---|
| 303 |
|
---|
| 304 | inline POutPersist& operator << (POutPersist& c, PPersist const& obj)
|
---|
| 305 | {
|
---|
| 306 | obj.Write(c);
|
---|
| 307 | return c;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | inline PInPersist& operator >> (PInPersist& c, PPersist& obj)
|
---|
| 311 | {
|
---|
| 312 | obj.Read(c);
|
---|
| 313 | return c;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | // Fabrication de fonction Create automatique, et enregistrement...
|
---|
| 317 | template <class T>
|
---|
| 318 | class PPersistRegistrar {
|
---|
| 319 | public:
|
---|
| 320 | static PPersist* Create() {return new T();}
|
---|
| 321 | static void Register() {PPersistMgr::RegisterClass(T::classId,Create);}
|
---|
| 322 | };
|
---|
| 323 |
|
---|
| 324 | #define PPRegister(className) PPersistRegistrar<className>::Register();
|
---|
| 325 |
|
---|
| 326 | #endif
|
---|