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-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::ICharStream
28 
29 Description
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 
34 See 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 
48 namespace Foam
49 {
50 
51 // Forward Declarations
52 class icharstream;
53 class ICharStream;
54 class OCharStream;
55 
56 // Older names (prior to 2023-08)
57 typedef ICharStream IListStream;
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)
67 class icharstream
68 :
69  virtual public std::ios,
71  public std::istream
72 {
74  typedef std::istream stream_type;
75 
76 public:
77 
78  // Constructors
79 
80  //- Default construct - empty
81  icharstream()
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  :
90  icharstream()
91  {
92  reset(buffer, nbytes);
93  }
94 
95  //- Move construct from List
96  icharstream(List<char>&& buffer)
97  :
98  icharstream()
99  {
100  swap(buffer);
101  }
102 
103  //- Move construct from DynamicList
104  template<int SizeMin>
106  :
107  icharstream()
108  {
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  {
118  return buffer_type::span_tellg();
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  //- Span of the input 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 istringstream 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  //- Rewind the stream, clearing any old errors
162  void rewind()
163  {
164  buffer_type::pubseekpos(0, std::ios_base::in);
165  stream_type::clear(); // Clear old errors
166  }
167 
168  //- Reset stream content (copy), reset positions
169  void reset(const char* buffer, size_t nbytes)
170  {
171  buffer_type::reset(buffer, nbytes);
172  stream_type::clear(); // Clear old errors
173  }
174 
175  //- Exchange stream content and parameter contents, reset positions
176  void swap(List<char>& other)
177  {
178  buffer_type::swap(other);
179  stream_type::clear(); // Clear old errors
180  }
181 
182  //- Exchange stream content and parameter contents, reset positions
183  template<int SizeMin>
184  void swap(DynamicList<char,SizeMin>& other)
185  {
186  buffer_type::swap(other);
187  stream_type::clear(); // Clear old errors
188  }
190  //- Reset stream and return contents as a List
192  {
194  stream_type::clear(); // Clear old errors
195  return chars;
196  }
197 
198  //- Some information about the input buffer position/capacity
199  void debug_info(Ostream& os) const
200  {
201  os << "get=" << input_pos() << '/' << capacity();
202  }
203 };
204 
205 
206 /*---------------------------------------------------------------------------*\
207  Class ICharStream Declaration
208 \*---------------------------------------------------------------------------*/
209 
210 //- An ISstream with internal List storage. Always UNCOMPRESSED.
211 class ICharStream
212 :
213  public Foam::Detail::StreamAllocator<Foam::icharstream>,
214  public Foam::ISstream
215 {
216  typedef
218  allocator_type;
219 
220 public:
221 
222  // Constructors
223 
224  //- Default construct (empty), optionally with specified stream option
225  explicit ICharStream
226  (
228  )
229  :
230  allocator_type(),
231  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
232  {}
233 
234  //- Copy construct from string content
235  explicit ICharStream
236  (
237  const std::string& buffer,
238  IOstreamOption streamOpt = IOstreamOption()
239  )
240  :
241  ICharStream(streamOpt)
242  {
243  stream_.reset(buffer.data(), buffer.size());
244  }
245 
246  //- Move construct from List
247  explicit ICharStream
248  (
249  List<char>&& buffer,
250  IOstreamOption streamOpt = IOstreamOption()
251  )
252  :
253  ICharStream(streamOpt)
254  {
255  stream_.swap(buffer);
256  }
257 
258  //- Move construct from DynamicList (uses current size)
259  template<int SizeMin>
260  explicit ICharStream
261  (
262  DynamicList<char,SizeMin>&& buffer,
263  IOstreamOption streamOpt = IOstreamOption()
264  )
265  :
266  ICharStream(streamOpt)
267  {
268  stream_.swap(buffer);
269  }
271 
272  // Member Functions
273 
274  //- Position of the get buffer
275  std::streampos tellg() const { return stream_.input_pos(); }
276 
277  //- The current get position within the buffer (tellg)
278  std::streampos input_pos() const { return stream_.input_pos(); }
279 
280  //- The input list size. Same as capacity()
281  label size() const { return label(stream_.capacity()); }
282 
283  //- The get buffer capacity
284  std::streamsize capacity() const { return stream_.capacity(); }
285 
286  //- The number of characters remaining in the get area.
287  //- Same as (capacity() - input_pos())
288  std::streamsize remaining() const { return stream_.remaining(); }
289 
290  //- Span of the input characters (is modifiable!)
291  UList<char> list() const { return stream_.list(); }
292 
293  //- A string_view (c++17) or span view (older c++) of buffer contents
294  auto view() const -> decltype(stream_.view())
295  {
296  return stream_.view();
297  }
298 
299  //- For IStringStream compatibility, return the buffer as string copy.
300  // Use sparingly - it creates a full copy!!
301  auto str() const -> decltype(stream_.str())
302  {
303  return stream_.str();
304  }
305 
306  //- Reset content (copy)
307  void reset(const char* buffer, size_t nbytes)
308  {
309  stream_.reset(buffer, nbytes);
310  syncState();
311  }
312 
313  //- Exchange stream content and parameter contents, reset positions
314  void swap(List<char>& other)
315  {
316  stream_.swap(other);
317  syncState();
318  }
319 
320  //- Exchange stream content and parameter contents, reset positions
321  template<int SizeMin>
322  void swap(DynamicList<char,SizeMin>& other)
323  {
324  stream_.swap(other);
326  }
327 
328  //- Reset stream and return contents as a List
330  {
332  syncState();
333  return chars;
334  }
335 
336  //- Rewind the stream, clearing any old errors
337  virtual void rewind() override
338  {
339  stream_.rewind();
340  syncState();
341  }
342 
343  //- Print stream description to Ostream
344  virtual void print(Ostream& os) const override;
345 
347  // Member Operators
348 
349  //- A non-const reference to const Istream
350  // Needed for read-constructors where the stream argument is temporary
351  Istream& operator()() const
352  {
353  // Could also rewind
354  return const_cast<ICharStream&>(*this);
355  }
356 };
357 
358 
359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
360 
361 } // End namespace Foam
362 
363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 #endif
366 
367 // ************************************************************************* //
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()) ...
Definition: ICharStream.H:138
DynamicList< char > release()
Reset stream and return contents as a List.
Definition: ICharStream.H:217
label size() const
The input list size. Same as capacity()
Definition: ICharStream.H:325
DynamicList< char > release()
Reset buffer and return contents.
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:600
auto str() const -> decltype(stream_.str())
For IStringStream compatibility, return the buffer as string copy.
Definition: ICharStream.H:356
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ICharStream.H:341
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ICharStream.H:402
void rewind()
Rewind the stream, clearing any old errors.
Definition: ICharStream.H:180
std::streamsize span_capacity() const
The get buffer capacity.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void syncState()
Set stream state to match that of the std::istream.
Definition: ISstream.H:195
Similar to std::istringstream, but with the ability to swap character content. Has some similarity to...
Definition: ICharStream.H:62
A simple container for options an IOstream can normally have.
std::streamsize capacity() const
The get buffer capacity.
Definition: ICharStream.H:129
auto view() const -> decltype(stream_.view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: ICharStream.H:346
void debug_info(Ostream &os) const
Some information about the input buffer position/capacity.
Definition: ICharStream.H:227
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ICharStream.H:146
std::streampos tellg() const
Position of the get buffer.
Definition: ICharStream.H:315
void reset(const char *buffer, size_t nbytes)
Reset stream content (copy), reset positions.
Definition: ICharStream.H:189
void swap(List< char > &other)
Exchange buffer content and parameter contents, reset positions.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ICharStream.H:121
ICharStream IListStream
Definition: ICharStream.H:47
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
Foam::icharstream stream_
The std::stream.
Definition: IOstream.H:609
void reset(const char *buffer, size_t nbytes)
Reset content (copy)
Definition: ICharStream.H:364
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: ICharStream.H:198
An OSstream with internal List storage.
Definition: OCharStream.H:231
std::streamsize span_remaining() const
The number of characters remaining in the get area.
ICharStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty), optionally with specified stream option.
Definition: ICharStream.H:258
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
std::streamsize capacity() const
The get buffer capacity.
Definition: ICharStream.H:330
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
icharstream()
Default construct - empty.
Definition: ICharStream.H:78
OBJstream os(runTime.globalPath()/outputName)
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()) ...
Definition: ICharStream.H:336
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: ICharStream.H:373
void reset(const char *s, std::streamsize n)
Reset content (copy)
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
surface1 clear()
An ISstream with internal List storage. Always UNCOMPRESSED.
Definition: ICharStream.H:241
versionNumber version() const noexcept
Get the stream version.
Istream & operator()() const
A non-const reference to const Istream.
Definition: ICharStream.H:421
std::streamsize size_bytes() const
The span size (number of input characters)
stdFoam::span< const char > view() const
DynamicList< char > release()
Reset stream and return contents as a List.
Definition: ICharStream.H:392
An output streambuf for memory access.
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ICharStream.H:320
std::string str() const
For istringstream compatibility, return the buffer as string copy.
Definition: ICharStream.H:168
streamFormat format() const noexcept
Get the current stream format.
char * data_bytes() const
The span data (start of input characters)
auto view() const -> decltype(buffer_type::view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: ICharStream.H:158
std::streamsize span_tellg() const
The current buffer get position.
Namespace for OpenFOAM.
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SpanStreams.C:42