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-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::OSpanStream
28 
29 Description
30  Similar to OStringStream 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 
75 See 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 "DynamicList.H"
87 #include "OSstream.H"
88 
89 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
90 
91 namespace Foam
92 {
93 
94 // Forward Declarations
95 class ospanstream;
96 class OSpanStream;
97 
98 // Older names (prior to 2023-08)
100 
101 
102 /*---------------------------------------------------------------------------*\
103  Class ospanstream Declaration
104 \*---------------------------------------------------------------------------*/
105 
106 //- Similar to std::ostringstream, but with an externally managed output buffer
107 //- which makes it most similar to std::ospanstream (C++23)
108 class ospanstream
109 :
110  virtual public std::ios,
111  protected Foam::memorybuf::out_base,
112  public std::ostream
113 {
115  typedef std::ostream stream_type;
116 
117 public:
118 
119  // Constructors
120 
121  //- Default construct - empty
122  ospanstream()
123  :
124  buffer_type(),
125  stream_type(static_cast<buffer_type*>(this))
126  {}
127 
128  //- Construct for character array and number of bytes
129  ospanstream(char* buffer, size_t nbytes)
130  :
131  buffer_type(buffer, nbytes),
132  stream_type(static_cast<buffer_type*>(this))
133  {}
134 
135 
136  // Member Functions
137 
138  //- The current output position within the buffer (tellp)
139  std::streampos output_pos() const
140  {
141  return buffer_type::span_tellp();
142  }
143 
144  //- The put buffer capacity
145  std::streamsize capacity() const
146  {
148  }
149 
150  //- Span of the current output characters (is modifiable!)
151  UList<char> list() const
152  {
153  return UList<char>
154  (
157  );
158  }
159 
160  //- A string_view (c++17) or span view (older c++) of buffer contents
161  auto view() const -> decltype(buffer_type::view())
162  {
163  return buffer_type::view();
164  }
165 
166  //- For ostringstream compatibility, return the buffer as string copy.
167  // Use sparingly - it creates a full copy!!
168  std::string str() const
169  {
170  return std::string
171  (
174  );
175  }
176 
177  //- Rewind the stream, clearing any old errors
178  void rewind()
179  {
180  buffer_type::pubseekpos(0, std::ios_base::out);
181  stream_type::clear(); // Clear any old errors
182  }
183 
184  //- Reset the put buffer area
185  void reset(char* buffer, size_t nbytes)
186  {
187  buffer_type::resetp(buffer, nbytes);
188  stream_type::clear(); // Clear any old errors
189  }
191  //- Reset the put buffer area to use the data area from a string
192  void reset(std::string& s)
193  {
194  s.resize(s.capacity());
195  buffer_type::resetp(&s[0], s.size());
196  stream_type::clear(); // Clear any old errors
197  }
198 
199  //- Some information about the output buffer position/capacity
200  void debug_info(Ostream& os) const
201  {
202  os << "put="
203  << output_pos() << '/' << capacity();
204  }
205 };
206 
207 
208 namespace Detail
209 {
210 
211 /*---------------------------------------------------------------------------*\
212  Class Detail::OSpanStreamAllocator Declaration
213 \*---------------------------------------------------------------------------*/
214 
215 //- An allocator for holding Foam::ospanstream
216 class OSpanStreamAllocator
217 {
218 protected:
219 
220  // Protected Data
221 
222  //- The stream
224 
225 
226  // Constructors
227 
228  //- Default construct (empty)
229  OSpanStreamAllocator() = default;
230 };
231 
232 } // End namespace Detail
233 
234 
235 /*---------------------------------------------------------------------------*\
236  Class OSpanStream Declaration
237 \*---------------------------------------------------------------------------*/
238 
239 class OSpanStream
240 :
242  public Foam::OSstream
243 {
244  typedef Detail::OSpanStreamAllocator allocator_type;
246 public:
247 
248  // Constructors
249 
250  //- Default construct (empty output)
251  explicit OSpanStream
252  (
253  IOstreamOption streamOpt = IOstreamOption()
254  )
255  :
256  allocator_type(),
257  OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
258  {}
259 
260  //- Use data area from string content
261  explicit OSpanStream
262  (
263  std::string& buffer,
264  IOstreamOption streamOpt = IOstreamOption()
265  )
266  :
267  OSpanStream(streamOpt)
268  {
269  stream_.reset(buffer);
270  }
271 
272  //- Construct using specified buffer and number of bytes
274  (
275  char* buffer,
276  size_t nbytes,
277  IOstreamOption streamOpt = IOstreamOption()
278  )
279  :
280  OSpanStream(streamOpt)
281  {
282  stream_.reset(buffer, nbytes);
283  }
284 
285  //- Construct using data area from a List and its inherent storage size
286  explicit OSpanStream
287  (
288  ::Foam::UList<char>& buffer,
289  IOstreamOption streamOpt = IOstreamOption()
290  )
291  :
292  OSpanStream(buffer.data(), buffer.size(), streamOpt)
293  {}
294 
295  //- Construct using full data area from DynamicList
296  template<int SizeMin>
297  explicit OSpanStream
298  (
300  IOstreamOption streamOpt = IOstreamOption()
301  )
302  :
303  OSpanStream(buffer.data(), buffer.capacity(), streamOpt)
304  {
305  buffer.resize(buffer.capacity()); // Uses entire space
306  }
307 
308 
309  // Member Functions
310 
311  //- Position of the put buffer
312  std::streampos tellp() const { return stream_.output_pos(); }
313 
314  //- The current output position within the buffer (tellp)
315  std::streampos output_pos() const { return stream_.output_pos(); }
316 
317  //- The current output size. Same as tellp(), output_pos()
318  label size() const { return label(stream_.output_pos()); }
320  //- The put buffer capacity
321  std::streamsize capacity() const { return stream_.capacity(); }
322 
323  //- Span of the current output characters (is modifiable!)
324  UList<char> list() const { return stream_.list(); }
325 
326  //- A string_view (c++17) or span view (older c++) of buffer contents
327  auto view() const -> decltype(stream_.view())
328  {
329  return stream_.view();
330  }
331 
332  //- For OStringStream compatibility, return buffer as string copy.
333  // Use sparingly - it creates a full copy!!
334  auto str() const -> decltype(stream_.str())
335  {
336  return stream_.str();
337  }
338 
339  //- Reset the put area
340  void reset(char* buffer, size_t nbytes)
341  {
342  stream_.reset(buffer, nbytes);
343  syncState();
344  }
345 
346  //- Reset the put buffer area to use the data area from a string
347  void reset(std::string& s)
348  {
349  stream_.reset(s);
350  syncState();
351  }
352 
353  //- Rewind the stream, clearing any old errors
354  virtual void rewind()
355  {
356  stream_.rewind();
357  syncState();
358  }
359 
360  //- Print stream description to Ostream
361  virtual void print(Ostream& os) const override;
362 };
364 
365 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
366 
367 } // End namespace Foam
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 #endif
372 
373 // ************************************************************************* //
label size() const
The current output size. Same as tellp(), output_pos()
Definition: OSpanStream.H:358
auto str() const -> decltype(stream_.str())
For OStringStream compatibility, return buffer as string copy.
Definition: OSpanStream.H:383
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
std::streamsize span_capacity() const
The put buffer capacity.
An allocator for holding Foam::ospanstream.
Definition: OSpanStream.H:236
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:165
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content...
Definition: DynamicListI.H:353
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OSpanStream.H:353
std::streamsize capacity() const
The put buffer capacity.
Definition: OSpanStream.H:363
A simple container for options an IOstream can normally have.
char * data_bytes() const
The span data (start of output characters)
std::string str() const
For ostringstream compatibility, return the buffer as string copy.
Definition: OSpanStream.H:178
void rewind()
Rewind the stream, clearing any old errors.
Definition: OSpanStream.H:190
OSpanStream UOListStream
Definition: OSpanStream.H:89
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: OSpanStream.H:409
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicList.H:225
std::streamsize capacity() const
The put buffer capacity.
Definition: OSpanStream.H:148
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
void reset(char *buffer, size_t nbytes)
Reset the put buffer area.
Definition: OSpanStream.H:199
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
UList< char > list() const
Span of the current output characters (is modifiable!)
Definition: OSpanStream.H:156
Foam::ospanstream stream_
The stream.
Definition: OSpanStream.H:245
patchWriters clear()
std::streampos tellp() const
Position of the put buffer.
Definition: OSpanStream.H:348
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void debug_info(Ostream &os) const
Some information about the output buffer position/capacity.
Definition: OSpanStream.H:218
void reset(char *buffer, size_t nbytes)
Reset the put area.
Definition: OSpanStream.H:391
OBJstream os(runTime.globalPath()/outputName)
std::streamsize span_tellp() const
The current buffer put position.
void resetp(char *s, std::streamsize n)
Reset put buffer with character data (can be nullptr) and count.
UList< char > list() const
Span of the current output characters (is modifiable!)
Definition: OSpanStream.H:368
std::streampos output_pos() const
The current output position within the buffer (tellp)
Definition: OSpanStream.H:140
auto view() const -> decltype(buffer_type::view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: OSpanStream.H:168
OSpanStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty output)
Definition: OSpanStream.H:278
versionNumber version() const noexcept
Get the stream version.
stdFoam::span< const char > view() const
std::streamsize size_bytes() const
The span size (size of output buffer)
OSpanStreamAllocator()=default
Default construct (empty)
An output streambuf for memory access.
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))
Similar to std::ostringstream, but with an externally managed output buffer which makes it most simil...
Definition: OSpanStream.H:103
Similar to OStringStream but using an externally managed buffer for its output.
Definition: OSpanStream.H:263
streamFormat format() const noexcept
Get the current stream format.
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SpanStreams.C:34
Namespace for OpenFOAM.
ospanstream()
Default construct - empty.
Definition: OSpanStream.H:119
auto view() const -> decltype(stream_.view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: OSpanStream.H:373