Loading...
Searching...
No Matches
ISpanStream.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-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::ISpanStream
28
29Description
30 Similar to IStringStream but using an externally managed buffer for its
31 input. This allows the input buffer to be filled (and refilled) from
32 various sources.
33
34 Note that this stream will normally be used as a "one-shot" reader.
35 Caution must be exercised that the referenced buffer remains valid and
36 without any intermediate resizing for the duration of the stream's use.
37
38 An example of possible use:
39 \code
40 DynamicList<char> buffer(4096); // allocate some large buffer
41
42 nread = something.read(buffer.data(),1024); // fill with content
43 buffer.resize(nread); // content size
44
45 // Construct dictionary, or something else
46 ISpanStream is(buffer)
47 dictionary dict1(is);
48
49 // Sometime later
50 nread = something.read(buffer.data(),2048); // fill with content
51 buffer.resize(nread); // content size
52
53 // Without intermediate variable
54 dictionary dict2(ISpanStream(buffer)());
55 \endcode
56
57See Also
58 Foam::ICharStream
59 Foam::OCharStream
60 Foam::OSpanStream
61
62\*---------------------------------------------------------------------------*/
63
64#ifndef Foam_ISpanStream_H
65#define Foam_ISpanStream_H
66
67#include "memoryStreamBuffer.H"
68#include "ISstream.H"
69
70// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71
72namespace Foam
75// Forward Declarations
76class ispanstream;
77class ISpanStream;
78
79// Older names (prior to 2023-08)
82
83
84/*---------------------------------------------------------------------------*\
85 Class ispanstream Declaration
86\*---------------------------------------------------------------------------*/
87
88//- Similar to std::istringstream, but with an externally managed input buffer
89//- which makes it most similar to std::ispanstream (C++23)
90// This allows the input buffer to be filled or refilled from various sources
91// without copying.
92class ispanstream
93:
94 virtual public std::ios,
96 public std::istream
97{
98 typedef Foam::memorybuf::in_base buffer_type;
99 typedef std::istream stream_type;
100
101public:
102
103 // Constructors
105 //- Default construct - empty
107 :
108 buffer_type(),
109 stream_type(static_cast<buffer_type*>(this))
110 {}
111
112 //- Construct (shallow copy) for character array and number of bytes
113 ispanstream(const char* buffer, size_t nbytes)
114 :
115 buffer_type(const_cast<char*>(buffer), nbytes),
116 stream_type(static_cast<buffer_type*>(this))
117 {}
118
119 //- Construct (shallow copy) from std::string_view content
120 explicit ispanstream(std::string_view s)
121 :
122 buffer_type(const_cast<char*>(s.data()), s.size()),
123 stream_type(static_cast<buffer_type*>(this))
124 {}
125
126
127 // Member Functions
128
129 //- The current get position within the buffer (tellg)
130 std::streampos input_pos() const
131 {
133 }
135 //- The get buffer capacity
136 std::streamsize capacity() const
137 {
139 }
140
141 //- The number of characters remaining in the get area.
142 //- Same as (capacity() - input_pos())
143 std::streamsize remaining() const
144 {
146 }
147
148 //- A list \em span of the input characters (is modifiable!)
149 UList<char> list() const
150 {
155 );
156 }
157
158 //- A string_view of buffer contents
159 auto view() const { return buffer_type::view(); }
160
161 //- A sub-slice string view of the buffer contents
162 auto view(size_t pos, size_t len = std::string::npos) const
163 {
164 return buffer_type::view(pos, len);
165 }
166
167 //- For istringstream compatibility, return the buffer as string copy.
168 // Use sparingly - it creates a full copy!!
169 std::string str() const
170 {
171 return std::string
172 (
175 );
177
178 //- Rewind the stream, clearing any old errors
179 void rewind()
180 {
181 buffer_type::pubseekpos(0, std::ios_base::in);
182 stream_type::clear(); // Clear old errors
183 }
184
185 //- Reposition the stream from the start
186 void seek(std::streampos pos)
187 {
189 {
190 buffer_type::pubseekpos(pos, std::ios_base::in);
191 stream_type::clear(); // Clear old errors
192 }
193 }
194
195 //- Reset the get buffer area
196 void reset(const char* buffer, size_t nbytes)
197 {
198 buffer_type::resetg(const_cast<char*>(buffer), nbytes);
199 stream_type::clear(); // Clear old errors
200 }
201
202 //- Reset the get buffer area to use the nul-terminated buffer
203 void reset(const char* s)
204 {
205 reset(s, (s ? std::char_traits<char>::length(s) : 0));
206 }
208 //- Reset the get buffer area to use the data from a string
209 void reset(const std::string& s)
210 {
211 buffer_type::resetg(const_cast<char*>(s.data()), s.size());
212 stream_type::clear(); // Clear old errors
213 }
214
215 //- Reset the get buffer area to use the data from a string_view
216 void reset(std::string_view s)
217 {
218 buffer_type::resetg(const_cast<char*>(s.data()), s.size());
219 stream_type::clear(); // Clear old errors
220 }
221
222 //- Some information about the input buffer position/capacity
223 void debug_info(Ostream& os) const
224 {
225 os << "get=" << input_pos() << '/' << capacity();
226 }
227
228 //- Information about stream
229 void print(Ostream& os) const { debug_info(os); os << '\n'; }
230};
231
232
233/*---------------------------------------------------------------------------*\
234 Class ISpanStream Declaration
235\*---------------------------------------------------------------------------*/
237class ISpanStream
238:
239 public Foam::Detail::StreamAllocator<Foam::ispanstream>,
240 public Foam::ISstream
241{
242 typedef
244 allocator_type;
246public:
247
248 // Constructors
249
250 //- Default construct (empty), optionally with specified stream option
251 explicit ISpanStream
252 (
253 IOstreamOption streamOpt = IOstreamOption()
255 :
256 allocator_type(),
257 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
258 {}
259
260 //- Construct using specified buffer and number of bytes
263 const char* buffer,
264 size_t nbytes,
265 IOstreamOption streamOpt = IOstreamOption()
266 )
267 :
268 ISpanStream(streamOpt)
269 {
270 reset(buffer, nbytes);
271 }
272
273 //- Construct using specified nul-terminated buffer
274 explicit ISpanStream(const char* buffer)
275 :
277 (
278 buffer,
279 (buffer ? std::char_traits<char>::length(buffer) : 0)
280 )
281 {}
282
283 //- Use data area from string content
284 explicit ISpanStream
285 (
286 const std::string& s,
287 IOstreamOption streamOpt = IOstreamOption()
288 )
289 :
290 ISpanStream(s.data(), s.size(), streamOpt)
291 {}
292
293 //- Use data area from string_view content
294 explicit ISpanStream
295 (
296 std::string_view s,
297 IOstreamOption streamOpt = IOstreamOption()
299 :
300 ISpanStream(s.data(), s.size(), streamOpt)
301 {}
302
303 //- Construct using data area from a List and its inherent storage size
304 // Uses addressed size, thus no special treatment for a DynamicList
305 explicit ISpanStream
306 (
307 const ::Foam::UList<char>& buffer,
308 IOstreamOption streamOpt = IOstreamOption()
309 )
310 :
311 ISpanStream(buffer.cdata(), buffer.size(), streamOpt)
312 {}
314
315 // Member Functions
316
317 //- Position of the get buffer
318 std::streampos tellg() const { return stream_.input_pos(); }
319
320 //- The current get position within the buffer (tellg)
321 std::streampos input_pos() const { return stream_.input_pos(); }
322
323 //- The input list size. Same as capacity()
324 label size() const { return label(stream_.capacity()); }
326 //- The get buffer capacity
327 std::streamsize capacity() const { return stream_.capacity(); }
328
329 //- The number of characters remaining in the get area.
330 //- Same as (capacity() - input_pos())
331 std::streamsize remaining() const { return stream_.remaining(); }
332
333 //- A string_view of buffer contents
334 auto view() const { return stream_.view(); }
335
336 //- A sub-slice string view of the buffer contents
337 auto view(size_t pos, size_t len = std::string::npos) const
338 {
339 return stream_.view(pos, len);
340 }
341
342 //- A list \em span of the input characters (is modifiable!)
343 UList<char> list() const { return stream_.list(); }
344
345 //- For IStringStream compatibility, return the buffer as string copy.
346 // Use sparingly - it creates a full copy!!
347 auto str() const { return stream_.str(); }
348
349
350 //- Reset input area, position to buffer start and clear errors
351 void reset(const char* buffer, size_t nbytes)
352 {
353 stream_.reset(buffer, nbytes);
354 syncState();
355 }
356
357 //- Reset input area to use data from a string
358 void reset(const std::string& s)
359 {
360 stream_.reset(s.data(), s.size());
361 syncState();
362 }
363
364 //- Reset input area to use data from a std::string_view
365 void reset(std::string_view s)
367 stream_.reset(s.data(), s.size());
368 syncState();
369 }
370
371 //- Rewind the stream, clearing any old errors
372 virtual void rewind() override
373 {
375 stream_.rewind();
377 }
378
379 //- Reposition the stream from the start
380 void seek(std::streampos pos)
382 stream_.seek(pos);
383 syncState();
384 }
385
386 //- Print stream description
387 virtual void print(Ostream& os) const override
388 {
389 os << "ispanstream: ";
390 stream_.debug_info(os);
391 os << '\n';
393
394
395 // Member Operators
396
397 //- A non-const reference to const Istream
398 // Needed for read-constructors where the stream argument is temporary
399 Istream& operator()() const
400 {
401 // Could also rewind
402 return const_cast<ISpanStream&>(*this);
403 }
404};
406
407// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408
409} // End namespace Foam
410
411// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413#endif
414
415// ************************************************************************* //
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition IOstream.H:620
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.
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
void reset(const std::string &s)
Reset input area to use data from a string.
ISpanStream(const std::string &s, IOstreamOption streamOpt=IOstreamOption())
Use data area from string content.
ISpanStream(const char *buffer)
Construct using specified nul-terminated buffer.
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()).
std::streampos tellg() const
Position of the get buffer.
Istream & operator()() const
A non-const reference to const Istream.
std::streamsize capacity() const
The get buffer capacity.
auto view(size_t pos, size_t len=std::string::npos) const
A sub-slice string view of the buffer contents.
UList< char > list() const
A list span of the input characters (is modifiable!).
auto str() const
For IStringStream compatibility, return the buffer as string copy.
void seek(std::streampos pos)
Reposition the stream from the start.
ISpanStream(const ::Foam::UList< char > &buffer, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a List and its inherent storage size.
ISpanStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty), optionally with specified stream option.
ISpanStream(std::string_view s, IOstreamOption streamOpt=IOstreamOption())
Use data area from string_view content.
label size() const
The input list size. Same as capacity().
auto view() const
A string_view of buffer contents.
ISpanStream(const char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
virtual void rewind() override
Rewind the stream, clearing any old errors.
std::streampos input_pos() const
The current get position within the buffer (tellg).
virtual void print(Ostream &os) const override
Print stream description.
void reset(const char *buffer, size_t nbytes)
Reset input area, position to buffer start and clear errors.
void reset(std::string_view s)
Reset input area to use data from a std::string_view.
Generic input stream using a standard (STL) stream.
Definition ISstream.H:54
void syncState()
Set stream state to match that of the std::istream.
Definition ISstream.H:195
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition ISstreamI.H:25
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
virtual void rewind()=0
Rewind the stream so that it may be read again.
Definition Istream.C:214
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
Similar to std::istringstream, but with an externally managed input buffer which makes it most simila...
Definition ISpanStream.H:93
void reset(const std::string &s)
Reset the get buffer area to use the data from a string.
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()).
std::streamsize capacity() const
The get buffer capacity.
ispanstream(std::string_view s)
Construct (shallow copy) from std::string_view content.
auto view(size_t pos, size_t len=std::string::npos) const
A sub-slice string view of the buffer contents.
void print(Ostream &os) const
Information about stream.
UList< char > list() const
A list span of the input characters (is modifiable!).
void debug_info(Ostream &os) const
Some information about the input buffer position/capacity.
ispanstream(const char *buffer, size_t nbytes)
Construct (shallow copy) for character array and number of bytes.
void seek(std::streampos pos)
Reposition the stream from the start.
auto view() const
A string_view of buffer contents.
void reset(const char *s)
Reset the get buffer area to use the nul-terminated buffer.
void rewind()
Rewind the stream, clearing any old errors.
std::streampos input_pos() const
The current get position within the buffer (tellg).
ispanstream()
Default construct - empty.
void reset(const char *buffer, size_t nbytes)
Reset the get buffer area.
void reset(std::string_view s)
Reset the get buffer area to use the data from a string_view.
std::string str() const
For istringstream compatibility, return the buffer as string copy.
The base input streambuf with memory access.
void resetg(char *s, std::streamsize n)
Reset get buffer with character data (can be nullptr) and count.
char * data_bytes() const
The span data (start of input characters).
std::streamsize span_capacity() const
The get buffer capacity.
bool in_range(std::streampos pos) const
True if position is within the current input range.
std::streamsize span_remaining() const
The number of characters remaining in the get area.
auto view() const
A string view of the current input region.
std::streamsize span_tellg() const
The current buffer get position.
std::streamsize size_bytes() const
The span size (number of input characters).
OBJstream os(runTime.globalPath()/outputName)
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))
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
ISpanStream UIListStream
Definition ISpanStream.H:74
ispanstream uiliststream
Definition ISpanStream.H:73
word format(conversionProperties.get< word >("format"))