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 -------------------------------------------------------------------------------
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::ISpanStream
28 
29 Description
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 
57 See 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 
72 namespace Foam
73 {
74 
75 // Forward Declarations
76 class ispanstream;
77 class 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.
92 class ispanstream
93 :
94  virtual public std::ios,
95  protected Foam::memorybuf::in_base,
96  public std::istream
97 {
99  typedef std::istream stream_type;
100 
101 public:
102 
103  // Constructors
105  //- Default construct - empty
106  ispanstream()
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  //- Construct (shallow copy) from span character content
128  :
129  buffer_type(const_cast<char*>(s.data()), s.size()),
130  stream_type(static_cast<buffer_type*>(this))
131  {}
132 
133  //- Construct (shallow copy) from span character content
135  :
136  buffer_type(const_cast<char*>(s.data()), s.size()),
137  stream_type(static_cast<buffer_type*>(this))
138  {}
139 
141  // Member Functions
142 
143  //- The current get position within the buffer (tellg)
144  std::streampos input_pos() const
145  {
146  return buffer_type::span_tellg();
147  }
148 
149  //- The get buffer capacity
150  std::streamsize capacity() const
151  {
153  }
154 
155  //- The number of characters remaining in the get area.
156  //- Same as (capacity() - input_pos())
157  std::streamsize remaining() const
158  {
160  }
161 
162  //- Span of the input characters (is modifiable!)
163  UList<char> list() const
164  {
165  return UList<char>
166  (
168  label(buffer_type::size_bytes())
169  );
170  }
171 
172  //- A string_view (c++17) or span view (older c++) of buffer contents
173  auto view() const -> decltype(buffer_type::view())
174  {
175  return buffer_type::view();
176  }
178  //- For istringstream compatibility, return the buffer as string copy.
179  // Use sparingly - it creates a full copy!!
180  std::string str() const
181  {
182  return std::string
183  (
186  );
187  }
188 
189  //- Rewind the stream, clearing any old errors
190  void rewind()
191  {
192  buffer_type::pubseekpos(0, std::ios_base::in);
193  stream_type::clear(); // Clear old errors
194  }
195 
196  //- Reset the get buffer area
197  void reset(const char* buffer, size_t nbytes)
198  {
199  buffer_type::resetg(const_cast<char*>(buffer), nbytes);
200  stream_type::clear(); // Clear old errors
201  }
202 
203  //- Reset the get buffer area to use the nul-terminated buffer
204  void reset(const char* s)
205  {
206  reset(s, (s ? std::char_traits<char>::length(s) : 0));
207  }
208 
209  //- Reset the get buffer area to use the data from a string
210  void reset(const std::string& s)
211  {
212  buffer_type::resetg(const_cast<char*>(s.data()), s.size());
213  stream_type::clear(); // Clear old errors
214  }
215 
216  //- Reset the get buffer area to use the data from a string_view
217  void reset(std::string_view s)
218  {
219  buffer_type::resetg(const_cast<char*>(s.data()), s.size());
220  stream_type::clear(); // Clear old errors
221  }
222 
223  //- Some information about the input buffer position/capacity
224  void debug_info(Ostream& os) const
225  {
226  os << "get=" << input_pos() << '/' << capacity();
227  }
228 };
230 
231 /*---------------------------------------------------------------------------*\
232  Class ISpanStream Declaration
233 \*---------------------------------------------------------------------------*/
234 
235 class ISpanStream
236 :
237  public Foam::Detail::StreamAllocator<Foam::ispanstream>,
238  public Foam::ISstream
239 {
240  typedef
242  allocator_type;
243 
244 public:
245 
246  // Constructors
247 
248  //- Default construct (empty), optionally with specified stream option
249  explicit ISpanStream
250  (
251  IOstreamOption streamOpt = IOstreamOption()
252  )
253  :
254  allocator_type(),
255  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
256  {}
257 
258  //- Construct using specified buffer and number of bytes
260  (
261  const char* buffer,
262  size_t nbytes,
263  IOstreamOption streamOpt = IOstreamOption()
264  )
265  :
266  ISpanStream(streamOpt)
267  {
268  reset(buffer, nbytes);
269  }
270 
271  //- Construct using specified nul-terminated buffer
272  explicit ISpanStream(const char* buffer)
273  :
275  (
276  buffer,
277  (buffer ? std::char_traits<char>::length(buffer) : 0)
278  )
279  {}
280 
281  //- Use data area from string content
282  explicit ISpanStream
283  (
284  const std::string& s,
285  IOstreamOption streamOpt = IOstreamOption()
286  )
287  :
288  ISpanStream(s.data(), s.size(), streamOpt)
289  {}
290 
291  //- Use data area from string_view content
292  explicit ISpanStream
293  (
294  std::string_view s,
296  )
297  :
298  ISpanStream(s.data(), s.size(), streamOpt)
299  {}
300 
301  //- Construct using data area from a List and its inherent storage size
302  // Uses addressed size, thus no special treatment for a DynamicList
303  explicit ISpanStream
304  (
305  const ::Foam::UList<char>& buffer,
306  IOstreamOption streamOpt = IOstreamOption()
307  )
308  :
309  ISpanStream(buffer.cdata(), buffer.size(), streamOpt)
310  {}
311 
312  //- Construct (shallow copy) from span character content
313  explicit ISpanStream
314  (
316  IOstreamOption streamOpt = IOstreamOption()
317  )
318  :
319  ISpanStream(s.data(), s.size(), streamOpt)
320  {}
321 
322  //- Construct (shallow copy) from span character content
323  explicit ISpanStream
324  (
326  IOstreamOption streamOpt = IOstreamOption()
327  )
328  :
329  ISpanStream(s.data(), s.size(), streamOpt)
330  {}
331 
332 
333  // Member Functions
335  //- Position of the get buffer
336  std::streampos tellg() const { return stream_.input_pos(); }
337 
338  //- The current get position within the buffer (tellg)
339  std::streampos input_pos() const { return stream_.input_pos(); }
340 
341  //- The input list size. Same as capacity()
342  label size() const { return label(stream_.capacity()); }
343 
344  //- The get buffer capacity
345  std::streamsize capacity() const { return stream_.capacity(); }
346 
347  //- The number of characters remaining in the get area.
348  //- Same as (capacity() - input_pos())
349  std::streamsize remaining() const { return stream_.remaining(); }
350 
351  //- Span of the current input characters (is modifiable!)
352  UList<char> list() const { return stream_.list(); }
353 
354  //- A string_view (c++17) or span view (older c++) of buffer contents
355  auto view() const -> decltype(stream_.view())
356  {
357  return stream_.view();
358  }
359 
360  //- For IStringStream compatibility, return the buffer as string copy.
361  // Use sparingly - it creates a full copy!!
362  auto str() const -> decltype(stream_.str())
363  {
364  return stream_.str();
365  }
366 
367  //- Reset input area, position to buffer start and clear errors
368  void reset(const char* buffer, size_t nbytes)
369  {
370  stream_.reset(buffer, nbytes);
371  syncState();
372  }
373 
374  //- Reset input area to use data from a string
375  void reset(const std::string& s)
376  {
377  stream_.reset(s.data(), s.size());
378  syncState();
379  }
380 
381  //- Reset input area to use data from a std::string_view
382  void reset(std::string_view s)
383  {
384  stream_.reset(s.data(), s.size());
385  syncState();
386  }
387 
388  //- Reset input area to use data from span character content
390  {
391  stream_.reset(s.data(), s.size());
392  syncState();
393  }
394 
395  //- Reset input area to use data from span character content
397  {
398  stream_.reset(s.data(), s.size());
399  syncState();
400  }
402  //- Rewind the stream, clearing any old errors
403  virtual void rewind() override
404  {
405  stream_.rewind();
406  syncState();
407  }
408 
409  //- Print stream description to Ostream
410  virtual void print(Ostream& os) const override;
411 
413  // Member Operators
414 
415  //- A non-const reference to const Istream
416  // Needed for read-constructors where the stream argument is temporary
418  {
419  // Could also rewind
420  return const_cast<ISpanStream&>(*this);
421  }
422 };
423 
424 
425 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
426 
427 } // End namespace Foam
428 
429 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
430 
431 #endif
432 
433 // ************************************************************************* //
auto str() const -> decltype(stream_.str())
For IStringStream compatibility, return the buffer as string copy.
Definition: ISpanStream.H:427
ispanstream uiliststream
Definition: ISpanStream.H:70
void resetg(char *s, std::streamsize n)
Reset get buffer with character data (can be nullptr) and count.
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:600
Istream & operator()() const
A non-const reference to const Istream.
Definition: ISpanStream.H:499
Similar to std::istringstream, but with an externally managed input buffer which makes it most simila...
Definition: ISpanStream.H:88
void reset(const char *buffer, size_t nbytes)
Reset input area, position to buffer start and clear errors.
Definition: ISpanStream.H:435
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
ispanstream(const char *buffer, size_t nbytes)
Construct (shallow copy) for character array and number of bytes.
Definition: ISpanStream.H:113
std::streamsize capacity() const
The get buffer capacity.
Definition: ISpanStream.H:401
A simple container for options an IOstream can normally have.
std::string str() const
For istringstream compatibility, return the buffer as string copy.
Definition: ISpanStream.H:199
void rewind()
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:211
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ISpanStream.H:152
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()) ...
Definition: ISpanStream.H:169
std::streamsize capacity() const
The get buffer capacity.
Definition: ISpanStream.H:160
Foam::ispanstream stream_
The std::stream.
Definition: IOstream.H:609
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()) ...
Definition: ISpanStream.H:407
The base input streambuf with memory access.
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ISpanStream.H:177
std::streamsize span_remaining() const
The number of characters remaining in the get area.
std::string_view view() const
void reset(const char *buffer, size_t nbytes)
Reset the get buffer area.
Definition: ISpanStream.H:220
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:480
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
void debug_info(Ostream &os) const
Some information about the input buffer position/capacity.
Definition: ISpanStream.H:255
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
std::streampos tellg() const
Position of the get buffer.
Definition: ISpanStream.H:386
UList< char > list() const
Span of the current input characters (is modifiable!)
Definition: ISpanStream.H:412
ISpanStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty), optionally with specified stream option.
Definition: ISpanStream.H:283
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
surface1 clear()
auto view() const -> decltype(buffer_type::view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: ISpanStream.H:189
versionNumber version() const noexcept
Get the stream version.
Rudimentary functionality similar to std::span for holding memory view.
Definition: stdFoam.H:365
std::streamsize size_bytes() const
The span size (number of input characters)
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ISpanStream.H:391
ISpanStream UIListStream
Definition: ISpanStream.H:74
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 IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: ISpanStream.H:266
streamFormat format() const noexcept
Get the current stream format.
char * data_bytes() const
The span data (start of input characters)
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SpanStreams.C:26
auto view() const -> decltype(stream_.view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: ISpanStream.H:417
ispanstream()
Default construct - empty.
Definition: ISpanStream.H:104
std::streamsize span_tellg() const
The current buffer get position.
Namespace for OpenFOAM.
label size() const
The input list size. Same as capacity()
Definition: ISpanStream.H:396