memoryStreamBuffer.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::memorybuf
28 
29 Description
30  A std::streambuf used for memory buffer streams such as
31  UIListStream, UOListStream, etc.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_memoryStreamBuffer_H
36 #define Foam_memoryStreamBuffer_H
37 
38 #include "UList.H"
39 #include <type_traits>
40 #include <sstream>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class memorybuf Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 //- A streambuf for memory
52 class memorybuf
53 :
54  public std::streambuf
55 {
56 protected:
57 
58  //- Set position pointer to relative position
59  virtual std::streampos seekoff
60  (
61  std::streamoff off,
62  std::ios_base::seekdir way,
63  std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
64  )
65  {
66  const bool testin = which & std::ios_base::in;
67  const bool testout = which & std::ios_base::out;
68 
69  if (way == std::ios_base::beg)
70  {
71  if (testin)
72  {
73  setg(eback(), eback(), egptr());
74  gbump(off);
75  }
76  if (testout)
77  {
78  setp(pbase(), epptr());
79  pbump(off);
80  }
81  }
82  else if (way == std::ios_base::cur)
83  {
84  if (testin)
85  {
86  gbump(off);
87  }
88  if (testout)
89  {
90  pbump(off);
91  }
92  }
93  else if (way == std::ios_base::end)
94  {
95  if (testin)
96  {
97  setg(eback(), eback(), egptr());
98  gbump(egptr() - eback() - off);
99  }
100  if (testout)
101  {
102  setp(pbase(), epptr());
103  pbump(epptr() - pbase() - off);
104  }
105  }
106 
107  if (testin)
108  {
109  return (gptr() - eback()); // tellg()
110  }
111  if (testout)
112  {
113  return (pptr() - pbase()); // tellp()
114  }
115 
116  return -1;
117  }
118 
119 
120  //- Set position pointer to absolute position
121  virtual std::streampos seekpos
122  (
123  std::streampos pos,
124  std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
125  )
126  {
127  return seekoff(pos, std::ios_base::beg, which);
128  }
129 
130 
131  //- \return the current buffer get position
132  inline std::streamsize tellg() const
133  {
134  return (gptr() - eback());
135  }
136 
137  //- \return the current buffer put position
138  inline std::streamsize tellp() const
139  {
140  return (pptr() - pbase());
141  }
142 
143 
144 public:
145 
146  // Forward Declarations
147  class in;
148  class out;
149 };
150 
151 
152 /*---------------------------------------------------------------------------*\
153  Class memorybuf::in Declaration
154 \*---------------------------------------------------------------------------*/
155 
156 //- An input streambuf for memory access
157 class memorybuf::in
158 :
159  public memorybuf
160 {
161 protected:
163  //- Default construct
164  in() = default;
165 
166  //- Get sequence of characters
167  virtual std::streamsize xsgetn(char* s, std::streamsize n)
168  {
169  std::streamsize count = 0;
170 
171  while (count < n && gptr() < egptr())
172  {
173  *(s + count++) = *(gptr());
174  gbump(1);
175  }
177  return count;
178  }
179 
180 
181 public:
182 
183  //- Construct for character array (can be nullptr) and number of bytes
184  in(char* s, std::streamsize n)
185  {
186  resetg(s, n);
187  }
188 
189  //- Reset for character array (can be nullptr) and number of bytes
190  // Sets get pointer to the begin.
191  inline void resetg(char* s, std::streamsize n)
192  {
193  if (s)
194  {
195  setg(s, s, s + n);
196  }
197  else
198  {
199  setg(nullptr, nullptr, nullptr);
200  }
201  }
202 
203  //- The buffer get position
204  using memorybuf::tellg;
206  //- The buffer capacity
207  inline std::streamsize capacity() const
208  {
209  return (egptr() - eback());
210  }
211 
212  //- Const UList access to the input characters (shallow copy).
213  inline const UList<char> list() const
214  {
215  return UList<char>(eback(), (egptr() - eback()));
216  }
217 
218  //- Non-const UList access to the input characters (shallow copy).
219  inline UList<char> list()
220  {
221  return UList<char>(eback(), (egptr() - eback()));
222  }
223 
224  //- Some information about the input buffer position/capacity
225  inline void printBufInfo(Ostream& os) const
226  {
227  os << "get=" << (gptr() - eback()) // tellp()
228  << "/" << (egptr() - eback()); // capacity
229  }
230 };
231 
232 
233 /*---------------------------------------------------------------------------*\
234  Class memorybuf::out Declaration
235 \*---------------------------------------------------------------------------*/
236 
237 //- An output streambuf for memory access
238 class memorybuf::out
239 :
240  public memorybuf
241 {
242 protected:
243 
244  //- Default construct
245  out() = default;
246 
247  //- Put sequence of characters
248  virtual std::streamsize xsputn(const char* s, std::streamsize n)
249  {
250  std::streamsize count = 0;
251  while (count < n && pptr() < epptr())
252  {
253  *(pptr()) = *(s + count++);
254  pbump(1);
255  }
256 
257  return count;
258  }
259 
260 
261 public:
262 
263  //- Construct for character array (can be nullptr) and number of bytes
264  out(char* s, std::streamsize n)
265  {
266  resetp(s, n);
267  }
268 
269  //- Reset for character array (can be nullptr) and number of bytes.
270  // Sets put pointer to the begin.
271  inline void resetp(char* s, std::streamsize n)
272  {
273  if (s)
274  {
275  setp(s, s + n);
276  }
277  else
278  {
279  setp(nullptr, nullptr);
280  }
281  }
282 
283  //- The buffer put position
284  using memorybuf::tellp;
285 
286  //- The buffer capacity
287  inline std::streamsize capacity() const
288  {
289  return (epptr() - pbase());
290  }
291 
292  //- Const UList access to the characters written (shallow copy).
293  inline const UList<char> list() const
294  {
295  return UList<char>(pbase(), (pptr() - pbase()));
296  }
297 
298  //- Non-const UList access to the characters written (shallow copy).
299  inline UList<char> list()
300  {
301  return UList<char>(pbase(), (pptr() - pbase()));
302  }
303 
304  //- Some information about the output buffer position/capacity
305  inline void printBufInfo(Ostream& os) const
306  {
307  os << "put=" << (pptr() - pbase()) // tellp()
308  << "/" << (epptr() - pbase()); // capacity
309  }
310 };
311 
312 
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314 
315 } // End namespace Foam
316 
317 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
318 
319 #endif
320 
321 // ************************************************************************* //
const UList< char > list() const
Const UList access to the characters written (shallow copy).
out()=default
Default construct.
void printBufInfo(Ostream &os) const
Some information about the input buffer position/capacity.
std::streamsize tellp() const
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
std::streamsize capacity() const
The buffer capacity.
void resetp(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
An output streambuf for memory access.
virtual std::streampos seekpos(std::streampos pos, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Set position pointer to absolute position.
virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Set position pointer to relative position.
virtual std::streamsize xsgetn(char *s, std::streamsize n)
Get sequence of characters.
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Put sequence of characters.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:99
void resetg(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:194
OBJstream os(runTime.globalPath()/outputName)
std::streamsize tellg() const
A streambuf for memory.
void printBufInfo(Ostream &os) const
Some information about the output buffer position/capacity.
label n
in()=default
Default construct.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Namespace for OpenFOAM.
std::streamsize capacity() const
The buffer capacity.