[241] | 1 | #include "machdefs.h"
|
---|
[251] | 2 | #include <stdio.h>
|
---|
| 3 | #include <sys/types.h>
|
---|
| 4 | #include <time.h>
|
---|
[241] | 5 | #include "pexceptions.h"
|
---|
[219] | 6 | #include "ppersist.h"
|
---|
[802] | 7 | #include "anydataobj.h"
|
---|
[2322] | 8 | #include <iostream>
|
---|
[241] | 9 | #include <typeinfo>
|
---|
[219] | 10 |
|
---|
[2475] | 11 | // ------------------------- Historique ---------------------------
|
---|
| 12 | // Le code de ppersist a ete separe en deux en Decembre 2003
|
---|
| 13 | // a partir de la version CVS (1.26 - ppersist.cc) et
|
---|
| 14 | // (1.19 - ppersist.h) .
|
---|
| 15 | // la partie sur l'ecriture des donnees de base et leurs tableaux
|
---|
| 16 | // a ete mis dans les fichiers ppfbinstream.h .cc
|
---|
| 17 | // (Classes PPFBinaryInputStream et PPFBinaryOutputStream
|
---|
| 18 | // -----------------------------------------------------------------
|
---|
[219] | 19 |
|
---|
| 20 |
|
---|
[802] | 21 | // strptime n'est pas defini sous Linux - Reza Mars 2000
|
---|
[1783] | 22 | #if defined(OS_LINUX) || defined(OS_MACOSX)
|
---|
[802] | 23 | extern "C" {
|
---|
[2111] | 24 | char *strptime(const char *buf, const char *format, struct tm *tm);
|
---|
[802] | 25 | }
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
[241] | 28 | #define MAXTAGLEN 255
|
---|
| 29 |
|
---|
[219] | 30 | //++
|
---|
[241] | 31 | // Class PIOPersist
|
---|
[219] | 32 | // Lib Outils++
|
---|
| 33 | // include ppersist.h
|
---|
| 34 | //
|
---|
[241] | 35 | // Root class for persistant files. Handles the registration of
|
---|
| 36 | // persistant classes
|
---|
[219] | 37 | //--
|
---|
| 38 |
|
---|
| 39 | //++
|
---|
[241] | 40 | // Links See
|
---|
[219] | 41 | // PPersist
|
---|
[241] | 42 | // PInPersist
|
---|
| 43 | // POutPersist
|
---|
[219] | 44 | //--
|
---|
| 45 |
|
---|
| 46 |
|
---|
[742] | 47 | MD5_CONTEXT PIOPersist::ctx;
|
---|
[802] | 48 | PIOPersist::ClassList * PIOPersist::ppclassList = NULL; // $CHECK$ Reza 26/04/99
|
---|
| 49 | map<string, uint_8> * PIOPersist::ppclassNameList = NULL;
|
---|
| 50 | map<string, uint_8> * PIOPersist::dobjclassNameList = NULL;
|
---|
[241] | 51 |
|
---|
[269] | 52 | //++
|
---|
| 53 | void
|
---|
| 54 | PIOPersist::Initialize()
|
---|
| 55 | // Initialisation globale (objets statiques) $CHECK$ Reza 26/04/99
|
---|
| 56 | //--
|
---|
| 57 | {
|
---|
[802] | 58 | ppclassList = new PIOPersist::ClassList;
|
---|
| 59 | ppclassNameList = new map<string, uint_8>;
|
---|
| 60 | dobjclassNameList = new map<string, uint_8>;
|
---|
[1900] | 61 | cout << " PIOPersist::Initialize() Starting Sophya Persistence management service " << endl;
|
---|
[269] | 62 | }
|
---|
[241] | 63 |
|
---|
[219] | 64 | //++
|
---|
| 65 | void
|
---|
[802] | 66 | PIOPersist::RegisterPPHandlerClass(uint_8 classId, string ppclass_name, ClassCreatorFunc f)
|
---|
[219] | 67 | //
|
---|
[802] | 68 | // Register a new persistence handler (PPersist) class.
|
---|
[2475] | 69 | // The classId is usually a hash of the class< name, and
|
---|
[802] | 70 | // ppclass_name is typeid(PPersistClass).name() .
|
---|
| 71 | // This method is called only through the PPersistRegistrar template
|
---|
[219] | 72 | //
|
---|
| 73 | //--
|
---|
| 74 | {
|
---|
[802] | 75 | if (ppclassList->size() && (ppclassList->find(classId) != ppclassList->end()) ) {
|
---|
| 76 | cerr << "RegisterClass : Error, " << hex << classId << dec
|
---|
| 77 | << " already registered." << endl;
|
---|
[816] | 78 | throw(DuplicateIdExc("PIOPersist::RegisterPPHandlerClass() Already registered (1)"));
|
---|
[802] | 79 | }
|
---|
| 80 | if (ppclassNameList->size() && (ppclassNameList->find(ppclass_name) != ppclassNameList->end())) {
|
---|
| 81 | cerr << "RegisterClass : Error (2) " << ppclass_name
|
---|
| 82 | << " already registered." << endl;
|
---|
[816] | 83 | throw(DuplicateIdExc("PIOPersist::RegisterPPHandlerClass() Already registered(2)"));
|
---|
[219] | 84 | }
|
---|
| 85 |
|
---|
[802] | 86 | (*ppclassList)[classId] = f;
|
---|
| 87 | (*ppclassNameList)[ppclass_name] = classId;
|
---|
[219] | 88 | }
|
---|
| 89 |
|
---|
[802] | 90 | //++
|
---|
| 91 | void
|
---|
| 92 | PIOPersist::RegisterDataObjClass(uint_8 classId, string class_name)
|
---|
| 93 | // Register a new DataObj class corresponding to a PPersist classId
|
---|
| 94 | // class_typename should be typeid(DataObject).name()
|
---|
| 95 | //--
|
---|
| 96 | {
|
---|
[816] | 97 | if (ppclassList->find(classId) == ppclassList->end() ) {
|
---|
| 98 | cerr << "PIOPersist::RegisterDataObjClass() Error (1) "
|
---|
| 99 | << hex << classId << dec << " Not Found !" << endl;
|
---|
| 100 | throw( NotFoundExc("PIOPersist::RegisterDataObjClass() Not found classId ") );
|
---|
| 101 | }
|
---|
| 102 | if (dobjclassNameList->size() && (dobjclassNameList->find(class_name) != dobjclassNameList->end())) {
|
---|
| 103 | cerr << "PIOPersist::RegisterDataObjClass() Error (2)" << class_name
|
---|
| 104 | << " already registered." << endl;
|
---|
| 105 | throw(DuplicateIdExc("PIOPersist::RegisterDataObjClass() - Already registered"));
|
---|
| 106 | }
|
---|
[219] | 107 |
|
---|
[802] | 108 | (*dobjclassNameList)[class_name] = classId;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | // class_typename should be typeid(DataObject).name(), to be
|
---|
| 112 | // used by POutPersist::PutDataObject() methods.
|
---|
| 113 |
|
---|
[241] | 114 | PIOPersist::ClassCreatorFunc
|
---|
| 115 | PIOPersist::FindCreatorFunc(uint_8 classId)
|
---|
[802] | 116 | // Returns the PPersist class creator function for the specified classId
|
---|
[219] | 117 | {
|
---|
[802] | 118 | ClassList::iterator i = ppclassList->find(classId);
|
---|
| 119 | if (i == ppclassList->end()) throw(NotFoundExc("PIOPersist::FindCreatorFunc() Not found classId"));
|
---|
[241] | 120 | return (*i).second;
|
---|
[219] | 121 | }
|
---|
| 122 |
|
---|
[802] | 123 | string
|
---|
| 124 | PIOPersist::getPPClassName(uint_8 classId)
|
---|
| 125 | // Returns the PPersist class name for the specified classId
|
---|
| 126 | {
|
---|
| 127 | map<string, uint_8>::iterator i;
|
---|
| 128 | for (i= ppclassNameList->begin(); i != ppclassNameList->end(); i++)
|
---|
| 129 | if ( (*i).second == classId ) return (*i).first;
|
---|
[219] | 130 |
|
---|
[802] | 131 | throw(NotFoundExc("PIOPersist::getPPClassName() Not found classId"));
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | uint_8
|
---|
| 135 | PIOPersist::getPPClassId(string const & typ_name)
|
---|
| 136 | // Returns the classId for the specified PPersist class type name
|
---|
| 137 | {
|
---|
| 138 | map<string, uint_8>::iterator i = ppclassNameList->find(typ_name);
|
---|
| 139 | if (i == ppclassNameList->end())
|
---|
| 140 | throw(NotFoundExc("PIOPersist::getPPClassId() Not found className"));
|
---|
| 141 | return (*i).second;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | uint_8
|
---|
| 145 | PIOPersist::getPPClassId(PPersist const & ppo)
|
---|
| 146 | // Returns the classId for the specified PPersist class
|
---|
| 147 | {
|
---|
| 148 | string typ_name = typeid(ppo).name() ;
|
---|
| 149 | return (getPPClassId(typ_name) );
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | string
|
---|
| 154 | PIOPersist::getDataObjClassName(uint_8 classId)
|
---|
| 155 | // Returns the PPersist class name for the specified classId
|
---|
| 156 | {
|
---|
| 157 | map<string, uint_8>::iterator i;
|
---|
| 158 | for (i= dobjclassNameList->begin(); i != dobjclassNameList->end(); i++)
|
---|
| 159 | if ( (*i).second == classId ) return (*i).first;
|
---|
| 160 |
|
---|
| 161 | throw(NotFoundExc("PIOPersist::getDataObjClassName() Not found classId"));
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | uint_8
|
---|
| 165 | PIOPersist::getDataObjClassId(string const & typ_name)
|
---|
| 166 | // Returns the classId for the specified PPersist class type name
|
---|
| 167 | {
|
---|
| 168 | map<string, uint_8>::iterator i = dobjclassNameList->find(typ_name);
|
---|
| 169 | if (i == dobjclassNameList->end())
|
---|
| 170 | throw(NotFoundExc("PIOPersist::getDataObjClassId() Not found className"));
|
---|
| 171 | return (*i).second;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | uint_8
|
---|
| 175 | PIOPersist::getDataObjClassId(AnyDataObj const & o)
|
---|
| 176 | // Returns the classId for the specified PPersist class
|
---|
| 177 | {
|
---|
| 178 | string typ_name = typeid(o).name() ;
|
---|
| 179 | return (getDataObjClassId(typ_name) );
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[2476] | 182 | static inline void bswap8_hash(void* p)
|
---|
| 183 | {
|
---|
| 184 | uint_8 tmp = *(uint_8*)p;
|
---|
| 185 | *(uint_8*)p = ((tmp >> (7*8)) & 0x000000FF) |
|
---|
| 186 | ((tmp >> (5*8)) & 0x0000FF00) |
|
---|
| 187 | ((tmp >> (3*8)) & 0x00FF0000) |
|
---|
| 188 | ((tmp >> (1*8)) & 0xFF000000) |
|
---|
| 189 | ((tmp & 0xFF000000) << (1*8)) |
|
---|
| 190 | ((tmp & 0x00FF0000) << (3*8)) |
|
---|
| 191 | ((tmp & 0x0000FF00) << (5*8)) |
|
---|
| 192 | ((tmp & 0x000000FF) << (7*8));
|
---|
| 193 | }
|
---|
[802] | 194 |
|
---|
[1202] | 195 |
|
---|
| 196 |
|
---|
| 197 | uint_8 PIOPersist::Hash(string const& typname) {
|
---|
| 198 | md5_init(&ctx);
|
---|
| 199 | md5_write(&ctx, (unsigned char*) typname.c_str(), typname.size());
|
---|
| 200 | md5_final(&ctx);
|
---|
| 201 | uint_8 hash1 = *((uint_8*) ctx.buf);
|
---|
| 202 | uint_8 hash2 = *((uint_8*) (ctx.buf+8));
|
---|
| 203 | #if IS_BIG_ENDIAN
|
---|
[2476] | 204 | bswap8_hash(&hash1);
|
---|
| 205 | bswap8_hash(&hash2);
|
---|
[1202] | 206 | #endif
|
---|
| 207 |
|
---|
| 208 | return (hash1+hash2);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 |
|
---|
[219] | 212 | //++
|
---|
| 213 | // Class PPersist
|
---|
| 214 | // Lib Outils++
|
---|
| 215 | // include ppersist.h
|
---|
| 216 | //
|
---|
| 217 | // Classe de base pour des objets persistants. Pour créer un objet
|
---|
| 218 | // persistant :
|
---|
| 219 | // - Hériter de PPersist.
|
---|
| 220 | // - Définir un numéro d'identification de la classe, unique dans Peida
|
---|
| 221 | // - Implémenter "ClassId()"
|
---|
| 222 | // - Implémenter "WriteSelf" et "ReadSelf", qui doivent écrire toutes les variables
|
---|
| 223 | // membres que l'on souhaite écrire, et les relire dans le même ordre.
|
---|
| 224 | // Pour écrire une référence à un objet : l'objet doit être un PPersist,
|
---|
| 225 | // et il suffit d'appeler "Write" sur cet objet, et "PPersistMgr::ReadObject".
|
---|
| 226 | // Si plusieurs objets font référence au même, pour éviter de l'écrire plusieurs
|
---|
| 227 | // fois, il faut que cet objet soit un PShPersist.
|
---|
| 228 | // - Pour que le fichier soit portable, écrire et lire les variables membres en utilisant
|
---|
| 229 | // les fonctions PutXX/GetXX de PInPersist/POutPersist.
|
---|
| 230 | //
|
---|
| 231 | // Attention: les méthodes à redéfinir sont WriteSelf et ReadSelf, mais il ne faut jamais
|
---|
| 232 | // les appeler directement. Seuls Write et Read peuvent être appelées par l'utilisateur.
|
---|
| 233 | //--
|
---|
| 234 |
|
---|
| 235 | //++
|
---|
[241] | 236 | // Links See
|
---|
[219] | 237 | // PInPersist
|
---|
| 238 | // POutPersist
|
---|
[241] | 239 | // PIOPersist
|
---|
[219] | 240 | //--
|
---|
| 241 |
|
---|
| 242 | //++
|
---|
| 243 | void
|
---|
| 244 | PPersist::Write(string const& fn) const
|
---|
| 245 | //
|
---|
| 246 | // Ecrit l'objet dans un nouveau fichier ppersist "fn".
|
---|
| 247 | //--
|
---|
| 248 | {
|
---|
| 249 | POutPersist of(fn);
|
---|
| 250 | Write(of);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | //++
|
---|
| 254 | void
|
---|
| 255 | PPersist::Read(string const& fn)
|
---|
| 256 | //
|
---|
| 257 | // Relit l'objet dans le fichier ppersist "fn". Il faut connaître a priori
|
---|
| 258 | // le type de l'objet. Pour une relecture avec création automatique du bon
|
---|
| 259 | // objet, utiliser PPersistMgr::ReadObject.
|
---|
| 260 | //--
|
---|
| 261 | {
|
---|
| 262 | PInPersist inf(fn);
|
---|
| 263 | Read(inf);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | //++
|
---|
| 267 | void
|
---|
| 268 | PPersist::Write(POutPersist& s) const
|
---|
| 269 | //
|
---|
| 270 | // Ecrit l'objet dans le fichier PPersist.
|
---|
| 271 | //--
|
---|
| 272 | {
|
---|
[802] | 273 | s.PutPPObject(this);
|
---|
[219] | 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | //++
|
---|
| 278 | void
|
---|
| 279 | PPersist::Read(PInPersist& s)
|
---|
| 280 | //
|
---|
| 281 | // Relit l'objet dans le fichier ppersist. Il faut connaître a priori
|
---|
| 282 | // le type de l'objet. Pour une relecture avec création automatique du bon
|
---|
[241] | 283 | // objet, utiliser PInPersist::ReadObject.
|
---|
| 284 | // Il faut qu'on soit un objet ecrit
|
---|
[219] | 285 | //--
|
---|
| 286 | {
|
---|
[241] | 287 | // We should be the exact type
|
---|
| 288 | // Check tag value
|
---|
[802] | 289 | unsigned char ppstype,ppstag;
|
---|
[588] | 290 | s.GetTypeTag(ppstype);
|
---|
[802] | 291 | if ( (ppstype != PInPersist::PPS_OBJECT) && ( ppstype != PInPersist::PPS_REFERENCE ) ) {
|
---|
[241] | 292 | }
|
---|
[802] | 293 | if (ppstype == PInPersist::PPS_OBJECT) {
|
---|
| 294 | // Check class id
|
---|
| 295 | uint_8 classId;
|
---|
| 296 | s.GetRawU8(classId);
|
---|
| 297 | uint_8 oid,oid2;
|
---|
| 298 | s.GetRawU8(oid);
|
---|
| 299 | if (classId != PIOPersist::getPPClassId(*this) )
|
---|
| 300 | throw FileFormatExc("PPersist::Read (): not the same object type");
|
---|
| 301 | ReadSelf(s);
|
---|
| 302 | // Read the ENDOBJECT
|
---|
| 303 | s.GetRawUByte(ppstag);
|
---|
| 304 | if (ppstag != PInPersist::PPS_ENDOBJECT)
|
---|
| 305 | throw FileFormatExc("PPersist::Read() No PPS_ENDOBJECT tag");
|
---|
| 306 | s.GetRawU8(oid2);
|
---|
| 307 | if (oid2 != oid)
|
---|
| 308 | throw FileFormatExc("PPersist::Read() Inconsistent PPS-OId at PPS_ENDOBJECT ");
|
---|
| 309 | s.KeepOId(oid, *this); // Object should be kept with its PPS_OId (if oid > 0)
|
---|
[241] | 310 | }
|
---|
[802] | 311 | else if ( ppstype == PInPersist::PPS_REFERENCE )
|
---|
| 312 | s.ReadReference(*this);
|
---|
[219] | 313 |
|
---|
[802] | 314 | else throw FileFormatExc("PPersist::Read() : not an object in flow");
|
---|
| 315 |
|
---|
[219] | 316 | }
|
---|
| 317 |
|
---|
| 318 | //++
|
---|
[241] | 319 | void
|
---|
| 320 | PPersist::Write(POutPersist& s, string const& tag) const
|
---|
[219] | 321 | //
|
---|
[241] | 322 | // Ecrit l'objet dans le fichier PPersist avec un tag
|
---|
[219] | 323 | //--
|
---|
| 324 | {
|
---|
[2441] | 325 | s.WriteNameTag(tag);
|
---|
[802] | 326 | s.PutPPObject(this);
|
---|
[219] | 327 | }
|
---|
| 328 |
|
---|
| 329 | //++
|
---|
| 330 | void
|
---|
[241] | 331 | PPersist::ReadAtTag(PInPersist& s, string const& tag)
|
---|
[219] | 332 | //
|
---|
| 333 | // Lit l'objet à la position du tag numéro "tagid".
|
---|
| 334 | //--
|
---|
| 335 | {
|
---|
[2459] | 336 | if (!s.GotoNameTag(tag))
|
---|
[241] | 337 | throw NotFoundExc("PPersist::ReadAtTag tag not found");
|
---|
| 338 | Read(s);
|
---|
[219] | 339 | }
|
---|
| 340 |
|
---|
[802] | 341 | // Renvoie l'identificateur de l'objet - par defaut=0
|
---|
[219] | 342 |
|
---|
[802] | 343 | uint_8
|
---|
| 344 | PPersist::getMemOId() const
|
---|
| 345 | {
|
---|
| 346 | return(0);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | // Ces deux methodes doivent etre redefinies si getMemOId() renvoie non nul (>0)
|
---|
| 350 | // ShareDataReference() et CloneSharedReference()
|
---|
| 351 | void
|
---|
| 352 | PPersist::ShareDataReference(PPersist & pcs)
|
---|
| 353 | {
|
---|
| 354 | throw NotAvailableOperation("PPersist::ShareDataReference() - Unsupported operation !");
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | PPersist *
|
---|
| 358 | PPersist::CloneSharedReference()
|
---|
| 359 | {
|
---|
| 360 | throw NotAvailableOperation("PPersist::CloneSharedReference() - Unsupported operation !");
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[219] | 363 | //++
|
---|
| 364 | // virtual void PPersist::ReadSelf(PInPersist&)=0
|
---|
| 365 | // Méthode virtuelle pure à redéfinir. Elle est appelée par Read
|
---|
| 366 | // et PPersistMgr::ReadObject. Il faut relire les variables membres,
|
---|
| 367 | // dans l'ordre où elles ont été écrites par WriteSelf.
|
---|
| 368 | // virtual void PPersist::WriteSelf(POutPersist&) const=0
|
---|
| 369 | // Méthode virtuelle pure à redéfinir. Elle est appelée par Write.
|
---|
| 370 | // Il faut écrire les variables membres,
|
---|
| 371 | // dans l'ordre où elles seront relues par ReadSelf.
|
---|
| 372 | //--
|
---|
| 373 |
|
---|
| 374 |
|
---|
| 375 |
|
---|
| 376 | //++
|
---|
| 377 | // Class PInPersist
|
---|
| 378 | // Lib Outils++
|
---|
| 379 | // include ppersist.h
|
---|
| 380 | //
|
---|
| 381 | // Fichier d'objets persistants, en lecture.
|
---|
| 382 | //--
|
---|
| 383 |
|
---|
| 384 | //++
|
---|
| 385 | PInPersist::PInPersist(string const& flnm, bool scan)
|
---|
[2475] | 386 | : PPFBinaryInputStream(flnm, scan)
|
---|
[219] | 387 | //
|
---|
| 388 | // Constructeur. Ouvre le fichier.
|
---|
| 389 | //--
|
---|
| 390 | {
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 |
|
---|
| 394 |
|
---|
| 395 | PInPersist::~PInPersist()
|
---|
| 396 | {
|
---|
[802] | 397 | ObjList::iterator i;
|
---|
| 398 | for(i=objList.begin(); i!= objList.end(); i++)
|
---|
| 399 | if ((*i).second) delete (*i).second;
|
---|
[219] | 400 | }
|
---|
| 401 |
|
---|
| 402 |
|
---|
[802] | 403 |
|
---|
[582] | 404 | string
|
---|
[802] | 405 | PInPersist::GetTagClassName(int itag)
|
---|
| 406 | {
|
---|
| 407 | // A faire
|
---|
| 408 | // if (itag<0 || itag >= (int)tags.size()) return "";
|
---|
| 409 | // map<string, int_8>::iterator i = tags.begin();
|
---|
| 410 | // for (int j=0; j<itag; j++) i++;
|
---|
| 411 | // uint_8 cid = (*i).second;
|
---|
| 412 | // return(GetClassName(cid));
|
---|
| 413 | return("");
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[241] | 416 | PPersist*
|
---|
| 417 | PInPersist::ReadObject()
|
---|
| 418 | {
|
---|
[802] | 419 | return(GetPPObject());
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | void
|
---|
| 423 | PInPersist::GetObject(AnyDataObj & o)
|
---|
| 424 | {
|
---|
| 425 | GetPPObject(&o);
|
---|
| 426 | return;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | void
|
---|
| 430 | PInPersist::GetObject(AnyDataObj & o, string tagname)
|
---|
| 431 | {
|
---|
[2459] | 432 | GotoNameTag(tagname);
|
---|
[802] | 433 | GetPPObject(&o);
|
---|
| 434 | return;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | PPersist*
|
---|
| 438 | PInPersist::GetPPObject(AnyDataObj * po)
|
---|
| 439 | {
|
---|
[241] | 440 | // Get tag
|
---|
[802] | 441 | unsigned char ppstype;
|
---|
[588] | 442 | GetTypeTag(ppstype);
|
---|
[241] | 443 | if (ppstype != PPS_OBJECT && ppstype != PPS_REFERENCE && ppstype != PPS_NULL) {
|
---|
[256] | 444 | throw FileFormatExc("PInPersist::ReadObject : not an object in flow");
|
---|
[241] | 445 | }
|
---|
| 446 |
|
---|
| 447 | if (ppstype == PPS_NULL) {
|
---|
| 448 | return NULL;
|
---|
| 449 | } else if (ppstype == PPS_OBJECT) {
|
---|
| 450 | // Get class id
|
---|
| 451 | uint_8 classId;
|
---|
| 452 | GetRawU8(classId);
|
---|
[802] | 453 | uint_8 oid,oid2;
|
---|
| 454 | GetRawU8(oid);
|
---|
[241] | 455 |
|
---|
| 456 | // Get factory method
|
---|
| 457 | ClassCreatorFunc f = FindCreatorFunc(classId);
|
---|
| 458 | if (!f) {
|
---|
| 459 | throw NotFoundExc("PInPersist::ReadObject class not registered");
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | // Create object
|
---|
| 463 | PPersist* object = f();
|
---|
[802] | 464 | // If a DataObject was specified , we assign it to the PPersistObject
|
---|
| 465 | if (po != NULL) object->SetDataObj(*po);
|
---|
| 466 |
|
---|
[241] | 467 | object->ReadSelf(*this);
|
---|
[802] | 468 | unsigned char ppstag;
|
---|
| 469 | // Read the ENDOBJECT
|
---|
| 470 | GetRawUByte(ppstag);
|
---|
| 471 | if (ppstag != PPS_ENDOBJECT)
|
---|
| 472 | throw FileFormatExc("PInPersist::ReadObject No PPS_ENDOBJECT tag");
|
---|
| 473 | GetRawU8(oid2);
|
---|
| 474 | if (oid2 != oid)
|
---|
| 475 | throw FileFormatExc("PInPersist::ReadObject Inconsistent PPS-OId at PPS_ENDOBJECT ");
|
---|
| 476 |
|
---|
| 477 | KeepOId(oid, *object);
|
---|
[241] | 478 | return object;
|
---|
[802] | 479 | }
|
---|
| 480 | else if (ppstype == PPS_REFERENCE)
|
---|
| 481 | return ReadReference();
|
---|
| 482 |
|
---|
| 483 | else throw FileFormatExc("PInPersist::ReadObject invalide Tag Type !");
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 |
|
---|
| 487 |
|
---|
| 488 | void
|
---|
| 489 | PInPersist::ReadReference(PPersist & ppo)
|
---|
[241] | 490 | {
|
---|
[802] | 491 | PPersist * pr = ReadReference();
|
---|
| 492 | ppo.ShareDataReference(*pr);
|
---|
[241] | 493 | }
|
---|
| 494 |
|
---|
[802] | 495 |
|
---|
| 496 | PPersist *
|
---|
| 497 | PInPersist::ReadReference()
|
---|
| 498 | {
|
---|
| 499 | uint_8 oid;
|
---|
| 500 | int_8 pos;
|
---|
| 501 | GetRawU8(oid);
|
---|
| 502 | GetRawI8(pos);
|
---|
| 503 | // cerr << " DBG - PInPersist::ReadReference-A " << oid << " Pos= " << pos << endl;
|
---|
| 504 | map<uint_8, PPersist *>::iterator i = objList.find(oid);
|
---|
| 505 | if (i != objList.end()) return (*i).second;
|
---|
| 506 | else { // We may have skeeped it !
|
---|
| 507 | // Let's try to read it
|
---|
| 508 | int_8 cpos;
|
---|
| 509 | cpos = s->tellg();
|
---|
| 510 | s->seekg(pos);
|
---|
| 511 | PPersist* ppo = ReadObject();
|
---|
| 512 | s->seekg(cpos);
|
---|
| 513 | delete ppo;
|
---|
| 514 | // cerr << " DBG - PInPersist::ReadReference-B ... " << endl;
|
---|
| 515 |
|
---|
| 516 | map<uint_8, PPersist *>::iterator i2 = objList.find(oid);
|
---|
| 517 | if (i2 == objList.end())
|
---|
| 518 | throw FileFormatExc("PInPersist::ReadReference() Not found PPS_OId ");
|
---|
| 519 | return (*i2).second;
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 |
|
---|
| 524 | void
|
---|
| 525 | PInPersist::KeepOId(uint_8 oid, PPersist & ppo)
|
---|
| 526 | {
|
---|
| 527 | if ((oid&0x1) == 0) return; // This is not an object which can be referenced
|
---|
| 528 | // cerr << " DBG - PInPersist::KeepOId() " << oid << endl;
|
---|
| 529 | if ((objList.size() > 0) && (objList.find(oid) != objList.end()) ) {
|
---|
[2459] | 530 | // Ceci ne devrait arriver que si on lit dans le desordre (avec GotoNameTag)
|
---|
[802] | 531 | // et pas avec une lecture sequentielle ... Reza 03/2000
|
---|
| 532 | // cerr << "PInPersist::KeepOId()/Warning - already present PPS_ObjectId ! " << oid << endl;
|
---|
| 533 | if (seqread) throw FileFormatExc("PInPersist::KeepOId() already present PPS_ObjectId ");
|
---|
| 534 | PPersist *pp = (*objList.find(oid)).second;
|
---|
| 535 | ppo.ShareDataReference(*pp);
|
---|
| 536 | }
|
---|
| 537 | else {
|
---|
| 538 | PPersist * npp = ppo.CloneSharedReference();
|
---|
| 539 | if (npp == NULL) throw PError("PInPersist::KeepOId() NULL returned by PPersist.Clone() ! ");
|
---|
| 540 | objList[oid] = npp;
|
---|
| 541 | }
|
---|
| 542 | return;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[219] | 545 | //++
|
---|
| 546 | // Class POutPersist
|
---|
| 547 | // Lib Outils++
|
---|
| 548 | // include ppersist.h
|
---|
| 549 | //
|
---|
| 550 | // Fichier d'objets persistants, en écriture.
|
---|
| 551 | //--
|
---|
| 552 |
|
---|
| 553 |
|
---|
| 554 | //++
|
---|
| 555 | // POutPersist(string const& flnm, int endianness = PPersist::PPS_NATIVE)
|
---|
| 556 | //
|
---|
| 557 | // Crée un nouveau fichier ppersist. Par défaut, il est petit=boutien
|
---|
| 558 | // sur machines petit-boutiennes, et gros-boutien sur machines
|
---|
| 559 | // gros-boutiennes. On peut explicitement spécifier PPersist::PPS_LITTLE_ENDIAN
|
---|
| 560 | // ou PPersist::PPS_BIG_ENDIAN.
|
---|
| 561 | //--
|
---|
| 562 | POutPersist::POutPersist(string const& flnm, int endianness)
|
---|
[2475] | 563 | : PPFBinaryOutputStream(flnm, endianness)
|
---|
[219] | 564 | {
|
---|
[802] | 565 | // PPS (POutPersist stream) Object Id initialisation
|
---|
| 566 | pps_OId = 0;
|
---|
[219] | 567 | }
|
---|
| 568 |
|
---|
| 569 | POutPersist::~POutPersist()
|
---|
| 570 | {
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 |
|
---|
[241] | 574 | void
|
---|
[802] | 575 | POutPersist::PutObject(AnyDataObj & o)
|
---|
| 576 | {
|
---|
| 577 | ClassCreatorFunc f = FindCreatorFunc(getDataObjClassId(o));
|
---|
| 578 | if (!f)
|
---|
| 579 | throw NotFoundExc("PInPersist::PutObject() class not registered");
|
---|
| 580 | PPersist* ppo = f();
|
---|
| 581 | ppo->SetDataObj(o);
|
---|
| 582 | PutPPObject(ppo);
|
---|
[219] | 583 | }
|
---|
| 584 |
|
---|
[241] | 585 | void
|
---|
[802] | 586 | POutPersist::PutObject(AnyDataObj & o, string tagname)
|
---|
[241] | 587 | {
|
---|
[2441] | 588 | WriteNameTag(tagname);
|
---|
[802] | 589 | PutObject(o);
|
---|
| 590 | }
|
---|
[219] | 591 |
|
---|
[802] | 592 |
|
---|
| 593 | void
|
---|
| 594 | POutPersist::PutPPObject(PPersist const* obj)
|
---|
| 595 | {
|
---|
| 596 | if (serializeNullAndRepeat(obj)) return; // NULL object or already written in stream
|
---|
| 597 |
|
---|
| 598 | // We have to write the object
|
---|
| 599 | uint_8 oid = assignObjectId(obj); // We assing a PPS Object Id
|
---|
| 600 | PutRawUByte(PPS_OBJECT); // We write the Object Tag
|
---|
| 601 | PutRawU8(getPPClassId(*obj)); // Writing the PPersist ClassId
|
---|
| 602 | PutRawU8(oid); // Write the PPS Object Id
|
---|
[241] | 603 | obj->WriteSelf(*this);
|
---|
[802] | 604 | PutRawUByte(PPS_ENDOBJECT); // We write the End-Of-Object Tag
|
---|
| 605 | PutRawU8(oid); // and again its PPS Object Id
|
---|
[241] | 606 | }
|
---|
[219] | 607 |
|
---|
[241] | 608 | bool
|
---|
| 609 | POutPersist::serializeNullAndRepeat(PPersist const* x)
|
---|
[219] | 610 | {
|
---|
[241] | 611 | if (x == NULL) {
|
---|
[802] | 612 | PutRawUByte(PPS_NULL);
|
---|
[241] | 613 | return true;
|
---|
| 614 | }
|
---|
[219] | 615 |
|
---|
[802] | 616 | int_8 pos;
|
---|
| 617 | uint_8 id = findObjectId(x, pos);
|
---|
| 618 | if (id > 0) {
|
---|
| 619 | PutRawUByte(PPS_REFERENCE);
|
---|
| 620 | PutRawU8(id); // Writing the corresponding object Id
|
---|
| 621 | PutRawI8(pos); // The original object position
|
---|
[241] | 622 | return true;
|
---|
| 623 | }
|
---|
[219] | 624 |
|
---|
[802] | 625 | return false; // Object have to be written in stream ...
|
---|
[219] | 626 | }
|
---|
| 627 |
|
---|
[802] | 628 | uint_8
|
---|
[241] | 629 | POutPersist::assignObjectId(PPersist const* x)
|
---|
[219] | 630 | {
|
---|
[802] | 631 | pps_OId += 16; // We keep the three first bytes for future usage
|
---|
| 632 | // Bit 1 non zero -> Object can be referenced
|
---|
| 633 | uint_8 id = pps_OId;
|
---|
| 634 | uint_8 mid = x->getMemOId();
|
---|
| 635 | if (mid > 0) {
|
---|
| 636 | int_8 pos;
|
---|
| 637 | if (findObjectId(x,pos) > 0)
|
---|
| 638 | throw PError("POutPersist::assignObjectId() Error - Already serialized object ! ");
|
---|
[821] | 639 | id += 1; // Bit 1 non zero -> Object can be referenced
|
---|
[802] | 640 | objreftag rt;
|
---|
| 641 | rt.ppsoid = id;
|
---|
[2476] | 642 | // cout << " DBG-rt.ppspos = s->tellp(); " << endl;
|
---|
[802] | 643 | rt.ppspos = s->tellp();
|
---|
[2476] | 644 | // cout << " DBG-rt.ppspos = s->tellp(); = " << rt.ppspos << endl;
|
---|
[802] | 645 | objList[mid] = rt;
|
---|
| 646 | }
|
---|
[241] | 647 | return id;
|
---|
[219] | 648 | }
|
---|
| 649 |
|
---|
[802] | 650 | uint_8
|
---|
| 651 | POutPersist::findObjectId(PPersist const* x, int_8 & pos)
|
---|
[219] | 652 | {
|
---|
[802] | 653 | pos = -1;
|
---|
| 654 | uint_8 mid = x->getMemOId();
|
---|
| 655 | if (mid == 0) return(0);
|
---|
| 656 | ObjList::iterator i = objList.find(mid);
|
---|
| 657 | if (i == objList.end()) return 0;
|
---|
| 658 | pos = (*i).second.ppspos;
|
---|
| 659 | return (*i).second.ppsoid;
|
---|
[219] | 660 | }
|
---|
| 661 |
|
---|
[241] | 662 |
|
---|