source: Sophya/trunk/SophyaLib/BaseTools/ppersist.h@ 4053

Last change on this file since 4053 was 4051, checked in by ansari, 14 years ago

modifs ds PPersist (classe PIOPersist) pour permettre l'enregistrement automatique (1ere lecture ou ecriture) de PPFWrapperSTLVector<T> (gestionnaire ppersist de std::vector<T> , Reza+cmv 25/02/2012

File size: 9.8 KB
RevLine 
[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]30namespace 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;
[4051]48 virtual void Read(PInPersist& s); // Reads the type tag and the object
[241]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
[895]70// Handles (statically) the registration of classes.
[219]71
[241]72 class PIOPersist {
73 public:
74 typedef PPersist* (*ClassCreatorFunc)();
75
[802]76 static void RegisterPPHandlerClass(uint_8 classId, string ppclass_name, ClassCreatorFunc f);
77 static void RegisterDataObjClass(uint_8 classId, string class_name);
78
[2657]79 static void ListPPHandlers();
80 static void ListDataObjClasses();
81
[241]82 static ClassCreatorFunc FindCreatorFunc(uint_8 classId);
[802]83
84 static string getPPClassName(uint_8 classId);
85 static uint_8 getPPClassId(string const & typ_name);
86 static uint_8 getPPClassId(PPersist const & ppo);
[4051]87 static bool checkPPClassId(PPersist const & ppo);
88
[802]89 static string getDataObjClassName(uint_8 classId);
90 static uint_8 getDataObjClassId(string const & typ_name);
91 static uint_8 getDataObjClassId(AnyDataObj const & o);
92
[1202]93 static uint_8 Hash(string const& typname);
[742]94 static MD5_CONTEXT ctx;
[241]95
[269]96 static void Initialize(); // Pour initialiser classList
[802]97
98
[241]99 private:
[219]100
[241]101 typedef map<uint_8, ClassCreatorFunc, less<uint_8> > ClassList;
[269]102 // Pas de createur appele pour objets statiques sur Linux - $CHECK$ Reza 26/04/99
[802]103 static ClassList * ppclassList; // PPersist class list
104 static map<string, uint_8> * ppclassNameList; // PPersist classId = f(PPersistClassName)
105 static map<string, uint_8> * dobjclassNameList; // PPersist classId = f(DataObjClassName)
[219]106
[241]107 };
[219]108
109
[2441]110// TBD : use hash tables instead of maps. Check hashtbl status in STL.
[219]111
[895]112//! Input stream for PPersit objects.
[2475]113 class PInPersist : public PPFBinaryInputStream, public PIOPersist {
[241]114 public:
[2477]115 PInPersist(RawInOutStream * is, bool ad, bool scan=false);
[241]116 PInPersist(string const& flnm, bool scan=true);
[2475]117 virtual ~PInPersist();
[219]118
[2475]119 // A faire - Reza Dec 2003
120 string GetTagClassName(int itag);
[802]121 // Object Reading
[241]122 PPersist* ReadObject();
[802]123 void GetObject(AnyDataObj & o);
124 void GetObject(AnyDataObj & o, string tagname);
125 PPersist* GetPPObject(AnyDataObj * po=NULL);
[219]126
[802]127 // Reza 03/2000
128 // Methodes qui pourraient etre protected, mais doivent etre utilisables par PPersist
129 void ReadReference(PPersist & ppo); // Fill the ppo object from the reference tag
130 PPersist * ReadReference(); // Creates object from the reference tag
131 void KeepOId(uint_8 oid, PPersist & ppo); // Keeps the ppo in the objList
132
[241]133 protected:
[802]134
135 // already read objects
136 typedef map<uint_8, PPersist * > ObjList;
[241]137 ObjList objList;
138 friend class PPersist;
139 };
[219]140
[895]141//! Output stream for PPersit objects.
[2475]142 class POutPersist : public PPFBinaryOutputStream, public PIOPersist {
[241]143 public:
[2477]144 POutPersist(RawInOutStream* os, bool ad, int endianness = PPS_NATIVE);
[241]145 POutPersist(string const& flnm, int endianness = PPS_NATIVE);
[2475]146 virtual ~POutPersist();
[219]147
[2475]148 // void Put(PPersist const* x) {PutPPObject(x);}
[219]149
[2441]150 // Objet Write - Ecriture des objets
151 void PutPPObject (PPersist const*); // Like doing Write(stream) on PPersist object
152
153 void PutObject(AnyDataObj & o); // Creates the corresponding PPersist Object and call Write()
154 void PutObject(AnyDataObj & o, string tagname);
155
156
[241]157 protected:
158 bool serializeNullAndRepeat(PPersist const* x);
[802]159 uint_8 findObjectId(PPersist const* x, int_8 & pos);
160 uint_8 assignObjectId(PPersist const* x);
[219]161
[802]162 // objreftag contains the assigned POutStream Object Id and the stream position
163 // of the original written object
164 typedef struct { uint_8 ppsoid; int_8 ppspos; } objreftag;
165 // already serialized objects are kept in a map as a function of the Objects memory Id
166 typedef map<uint_8, objreftag, less<uint_8> > ObjList;
[241]167 ObjList objList;
[802]168 uint_8 pps_OId; // PPS Object Id
[2477]169 int wobj_level; // Niveau d'imbrication lors de l'ecriture d'objet
[241]170 };
171
[2805]172//! \cond
[2441]173// Le macro suivant permettent de simplifier la declaration
174// des operateurs >> << sur les POutPersist et PInPersist
[219]175#define RAWPERSISTIO(_Type_,_xtyp_) \
[241]176 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
177 { \
178 c.Put##_xtyp_(data); \
179 return c; \
180 } \
[219]181 \
[241]182 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
183 { \
184 c.Get##_xtyp_(data); \
185 return c; \
186 }
[219]187
[2441]188// On utilise le macro RAWPERSISTIO pour declarer POutPersist << et PInPersist >>
189// pour les types de base r_4 r_8 int_4 int_8 ...
[2660]190 RAWPERSISTIO(int_8,I8);
191 RAWPERSISTIO(uint_8,U8);
[241]192 RAWPERSISTIO(int_4,I4);
193 RAWPERSISTIO(uint_4,U4);
194 RAWPERSISTIO(int_2,I2);
195 RAWPERSISTIO(uint_2,U2);
196 RAWPERSISTIO(char,Byte);
197 RAWPERSISTIO(r_4,R4);
198 RAWPERSISTIO(r_8,R8);
[2441]199 RAWPERSISTIO(complex<r_4>,Z4);
200 RAWPERSISTIO(complex<r_8>,Z8);
201 RAWPERSISTIO(string,Str);
[241]202
[219]203#if 0
204#define STRUCTPERSISTIO(_Type_, _field_, _size_) \
[241]205 inline POutPersist& operator << (POutPersist& c, _Type_ const& data) \
206 { \
207 c.PutBytes(&data._field_, _size_); \
208 return c; \
209 } \
[219]210 \
[241]211 inline PInPersist& operator >> (PInPersist& c, _Type_& data) \
212 { \
213 c.GetBytes(&data._field_, _size_); \
214 return c; \
215 }
[219]216
217#endif
[802]218
[2805]219//! \endcond
220
[802]221// --- Cela risque d'etre dangereux --- On le laisse au niveau des DataObj
222// Reza 24/3/2000
223// inline POutPersist& operator << (POutPersist& c, PPersist const& obj)
224// {
225// obj.Write(c);
226// return c;
227// }
[241]228
[802]229// inline PInPersist& operator >> (PInPersist& c, PPersist& obj)
230// {
231// obj.Read(c);
232// return c;
233// }
234
[241]235 // Utility class to
236 // - compute the class ID from a MD5 hash of the class name
237 // - register classes with PIOPersist, through PPRegister macro
[895]238
239//! template class for handling the PPersist registration mechanism.
[241]240 template <class T>
241 class PPersistRegistrar {
242 public:
243 static PPersist* Create() {return new T();}
[802]244 static void Register(string id) { PIOPersist::RegisterPPHandlerClass(Hash(id), typeid(T).name(), Create); }
[576]245 static uint_8 Hash(string id) {
246 return PIOPersist::Hash(id);
[241]247 }
248 };
249
[2774]250#define PPRegister(className) PPersistRegistrar< className >::Register( #className );
[802]251#define DObjRegister(ppclassName, className) PIOPersist::RegisterDataObjClass(PIOPersist::Hash(#ppclassName), typeid(className).name());
[754]252
[2475]253} // namespace SOPHYA
[219]254
[2475]255// La classe PPFNameTag facilite la manipulation des Nametag ds les fichiers
256// PPersist - definie ds ppfnametag.h
257#include "ppfnametag.h"
258
259
[219]260#endif
Note: See TracBrowser for help on using the repository browser.