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 memoryStreamBuffer_H
36 #define memoryStreamBuffer_H
37 
38 #include "UList.H"
39 #include <type_traits>
40 #include <sstream>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // Forward Declarations
48 class Ostream;
49 
50 /*---------------------------------------------------------------------------*\
51  Class memorybuf Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 //- A streambuf for memory
55 class memorybuf
56 :
57  public std::streambuf
58 {
59 protected:
60 
61  //- Set position pointer to relative position
62  virtual std::streampos seekoff
63  (
64  std::streamoff off,
65  std::ios_base::seekdir way,
66  std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
67  )
68  {
69  const bool testin = which & std::ios_base::in;
70  const bool testout = which & std::ios_base::out;
71 
72  if (way == std::ios_base::beg)
73  {
74  if (testin)
75  {
76  setg(eback(), eback(), egptr());
77  gbump(off);
78  }
79  if (testout)
80  {
81  setp(pbase(), epptr());
82  pbump(off);
83  }
84  }
85  else if (way == std::ios_base::cur)
86  {
87  if (testin)
88  {
89  gbump(off);
90  }
91  if (testout)
92  {
93  pbump(off);
94  }
95  }
96  else if (way == std::ios_base::end)
97  {
98  if (testin)
99  {
100  setg(eback(), eback(), egptr());
101  gbump(egptr() - eback() - off);
102  }
103  if (testout)
104  {
105  setp(pbase(), epptr());
106  pbump(epptr() - pbase() - off);
107  }
108  }
109 
110  if (testin)
111  {
112  return (gptr() - eback()); // tellg()
113  }
114  if (testout)
115  {
116  return (pptr() - pbase()); // tellp()
117  }
118 
119  return -1;
120  }
121 
122 
123  //- Set position pointer to absolute position
124  virtual std::streampos seekpos
125  (
126  std::streampos pos,
127  std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
128  )
129  {
130  return seekoff(pos, std::ios_base::beg, which);
131  }
132 
133 
134  //- \return the current buffer get position
135  inline std::streamsize tellg() const
136  {
137  return (gptr() - eback());
138  }
139 
140  //- \return the current buffer put position
141  inline std::streamsize tellp() const
142  {
143  return (pptr() - pbase());
144  }
145 
146 
147 public:
148 
149  // Forward Declarations
150  class in;
151  class out;
152 };
153 
154 
155 /*---------------------------------------------------------------------------*\
156  Class memorybuf::in Declaration
157 \*---------------------------------------------------------------------------*/
158 
159 //- An input streambuf for memory access
160 class memorybuf::in
161 :
162  public memorybuf
163 {
164 protected:
165 
166  //- Default construct
167  in() = default;
168 
169  //- Get sequence of characters
170  virtual std::streamsize xsgetn(char* s, std::streamsize n)
171  {
172  std::streamsize count = 0;
173 
174  while (count < n && gptr() < egptr())
175  {
176  *(s + count++) = *(gptr());
177  gbump(1);
178  }
179 
180  return count;
181  }
182 
183 
184 public:
185 
186  //- Construct for character array (can be nullptr) and number of bytes
187  in(char* s, std::streamsize n)
188  {
189  resetg(s, n);
190  }
191 
192  //- Reset for character array (can be nullptr) and number of bytes
193  // Sets get pointer to the begin.
194  inline void resetg(char* s, std::streamsize n)
195  {
196  if (s)
197  {
198  setg(s, s, s + n);
199  }
200  else
201  {
202  setg(nullptr, nullptr, nullptr);
203  }
204  }
205 
206  //- The buffer get position
207  using memorybuf::tellg;
208 
209  //- The buffer capacity
210  inline std::streamsize capacity() const
211  {
212  return (egptr() - eback());
213  }
214 
215  //- Const UList access to the input characters (shallow copy).
216  inline const UList<char> list() const
217  {
218  return UList<char>(eback(), (egptr() - eback()));
219  }
220 
221  //- Non-const UList access to the input characters (shallow copy).
222  inline UList<char> list()
223  {
224  return UList<char>(eback(), (egptr() - eback()));
225  }
226 
227  //- Some information about the input buffer position/capacity
228  inline void printBufInfo(Ostream& os) const
229  {
230  os << "get=" << (gptr() - eback()) // tellp()
231  << "/" << (egptr() - eback()); // capacity
232  }
233 };
234 
235 
236 /*---------------------------------------------------------------------------*\
237  Class memorybuf::out Declaration
238 \*---------------------------------------------------------------------------*/
239 
240 //- An output streambuf for memory access
241 class memorybuf::out
242 :
243  public memorybuf
244 {
245 protected:
246 
247  //- Default construct
248  out() = default;
249 
250  //- Put sequence of characters
251  virtual std::streamsize xsputn(const char* s, std::streamsize n)
252  {
253  std::streamsize count = 0;
254  while (count < n && pptr() < epptr())
255  {
256  *(pptr()) = *(s + count++);
257  pbump(1);
258  }
259 
260  return count;
261  }
262 
263 
264 public:
265 
266  //- Construct for character array (can be nullptr) and number of bytes
267  out(char* s, std::streamsize n)
268  {
269  resetp(s, n);
270  }
271 
272  //- Reset for character array (can be nullptr) and number of bytes.
273  // Sets put pointer to the begin.
274  inline void resetp(char* s, std::streamsize n)
275  {
276  if (s)
277  {
278  setp(s, s + n);
279  }
280  else
281  {
282  setp(nullptr, nullptr);
283  }
284  }
285 
286  //- The buffer put position
287  using memorybuf::tellp;
288 
289  //- The buffer capacity
290  inline std::streamsize capacity() const
291  {
292  return (epptr() - pbase());
293  }
294 
295  //- Const UList access to the characters written (shallow copy).
296  inline const UList<char> list() const
297  {
298  return UList<char>(pbase(), (pptr() - pbase()));
299  }
300 
301  //- Non-const UList access to the characters written (shallow copy).
302  inline UList<char> list()
303  {
304  return UList<char>(pbase(), (pptr() - pbase()));
305  }
306 
307  //- Some information about the output buffer position/capacity
308  inline void printBufInfo(Ostream& os) const
309  {
310  os << "put=" << (pptr() - pbase()) // tellp()
311  << "/" << (epptr() - pbase()); // capacity
312  }
313 };
314 
315 
316 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 
318 } // End namespace Foam
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 #endif
323 
324 // ************************************************************************* //
Foam::memorybuf::out::list
const UList< char > list() const
Const UList access to the characters written (shallow copy).
Definition: memoryStreamBuffer.H:295
Foam::memorybuf::in::in
in(char *s, std::streamsize n)
Construct for character array (can be nullptr) and number of bytes.
Definition: memoryStreamBuffer.H:186
Foam::memorybuf::in::in
in()=default
Default construct.
Foam::memorybuf::out::capacity
std::streamsize capacity() const
The buffer capacity.
Definition: memoryStreamBuffer.H:289
Foam::memorybuf::in::printBufInfo
void printBufInfo(Ostream &os) const
Some information about the input buffer position/capacity.
Definition: memoryStreamBuffer.H:227
Foam::memorybuf::out::out
out()=default
Default construct.
Foam::memorybuf::in::list
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: memoryStreamBuffer.H:215
s
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))
Definition: gmvOutputSpray.H:25
Foam::memorybuf::out::list
UList< char > list()
Non-const UList access to the characters written (shallow copy).
Definition: memoryStreamBuffer.H:301
Foam::memorybuf::in::list
UList< char > list()
Non-const UList access to the input characters (shallow copy).
Definition: memoryStreamBuffer.H:221
Foam::memorybuf::out::resetp
void resetp(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
Definition: memoryStreamBuffer.H:273
Foam::memorybuf::in::capacity
std::streamsize capacity() const
The buffer capacity.
Definition: memoryStreamBuffer.H:209
Foam::memorybuf::tellp
std::streamsize tellp() const
Definition: memoryStreamBuffer.H:140
Foam::memorybuf::seekoff
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.
Definition: memoryStreamBuffer.H:62
Foam::memorybuf::in
An input streambuf for memory access.
Definition: memoryStreamBuffer.H:159
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::memorybuf::in::xsgetn
virtual std::streamsize xsgetn(char *s, std::streamsize n)
Get sequence of characters.
Definition: memoryStreamBuffer.H:169
Foam::memorybuf
A streambuf for memory.
Definition: memoryStreamBuffer.H:54
Foam::memorybuf::in::resetg
void resetg(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
Definition: memoryStreamBuffer.H:193
Foam::memorybuf::out
An output streambuf for memory access.
Definition: memoryStreamBuffer.H:240
os
OBJstream os(runTime.globalPath()/outputName)
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
UList.H
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::UList
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:103
Foam::memorybuf::seekpos
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.
Definition: memoryStreamBuffer.H:124
Foam::memorybuf::out::printBufInfo
void printBufInfo(Ostream &os) const
Some information about the output buffer position/capacity.
Definition: memoryStreamBuffer.H:307
Foam::memorybuf::out::xsputn
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Put sequence of characters.
Definition: memoryStreamBuffer.H:250
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::memorybuf::tellg
std::streamsize tellg() const
Definition: memoryStreamBuffer.H:134
Foam::memorybuf::out::out
out(char *s, std::streamsize n)
Construct for character array (can be nullptr) and number of bytes.
Definition: memoryStreamBuffer.H:266
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177