source: Sophya/trunk/SophyaLib/BaseTools/swsegdb.h@ 2703

Last change on this file since 2703 was 2698, checked in by ansari, 20 years ago

1/ Simplification de la classe gestionnairee PPersist ObjFile<T> (objfio.h)
2/ Ajout DECL_TEMP_SPEC ds ppftpointerio.h
3/ Nom specifique pour methode SegDBInterface::GetSegment() const devenu
SegDBInterface::GetCstSegment() const pour eviter l'appel a la methode non const
et adapatation classes derivees (segdatablock.h et swsegdb.h)
4/ Ajout SkipToNextObjet() ds PInPersist::ReadObject() (ppersist.cc) et protection contre pointeur NULL -> new DVList pour la lecture PPersist de DVList

Reza - 27 Avril 2005

File size: 6.1 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2// Gestion de block de donnees swapable
3// R. Ansari Mars 2005
4// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
5#ifndef SWSEGDATABLOCK_H
6#define SWSEGDATABLOCK_H
7
8#include "machdefs.h"
9#include "segdatablock.h"
10#include "pexceptions.h"
11#include <vector>
12#include <typeinfo>
13
14namespace SOPHYA {
15
16////////////////////////////////////////////////////////////////
17//// ------------- Class DataSwapperInterface --------------- //
18//// ---------------- Class SwSegDataBlock ------------------ //
19////////////////////////////////////////////////////////////////
20
21/*!
22 \class SOPHYA::DataSwapperInterface
23 \ingroup BaseTools
24 Interface definition for data swapper (pure virtual) classes to be used
25 with SOPHYA::SwSegDataBlock classes.
26*/
27template <class T>
28class DataSwapperInterface {
29public:
30 virtual ~DataSwapperInterface() { }
31 //! Swap out the data array pointed by \b d with size \b sz
32 virtual int_8 WriteToSwap(const T * d, size_t sz, int_8 idx, int_8 oswp=0, bool osw=false) = 0;
33 //! Swap in the data array pointed by \b d with size \b sz
34 virtual void ReadFromSwap(int_8 idx, int_8 swp, T* d, size_t sz) = 0;
35};
36
37//! Segmented data structure with swap management
38/*!
39 \class SOPHYA::SwSegDataBlock
40 \ingroup BaseTools
41 Segmented data structure with swap space management.
42*/
43template <class T>
44class SwSegDataBlock : public SegDBInterface<T> {
45public:
46 //! Constructor - creation from swap position tags (values)
47 SwSegDataBlock(DataSwapperInterface<T> & dsw, vector<int_8> const & swpos, size_t segsz)
48 {
49 mSRef = NULL;
50 SetSize(segsz, swpos.size());
51 mSRef->swapper = &dsw;
52 mSRef->swp = swpos;
53 for(size_t k=0; k<mSRef->fgwp.size(); k++) mSRef->fgwp[k] = true;
54 }
55 //! Constructor - optional specification of segment size and number of segments
56 SwSegDataBlock(DataSwapperInterface<T> & dsw, size_t segsz=32, size_t nbseg=0)
57 {
58 mSRef = NULL;
59 SetSize(segsz, nbseg);
60 mSRef->swapper = &dsw;
61 }
62 //! copy constructor - shares the data
63 SwSegDataBlock(const SwSegDataBlock<T>& a)
64 {
65 mSRef = a.mSRef;
66 mSRef->nref++;
67 }
68
69 //! Destructor. The memory is freed when the last object referencing the data segment is destroyed
70 virtual ~SwSegDataBlock() { Delete(); }
71 //! Adds one segment to the data structure - returns the pointer to the allocated segment.
72 virtual size_t Extend()
73 {
74 mSRef->swp.push_back(0);
75 mSRef->fgwp.push_back(false);
76 return mSRef->swp.size();
77 }
78 //! Changes the data segment size and reallocates the memory segments
79 // segsz : Segment size ; nbseg : Number of data segments
80 virtual void SetSize(size_t segsz, size_t nbseg=0)
81 {
82 Delete();
83 mSRef = new SWSDREF;
84 mSRef->nref = 1;
85 mSRef->segsize = segsz;
86 mSRef->dsid = AnyDataObj::getUniqueId();
87 mSRef->buff = new T[segsz];
88 mSRef->bidx = -1;
89 mSRef->fgcstbuff = true;
90 for(size_t k=0; k<nbseg; k++) {
91 mSRef->swp.push_back(0);
92 mSRef->fgwp.push_back(false);
93 }
94 }
95 //! Return the segment size data structure
96 virtual size_t SegmentSize() const { return mSRef->segsize; }
97 //! Return the number of data segments
98 virtual size_t NbSegments() const { return mSRef->swp.size(); } ;
99 //! Return the current size of the segmented data structure
100 inline size_t Size() const { return mSRef->swp.size()*mSRef->segsize; }
101 //! Return the pointer to data segment \b k
102 virtual T* GetSegment(size_t k)
103 {
104 getSeg(k);
105 mSRef->fgcstbuff = false;
106 return mSRef->buff;
107 }
108 //! Return the const (read-only) pointer to data segment \b k
109 virtual T const * GetCstSegment(size_t k) const
110 {
111 getSeg(k);
112 mSRef->fgcstbuff = true;
113 return mSRef->buff;
114 }
115
116 //! Equal operator. Shares the data with \b a
117 inline SwSegDataBlock<T>& operator = (const SwSegDataBlock<T>& a)
118 {
119 Delete();
120 mSRef = a.mSRef;
121 mSRef->nref++;
122 return *this;
123 }
124
125 //! Empties all memory buffers to swap stream
126 void SwapOutBuffer() const
127 {
128 if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
129 int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
130 mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
131 mSRef->swp[mSRef->bidx] = nswp;
132 mSRef->fgwp[mSRef->bidx] = true;
133 mSRef->bidx = -1;
134 mSRef->fgcstbuff = true;
135 }
136 }
137 //! Return the position tag (swap position) table, after call to SwapOutBuffer()
138 std::vector< int_8 > & GetSwapPosTagTable() const
139 {
140 SwapOutBuffer();
141 return mSRef->swp;
142 }
143
144protected:
145 SwSegDataBlock()
146 {
147 throw ForbiddenError("SwSegDataBlock() default constructor not allowed (swsegdb.h)");
148 }
149 void Delete()
150 {
151 if (mSRef == NULL) return;
152 mSRef->nref--;
153 if (mSRef->nref > 0) { mSRef = NULL; return; }
154 delete[] mSRef->buff;
155 delete mSRef;
156 mSRef = NULL;
157 }
158 void getSeg(size_t k) const
159 {
160 if (k == mSRef->bidx) return ;
161 if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
162 int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
163 mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
164 mSRef->swp[mSRef->bidx] = nswp;
165 mSRef->fgwp[mSRef->bidx] = true;
166 }
167 if (mSRef->fgwp[k])
168 mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], mSRef->buff, mSRef->segsize);
169 else { delete[] mSRef->buff; mSRef->buff = new T[mSRef->segsize]; }
170 mSRef->bidx = k;
171 return;
172 }
173
174 typedef struct {
175 size_t nref; // Number of references to the data structure
176 uint_8 dsid; // Data structure id
177 size_t segsize; // data segment size
178 mutable std::vector< int_8 > swp; // swap position tag for each segment
179 mutable std::vector< bool > fgwp; // swap flag (true = already swapped) for each segment
180 mutable T * buff; // Data buffer
181 mutable int_8 bidx; // segment index (number) corresponding to buffer
182 mutable bool fgcstbuff; // true : this is a constant T * buff<
183 DataSwapperInterface<T> * swapper; // Data swapper
184 } SWSDREF;
185
186 SWSDREF * mSRef; // SWSDREF structure for reference sharing
187};
188
189
190} // Fin du namespace
191
192#endif
Note: See TracBrowser for help on using the repository browser.