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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::memorybuf
28
29Description
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
44namespace Foam
45{
46
47// Forward Declarations
48class Ostream;
49
50/*---------------------------------------------------------------------------*\
51 Class memorybuf Declaration
52\*---------------------------------------------------------------------------*/
53
54//- A streambuf for memory
55class memorybuf
56:
57 public std::streambuf
58{
59protected:
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
147public:
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
160class memorybuf::in
161:
162 public memorybuf
163{
164protected:
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
184public:
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
241class memorybuf::out
242:
243 public memorybuf
244{
245protected:
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
264public:
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// ************************************************************************* //
label n
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
An input streambuf for memory access.
in(char *s, std::streamsize n)
Construct for character array (can be nullptr) and number of bytes.
void printBufInfo(Ostream &os) const
Some information about the input buffer position/capacity.
virtual std::streamsize xsgetn(char *s, std::streamsize n)
Get sequence of characters.
void resetg(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
UList< char > list()
Non-const UList access to the input characters (shallow copy).
const UList< char > list() const
Const UList access to the input characters (shallow copy).
std::streamsize capacity() const
The buffer capacity.
in()=default
Default construct.
An output streambuf for memory access.
void printBufInfo(Ostream &os) const
Some information about the output buffer position/capacity.
out(char *s, std::streamsize n)
Construct for character array (can be nullptr) and number of bytes.
UList< char > list()
Non-const UList access to the characters written (shallow copy).
void resetp(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
out()=default
Default construct.
const UList< char > list() const
Const UList access to the characters written (shallow copy).
std::streamsize capacity() const
The buffer capacity.
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Put sequence of characters.
A streambuf for memory.
std::streamsize tellp() const
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.
std::streamsize tellg() const
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.
OBJstream os(runTime.globalPath()/outputName)
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))
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)