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