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-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::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 #include "List.H"
46 #include "DynamicList.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 class icharstream;
55 class ICharStream;
56 class OCharStream;
57 
58 // Older names (prior to 2023-08)
59 typedef ICharStream IListStream;
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class icharstream Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 //- Similar to std::istringstream, but with the ability to swap
67 //- character content.
68 //- Has some similarity to std::ispanstream (C++23)
69 class icharstream
70 :
71  virtual public std::ios,
73  public std::istream
74 {
76  typedef std::istream stream_type;
77 
78 public:
79 
80  // Constructors
81 
82  //- Default construct - empty
83  icharstream()
84  :
85  buffer_type(),
86  stream_type(static_cast<buffer_type*>(this))
87  {}
88 
89  //- Copy construct from content
90  icharstream(const char* buffer, size_t nbytes)
91  :
92  icharstream()
93  {
94  reset(buffer, nbytes);
95  }
96 
97  //- Move construct from List
98  icharstream(List<char>&& buffer)
99  :
100  icharstream()
101  {
102  swap(buffer);
103  }
104 
105  //- Move construct from DynamicList
106  template<int SizeMin>
108  :
109  icharstream()
110  {
111  swap(buffer);
112  }
113 
114 
115  // Member Functions
116 
117  //- The current get position within the buffer (tellg)
118  std::streampos input_pos() const
119  {
120  return buffer_type::span_tellg();
121  }
122 
123  //- The get buffer capacity
124  std::streamsize capacity() const
125  {
127  }
128 
129  //- The number of characters remaining in the get area.
130  //- Same as (capacity() - input_pos())
131  std::streamsize remaining() const
132  {
134  }
135 
136  //- Span of the input characters (is modifiable!)
137  UList<char> list() const
138  {
139  return UList<char>
140  (
142  label(buffer_type::size_bytes())
143  );
144  }
145 
146  //- A string_view (c++17) or span view (older c++) of buffer contents
147  auto view() const -> decltype(buffer_type::view())
148  {
149  return buffer_type::view();
150  }
151 
152  //- For istringstream compatibility, return the buffer as string copy.
153  // Use sparingly - it creates a full copy!!
154  std::string str() const
155  {
156  return std::string
157  (
160  );
161  }
162 
163  //- Rewind the stream, clearing any old errors
164  void rewind()
165  {
166  buffer_type::pubseekpos(0, std::ios_base::in);
167  stream_type::clear(); // Clear old errors
168  }
169 
170  //- Reset stream content (copy), reset positions
171  void reset(const char* buffer, size_t nbytes)
172  {
173  buffer_type::reset(buffer, nbytes);
174  stream_type::clear(); // Clear old errors
175  }
176 
177  //- Exchange stream content and parameter contents, reset positions
178  void swap(List<char>& other)
179  {
180  buffer_type::swap(other);
181  stream_type::clear(); // Clear old errors
182  }
183 
184  //- Exchange stream content and parameter contents, reset positions
185  template<int SizeMin>
186  void swap(DynamicList<char,SizeMin>& other)
187  {
188  buffer_type::swap(other);
189  stream_type::clear(); // Clear old errors
190  }
192  //- Reset stream and return contents as a List
194  {
196  stream_type::clear(); // Clear old errors
197  return chars;
198  }
199 
200  //- Some information about the input buffer position/capacity
201  void debug_info(Ostream& os) const
202  {
203  os << "get=" << input_pos() << '/' << capacity();
204  }
205 };
206 
207 
208 namespace Detail
209 {
211 /*---------------------------------------------------------------------------*\
212  Class Detail::ICharStreamAllocator Declaration
213 \*---------------------------------------------------------------------------*/
214 
215 //- An allocator for holding Foam::icharstream
216 class ICharStreamAllocator
217 {
218 protected:
220  // Protected Data
221 
222  //- The stream
224 
225 
226  // Constructors
227 
228  //- Default construct
229  ICharStreamAllocator() = default;
230 };
231 
232 } // End namespace Detail
233 
234 
235 /*---------------------------------------------------------------------------*\
236  Class ICharStream Declaration
237 \*---------------------------------------------------------------------------*/
238 
239 //- An ISstream with internal List storage. Always UNCOMPRESSED.
240 class ICharStream
241 :
242  public Detail::ICharStreamAllocator,
243  public Foam::ISstream
244 {
245  typedef Detail::ICharStreamAllocator allocator_type;
247 public:
248 
249  // Constructors
250 
251  //- Default construct (empty), optionally with specified stream option
252  explicit ICharStream
253  (
254  IOstreamOption streamOpt = IOstreamOption()
255  )
256  :
257  allocator_type(),
258  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
259  {}
260 
261  //- Copy construct from string content
262  explicit ICharStream
263  (
264  const std::string& buffer,
265  IOstreamOption streamOpt = IOstreamOption()
266  )
267  :
268  ICharStream(streamOpt)
269  {
270  stream_.reset(buffer.data(), buffer.size());
271  }
272 
273  //- Move construct from List
274  explicit ICharStream
275  (
276  List<char>&& buffer,
277  IOstreamOption streamOpt = IOstreamOption()
278  )
279  :
280  ICharStream(streamOpt)
281  {
282  stream_.swap(buffer);
283  }
284 
285  //- Move construct from DynamicList (uses current size)
286  template<int SizeMin>
287  explicit ICharStream
288  (
289  DynamicList<char,SizeMin>&& buffer,
290  IOstreamOption streamOpt = IOstreamOption()
291  )
292  :
293  ICharStream(streamOpt)
294  {
295  stream_.swap(buffer);
296  }
297 
298 
299  // Member Functions
300 
301  //- Position of the get buffer
302  std::streampos tellg() const { return stream_.input_pos(); }
304  //- The current get position within the buffer (tellg)
305  std::streampos input_pos() const { return stream_.input_pos(); }
306 
307  //- The input list size. Same as capacity()
308  label size() const { return label(stream_.capacity()); }
309 
310  //- The get buffer capacity
311  std::streamsize capacity() const { return stream_.capacity(); }
312 
313  //- The number of characters remaining in the get area.
314  //- Same as (capacity() - input_pos())
315  std::streamsize remaining() const { return stream_.remaining(); }
316 
317  //- Span of the input characters (is modifiable!)
318  UList<char> list() const { return stream_.list(); }
319 
320  //- A string_view (c++17) or span view (older c++) of buffer contents
321  auto view() const -> decltype(stream_.view())
322  {
323  return stream_.view();
324  }
325 
326  //- For IStringStream compatibility, return the buffer as string copy.
327  // Use sparingly - it creates a full copy!!
328  auto str() const -> decltype(stream_.str())
329  {
330  return stream_.str();
331  }
333  //- Reset content (copy)
334  void reset(const char* buffer, size_t nbytes)
335  {
336  stream_.reset(buffer, nbytes);
337  syncState();
338  }
339 
340  //- Exchange stream content and parameter contents, reset positions
341  void swap(List<char>& other)
342  {
343  stream_.swap(other);
344  syncState();
345  }
346 
347  //- Exchange stream content and parameter contents, reset positions
348  template<int SizeMin>
349  void swap(DynamicList<char,SizeMin>& other)
350  {
351  stream_.swap(other);
352  syncState();
353  }
354 
355  //- Reset stream and return contents as a List
357  {
359  syncState();
360  return chars;
361  }
362 
363  //- Rewind the stream, clearing any old errors
364  virtual void rewind() override
365  {
366  stream_.rewind();
367  syncState();
368  }
370  //- Print stream description to Ostream
371  virtual void print(Ostream& os) const override;
372 
373 
374  // Member Operators
375 
376  //- A non-const reference to const Istream
377  // Needed for read-constructors where the stream argument is temporary
378  Istream& operator()() const
379  {
380  // Could also rewind
381  return const_cast<ICharStream&>(*this);
382  }
383 };
384 
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 } // End namespace Foam
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391 
392 #endif
393 
394 // ************************************************************************* //
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()) ...
Definition: ICharStream.H:140
DynamicList< char > release()
Reset stream and return contents as a List.
Definition: ICharStream.H:219
label size() const
The input list size. Same as capacity()
Definition: ICharStream.H:358
DynamicList< char > release()
Reset buffer and return contents.
auto str() const -> decltype(stream_.str())
For IStringStream compatibility, return the buffer as string copy.
Definition: ICharStream.H:389
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ICharStream.H:374
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ICharStream.H:435
void rewind()
Rewind the stream, clearing any old errors.
Definition: ICharStream.H:182
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:183
Similar to std::istringstream, but with the ability to swap character content. Has some similarity to...
Definition: ICharStream.H:64
A simple container for options an IOstream can normally have.
std::streamsize capacity() const
The get buffer capacity.
Definition: ICharStream.H:131
auto view() const -> decltype(stream_.view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: ICharStream.H:379
Foam::icharstream stream_
The stream.
Definition: ICharStream.H:255
void debug_info(Ostream &os) const
Some information about the input buffer position/capacity.
Definition: ICharStream.H:229
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ICharStream.H:148
std::streampos tellg() const
Position of the get buffer.
Definition: ICharStream.H:348
void reset(const char *buffer, size_t nbytes)
Reset stream content (copy), reset positions.
Definition: ICharStream.H:191
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:123
ICharStream IListStream
Definition: ICharStream.H:49
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
void reset(const char *buffer, size_t nbytes)
Reset content (copy)
Definition: ICharStream.H:397
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: ICharStream.H:200
An OSstream with internal List storage.
Definition: OCharStream.H:256
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:291
patchWriters clear()
ICharStreamAllocator()=default
Default construct.
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:363
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:80
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:369
void swap(List< char > &other)
Exchange stream content and parameter contents, reset positions.
Definition: ICharStream.H:406
void reset(const char *s, std::streamsize n)
Reset content (copy)
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
An ISstream with internal List storage. Always UNCOMPRESSED.
Definition: ICharStream.H:276
versionNumber version() const noexcept
Get the stream version.
Istream & operator()() const
A non-const reference to const Istream.
Definition: ICharStream.H:454
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:425
An output streambuf for memory access.
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ICharStream.H:353
std::string str() const
For istringstream compatibility, return the buffer as string copy.
Definition: ICharStream.H:170
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:160
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