Loading...
Searching...
No Matches
OCharStream.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) 2017-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::OCharStream
28
29Description
30 An output stream that writes to a List and manages the List storage.
31 Similar to OStringStream but with a List for its storage instead of
32 as string to allow reuse of List contents without copying.
33
34 Internally imposes a 512 byte min-size and uses capacity doubling.
35
36See Also
37 Foam::ICharStream
38 Foam::OSpanStream
39 Foam::ISpanStream
40
41\*---------------------------------------------------------------------------*/
42
43#ifndef Foam_OCharStream_H
44#define Foam_OCharStream_H
45
46#include "OSpanStream.H"
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
52
53// Forward Declarations
54class ocharstream;
55class OCharStream;
56
57// Older names (prior to 2023-08)
59
60
61/*---------------------------------------------------------------------------*\
62 Class ocharstream Declaration
63\*---------------------------------------------------------------------------*/
64
65//- Similar to std::ostringstream, but with the ability to swap
66//- character content.
67//- Has some similarity to std::ospanstream (C++23)
68class ocharstream
69:
70 virtual public std::ios,
72 public std::ostream
73{
74 typedef Foam::memorybuf::out_dynamic buffer_type;
75 typedef std::ostream stream_type;
76
77public:
78
79 // Constructors
80
81 //- Default construct - empty
83 :
84 buffer_type(),
85 stream_type(static_cast<buffer_type*>(this))
86 {}
87
88 //- Move construct from List
89 ocharstream(List<char>&& buffer)
90 :
92 {
93 swap(buffer);
94 }
95
96 //- Move construct from DynamicList
97 template<int SizeMin>
99 :
101 {
102 swap(buffer);
103 }
104
105
106 // Member Functions
107
108 //- The current output position within the buffer (tellp)
109 std::streampos output_pos() const
110 {
113
114 //- The number of bytes outputted
115 std::streamsize count() const
116 {
118 }
119
120 //- The put buffer capacity
121 std::streamsize capacity() const
122 {
124 }
125
126 //- Reserve output space for at least this amount.
127 //- Applies a min-size and capacity doubling.
128 void reserve(std::streamsize n)
129 {
131 }
132
133 //- Reserve output space for at least this amount.
134 //- Does not apply min-size or capacity doubling etc.
135 void reserve_exact(std::streamsize n)
136 {
138 }
139
140 //- Increase (reserve) space for another \c count entries
141 void extend(std::streamsize count)
142 {
144 }
145
146 //- Increase (reserve) space for another \c count entries
147 void extend_exact(std::streamsize count)
148 {
150 }
151
152 //- Rewind the stream, clearing any old errors
153 void rewind()
155 buffer_type::pubseekpos(0, std::ios_base::out);
156 stream_type::clear(); // Clear old errors
157 }
158
159 //- Reposition the stream from the start
160 void seek(std::streampos pos)
161 {
163 {
164 buffer_type::pubseekpos(pos, std::ios_base::out);
165 stream_type::clear(); // Clear old errors
166 }
167 }
168
169 //- A string_view of buffer contents
170 auto view() const { return buffer_type::view(); }
171
172 //- A sub-slice string view of the buffer contents
173 auto view(size_t pos, size_t len = std::string::npos) const
174 {
175 return buffer_type::view(pos, len);
176 }
177
178 //- A list \em span of current output contents (is modifiable!!)
180 {
181 return UList<char>
182 (
185 );
186 }
187
188 //- For ostringstream compatibility, return the buffer as string copy.
189 // Use sparingly - it creates a full copy!!
190 std::string str() const
192 return std::string
193 (
196 );
197 }
198
199 //- Exchange stream content and parameter contents, reset positions
200 void swap(List<char>& other)
201 {
202 buffer_type::swap(other);
203 stream_type::clear(); // Clear old errors
205
206 //- Exchange stream content and parameter contents, reset positions
207 template<int SizeMin>
209 {
210 buffer_type::swap(other);
211 stream_type::clear(); // Clear old errors
212 }
213
214 //- Reset buffer and return contents
215 DynamicList<char> release()
216 {
217 DynamicList<char> chars(buffer_type::release());
218 stream_type::clear(); // Clear old errors
219 return chars;
220 }
221
222 //- Some information about the output buffer position/capacity
223 void debug_info(Ostream& os) const
224 {
225 os << "put=" << output_pos() << '/' << capacity();
226 }
227
228 //- Information about stream
229 void print(Ostream& os) const { debug_info(os); os << '\n'; }
231
232 // Extra/Convenience Methods
233
234 //- Append a single character to the end
235 void push_back(char c) { stream_type::put(c); }
236
237 //- Rewind the end by 1 or more elements
238 void pop_back(int n = 1) { buffer_type::pop_back(n); }
239
240 //- Append repeated character content
241 void append(std::streamsize count, char c)
242 {
243 if (count > 0)
244 {
246 while (count-- > 0)
247 {
248 stream_type::put(c);
250 }
251 }
252
253 //- Append character content - like a plain write()
254 void append(const char* data, std::streamsize count)
255 {
256 if (data && count > 0)
257 {
259 write(data, count);
260 }
261 }
262
263 //- Overwrite a single character
264 void overwrite(std::streampos pos, char c)
265 {
268
269 //- Overwrite a sub-slice with character content
270 void overwrite
271 (
272 std::streampos pos,
273 const char* data,
274 std::streamsize count
276 {
278 }
279
280 //- The output data (start of output characters)
281 const char* cdata_bytes() const { return buffer_type::data_bytes(); }
282
283 //- The output data (start of output characters)
284 char* data_bytes() { return buffer_type::data_bytes(); }
286 //- The current number of output characters
287 std::streamsize size_bytes() const { return buffer_type::size_bytes(); }
288};
289
290
291/*---------------------------------------------------------------------------*\
292 Class OCharStream Declaration
293\*---------------------------------------------------------------------------*/
294
295//- An OSstream with internal List storage
296class OCharStream
297:
298 public Foam::Detail::StreamAllocator<Foam::ocharstream>,
299 public Foam::OSstream
301 typedef
303 allocator_type;
304
305public:
306
307 // Constructors
308
309 //- Default construct (empty output)
310 explicit OCharStream
311 (
313 )
314 :
315 allocator_type(),
316 OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
317 {}
318
319 //- Move construct from a List of initial storage
320 explicit OCharStream
321 (
322 ::Foam::List<char>&& buffer,
323 IOstreamOption streamOpt = IOstreamOption()
324 )
325 :
326 OCharStream(streamOpt)
327 {
328 stream_.swap(buffer);
329 }
330
331 //- Move construct from a DynamicList of initial storage
332 //- (uses entire capacity)
333 template<int SizeMin>
334 explicit OCharStream
335 (
337 IOstreamOption streamOpt = IOstreamOption()
339 :
340 OCharStream(streamOpt)
341 {
342 stream_.swap(buffer);
344
345
346 // Member Functions
347
348 //- Position of the put buffer
349 std::streampos tellp() const { return stream_.output_pos(); }
350
351 //- The current output position within the buffer (tellp)
352 std::streampos output_pos() const { return stream_.output_pos(); }
353
354 //- The number of bytes outputted
355 std::streamsize count() const { return stream_.count(); }
356
357 //- The current output size. Same as count(), output_pos(), tellp().
358 label size() const { return label(stream_.count()); }
359
360 //- The put buffer capacity
361 std::streamsize capacity() const { return stream_.capacity(); }
362
363 //- Reserve output space for at least this amount
364 void reserve(std::streamsize n) { stream_.reserve(n); }
365
366 //- Reserve output space for at least this amount.
367 //- Does not apply min-size or capacity doubling etc.
368 void reserve_exact(std::streamsize n) { stream_.reserve_exact(n); }
369
370 //- Increase (reserve) space for another \c n entries
371 void extend(std::streamsize n) { stream_.extend(n); }
372
373 //- Increase (reserve) space for another \c n entries
374 void extend_exact(std::streamsize n) { stream_.extend_exact(n); }
375
376 //- A string_view of buffer contents
377 auto view() const { return stream_.view(); }
378
379 //- A sub-slice string view of the buffer contents
380 auto view(size_t pos, size_t len = std::string::npos) const
381 {
382 return stream_.view(pos, len);
383 }
384
385 //- A list \em span of the current output characters (is modifiable!)
386 UList<char> list() const { return stream_.list(); }
387
388 //- For OStringStream compatibility, return the buffer as string copy.
389 // Use sparingly - it creates a full copy!!
390 auto str() const { return stream_.str(); }
391
392 //- Exchange stream content and parameter contents, reset positions
393 void swap(List<char>& other)
394 {
395 stream_.swap(other);
396 syncState();
397 }
399 //- Exchange stream content and parameter contents, reset positions
400 template<int SizeMin>
402 {
403 stream_.swap(other);
404 syncState();
405 }
406
407 //- Reset buffer and return contents
409 {
411 syncState();
412 return chars;
413 }
414
415 //- Rewind the stream, clearing any old errors
416 virtual void rewind()
417 {
418 stream_.rewind();
419 syncState();
421
422 //- Reposition the stream from the start
423 void seek(std::streampos pos)
424 {
426 syncState();
427 }
428
429 //- Print stream description
430 virtual void print(Ostream& os) const override
431 {
432 os << "ocharstream: ";
433 stream_.debug_info(os);
434 os << '\n';
436
437
438 // Extra/Convenience Methods
439
440 //- Append a single character to the end
441 void push_back(char c) { stream_.push_back(c); }
442
443 //- Rewind the end by 1 or more elements
444 void pop_back(int n = 1) { stream_.pop_back(n); }
445
446 //- Append repeated character content
447 void append(std::streamsize count, char c)
448 {
449 stream_.append(count, c);
450 }
452 //- Append character content
453 void append(const char* data, std::streamsize count)
454 {
455 stream_.append(data, count);
457
458 //- Append character content
459 void append(std::string_view sv)
460 {
461 stream_.append(sv.data(), sv.size());
462 }
463
464 //- Overwrite a single character
465 void overwrite(std::streampos pos, char c)
467 stream_.overwrite(pos, c);
468 }
469
470 //- Overwrite a sub-slice with character content
471 void overwrite
472 (
473 std::streampos pos,
474 const char* data,
475 std::streamsize count
476 )
477 {
478 stream_.overwrite(pos, data, count);
479 }
480
481 //- Overwrite a sub-slice with character content
482 void overwrite(std::streampos pos, std::string_view sv)
483 {
484 stream_.overwrite(pos, sv.data(), sv.size());
485 }
487
488 // Housekeeping
489
490 //- Block size was used in OpenFOAM-v2306 and earlier
491 void setBlockSize(int n) {}
492};
493
494
495// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
497} // End namespace Foam
498
499// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
500
501#endif
502
503// ************************************************************************* //
label n
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
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.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An OSstream with internal List storage.
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
void overwrite(std::streampos pos, char c)
Overwrite a single character.
void setBlockSize(int n)
Block size was used in OpenFOAM-v2306 and earlier.
void swap(DynamicList< char, SizeMin > &other)
Exchange stream content and parameter contents, reset positions.
void reserve_exact(std::streamsize n)
Reserve output space for at least this amount. Does not apply min-size or capacity doubling etc.
void append(const char *data, std::streamsize count)
Append character content.
std::streampos tellp() const
Position of the put buffer.
void push_back(char c)
Append a single character to the end.
std::streamsize capacity() const
The put buffer capacity.
OCharStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty output).
void append(std::streamsize count, char c)
Append repeated character content.
DynamicList< char > release()
Reset buffer and return contents.
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.
void extend(std::streamsize n)
Increase (reserve) space for another n entries.
UList< char > list() const
A list span of the current output characters (is modifiable!).
auto str() const
For OStringStream compatibility, return the buffer as string copy.
void seek(std::streampos pos)
Reposition the stream from the start.
void reserve(std::streamsize n)
Reserve output space for at least this amount.
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.
void append(std::string_view sv)
Append character content.
OCharStream(::Foam::DynamicList< char, SizeMin > &&buffer, IOstreamOption streamOpt=IOstreamOption())
Move construct from a DynamicList of initial storage (uses entire capacity).
std::streamsize count() const
The number of bytes outputted.
virtual void print(Ostream &os) const override
Print stream description.
void pop_back(int n=1)
Rewind the end by 1 or more elements.
OCharStream(::Foam::List< char > &&buffer, IOstreamOption streamOpt=IOstreamOption())
Move construct from a List of initial storage.
void extend_exact(std::streamsize n)
Increase (reserve) space for another n entries.
virtual void rewind()
Rewind the stream, clearing any old errors.
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.
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
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.
void pop_back(int n=1)
Decrease the put area by 1 or more elements.
std::streamsize size_bytes() const
The span size (size of output buffer).
An output streambuf for memory access.
void swap(List< char > &other)
Exchange buffer content and parameter contents, reset positions.
void reserve_exact(const std::streamsize len)
Increase capacity for at least this size. Does not apply min-size or capacity doubling etc.
void extend(std::streamsize count)
Increase (reserve) space for another count entries.
DynamicList< char > release()
Reset buffer and return contents as a DynamicList. The list size corresponds to the region of output.
void extend_exact(std::streamsize count)
Increase (reserve) space for another count entries.
void reserve(const std::streamsize len)
Increase capacity (if needed) and adjust buffer pointers. Applies a min-size and capacity doubling.
Similar to std::ostringstream, but with the ability to swap character content. Has some similarity to...
Definition OCharStream.H:68
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
void overwrite(std::streampos pos, char c)
Overwrite a single character.
char * data_bytes()
The output data (start of output characters).
ocharstream(DynamicList< char, SizeMin > &&buffer)
Move construct from DynamicList.
Definition OCharStream.H:99
void swap(DynamicList< char, SizeMin > &other)
Exchange stream content and parameter contents, reset positions.
void reserve_exact(std::streamsize n)
Reserve output space for at least this amount. Does not apply min-size or capacity doubling etc.
void append(const char *data, std::streamsize count)
Append character content - like a plain write().
void push_back(char c)
Append a single character to the end.
std::streamsize capacity() const
The put buffer capacity.
void extend(std::streamsize count)
Increase (reserve) space for another count entries.
void append(std::streamsize count, char c)
Append repeated character content.
DynamicList< char > release()
Reset buffer and return contents.
auto view(size_t pos, size_t len=std::string::npos) const
A sub-slice string view of the buffer contents.
ocharstream(List< char > &&buffer)
Move construct from List.
Definition OCharStream.H:88
void print(Ostream &os) const
Information about stream.
UList< char > list() const
A list span of current output contents (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 reserve(std::streamsize n)
Reserve output space for at least this amount. Applies a min-size and capacity doubling.
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.
std::streamsize count() const
The number of bytes outputted.
ocharstream()
Default construct - empty.
Definition OCharStream.H:79
void rewind()
Rewind the stream, clearing any old errors.
void extend_exact(std::streamsize count)
Increase (reserve) space for another count entries.
void pop_back(int n=1)
Rewind the end by 1 or more elements.
std::string str() const
For ostringstream compatibility, return the buffer as string copy.
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)
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
OCharStream OListStream
Definition OCharStream.H:51
runTime write()
word format(conversionProperties.get< word >("format"))