[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
|
---|
[802] | 9 | // R. Ansari LAL IN2P3/CNRS 03/2000
|
---|
[219] | 10 |
|
---|
[2475] | 11 | // -------------------- Historique -----------------------
|
---|
| 12 | // Separe en deux (CVS version 1.19 - ppersist.h)
|
---|
| 13 | // Classes PPFBinaryIn/OutputStream ds ppfbinstream.h
|
---|
| 14 | // -------------------------------------------------------
|
---|
[219] | 15 |
|
---|
[2475] | 16 |
|
---|
[241] | 17 | #include "machdefs.h"
|
---|
| 18 | #include "pexceptions.h"
|
---|
[742] | 19 | #include "gnumd5.h"
|
---|
[2475] | 20 | #include "ppfbinstream.h"
|
---|
[241] | 21 |
|
---|
[219] | 22 | #include <string>
|
---|
| 23 | #include <map>
|
---|
[241] | 24 | #include <vector>
|
---|
| 25 | #include <typeinfo>
|
---|
[219] | 26 |
|
---|
| 27 |
|
---|
[269] | 28 | // Classe de base pour les objets qui peuvent devenir PPersist
|
---|
| 29 |
|
---|
[552] | 30 | namespace SOPHYA {
|
---|
[241] | 31 |
|
---|
[269] | 32 | class AnyDataObj;
|
---|
| 33 |
|
---|
[241] | 34 | class PIOPersist;
|
---|
| 35 | class PInPersist;
|
---|
| 36 | class POutPersist;
|
---|
| 37 | class PPersist;
|
---|
[219] | 38 |
|
---|
[895] | 39 | //! Persistent (delegate or mixin) base class
|
---|
[241] | 40 | class PPersist {
|
---|
| 41 | public:
|
---|
| 42 | virtual ~PPersist() {}
|
---|
[219] | 43 |
|
---|
[241] | 44 | void Write(string const& fn) const;
|
---|
| 45 | void Read(string const& fn);
|
---|
[219] | 46 |
|
---|
[241] | 47 | virtual void Write(POutPersist&) const;
|
---|
| 48 | void Read(PInPersist& s); // Reads the type tag and the object
|
---|
| 49 | void Write(POutPersist&, string const& tag) const;
|
---|
| 50 | void ReadAtTag(PInPersist& s, string const& tag);
|
---|
[269] | 51 |
|
---|
[754] | 52 | virtual AnyDataObj* DataObj()=0; // Retourne l'objet reel
|
---|
[802] | 53 | virtual void SetDataObj(AnyDataObj &)=0;
|
---|
[754] | 54 |
|
---|
[802] | 55 | virtual uint_8 getMemOId() const ; // Renvoie l'identificateur de l'objet - par defaut=0
|
---|
| 56 | // Ces deux methodes doivent etre redefinies si getMemOId() renvoie non nul (>0)
|
---|
| 57 | virtual void ShareDataReference(PPersist &);
|
---|
| 58 | virtual PPersist* CloneSharedReference();
|
---|
| 59 | // doit etre surcharge pour renvoyer un mem-oid correct
|
---|
[241] | 60 | protected:
|
---|
| 61 | virtual void ReadSelf(PInPersist&)=0;
|
---|
[802] | 62 | virtual void WriteSelf(POutPersist&) const=0;
|
---|
[219] | 63 |
|
---|
[241] | 64 | friend class PInPersist;
|
---|
| 65 | friend class POutPersist;
|
---|
[219] | 66 | };
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 |
|
---|
[2475] | 70 | //! Ancestor for PInPersist and POutPersist object serialisation streams.
|
---|
[895] | 71 | // Handles (statically) the registration of classes.
|
---|
[219] | 72 |
|
---|
[241] | 73 | class PIOPersist {
|
---|
| 74 | public:
|
---|
| 75 | typedef PPersist* (*ClassCreatorFunc)();
|
---|
| 76 |
|
---|
[802] | 77 | static void RegisterPPHandlerClass(uint_8 classId, string ppclass_name, ClassCreatorFunc f);
|
---|
| 78 | static void RegisterDataObjClass(uint_8 classId, string class_name);
|
---|
| 79 |
|
---|
[241] | 80 | static ClassCreatorFunc FindCreatorFunc(uint_8 classId);
|
---|
[802] | 81 |
|
---|
| 82 | static string getPPClassName(uint_8 classId);
|
---|
| 83 | static uint_8 getPPClassId(string const & typ_name);
|
---|
| 84 | static uint_8 getPPClassId(PPersist const & ppo);
|
---|
| 85 | static string getDataObjClassName(uint_8 classId);
|
---|
| 86 | static uint_8 getDataObjClassId(string const & typ_name);
|
---|
| 87 | static uint_8 getDataObjClassId(AnyDataObj const & o);
|
---|
| 88 |
|
---|
[1202] | 89 | static uint_8 Hash(string const& typname);
|
---|
[742] | 90 | static MD5_CONTEXT ctx;
|
---|
[241] | 91 |
|
---|
[269] | 92 | static void Initialize(); // Pour initialiser classList
|
---|
[802] | 93 |
|
---|
| 94 |
|
---|
[241] | 95 | private:
|
---|
[219] | 96 |
|
---|
[241] | 97 | typedef map<uint_8, ClassCreatorFunc, less<uint_8> > ClassList;
|
---|
[269] | 98 | // Pas de createur appele pour objets statiques sur Linux - $CHECK$ Reza 26/04/99
|
---|
[802] | 99 | static ClassList * ppclassList; // PPersist class list
|
---|
| 100 | static map<string, uint_8> * ppclassNameList; // PPersist classId = f(PPersistClassName)
|
---|
| 101 | static map<string, uint_8> * dobjclassNameList; // PPersist classId = f(DataObjClassName)
|
---|
[219] | 102 |
|
---|
[241] | 103 | };
|
---|
[219] | 104 |
|
---|
| 105 |
|
---|
[2441] | 106 | // TBD : use hash tables instead of maps. Check hashtbl status in STL.
|
---|
[219] | 107 |
|
---|
[895] | 108 | //! Input stream for PPersit objects.
|
---|
[2475] | 109 | class PInPersist : public PPFBinaryInputStream, public PIOPersist {
|
---|
[241] | 110 | public:
|
---|
| 111 | PInPersist(string const& flnm, bool scan=true);
|
---|
[2475] | 112 | virtual ~PInPersist();
|
---|
[219] | 113 |
|
---|
[2475] | 114 | // A faire - Reza Dec 2003
|
---|
| 115 | string GetTagClassName(int itag);
|
---|
[802] | 116 | // Object Reading
|
---|
[241] | 117 | PPersist* ReadObject();
|
---|
[802] | 118 | void GetObject(AnyDataObj & o);
|
---|
| 119 | void GetObject(AnyDataObj & o, string tagname);
|
---|
| 120 | PPersist* GetPPObject(AnyDataObj * po=NULL);
|
---|
[219] | 121 |
|
---|
[802] | 122 | // Reza 03/2000
|
---|
| 123 | // Methodes qui pourraient etre protected, mais doivent etre utilisables par PPersist
|
---|
| 124 | void ReadReference(PPersist & ppo); // Fill the ppo object from the reference tag
|
---|
| 125 | PPersist * ReadReference(); // Creates object from the reference tag
|
---|
| 126 | void KeepOId(uint_8 oid, PPersist & ppo); // Keeps the ppo in the objList
|
---|
| 127 |
|
---|
[241] | 128 | protected:
|
---|
[802] | 129 |
|
---|
| 130 | // already read objects
|
---|
| 131 | typedef map<uint_8, PPersist * > ObjList;
|
---|
[241] | 132 | ObjList objList;
|
---|
| 133 | friend class PPersist;
|
---|
| 134 | };
|
---|
[219] | 135 |
|
---|
[895] | 136 | //! Output stream for PPersit objects.
|
---|
[2475] | 137 | class POutPersist : public PPFBinaryOutputStream, public PIOPersist {
|
---|
[241] | 138 | public:
|
---|
| 139 | POutPersist(string const& flnm, int endianness = PPS_NATIVE);
|
---|
[2475] | 140 | virtual ~POutPersist();
|
---|
[219] | 141 |
|
---|
[2475] | 142 | // void Put(PPersist const* x) {PutPPObject(x);}
|
---|
[219] | 143 |
|
---|
[2441] | 144 | // Objet Write - Ecriture des objets
|
---|
| 145 | void PutPPObject (PPersist const*); // Like doing Write(stream) on PPersist object
|
---|
| 146 |
|
---|
| 147 | void PutObject(AnyDataObj & o); // Creates the corresponding PPersist Object and call Write()
|
---|
| 148 | void PutObject(AnyDataObj & o, string tagname);
|
---|
| 149 |
|
---|
| 150 |
|
---|
[241] | 151 | protected:
|
---|
| 152 | bool serializeNullAndRepeat(PPersist const* x);
|
---|
[802] | 153 | uint_8 findObjectId(PPersist const* x, int_8 & pos);
|
---|
| 154 | uint_8 assignObjectId(PPersist const* x);
|
---|
[219] | 155 |
|
---|
[802] | 156 | // objreftag contains the assigned POutStream Object Id and the stream position
|
---|
| 157 | // of the original written object
|
---|
| 158 | typedef struct { uint_8 ppsoid; int_8 ppspos; } objreftag;
|
---|
| 159 | // already serialized objects are kept in a map as a function of the Objects memory Id
|
---|
| 160 | typedef map<uint_8, objreftag, less<uint_8> > ObjList;
|
---|
[241] | 161 | ObjList objList;
|
---|
[802] | 162 | uint_8 pps_OId; // PPS Object Id
|
---|
[241] | 163 | };
|
---|
| 164 |
|
---|
[2441] | 165 |
|
---|
| 166 | // Le macro suivant permettent de simplifier la declaration
|
---|
| 167 | // des operateurs >> << sur les POutPersist et PInPersist
|
---|
[219] | 168 | #define RAWPERSISTIO(_Type_,_xtyp_) \
|
---|
[241] | 169 | inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
|
---|
| 170 | { \
|
---|
| 171 | c.Put##_xtyp_(data); \
|
---|
| 172 | return c; \
|
---|
| 173 | } \
|
---|
[219] | 174 | \
|
---|
[241] | 175 | inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
|
---|
| 176 | { \
|
---|
| 177 | c.Get##_xtyp_(data); \
|
---|
| 178 | return c; \
|
---|
| 179 | }
|
---|
[219] | 180 |
|
---|
[2441] | 181 | // On utilise le macro RAWPERSISTIO pour declarer POutPersist << et PInPersist >>
|
---|
| 182 | // pour les types de base r_4 r_8 int_4 int_8 ...
|
---|
[241] | 183 | RAWPERSISTIO(int_4,I4);
|
---|
| 184 | RAWPERSISTIO(uint_4,U4);
|
---|
| 185 | RAWPERSISTIO(int_2,I2);
|
---|
| 186 | RAWPERSISTIO(uint_2,U2);
|
---|
| 187 | RAWPERSISTIO(char,Byte);
|
---|
| 188 | RAWPERSISTIO(r_4,R4);
|
---|
| 189 | RAWPERSISTIO(r_8,R8);
|
---|
[2441] | 190 | RAWPERSISTIO(complex<r_4>,Z4);
|
---|
| 191 | RAWPERSISTIO(complex<r_8>,Z8);
|
---|
| 192 | RAWPERSISTIO(string,Str);
|
---|
[241] | 193 |
|
---|
[219] | 194 | #if 0
|
---|
| 195 | #define STRUCTPERSISTIO(_Type_, _field_, _size_) \
|
---|
[241] | 196 | inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
|
---|
| 197 | { \
|
---|
| 198 | c.PutBytes(&data._field_, _size_); \
|
---|
| 199 | return c; \
|
---|
| 200 | } \
|
---|
[219] | 201 | \
|
---|
[241] | 202 | inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
|
---|
| 203 | { \
|
---|
| 204 | c.GetBytes(&data._field_, _size_); \
|
---|
| 205 | return c; \
|
---|
| 206 | }
|
---|
[219] | 207 |
|
---|
| 208 | #endif
|
---|
[241] | 209 |
|
---|
[802] | 210 |
|
---|
| 211 | // --- Cela risque d'etre dangereux --- On le laisse au niveau des DataObj
|
---|
| 212 | // Reza 24/3/2000
|
---|
| 213 | // inline POutPersist& operator << (POutPersist& c, PPersist const& obj)
|
---|
| 214 | // {
|
---|
| 215 | // obj.Write(c);
|
---|
| 216 | // return c;
|
---|
| 217 | // }
|
---|
[241] | 218 |
|
---|
[802] | 219 | // inline PInPersist& operator >> (PInPersist& c, PPersist& obj)
|
---|
| 220 | // {
|
---|
| 221 | // obj.Read(c);
|
---|
| 222 | // return c;
|
---|
| 223 | // }
|
---|
| 224 |
|
---|
[241] | 225 | // Utility class to
|
---|
| 226 | // - compute the class ID from a MD5 hash of the class name
|
---|
| 227 | // - register classes with PIOPersist, through PPRegister macro
|
---|
[895] | 228 |
|
---|
| 229 | //! template class for handling the PPersist registration mechanism.
|
---|
[241] | 230 | template <class T>
|
---|
| 231 | class PPersistRegistrar {
|
---|
| 232 | public:
|
---|
| 233 | static PPersist* Create() {return new T();}
|
---|
[802] | 234 | static void Register(string id) { PIOPersist::RegisterPPHandlerClass(Hash(id), typeid(T).name(), Create); }
|
---|
[576] | 235 | static uint_8 Hash(string id) {
|
---|
| 236 | return PIOPersist::Hash(id);
|
---|
[241] | 237 | }
|
---|
| 238 | };
|
---|
| 239 |
|
---|
[576] | 240 | #define PPRegister(className) PPersistRegistrar<className>::Register(#className);
|
---|
[802] | 241 | #define DObjRegister(ppclassName, className) PIOPersist::RegisterDataObjClass(PIOPersist::Hash(#ppclassName), typeid(className).name());
|
---|
[754] | 242 |
|
---|
[2475] | 243 | } // namespace SOPHYA
|
---|
[219] | 244 |
|
---|
[2475] | 245 | // La classe PPFNameTag facilite la manipulation des Nametag ds les fichiers
|
---|
| 246 | // PPersist - definie ds ppfnametag.h
|
---|
| 247 | #include "ppfnametag.h"
|
---|
| 248 |
|
---|
| 249 |
|
---|
[219] | 250 | #endif
|
---|