| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | // Classe pour la gestion de persistance PPF des SegDataBlock | 
|---|
| 3 | //                         R. Ansari - Avril 2005 | 
|---|
| 4 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| 5 |  | 
|---|
| 6 | #ifndef FIOSEGDATABLOCK_H | 
|---|
| 7 | #define FIOSEGDATABLOCK_H | 
|---|
| 8 |  | 
|---|
| 9 | #include "machdefs.h" | 
|---|
| 10 | #include "ppersist.h" | 
|---|
| 11 | #include "segdatablock.h" | 
|---|
| 12 | #include "ppftpointerio.h" | 
|---|
| 13 | #include <typeinfo> | 
|---|
| 14 |  | 
|---|
| 15 | /*! | 
|---|
| 16 | \class SOPHYA::FIO_SegDataBlock | 
|---|
| 17 | \ingroup BaseTools | 
|---|
| 18 | Class implementing PPF persistence (PPersist handler) for segmented data structures | 
|---|
| 19 | (class SegDataBlock). | 
|---|
| 20 | \sa PPersist | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | namespace SOPHYA { | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | template <class T> | 
|---|
| 27 | class FIO_SegDataBlock : public  PPersist  { | 
|---|
| 28 | public: | 
|---|
| 29 | FIO_SegDataBlock() | 
|---|
| 30 | { | 
|---|
| 31 | dobj = NULL ; | 
|---|
| 32 | ownobj=true; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | //   FIO_SegDataBlock(string const & filename); | 
|---|
| 36 | FIO_SegDataBlock(SegDataBlock<T> & obj) | 
|---|
| 37 | { | 
|---|
| 38 | dobj = &obj; | 
|---|
| 39 | ownobj = false; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | FIO_SegDataBlock(SegDataBlock<T> * obj) | 
|---|
| 43 | { | 
|---|
| 44 | if (obj == NULL) | 
|---|
| 45 | throw ParmError("FIO_SegDataBlock<T>::FIO_SegDataBlock(* obj) obj=NULL (ppfwrapstlv.h)"); | 
|---|
| 46 | dobj = obj; | 
|---|
| 47 | ownobj = false; | 
|---|
| 48 | } | 
|---|
| 49 | virtual ~FIO_SegDataBlock() | 
|---|
| 50 | { | 
|---|
| 51 | if (ownobj && dobj)    delete dobj; | 
|---|
| 52 | } | 
|---|
| 53 | virtual AnyDataObj* DataObj() { return dobj; } | 
|---|
| 54 | virtual void        SetDataObj(AnyDataObj & o) | 
|---|
| 55 | { | 
|---|
| 56 | SegDataBlock<T> * po = dynamic_cast< SegDataBlock<T> * >(&o); | 
|---|
| 57 | if (po == NULL) { | 
|---|
| 58 | char buff[160]; | 
|---|
| 59 | sprintf(buff,"FIO_SegDataBlock<%s>::SetDataObj(%s) - Object type  error ! ", | 
|---|
| 60 | typeid(T).name(), typeid(o).name()); | 
|---|
| 61 | throw TypeMismatchExc(PExcLongMessage(buff)); | 
|---|
| 62 | } | 
|---|
| 63 | if (ownobj && dobj) delete dobj; | 
|---|
| 64 | dobj = po; ownobj = false; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | inline operator SegDataBlock<T>()  { return(*dobj); } | 
|---|
| 68 |  | 
|---|
| 69 | // Les methodes suivantes implementent les fonctionalites necessaires | 
|---|
| 70 | // au  partage de reference ds les fichiers PPF | 
|---|
| 71 | uint_8 getMemOId() const | 
|---|
| 72 | { | 
|---|
| 73 | return ( (dobj) ? dobj->DRefId() : 0 ); | 
|---|
| 74 | } | 
|---|
| 75 | void ShareDataReference(PPersist & pp) | 
|---|
| 76 | { | 
|---|
| 77 | FIO_SegDataBlock<T> *ppo = dynamic_cast< FIO_SegDataBlock<T> * >(&pp); | 
|---|
| 78 | if (ppo == NULL) throw TypeMismatchExc("FIO_SegDataBlock<T>::ShareDataReference() - Type Mismatch Error"); | 
|---|
| 79 | if (ppo->dobj) { | 
|---|
| 80 | if (dobj) dobj->Share(*(ppo->dobj)); | 
|---|
| 81 | else { dobj = new SegDataBlock<T>(*(ppo->dobj)); ownobj = true; } | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 | PPersist* CloneSharedReference() | 
|---|
| 85 | { | 
|---|
| 86 | FIO_SegDataBlock<T> * ppo = new FIO_SegDataBlock<T>; | 
|---|
| 87 | ppo->dobj = new SegDataBlock<T>(*(dobj)); | 
|---|
| 88 | ppo->ownobj = true; | 
|---|
| 89 | return(ppo); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | protected : | 
|---|
| 93 | virtual void       ReadSelf(PInPersist& is) | 
|---|
| 94 | { | 
|---|
| 95 | // On lit les 4 premiers uint_8 | 
|---|
| 96 | // 0: Numero de version   , 3 : reserve | 
|---|
| 97 | // 1: SegmentSize  2: NbSegments | 
|---|
| 98 | uint_8 itab[4]; | 
|---|
| 99 | is.Get(itab, 4); | 
|---|
| 100 | if (dobj == NULL) { | 
|---|
| 101 | dobj = new SegDataBlock<T>(itab[1], itab[2]); | 
|---|
| 102 | ownobj = true; | 
|---|
| 103 | } | 
|---|
| 104 | else dobj->SetSize(itab[1], itab[2]); | 
|---|
| 105 | // On lit les donnees par paquet ... | 
|---|
| 106 | for(uint_8 k=0; k<itab[2]; k++) | 
|---|
| 107 | PPF_TPointer_IO<T>::Read(is, dobj->GetSegment(k), itab[1]); | 
|---|
| 108 | // On lit la table des tags de positionnement ... | 
|---|
| 109 | vector<int_8> postags; | 
|---|
| 110 | is.GetPosTagTable(postags); | 
|---|
| 111 | } | 
|---|
| 112 | virtual void       WriteSelf(POutPersist& os) const | 
|---|
| 113 | { | 
|---|
| 114 | if (dobj == NULL) | 
|---|
| 115 | throw ParmError("FIO_SegDataBlock<T>::WriteSelf() dobj=NULL (fiosegdb.h)"); | 
|---|
| 116 | // On ecrit les 4  uint_8 | 
|---|
| 117 | // 0: Numero de version   , 3 : reserve | 
|---|
| 118 | // 1: SegmentSize  2: NbSegments | 
|---|
| 119 | uint_8 itab[4]; | 
|---|
| 120 | itab[0] = 1; | 
|---|
| 121 | itab[1] = dobj->SegmentSize(); | 
|---|
| 122 | itab[2] = dobj->NbSegments(); | 
|---|
| 123 | itab[3] = 0; | 
|---|
| 124 | os.Put(itab, 4); | 
|---|
| 125 | // On ecrit les donnees par paquets ... | 
|---|
| 126 | vector<int_8> postags; | 
|---|
| 127 | int_8 tag; | 
|---|
| 128 | for(uint_8 k=0; k<itab[2]; k++) { | 
|---|
| 129 | tag = os.WritePositionTag(); | 
|---|
| 130 | PPF_TPointer_IO<T>::Write(os, dobj->GetSegment(k), itab[1]); | 
|---|
| 131 | postags.push_back(tag); | 
|---|
| 132 | } | 
|---|
| 133 | // On ecrit la table des tags de positionnement | 
|---|
| 134 | // au cas ou on voudrait l'utiliser en mode swap | 
|---|
| 135 | os.PutPosTagTable(postags); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | // Variables membres | 
|---|
| 139 | SegDataBlock<T> * dobj;  // Le vecteur de la STL | 
|---|
| 140 | bool ownobj;  // true -> objet cree par le wrapper | 
|---|
| 141 | }; | 
|---|
| 142 |  | 
|---|
| 143 | /*! Writes the segmented data structure \b obj in the POutPersist stream \b os */ | 
|---|
| 144 | template <class T> | 
|---|
| 145 | inline POutPersist& operator << (POutPersist& os, SegDataBlock<T> & obj) | 
|---|
| 146 | { FIO_SegDataBlock<T> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| 147 | /*! Reads in and initializes the segmented data structure \b obj | 
|---|
| 148 | from the PInPersist stream \b is */ | 
|---|
| 149 | template <class T> | 
|---|
| 150 | inline PInPersist& operator >> (PInPersist& is, SegDataBlock<T> & obj) | 
|---|
| 151 | { FIO_SegDataBlock<T> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); } | 
|---|
| 152 |  | 
|---|
| 153 | } // Fin du namespace | 
|---|
| 154 |  | 
|---|
| 155 | #endif | 
|---|