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