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-2024 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  The default initial size is 512-bytes and uses size doubling.
35  After construction can use the reserve() method to adjust this.
36 
37 See Also
38  Foam::ICharStream
39  Foam::OSpanStream
40  Foam::ISpanStream
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef Foam_OCharStream_H
45 #define Foam_OCharStream_H
46 
47 #include "OSpanStream.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 class ocharstream;
56 class OCharStream;
57 
58 // Older names (prior to 2023-08)
59 typedef OCharStream OListStream;
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class ocharstream Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 //- Similar to std::ostringstream, but with the ability to swap
67 //- character content.
68 //- Has some similarity to std::ospanstream (C++23)
69 class ocharstream
70 :
71  virtual public std::ios,
73  public std::ostream
74 {
76  typedef std::ostream stream_type;
77 
78 public:
79 
80  // Constructors
81 
82  //- Default construct - empty
83  ocharstream()
84  :
85  buffer_type(),
86  stream_type(static_cast<buffer_type*>(this))
87  {}
88 
89  //- Move construct from List
90  ocharstream(List<char>&& buffer)
91  :
92  ocharstream()
93  {
94  swap(buffer);
95  }
96 
97  //- Move construct from DynamicList
98  template<int SizeMin>
100  :
101  ocharstream()
102  {
103  swap(buffer);
104  }
105 
106 
107  // Member Functions
108 
109  //- The current output position within the buffer (tellp)
110  std::streampos output_pos() const
111  {
112  return buffer_type::span_tellp();
113  }
114 
115  //- The number of bytes outputted
116  std::streamsize count() const
117  {
118  return buffer_type::size_bytes();
119  }
120 
121  //- The put buffer capacity
122  std::streamsize capacity() const
123  {
125  }
126 
127  //- Reserve output space for at least this amount
128  void reserve(const std::streamsize n)
129  {
131  }
132 
133  //- Rewind the stream, clearing any old errors
134  void rewind()
135  {
136  buffer_type::pubseekpos(0, std::ios_base::out);
137  stream_type::clear(); // Clear old errors
138  }
139 
140  //- Span of the current output characters (is modifiable!)
141  UList<char> list() const
142  {
143  return UList<char>
144  (
146  label(buffer_type::size_bytes())
147  );
148  }
149 
150  //- A string_view (c++17) or span view (older c++) of buffer contents
151  auto view() const -> decltype(buffer_type::view())
152  {
153  return buffer_type::view();
154  }
155 
156  //- For ostringstream compatibility, return the buffer as string copy.
157  // Use sparingly - it creates a full copy!!
158  std::string str() const
159  {
160  return std::string
161  (
164  );
165  }
167  //- Exchange stream content and parameter contents, reset positions
168  void swap(List<char>& other)
169  {
170  buffer_type::swap(other);
171  stream_type::clear(); // Clear old errors
172  }
173 
174  //- Exchange stream content and parameter contents, reset positions
175  template<int SizeMin>
177  {
178  buffer_type::swap(other);
179  stream_type::clear(); // Clear old errors
180  }
181 
182  //- Reset buffer and return contents
184  {
186  stream_type::clear(); // Clear old errors
187  return chars;
188  }
189 
190  //- Some information about the output buffer position/capacity
191  void debug_info(Ostream& os) const
192  {
193  os << "put=" << output_pos() << '/' << capacity();
194  }
195 };
196 
197 
198 /*---------------------------------------------------------------------------*\
199  Class OCharStream Declaration
200 \*---------------------------------------------------------------------------*/
201 
202 //- An OSstream with internal List storage
203 class OCharStream
204 :
205  public Foam::Detail::StreamAllocator<Foam::ocharstream>,
206  public Foam::OSstream
207 {
208  typedef
210  allocator_type;
211 
212 public:
213 
214  // Constructors
215 
216  //- Default construct (empty output)
217  explicit OCharStream
218  (
219  IOstreamOption streamOpt = IOstreamOption()
220  )
221  :
222  allocator_type(),
223  OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
224  {}
225 
226  //- Move construct from a List of initial storage
227  explicit OCharStream
228  (
229  ::Foam::List<char>&& buffer,
230  IOstreamOption streamOpt = IOstreamOption()
231  )
232  :
233  OCharStream(streamOpt)
234  {
235  stream_.swap(buffer);
236  }
237 
238  //- Move construct from a DynamicList of initial storage
239  //- (uses entire capacity)
240  template<int SizeMin>
241  explicit OCharStream
242  (
244  IOstreamOption streamOpt = IOstreamOption()
245  )
246  :
247  OCharStream(streamOpt)
248  {
249  stream_.swap(buffer);
250  }
251 
252 
253  // Member Functions
254 
255  //- Position of the put buffer
256  std::streampos tellp() const { return stream_.output_pos(); }
257 
258  //- The current output position within the buffer (tellp)
259  std::streampos output_pos() const { return stream_.output_pos(); }
261  //- The number of bytes outputted
262  std::streamsize count() const { return stream_.count(); }
263 
264  //- The current output size. Same as count(), output_pos(), tellp().
265  label size() const { return label(stream_.count()); }
266 
267  //- The put buffer capacity
268  std::streamsize capacity() const { return stream_.capacity(); }
269 
270  //- Reserve output space for at least this amount
271  void reserve(const std::streamsize n) { stream_.reserve(n); }
272 
273  //- Span of the current output characters (is modifiable!)
274  UList<char> list() const { return stream_.list(); }
275 
276  //- A string_view (c++17) or span view (older c++) of buffer contents
277  auto view() const -> decltype(stream_.view())
278  {
279  return stream_.view();
280  }
281 
282  //- For OStringStream compatibility, return the buffer as string copy.
283  // Use sparingly - it creates a full copy!!
284  auto str() const -> decltype(stream_.str())
285  {
286  return stream_.str();
287  }
288 
289  //- Exchange stream content and parameter contents, reset positions
290  void swap(List<char>& other)
291  {
292  stream_.swap(other);
293  syncState();
294  }
295 
296  //- Exchange stream content and parameter contents, reset positions
297  template<int SizeMin>
298  void swap(DynamicList<char,SizeMin>& other)
299  {
300  stream_.swap(other);
301  syncState();
302  }
303 
304  //- Reset buffer and return contents
306  {
308  syncState();
309  return chars;
310  }
311 
312  //- Rewind the stream, clearing any old errors
313  virtual void rewind()
314  {
315  stream_.rewind();
316  syncState();
317  }
318 
319  //- Print stream description to Ostream
320  virtual void print(Ostream& os) const override;
321 
323  // Houskeeping
324 
325  //- Block size was used in OpenFOAM-v2306 and earlier
326  void setBlockSize(int n) {}
327 };
328 
329 
330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 
332 } // End namespace Foam
333 
334 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335 
336 #endif
338 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:600
std::streamsize span_capacity() const
The put buffer capacity.
auto str() const -> decltype(stream_.str())
For OStringStream compatibility, return the buffer as string copy.
Definition: OCharStream.H:337
label size() const
The current output size. Same as count(), output_pos(), tellp().
Definition: OCharStream.H:307
OCharStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty output)
Definition: OCharStream.H:248
void rewind()
Rewind the stream, clearing any old errors.
Definition: OCharStream.H:145
UList< char > list() const
Span of the current output characters (is modifiable!)
Definition: OCharStream.H:322
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:182
ocharstream()
Default construct - empty.
Definition: OCharStream.H:80
Similar to std::ostringstream, but with the ability to swap character content. Has some similarity to...
Definition: OCharStream.H:64
std::streamsize count() const
The number of bytes outputted.
Definition: OCharStream.H:121
A simple container for options an IOstream can normally have.
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:217
std::streamsize capacity() const
The put buffer capacity.
Definition: OCharStream.H:129
auto view() const -> decltype(stream_.view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: OCharStream.H:327
UList< char > list() const
Span of the current output characters (is modifiable!)
Definition: OCharStream.H:154
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
Foam::ocharstream stream_
The std::stream.
Definition: IOstream.H:609
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OCharStream.H:297
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: OCharStream.H:188
std::streamsize count() const
The number of bytes outputted.
Definition: OCharStream.H:302
An OSstream with internal List storage.
Definition: OCharStream.H:231
void reserve(const std::streamsize n)
Reserve output space for at least this amount.
Definition: OCharStream.H:317
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:312
OBJstream os(runTime.globalPath()/outputName)
void reserve(const std::streamsize len)
Increment capacity (if needed) and adjust buffer pointers.
void setBlockSize(int n)
Block size was used in OpenFOAM-v2306 and earlier.
Definition: OCharStream.H:391
DynamicList< char > release()
Reset buffer and return contents as a DynamicList. The list size corresponds to the region of output...
void reserve(const std::streamsize n)
Reserve output space for at least this amount.
Definition: OCharStream.H:137
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: OCharStream.H:374
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:345
std::streampos tellp() const
Position of the put buffer.
Definition: OCharStream.H:292
surface1 clear()
versionNumber version() const noexcept
Get the stream version.
stdFoam::span< const char > view() const
label n
DynamicList< char > release()
Reset buffer and return contents.
Definition: OCharStream.H:364
std::streamsize size_bytes() const
The span size (size of output buffer)
std::string str() const
For ostringstream compatibility, return the buffer as string copy.
Definition: OCharStream.H:176
OCharStream OListStream
Definition: OCharStream.H:49
streamFormat format() const noexcept
Get the current stream format.
auto view() const -> decltype(buffer_type::view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: OCharStream.H:166
Namespace for OpenFOAM.
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OCharStream.H:113
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SpanStreams.C:50
DynamicList< char > release()
Reset buffer and return contents.
Definition: OCharStream.H:207