Loading...
Searching...
No Matches
OSpanStream.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-2025 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::OSpanStream
28
29Description
30 Similar to OCharStream but using an externally managed buffer for
31 its output.
32
33 This allows the output buffer to be reused and can make it easier when
34 writing out data. It is the user's responsibility to ensure proper
35 synchronization in the sizes. Provided that the external buffer is large
36 enough that overflow does not occur, the following usage pattern
37 works.
38
39 \code
40 DynamicList<char> buffer(4096); // allocate some large buffer
41
42 {
43 OSpanStream os(buffer);
44 os << "content1" << " and more content";
45 buffer.resize(os.size()); // synchronize sizes
46 }
47
48 something.write(buffer, buffer.size());
49 \endcode
50
51 Although the OSpanStream is quite lightweight, there may be cases
52 where it is preferable to reuse the stream as well.
53 \code
54 DynamicList<char> buffer(4096); // allocate some large buffer
55
56 OSpanStream os(buffer);
57 os << "content1" << " and more content";
58 buffer.resize(os.size()); // synchronize sizes
59
60 something.write(buffer, buffer.size());
61
62 os.rewind();
63 os << "content2";
64 buffer.resize(os.size()); // synchronize sizes
65
66 something.write(buffer, buffer.size());
67
68 // or simply using the output size directly (without sync)
69 os.rewind();
70 os << "content3";
71
72 something.write(buffer, os.size());
73 \endcode
74
75See Also
76 Foam::ICharStream
77 Foam::ISpanStream
78 Foam::OCharStream
79
80\*---------------------------------------------------------------------------*/
81
82#ifndef Foam_OSpanStream_H
83#define Foam_OSpanStream_H
84
85#include "memoryStreamBuffer.H"
86#include "OSstream.H"
87
88// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89
90namespace Foam
92
93// Forward Declarations
94class ospanstream;
95class OSpanStream;
96
97// Older names (prior to 2023-08)
99
100
101/*---------------------------------------------------------------------------*\
102 Class ospanstream Declaration
103\*---------------------------------------------------------------------------*/
104
105//- Similar to std::ostringstream, but with an externally managed output buffer
106//- which makes it most similar to std::ospanstream (C++23)
107class ospanstream
108:
109 virtual public std::ios,
111 public std::ostream
112{
113 typedef Foam::memorybuf::out_base buffer_type;
114 typedef std::ostream stream_type;
115
116public:
117
118 // Constructors
119
120 //- Default construct - empty
122 :
123 buffer_type(),
124 stream_type(static_cast<buffer_type*>(this))
125 {}
126
127 //- Construct for character array and number of bytes
128 ospanstream(char* buffer, size_t nbytes)
129 :
130 buffer_type(buffer, nbytes),
131 stream_type(static_cast<buffer_type*>(this))
132 {}
133
134
135 // Member Functions
136
137 //- The current output position within the buffer (tellp)
138 std::streampos output_pos() const
141 }
142
143 //- The number of bytes outputted
144 std::streamsize count() const
145 {
148
149 //- The put buffer capacity
150 std::streamsize capacity() const
151 {
153 }
154
155 //- Rewind the stream, clearing any old errors
156 void rewind()
157 {
158 buffer_type::pubseekpos(0, std::ios_base::out);
159 stream_type::clear(); // Clear old errors
160 }
161
162 //- Reposition the stream from the start
163 void seek(std::streampos pos)
164 {
166 {
167 buffer_type::pubseekpos(pos, std::ios_base::out);
168 stream_type::clear(); // Clear old errors
169 }
170 }
171
172 //- A string_view of buffer contents
173 auto view() const { return buffer_type::view(); }
174
175 //- A sub-slice string view of the buffer contents
176 auto view(size_t pos, size_t len = std::string::npos) const
177 {
178 return buffer_type::view(pos, len);
179 }
180
181 //- Span of the current output characters (is modifiable!)
182 UList<char> list() const
183 {
185 (
188 );
190
191 //- For ostringstream compatibility, return the buffer as string copy.
192 // Use sparingly - it creates a full copy!!
193 std::string str() const
194 {
195 return std::string
196 (
199 );
200 }
201
202 //- Reset the put buffer area
203 void reset(char* buffer, size_t nbytes)
204 {
205 buffer_type::resetp(buffer, nbytes);
206 stream_type::clear(); // Clear old errors
207 }
208
209 //- Reset the put buffer area to use the data area from a string
210 void reset(std::string& s)
212 s.resize(s.capacity());
213 buffer_type::resetp(s.data(), s.size());
214 stream_type::clear(); // Clear old errors
215 }
216
217 //- Some information about the output buffer position/capacity
218 void debug_info(Ostream& os) const
219 {
220 os << "put=" << output_pos() << '/' << capacity();
221 }
222
223 //- Information about stream
224 void print(Ostream& os) const { debug_info(os); os << '\n'; }
225
226
227 // Extra/Convenience Methods
228
229 //- Overwrite a single character
230 void overwrite(std::streampos pos, char c)
231 {
233 }
234
235 //- Overwrite a sub-slice with character content
236 void overwrite
237 (
238 std::streampos pos,
239 const char* data,
240 std::streamsize count
241 )
244 }
245
246 //- The output data (start of output characters)
247 const char* cdata_bytes() const { return buffer_type::data_bytes(); }
248
249 //- The output data (start of output characters)
251
252 //- The current number of output characters
253 std::streamsize size_bytes() const { return buffer_type::size_bytes(); }
254};
255
256
257/*---------------------------------------------------------------------------*\
258 Class OSpanStream Declaration
259\*---------------------------------------------------------------------------*/
260
261class OSpanStream
262:
263 public Foam::Detail::StreamAllocator<Foam::ospanstream>,
264 public Foam::OSstream
265{
266 typedef
268 allocator_type;
269
270public:
271
272 // Constructors
273
274 //- Default construct (empty output)
275 explicit OSpanStream
276 (
277 IOstreamOption streamOpt = IOstreamOption()
278 )
280 allocator_type(),
281 OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
282 {}
283
284 //- Use data area from string content
285 explicit OSpanStream
286 (
287 std::string& buffer,
288 IOstreamOption streamOpt = IOstreamOption()
290 :
291 OSpanStream(streamOpt)
292 {
293 stream_.reset(buffer);
294 }
295
296 //- Construct using specified buffer and number of bytes
298 (
299 char* buffer,
300 size_t nbytes,
301 IOstreamOption streamOpt = IOstreamOption()
302 )
303 :
304 OSpanStream(streamOpt)
305 {
306 stream_.reset(buffer, nbytes);
307 }
308
309 //- Construct using data area from a List and its inherent storage size
310 explicit OSpanStream
311 (
312 ::Foam::UList<char>& buffer,
314 )
315 :
316 OSpanStream(buffer.data(), buffer.size(), streamOpt)
317 {}
318
319 //- Construct using full data area from DynamicList
320 template<int SizeMin>
321 explicit OSpanStream
322 (
324 IOstreamOption streamOpt = IOstreamOption()
326 :
327 OSpanStream(buffer.data(), buffer.capacity(), streamOpt)
328 {
329 buffer.resize(buffer.capacity()); // Uses entire space
330 }
331
332
333 // Member Functions
334
335 //- Position of the put buffer
336 std::streampos tellp() const { return stream_.output_pos(); }
337
338 //- The current output position within the buffer (tellp)
339 std::streampos output_pos() const { return stream_.output_pos(); }
340
341 //- The number of bytes outputted
342 std::streamsize count() const { return stream_.count(); }
343
344 //- The current output size. Same as count(), output_pos(), tellp().
345 label size() const { return label(stream_.count()); }
346
347 //- The put buffer capacity
348 std::streamsize capacity() const { return stream_.capacity(); }
349
350 //- A string_view of buffer contents
351 auto view() const { return stream_.view(); }
352
353 //- A sub-slice string view of the buffer contents
354 auto view(size_t pos, size_t len = std::string::npos) const
355 {
356 return stream_.view(pos, len);
357 }
358
359 //- Span of the current output characters (is modifiable!)
360 UList<char> list() const { return stream_.list(); }
361
362 //- For OStringStream compatibility, return buffer as string copy.
363 // Use sparingly - it creates a full copy!!
364 auto str() const { return stream_.str(); }
365
366 //- Reset the put area
367 void reset(char* buffer, size_t nbytes)
368 {
369 stream_.reset(buffer, nbytes);
370 syncState();
371 }
372
373 //- Reset the put buffer area to use the data area from a string
374 void reset(std::string& s)
375 {
376 stream_.reset(s);
377 syncState();
378 }
379
380 //- Rewind the stream, clearing any old errors
381 virtual void rewind()
382 {
383 stream_.rewind();
385 }
386
387 //- Reposition the stream from the start
388 void seek(std::streampos pos)
390 stream_.seek(pos);
391 syncState();
392 }
393
394 //- Print stream description
395 virtual void print(Ostream& os) const override
396 {
397 os << "ospanstream: ";
398 stream_.debug_info(os);
399 os << '\n';
400 }
401
402
403 // Extra/Convenience Methods
405 //- Append character content - same as writeRaw()
406 void append(const char* data, std::streamsize count)
407 {
408 if (data && count > 0)
410 writeRaw(data, count);
411 }
412 }
413
414 //- Append character content - same as writeRaw()
415 void append(std::string_view sv)
416 {
417 append(sv.data(), sv.size());
418 }
419
420 //- Overwrite a single character
421 void overwrite(std::streampos pos, char c)
423 stream_.overwrite(pos, c);
424 }
425
426 //- Overwrite a sub-slice with character content
427 void overwrite
428 (
429 std::streampos pos,
430 const char* data,
431 std::streamsize count
432 )
433 {
434 stream_.overwrite(pos, data, count);
435 }
436
437 //- Overwrite a sub-slice with character content
438 void overwrite(std::streampos pos, std::string_view sv)
439 {
440 stream_.overwrite(pos, sv.data(), sv.size());
441 }
442};
444
445// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
446
447} // End namespace Foam
448
449// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
450
451#endif
453// ************************************************************************* //
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition IOstream.H:620
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
label capacity() const noexcept
Size of the underlying storage.
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content.
A simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression.
streamFormat format() const noexcept
Get the current stream format.
Similar to OCharStream but using an externally managed buffer for its output.
OSpanStream(std::string &buffer, IOstreamOption streamOpt=IOstreamOption())
Use data area from string content.
void overwrite(std::streampos pos, char c)
Overwrite a single character.
OSpanStream(::Foam::UList< char > &buffer, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a List and its inherent storage size.
void append(const char *data, std::streamsize count)
Append character content - same as writeRaw().
std::streampos tellp() const
Position of the put buffer.
std::streamsize capacity() const
The put buffer capacity.
void overwrite(std::streampos pos, std::string_view sv)
Overwrite a sub-slice with character content.
auto view(size_t pos, size_t len=std::string::npos) const
A sub-slice string view of the buffer contents.
OSpanStream(char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
UList< char > list() const
Span of the current output characters (is modifiable!).
auto str() const
For OStringStream compatibility, return buffer as string copy.
void seek(std::streampos pos)
Reposition the stream from the start.
void overwrite(std::streampos pos, const char *data, std::streamsize count)
Overwrite a sub-slice with character content.
label size() const
The current output size. Same as count(), output_pos(), tellp().
auto view() const
A string_view of buffer contents.
OSpanStream(::Foam::DynamicList< char, SizeMin > &buffer, IOstreamOption streamOpt=IOstreamOption())
Construct using full data area from DynamicList.
void append(std::string_view sv)
Append character content - same as writeRaw().
void reset(char *buffer, size_t nbytes)
Reset the put area.
std::streamsize count() const
The number of bytes outputted.
virtual void print(Ostream &os) const override
Print stream description.
virtual void rewind()
Rewind the stream, clearing any old errors.
OSpanStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty output).
void reset(std::string &s)
Reset the put buffer area to use the data area from a string.
std::streampos output_pos() const
The current output position within the buffer (tellp).
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
OSstream(const OSstream &)=default
Copy construct.
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition OSstream.C:280
void syncState()
Set stream state to match that of the std::ostream.
Definition OSstream.H:182
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
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:89
An output streambuf for memory access.
void resetp(char *s, std::streamsize n)
Reset put buffer with character data (can be nullptr) and count.
std::streamsize span_tellp() const
The current buffer put position.
char * data_bytes() const
The span data (start of output characters).
std::streamsize span_capacity() const
The put buffer capacity.
bool in_range(std::streampos pos) const
True if position is within the current output range.
void overwrite(std::streampos pos, const char *data, std::streamsize count)
Overwrite a sub-slice with character content.
auto view() const
A string view of the current output region.
std::streamsize size_bytes() const
The span size (size of output buffer).
Similar to std::ostringstream, but with an externally managed output buffer which makes it most simil...
ospanstream()
Default construct - empty.
void overwrite(std::streampos pos, char c)
Overwrite a single character.
ospanstream(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
char * data_bytes()
The output data (start of output characters).
std::streamsize capacity() const
The put buffer capacity.
auto view(size_t pos, size_t len=std::string::npos) const
A sub-slice string view of the buffer contents.
void print(Ostream &os) const
Information about stream.
UList< char > list() const
Span of the current output characters (is modifiable!).
void debug_info(Ostream &os) const
Some information about the output buffer position/capacity.
void seek(std::streampos pos)
Reposition the stream from the start.
void overwrite(std::streampos pos, const char *data, std::streamsize count)
Overwrite a sub-slice with character content.
auto view() const
A string_view of buffer contents.
void reset(char *buffer, size_t nbytes)
Reset the put buffer area.
std::streamsize count() const
The number of bytes outputted.
void rewind()
Rewind the stream, clearing any old errors.
std::string str() const
For ostringstream compatibility, return the buffer as string copy.
void reset(std::string &s)
Reset the put buffer area to use the data area from a string.
std::streampos output_pos() const
The current output position within the buffer (tellp).
std::streamsize size_bytes() const
The current number of output characters.
const char * cdata_bytes() const
The output data (start of output characters).
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)
OSpanStream UOListStream
Definition OSpanStream.H:91
word format(conversionProperties.get< word >("format"))