NeoPZ
TPZCircBufferedStream.cpp
Go to the documentation of this file.
2 #include <iostream>
3 #include <stdexcept>
4 #include <string.h>
5 
6 
8 
10  fBuffer = new char[fNAllocatedBytes];
11  fFirst = fBuffer;
12  fSize = 0;
13  fLast = fBuffer - 1;
14 }
15 
17  : TPZStream(other), fBuffer(NULL) {
18  *this = other;
19 }
20 
24  if (fBuffer)
25  delete[] fBuffer;
26  fBuffer = new char[fNAllocatedBytes];
27  memcpy(fBuffer, other.fBuffer, other.fSize);
28  fSize = other.fSize;
29  fFirst = fBuffer;
30  fLast = fBuffer - 1 + fSize;
31  return *this;
32 }
33 
35 
37  const unsigned int nBytesOther = other.fSize;
38  char *temp = new char[nBytesOther];
39  other.ReadFromBuffer(temp, nBytesOther);
40  WriteToBuffer(temp, nBytesOther);
41  delete[] temp;
42  return *this;
43 }
44 
47  const unsigned int nBytesOther = other.fSize;
48  char *temp = new char[nBytesOther];
49  other.ConstRead(temp, nBytesOther);
50  WriteToBuffer(temp, nBytesOther);
51  delete[] temp;
52  return *this;
53 }
54 
55 void TPZCircBufferedStream::ReadFromBuffer(char *dest, const size_t &nBytes) {
56  if (nBytes > fSize) {
57  std::string msg("TPZCircBufferedStream: Cannot read ");
58  msg.append(std::to_string(nBytes));
59  msg.append(" bytes; there are only ");
60  msg.append(std::to_string(fSize));
61  msg.append(" available.");
62  PZError << msg << std::endl;
63  DebugStop();
64  }
65  char *endBuffer = fBuffer + fNAllocatedBytes;
66 
67  if (fFirst + nBytes < endBuffer) {
68  // direct reading (we do not need to cycle to the beginning of the
69  // buffer to read)
70  memcpy(dest, fFirst, nBytes);
71  fFirst += nBytes;
72  } else {
73  // we need to read past the end of the buffer.
74  const size_t nBytesRead(endBuffer - fFirst);
75  memcpy(dest, fFirst, nBytesRead);
76  if (nBytes != nBytesRead) {
77  memcpy(dest + nBytesRead, fBuffer, nBytes - nBytesRead);
78  }
79  fFirst = fBuffer + nBytes - nBytesRead;
80  }
81 
82  fSize -= nBytes;
83 }
84 
85 void TPZCircBufferedStream::ConstRead(char *dest, const size_t &nBytes) const {
86  ConstReadFromBuffer(dest, nBytes);
87 }
88 
90  const size_t &nBytes) const {
91  if (nBytes > fSize) {
92  std::string msg("TPZCircBufferedStream: Cannot read ");
93  msg.append(std::to_string(nBytes));
94  msg.append(" bytes; there are only ");
95  msg.append(std::to_string(fSize));
96  msg.append(" available.");
97  PZError << msg << std::endl;
98  DebugStop();
99  }
100  char *endBuffer = fBuffer + fNAllocatedBytes;
101 
102  if (fFirst + nBytes < endBuffer) {
103  // direct reading (we do not need to cycle to the beginning of the
104  // buffer to read)
105  memcpy(dest, fFirst, nBytes);
106  } else {
107  // we need to read past the end of the buffer.
108  const size_t nBytesRead(endBuffer - fFirst);
109  memcpy(dest, fFirst, nBytesRead);
110  if (nBytes != nBytesRead) {
111  memcpy(dest + nBytesRead, fBuffer, nBytes - nBytesRead);
112  }
113  }
114 }
115 
116 void TPZCircBufferedStream::WriteToBuffer(const char *source,
117  const size_t &nBytes) {
118  if (fSize + nBytes > fNAllocatedBytes) {
119  const size_t oldSize = fSize;
120  const size_t newAllocatedBytes =
121  oldSize * 1.1 + nBytes +
122  MIN_SIZE_INCREMENT; // 10% increase + nBytes + MIN_SIZE_INCREMENT
123  char *temp = new char[newAllocatedBytes];
124  ConstReadFromBuffer(temp, oldSize);
125  memcpy(temp + oldSize, source, nBytes);
126  delete[] fBuffer;
127  fBuffer = temp;
128  fNAllocatedBytes = newAllocatedBytes;
129  fFirst = fBuffer;
130  fSize = oldSize + nBytes;
131  fLast = fBuffer - 1 + fSize;
132  } else {
133  char *endBuffer = fBuffer + fNAllocatedBytes;
134 
135  if (fLast + nBytes < endBuffer) {
136  // direct writing (we do not need to cycle to the beginning of the
137  // buffer to write)
138  memcpy(fLast + 1, source, nBytes);
139  fLast += nBytes;
140  } else {
141  // we need to write past the end of the buffer.
142  const size_t nBytesWritten(endBuffer - fLast - 1);
143  memcpy(fLast + 1, source, nBytesWritten);
144  if (nBytes != nBytesWritten) {
145  memcpy(fBuffer, source + nBytesWritten, nBytes - nBytesWritten);
146  }
147  fLast = fBuffer - 1 + nBytes - nBytesWritten;
148  }
149 
150  fSize += nBytes;
151  }
152 }
153 
155  std::cout << "fSize=" << fSize << std::endl;
156  double *temp = new double[fSize / 8];
157  ConstRead(reinterpret_cast<char *>(temp), fSize);
158  for (unsigned int i = 0; i < fSize / 8; ++i) {
159  std::cout << temp[i] << " ";
160  }
161  delete[] temp;
162  std::cout << std::endl;
163 }
164 
165 
166 void TPZCircBufferedStream::Write(const int *p, int howMany) {
167  WriteData<int>(p, howMany);
168 }
169 
170 void TPZCircBufferedStream::Write(const unsigned int *p, int howMany) {
171  WriteData<unsigned int>(p, howMany);
172 }
173 
174 void TPZCircBufferedStream::Write(const uint64_t *p, int howMany) {
175  WriteData<uint64_t>(p, howMany);
176 }
177 
178 void TPZCircBufferedStream::Write(const int64_t *p, int howMany) {
179  WriteData<int64_t>(p, howMany);
180 }
181 
182 void TPZCircBufferedStream::Write(const float *p, int howMany) {
183  WriteData<float>(p, howMany);
184 }
185 
186 void TPZCircBufferedStream::Write(const double *p, int howMany) {
187  WriteData<double>(p, howMany);
188 }
189 
190 void TPZCircBufferedStream::Write(const unsigned char *p, int howMany) {
191  WriteData<unsigned char>(p, howMany);
192 }
193 
194 void TPZCircBufferedStream::Write(const char *p, int howMany) {
195  WriteData<char>(p, howMany);
196 }
197 
198 void TPZCircBufferedStream::Write(const std::complex<float> *p, int howMany) {
199  WriteData<std::complex<float>>(p, howMany);
200 }
201 
202 void TPZCircBufferedStream::Write(const std::complex<double> *p, int howMany) {
203  WriteData<std::complex<double>>(p, howMany);
204 }
205 
206 #ifdef _AUTODIFF
207 
208 void TPZCircBufferedStream::Write(const TFad<1,REAL> *p, int howMany) {
209  WriteData<TFad<1,REAL>>(p, howMany);
210 }
211 
212 void TPZCircBufferedStream::Write(const TFad<6,REAL> *p, int howMany) {
213  WriteData<TFad<6,REAL>>(p, howMany);
214 }
215 
216 void TPZCircBufferedStream::Write(const TFad<8,REAL> *p, int howMany) {
217  WriteData<TFad<8,REAL>>(p, howMany);
218 }
219 
220 void TPZCircBufferedStream::Write(const TFad<9,REAL> *p, int howMany) {
221  WriteData<TFad<9,REAL>>(p, howMany);
222 }
223 
224 void TPZCircBufferedStream::Write(const TFad<10,REAL> *p, int howMany) {
225  WriteData<TFad<10,REAL>>(p, howMany);
226 }
227 
228 void TPZCircBufferedStream::Write(const TFad<14,REAL> *p, int howMany) {
229  WriteData<TFad<14,REAL>>(p, howMany);
230 }
231 
232 void TPZCircBufferedStream::Write(const Fad<float> *p, int howMany) {
233  WriteData<Fad<float>>(p, howMany);
234 }
235 
236 void TPZCircBufferedStream::Write(const Fad<double> *p, int howMany) {
237  WriteData<Fad<double>>(p, howMany);
238 }
239 
240 #endif
241 
242 void TPZCircBufferedStream::Read(int *p, int howMany) {
243  ReadData<int>(p, howMany);
244 }
245 
246 void TPZCircBufferedStream::Read(unsigned int *p, int howMany) {
247  ReadData<unsigned int>(p, howMany);
248 }
249 
250 void TPZCircBufferedStream::Read(uint64_t *p, int howMany) {
251  ReadData<uint64_t>(p, howMany);
252 }
253 
254 void TPZCircBufferedStream::Read(int64_t *p, int howMany) {
255  ReadData<int64_t>(p, howMany);
256 }
257 
258 void TPZCircBufferedStream::Read(float *p, int howMany) {
259  ReadData<float>(p, howMany);
260 }
261 
262 void TPZCircBufferedStream::Read(double *p, int howMany) {
263  ReadData<double>(p, howMany);
264 }
265 
266 void TPZCircBufferedStream::Read(unsigned char *p, int howMany) {
267  ReadData<unsigned char>(p, howMany);
268 }
269 
270 void TPZCircBufferedStream::Read(char *p, int howMany) {
271  ReadData<char>(p, howMany);
272 }
273 
274 void TPZCircBufferedStream::Read(std::complex<float> *p, int howMany) {
275  ReadData<std::complex<float>>(p, howMany);
276 }
277 
278 void TPZCircBufferedStream::Read(std::complex<double> *p, int howMany) {
279  ReadData<std::complex<double>>(p, howMany);
280 }
281 
282 #ifdef _AUTODIFF
283 
284 void TPZCircBufferedStream::Read(TFad<1,REAL> *p, int howMany) {
285  ReadData<TFad<1,REAL>>(p, howMany);
286 }
287 
288 void TPZCircBufferedStream::Read(TFad<6,REAL> *p, int howMany) {
289  ReadData<TFad<6,REAL>>(p, howMany);
290 }
291 
292 void TPZCircBufferedStream::Read(TFad<8,REAL> *p, int howMany) {
293  ReadData<TFad<8,REAL>>(p, howMany);
294 }
295 
296 void TPZCircBufferedStream::Read(TFad<9,REAL> *p, int howMany) {
297  ReadData<TFad<9,REAL>>(p, howMany);
298 }
299 
300 void TPZCircBufferedStream::Read(TFad<10,REAL> *p, int howMany) {
301  ReadData<TFad<10,REAL>>(p, howMany);
302 }
303 
304 void TPZCircBufferedStream::Read(TFad<14,REAL> *p, int howMany) {
305  ReadData<TFad<14,REAL>>(p, howMany);
306 }
307 
308 void TPZCircBufferedStream::Read(Fad<float> *p, int howMany) {
309  ReadData<Fad<float>>(p, howMany);
310 }
311 
312 void TPZCircBufferedStream::Read(Fad<double> *p, int howMany) {
313  ReadData<Fad<double>>(p, howMany);
314 }
315 #endif
TPZCircBufferedStream()
Creates a bidirectional buffer.
void Print()
Prints buffer info and data.
virtual void ConstRead(char *dest, const size_t &nBytes) const
Reads from buffer WITHOUT consuming it. Unless it is still reading from its underlying stream...
Definition: fad.h:54
virtual void ConstReadFromBuffer(char *dest, const size_t &nBytes) const
Reads from buffer WITHOUT consuming it.
~TPZCircBufferedStream()
Destroys the object.
virtual void ReadFromBuffer(char *dest, const size_t &nBytes)
Reads from buffer.
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
Definition: tfad.h:64
Class for creating a bidirectional circular buffer.
virtual void Read(int *p, int howMany)
virtual TPZCircBufferedStream & operator<<(TPZCircBufferedStream &other)
It reads all data in a buffer, consuming it.
virtual void WriteToBuffer(const char *source, const size_t &nBytes)
Writes to buffer.
static const size_t MIN_SIZE_INCREMENT
string to_string(const string &value)
virtual void Write(const int *p, int howMany)
TPZCircBufferedStream & operator=(const TPZCircBufferedStream &other)
Assingment operator. Both buffers will have the same underlying stream, so this must be used with car...
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15