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 -------------------------------------------------------------------------------
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::OCharStream
28 
29 Description
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 
36 See 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 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 class ocharstream;
55 class OCharStream;
56 
57 // Older names (prior to 2023-08)
58 typedef OCharStream OListStream;
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)
68 class ocharstream
69 :
70  virtual public std::ios,
72  public std::ostream
73 {
75  typedef std::ostream stream_type;
76 
77 public:
78 
79  // Constructors
80 
81  //- Default construct - empty
82  ocharstream()
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  :
91  ocharstream()
92  {
93  swap(buffer);
94  }
95 
96  //- Move construct from DynamicList
97  template<int SizeMin>
99  :
100  ocharstream()
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  {
111  return buffer_type::span_tellp();
112  }
113 
114  //- The number of bytes outputted
115  std::streamsize count() const
116  {
117  return buffer_type::size_bytes();
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()
154  {
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  (
184  label(buffer_type::size_bytes())
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
191  {
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
204  }
205 
206  //- Exchange stream content and parameter contents, reset positions
207  template<int SizeMin>
208  void swap(DynamicList<char,SizeMin>& other)
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);
249  }
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  {
267  }
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
275  )
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
296 class OCharStream
297 :
298  public Foam::Detail::StreamAllocator<Foam::ocharstream>,
299  public Foam::OSstream
300 {
301  typedef
303  allocator_type;
304 
305 public:
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()
338  )
339  :
340  OCharStream(streamOpt)
341  {
342  stream_.swap(buffer);
343  }
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  }
398 
399  //- Exchange stream content and parameter contents, reset positions
400  template<int SizeMin>
401  void swap(DynamicList<char,SizeMin>& other)
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();
420  }
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';
435  }
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);
456  }
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)
466  {
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 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
void reserve_exact(std::streamsize n)
Reserve output space for at least this amount. Does not apply min-size or capacity doubling etc...
Definition: OCharStream.H:146
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:619
std::streamsize span_capacity() const
The put buffer capacity.
label size() const
The current output size. Same as count(), output_pos(), tellp().
Definition: OCharStream.H:430
virtual void print(Ostream &os) const override
Print stream description.
Definition: OCharStream.H:533
auto view() const
A string_view of buffer contents.
Definition: OCharStream.H:461
OCharStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty output)
Definition: OCharStream.H:371
void rewind()
Rewind the stream, clearing any old errors.
Definition: OCharStream.H:170
UList< char > list() const
A list span of the current output characters (is modifiable!)
Definition: OCharStream.H:474
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:182
ocharstream()
Default construct - empty.
Definition: OCharStream.H:79
void extend_exact(std::streamsize count)
Increase (reserve) space for another count entries.
Similar to std::ostringstream, but with the ability to swap character content. Has some similarity to...
Definition: OCharStream.H:63
void overwrite(std::streampos pos, char c)
Overwrite a single character.
Definition: OCharStream.H:580
auto view() const
A string view of the current output region.
void seek(std::streampos pos)
Reposition the stream from the start.
Definition: OCharStream.H:524
void overwrite(std::streampos pos, char c)
Overwrite a single character.
Definition: OCharStream.H:312
void reserve_exact(const std::streamsize len)
Increase capacity for at least this size. Does not apply min-size or capacity doubling etc...
std::streamsize count() const
The number of bytes outputted.
Definition: OCharStream.H:120
A simple container for options an IOstream can normally have.
const char * cdata_bytes() const
The output data (start of output characters)
Definition: OCharStream.H:333
char * data_bytes() const
The span data (start of output characters)
void debug_info(Ostream &os) const
Some information about the output buffer position/capacity.
Definition: OCharStream.H:259
void pop_back(int n=1)
Decrease the put area by 1 or more elements.
std::streamsize capacity() const
The put buffer capacity.
Definition: OCharStream.H:128
UList< char > list() const
A list span of current output contents (is modifiable!!)
Definition: OCharStream.H:204
auto view() const
A string_view of buffer contents.
Definition: OCharStream.H:191
void extend_exact(std::streamsize n)
Increase (reserve) space for another n entries.
Definition: OCharStream.H:456
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
dimensionedScalar pos(const dimensionedScalar &ds)
auto str() const
For OStringStream compatibility, return the buffer as string copy.
Definition: OCharStream.H:481
void extend(std::streamsize count)
Increase (reserve) space for another count entries.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
void reserve_exact(std::streamsize n)
Reserve output space for at least this amount. Does not apply min-size or capacity doubling etc...
Definition: OCharStream.H:446
void append(std::streamsize count, char c)
Append repeated character content.
Definition: OCharStream.H:556
Foam::ocharstream stream_
The std::stream.
Definition: IOstream.H:628
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OCharStream.H:420
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: OCharStream.H:230
std::streamsize count() const
The number of bytes outputted.
Definition: OCharStream.H:425
void print(Ostream &os) const
Information about stream.
Definition: OCharStream.H:267
void pop_back(int n=1)
Rewind the end by 1 or more elements.
Definition: OCharStream.H:551
void push_back(char c)
Append a single character to the end.
Definition: OCharStream.H:275
std::streamsize size_bytes() const
The current number of output characters.
Definition: OCharStream.H:343
An OSstream with internal List storage.
Definition: OCharStream.H:354
void pop_back(int n=1)
Rewind the end by 1 or more elements.
Definition: OCharStream.H:280
An output streambuf for memory access.
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:105
void swap(List< char > &other)
Exchange buffer content and parameter contents, reset positions.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
std::streamsize capacity() const
The put buffer capacity.
Definition: OCharStream.H:435
void reserve(std::streamsize n)
Reserve output space for at least this amount. Applies a min-size and capacity doubling.
Definition: OCharStream.H:137
void reserve(const std::streamsize len)
Increase capacity (if needed) and adjust buffer pointers. Applies a min-size and capacity doubling...
void setBlockSize(int n)
Block size was used in OpenFOAM-v2306 and earlier.
Definition: OCharStream.H:612
DynamicList< char > release()
Reset buffer and return contents as a DynamicList. The list size corresponds to the region of output...
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: OCharStream.H:515
std::streamsize span_tellp() const
The current buffer put position.
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: OCharStream.H:486
void push_back(char c)
Append a single character to the end.
Definition: OCharStream.H:546
void extend(std::streamsize n)
Increase (reserve) space for another n entries.
Definition: OCharStream.H:451
void seek(std::streampos pos)
Reposition the stream from the start.
Definition: OCharStream.H:179
char * data_bytes()
The output data (start of output characters)
Definition: OCharStream.H:338
std::streampos tellp() const
Position of the put buffer.
Definition: OCharStream.H:415
surface1 clear()
const dimensionedScalar c
Speed of light in a vacuum.
versionNumber version() const noexcept
Get the stream version.
void extend(std::streamsize count)
Increase (reserve) space for another count entries.
Definition: OCharStream.H:154
bool in_range(std::streampos pos) const
True if position is within the current output range.
label n
DynamicList< char > release()
Reset buffer and return contents.
Definition: OCharStream.H:505
void append(std::streamsize count, char c)
Append repeated character content.
Definition: OCharStream.H:285
void reserve(std::streamsize n)
Reserve output space for at least this amount.
Definition: OCharStream.H:440
std::streamsize size_bytes() const
The span size (size of output buffer)
void extend_exact(std::streamsize count)
Increase (reserve) space for another count entries.
Definition: OCharStream.H:162
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
std::string str() const
For ostringstream compatibility, return the buffer as string copy.
Definition: OCharStream.H:218
OCharStream OListStream
Definition: OCharStream.H:48
streamFormat format() const noexcept
Get the current stream format.
void overwrite(std::streampos pos, const char *data, std::streamsize count)
Overwrite a sub-slice with character content.
Namespace for OpenFOAM.
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OCharStream.H:112
DynamicList< char > release()
Reset buffer and return contents.
Definition: OCharStream.H:249