Loading...
Searching...
No Matches
ICharStream.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-------------------------------------------------------------------------------
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::ICharStream
28
29Description
30 An input stream that reads from a List and manages the List storage.
31 Similar to IStringStream but with a List for its storage instead of
32 as string to allow reuse of List contents without copying.
33
34See Also
35 Foam::OCharStream
36 Foam::ISpanStream
37 Foam::OSpanStream
38
39\*---------------------------------------------------------------------------*/
40
41#ifndef Foam_ICharStream_H
42#define Foam_ICharStream_H
43
44#include "ISpanStream.H"
45
46// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47
48namespace Foam
49{
51// Forward Declarations
52class icharstream;
53class ICharStream;
54class OCharStream;
55
56// Older names (prior to 2023-08)
58
59
60/*---------------------------------------------------------------------------*\
61 Class icharstream Declaration
62\*---------------------------------------------------------------------------*/
63
64//- Similar to std::istringstream, but with the ability to swap
65//- character content.
66//- Has some similarity to std::ispanstream (C++23)
67class icharstream
68:
69 virtual public std::ios,
71 public std::istream
72{
73 typedef Foam::memorybuf::in_dynamic buffer_type;
74 typedef std::istream stream_type;
75
76public:
77
78 // Constructors
79
80 //- Default construct - empty
82 :
83 buffer_type(),
84 stream_type(static_cast<buffer_type*>(this))
85 {}
86
87 //- Copy construct from content
88 icharstream(const char* buffer, size_t nbytes)
89 :
91 {
92 reset(buffer, nbytes);
93 }
94
95 //- Move construct from List
96 icharstream(List<char>&& buffer)
97 :
99 {
100 swap(buffer);
101 }
102
103 //- Move construct from DynamicList
104 template<int SizeMin>
106 :
109 swap(buffer);
110 }
111
112
113 // Member Functions
114
115 //- The current get position within the buffer (tellg)
116 std::streampos input_pos() const
117 {
119 }
120
121 //- The get buffer capacity
122 std::streamsize capacity() const
123 {
125 }
126
127 //- The number of characters remaining in the get area.
128 //- Same as (capacity() - input_pos())
129 std::streamsize remaining() const
130 {
132 }
133
134 //- A string_view of buffer contents
135 auto view() const { return buffer_type::view(); }
136
137 //- A sub-slice string view of the buffer contents
138 auto view(size_t pos, size_t len = std::string::npos) const
139 {
140 return buffer_type::view(pos, len);
141 }
142
143 //- A list 'span' of buffer contents (caution: is modifiable!!)
144 UList<char> list() const
145 {
147 (
150 );
152
153 //- For istringstream compatibility, return the buffer as string copy.
154 // Use sparingly - it creates a full copy!!
155 std::string str() const
156 {
157 return std::string
158 (
161 );
162 }
163
164 //- Rewind the stream, clearing any old errors
165 void rewind()
166 {
167 buffer_type::pubseekpos(0, std::ios_base::in);
168 stream_type::clear(); // Clear old errors
169 }
170
171 //- Reposition the stream from the start
172 void seek(std::streampos pos)
175 {
176 buffer_type::pubseekpos(pos, std::ios_base::in);
177 stream_type::clear(); // Clear old errors
178 }
179 }
180
181 //- Reset stream content (copy), reset positions
182 void reset(const char* buffer, size_t nbytes)
183 {
184 buffer_type::reset(buffer, nbytes);
185 stream_type::clear(); // Clear old errors
186 }
187
188 //- Exchange stream content and parameter contents, reset positions
189 void swap(List<char>& other)
190 {
191 buffer_type::swap(other);
192 stream_type::clear(); // Clear old errors
193 }
195 //- Exchange stream content and parameter contents, reset positions
196 template<int SizeMin>
198 {
199 buffer_type::swap(other);
200 stream_type::clear(); // Clear old errors
201 }
202
203 //- Reset stream and return contents as a List
205 {
207 stream_type::clear(); // Clear old errors
208 return chars;
209 }
210
211 //- Some information about the input buffer position/capacity
212 void debug_info(Ostream& os) const
213 {
214 os << "get=" << input_pos() << '/' << capacity();
216
217 //- Information about stream
218 void print(Ostream& os) const { debug_info(os); os << '\n'; }
219};
220
221
222/*---------------------------------------------------------------------------*\
223 Class ICharStream Declaration
224\*---------------------------------------------------------------------------*/
226//- An ISstream with internal List storage. Always UNCOMPRESSED.
227class ICharStream
228:
229 public Foam::Detail::StreamAllocator<Foam::icharstream>,
230 public Foam::ISstream
231{
232 typedef
234 allocator_type;
235
236public:
237
238 // Constructors
239
240 //- Default construct (empty), optionally with specified stream option
241 explicit ICharStream
242 (
243 IOstreamOption streamOpt = IOstreamOption()
245 :
246 allocator_type(),
247 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
248 {}
249
250 //- Copy construct from string content
251 explicit ICharStream
253 const std::string& buffer,
254 IOstreamOption streamOpt = IOstreamOption()
255 )
256 :
257 ICharStream(streamOpt)
258 {
259 stream_.reset(buffer.data(), buffer.size());
260 }
261
262 //- Move construct from List
263 explicit ICharStream
264 (
265 List<char>&& buffer,
266 IOstreamOption streamOpt = IOstreamOption()
267 )
268 :
269 ICharStream(streamOpt)
270 {
271 stream_.swap(buffer);
272 }
273
274 //- Move construct from DynamicList (uses current size)
275 template<int SizeMin>
276 explicit ICharStream
277 (
280 )
281 :
282 ICharStream(streamOpt)
283 {
284 stream_.swap(buffer);
285 }
286
287
288 // Member Functions
289
290 // Access/Query
292 //- Position of the get buffer
293 std::streampos tellg() const { return stream_.input_pos(); }
294
295 //- The current get position within the buffer (tellg)
296 std::streampos input_pos() const { return stream_.input_pos(); }
297
298 //- The input list size. Same as capacity()
299 label size() const { return label(stream_.capacity()); }
300
301 //- The get buffer capacity
302 std::streamsize capacity() const { return stream_.capacity(); }
303
304 //- The number of characters remaining in the get area.
305 //- Same as (capacity() - input_pos())
306 std::streamsize remaining() const { return stream_.remaining(); }
307
308 //- A string_view of buffer contents
309 auto view() const { return stream_.view(); }
310
311 //- A sub-slice string view of the buffer contents
312 auto view(size_t pos, size_t len = std::string::npos) const
313 {
314 return stream_.view(pos, len);
315 }
316
317 //- A list \em span of the input characters (is modifiable!)
318 UList<char> list() const { return stream_.list(); }
319
320 //- For IStringStream compatibility, return the buffer as string copy.
321 // Use sparingly - it creates a full copy!!
322 auto str() const { return stream_.str(); }
323
324
325 // Edit
326
327 //- Reset content (copy)
328 void reset(const char* buffer, size_t nbytes)
329 {
330 stream_.reset(buffer, nbytes);
331 syncState();
332 }
333
334 //- Exchange stream content and parameter contents, reset positions
335 void swap(List<char>& other)
336 {
337 stream_.swap(other);
338 syncState();
340
341 //- Exchange stream content and parameter contents, reset positions
342 template<int SizeMin>
345 stream_.swap(other);
346 syncState();
347 }
348
349 //- Reset stream and return contents as a List
351 {
352 DynamicList<char> chars(stream_.release());
353 syncState();
354 return chars;
355 }
356
357 //- Rewind the stream, clearing any old errors
358 virtual void rewind() override
359 {
361 stream_.rewind();
362 syncState();
363 }
364
365 //- Reposition the stream from the start
366 void seek(std::streampos pos)
367 {
368 stream_.seek(pos);
369 syncState();
371
372
373 // Other
374
375 //- Print stream description
376 virtual void print(Ostream& os) const override
377 {
378 os << "icharstream: ";
379 stream_.debug_info(os);
380 os << '\n';
381 }
382
383
384 // Member Operators
386 //- A non-const reference to const Istream
387 // Needed for read-constructors where the stream argument is temporary
388 Istream& operator()() const
389 {
390 // Could also rewind
391 return const_cast<ICharStream&>(*this);
392 }
394
395
396// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397
398} // End namespace Foam
399
400// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401
402#endif
403
404// ************************************************************************* //
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition IOstream.H:620
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
An ISstream with internal List storage. Always UNCOMPRESSED.
ICharStream(List< char > &&buffer, IOstreamOption streamOpt=IOstreamOption())
Move construct from List.
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
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.
ICharStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty), optionally with specified stream option.
void swap(DynamicList< char, SizeMin > &other)
Exchange stream content and parameter contents, reset positions.
std::streamsize capacity() const
The get buffer capacity.
ICharStream(DynamicList< char, SizeMin > &&buffer, IOstreamOption streamOpt=IOstreamOption())
Move construct from DynamicList (uses current size).
DynamicList< char > release()
Reset stream and return contents as a List.
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.
label size() const
The input list size. Same as capacity().
auto view() const
A string_view of buffer contents.
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 content (copy).
ICharStream(const std::string &buffer, IOstreamOption streamOpt=IOstreamOption())
Copy construct from string content.
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.
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
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An OSstream with internal List storage.
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 the ability to swap character content. Has some similarity to...
Definition ICharStream.H:67
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()).
void swap(DynamicList< char, SizeMin > &other)
Exchange stream content and parameter contents, reset positions.
icharstream(DynamicList< char, SizeMin > &&buffer)
Move construct from DynamicList.
std::streamsize capacity() const
The get buffer capacity.
icharstream()
Default construct - empty.
Definition ICharStream.H:78
DynamicList< char > release()
Reset stream and return contents as a List.
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 buffer contents (caution: is modifiable!!).
void debug_info(Ostream &os) const
Some information about the input buffer position/capacity.
void seek(std::streampos pos)
Reposition the stream from the start.
auto view() const
A string_view of buffer contents.
void rewind()
Rewind the stream, clearing any old errors.
std::streampos input_pos() const
The current get position within the buffer (tellg).
icharstream(List< char > &&buffer)
Move construct from List.
Definition ICharStream.H:97
icharstream(const char *buffer, size_t nbytes)
Copy construct from content.
Definition ICharStream.H:87
void reset(const char *buffer, size_t nbytes)
Reset stream content (copy), reset positions.
std::string str() const
For istringstream compatibility, return the buffer as string copy.
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).
An output streambuf for memory access.
void swap(List< char > &other)
Exchange buffer content and parameter contents, reset positions.
DynamicList< char > release()
Reset buffer and return contents. The list size and capacity are identical.
void reset(const char *s, std::streamsize n)
Reset content (copy).
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
ICharStream IListStream
Definition ICharStream.H:50
word format(conversionProperties.get< word >("format"))