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-2023 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 put buffer capacity
116  std::streamsize capacity() const
117  {
119  }
120 
121  //- Reserve output space for at least this amount
122  void reserve(const std::streamsize n)
123  {
125  }
126 
127  //- Rewind the stream, clearing any old errors
128  void rewind()
129  {
130  buffer_type::pubseekpos(0, std::ios_base::out);
131  stream_type::clear(); // Clear old errors
132  }
133 
134  //- Span of the current output characters (is modifiable!)
135  UList<char> list() const
136  {
137  return UList<char>
138  (
140  label(buffer_type::size_bytes())
141  );
142  }
143 
144  //- A string_view (c++17) or span view (older c++) of buffer contents
145  auto view() const -> decltype(buffer_type::view())
146  {
147  return buffer_type::view();
148  }
149 
150  //- For ostringstream compatibility, return the buffer as string copy.
151  // Use sparingly - it creates a full copy!!
152  std::string str() const
153  {
154  return std::string
155  (
158  );
159  }
160 
161  //- Exchange stream content and parameter contents, reset positions
162  void swap(List<char>& other)
163  {
164  buffer_type::swap(other);
165  stream_type::clear(); // Clear old errors
166  }
167 
168  //- Exchange stream content and parameter contents, reset positions
169  template<int SizeMin>
170  void swap(DynamicList<char,SizeMin>& other)
171  {
172  buffer_type::swap(other);
173  stream_type::clear(); // Clear old errors
174  }
175 
176  //- Reset buffer and return contents
178  {
180  stream_type::clear(); // Clear old errors
181  return chars;
182  }
183 
184  //- Some information about the output buffer position/capacity
185  void debug_info(Ostream& os) const
186  {
187  os << "put=" << output_pos() << '/' << capacity();
188  }
189 };
191 
192 namespace Detail
193 {
194 
195 /*---------------------------------------------------------------------------*\
196  Class Detail::OCharStreamAllocator Declaration
197 \*---------------------------------------------------------------------------*/
198 
199 //- An allocator for holding Foam::ocharstream
200 class OCharStreamAllocator
201 {
202 protected:
203 
204  // Protected Data
205 
206  //- The stream
208 
210  // Constructors
211 
212  //- Default construct - empty
213  OCharStreamAllocator() = default;
214 };
215 
216 } // End namespace Detail
217 
218 
219 /*---------------------------------------------------------------------------*\
220  Class OCharStream Declaration
221 \*---------------------------------------------------------------------------*/
222 
223 //- An OSstream with internal List storage
224 class OCharStream
225 :
227  public Foam::OSstream
228 {
229  typedef Detail::OCharStreamAllocator allocator_type;
230 
231 public:
232 
233  // Constructors
234 
235  //- Default construct (empty output)
236  explicit OCharStream
237  (
238  IOstreamOption streamOpt = IOstreamOption()
239  )
240  :
241  allocator_type(),
242  OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
243  {}
244 
245  //- Move construct from a List of initial storage
246  explicit OCharStream
247  (
248  ::Foam::List<char>&& buffer,
249  IOstreamOption streamOpt = IOstreamOption()
250  )
251  :
252  OCharStream(streamOpt)
253  {
254  stream_.swap(buffer);
255  }
257  //- Move construct from a DynamicList of initial storage
258  //- (uses entire capacity)
259  template<int SizeMin>
260  explicit OCharStream
261  (
263  IOstreamOption streamOpt = IOstreamOption()
264  )
265  :
266  OCharStream(streamOpt)
267  {
268  stream_.swap(buffer);
269  }
270 
272  // Member Functions
273 
274  //- Position of the put buffer
275  std::streampos tellp() const { return stream_.output_pos(); }
276 
277  //- The current output position within the buffer (tellp)
278  std::streampos output_pos() const { return stream_.output_pos(); }
279 
280  //- The current output size. Same as tellp(), output_pos()
281  label size() const { return label(stream_.output_pos()); }
282 
283  //- The put buffer capacity
284  std::streamsize capacity() const { return stream_.capacity(); }
285 
286  //- Reserve output space for at least this amount
287  void reserve(const std::streamsize n) { stream_.reserve(n); }
288 
289  //- Span of the current output characters (is modifiable!)
290  UList<char> list() const { return stream_.list(); }
291 
292  //- A string_view (c++17) or span view (older c++) of buffer contents
293  auto view() const -> decltype(stream_.view())
294  {
295  return stream_.view();
296  }
297 
298  //- For OStringStream compatibility, return the buffer as string copy.
299  // Use sparingly - it creates a full copy!!
300  auto str() const -> decltype(stream_.str())
301  {
302  return stream_.str();
303  }
304 
305  //- Exchange stream content and parameter contents, reset positions
306  void swap(List<char>& other)
307  {
308  stream_.swap(other);
309  syncState();
310  }
311 
312  //- Exchange stream content and parameter contents, reset positions
313  template<int SizeMin>
314  void swap(DynamicList<char,SizeMin>& other)
315  {
316  stream_.swap(other);
317  syncState();
318  }
319 
320  //- Reset buffer and return contents
322  {
324  syncState();
325  return chars;
326  }
327 
328  //- Rewind the stream, clearing any old errors
329  virtual void rewind()
330  {
331  stream_.rewind();
332  syncState();
333  }
334 
335  //- Print stream description to Ostream
336  virtual void print(Ostream& os) const override;
337 
338 
339  // Houskeeping
341  //- Block size was used in OpenFOAM-v2306 and earlier
342  void setBlockSize(int n) {}
343 };
344 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 
348 } // End namespace Foam
349 
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
351 
352 #endif
353 
354 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
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:355
label size() const
The current output size. Same as tellp(), output_pos()
Definition: OCharStream.H:325
OCharStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty output)
Definition: OCharStream.H:271
void rewind()
Rewind the stream, clearing any old errors.
Definition: OCharStream.H:137
UList< char > list() const
Span of the current output characters (is modifiable!)
Definition: OCharStream.H:340
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:165
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
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:209
std::streamsize capacity() const
The put buffer capacity.
Definition: OCharStream.H:121
auto view() const -> decltype(stream_.view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: OCharStream.H:345
UList< char > list() const
Span of the current output characters (is modifiable!)
Definition: OCharStream.H:146
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
Foam::ocharstream stream_
The stream.
Definition: OCharStream.H:235
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OCharStream.H:320
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: OCharStream.H:180
An OSstream with internal List storage.
Definition: OCharStream.H:256
patchWriters clear()
void reserve(const std::streamsize n)
Reserve output space for at least this amount.
Definition: OCharStream.H:335
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:330
OBJstream os(runTime.globalPath()/outputName)
void reserve(const std::streamsize len)
Increment capacity (if needed) and adjust buffer pointers.
An allocator for holding Foam::ocharstream.
Definition: OCharStream.H:226
void setBlockSize(int n)
Block size was used in OpenFOAM-v2306 and earlier.
Definition: OCharStream.H:409
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:129
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: OCharStream.H:392
OCharStreamAllocator()=default
Default construct - empty.
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:363
std::streampos tellp() const
Position of the put buffer.
Definition: OCharStream.H:315
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:382
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:168
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:158
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:199