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-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::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  #if __cplusplus >= 201703L
120  //- Construct (shallow copy) from std::string_view content
121  explicit ispanstream(std::string_view s)
122  {
123  buffer_type(const_cast<char*>(s.data()), s.size()),
124  stream_type(static_cast<buffer_type*>(this));
125  }
126  #endif
127 
128  //- Construct (shallow copy) from span character content
130  :
131  buffer_type(const_cast<char*>(s.data()), s.size()),
132  stream_type(static_cast<buffer_type*>(this))
133  {}
134 
135  //- Construct (shallow copy) from span character content
137  :
138  buffer_type(const_cast<char*>(s.data()), s.size()),
139  stream_type(static_cast<buffer_type*>(this))
140  {}
141 
143  // Member Functions
144 
145  //- The current get position within the buffer (tellg)
146  std::streampos input_pos() const
147  {
148  return buffer_type::span_tellg();
149  }
150 
151  //- The get buffer capacity
152  std::streamsize capacity() const
153  {
155  }
156 
157  //- The number of characters remaining in the get area.
158  //- Same as (capacity() - input_pos())
159  std::streamsize remaining() const
160  {
162  }
163 
164  //- Span of the input characters (is modifiable!)
165  UList<char> list() const
166  {
167  return UList<char>
168  (
170  label(buffer_type::size_bytes())
171  );
172  }
173 
174  //- A string_view (c++17) or span view (older c++) of buffer contents
175  auto view() const -> decltype(buffer_type::view())
176  {
177  return buffer_type::view();
178  }
180  //- For istringstream compatibility, return the buffer as string copy.
181  // Use sparingly - it creates a full copy!!
182  std::string str() const
183  {
184  return std::string
185  (
188  );
189  }
190 
191  //- Rewind the stream, clearing any old errors
192  void rewind()
193  {
194  buffer_type::pubseekpos(0, std::ios_base::in);
195  stream_type::clear(); // Clear old errors
196  }
197 
198  //- Reset the get buffer area
199  void reset(const char* buffer, size_t nbytes)
200  {
201  buffer_type::resetg(const_cast<char*>(buffer), nbytes);
202  stream_type::clear(); // Clear old errors
203  }
204 
205  //- Reset the get buffer area to use the data from a string
206  void reset(const std::string& s)
207  {
208  buffer_type::resetg(const_cast<char*>(&s[0]), s.size());
209  stream_type::clear(); // Clear old errors
210  }
211 
212  //- Some information about the input buffer position/capacity
213  void debug_info(Ostream& os) const
214  {
215  os << "get=" << input_pos() << '/' << capacity();
216  }
217 };
218 
219 
220 /*---------------------------------------------------------------------------*\
221  Class ISpanStream Declaration
222 \*---------------------------------------------------------------------------*/
223 
224 class ISpanStream
225 :
226  public Foam::Detail::StreamAllocator<Foam::ispanstream>,
227  public Foam::ISstream
228 {
229  typedef
231  allocator_type;
232 
233 public:
234 
235  // Constructors
236 
237  //- Default construct (empty), optionally with specified stream option
238  explicit ISpanStream
239  (
241  )
242  :
243  allocator_type(),
244  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
245  {}
246 
247  //- Construct using specified buffer and number of bytes
249  (
250  const char* buffer,
251  size_t nbytes,
252  IOstreamOption streamOpt = IOstreamOption()
253  )
254  :
255  ISpanStream(streamOpt)
256  {
257  reset(buffer, nbytes);
258  }
259 
260  //- Use data area from string content
261  explicit ISpanStream
262  (
263  const std::string& buffer,
264  IOstreamOption streamOpt = IOstreamOption()
265  )
266  :
267  ISpanStream(streamOpt)
268  {
269  reset(buffer);
270  }
271 
272  //- Construct using data area from a List and its inherent storage size
273  // Uses addressed size, thus no special treatment for a DynamicList
274  explicit ISpanStream
275  (
276  const ::Foam::UList<char>& buffer,
277  IOstreamOption streamOpt = IOstreamOption()
278  )
279  :
280  ISpanStream(buffer.cdata(), buffer.size(), streamOpt)
281  {}
282 
283  #if __cplusplus >= 201703L
284  //- Construct (shallow copy) from std::string_view content
285  explicit ISpanStream
286  (
287  std::string_view s,
288  IOstreamOption streamOpt = IOstreamOption()
289  )
290  :
291  ISpanStream(s.data(), s.size(), streamOpt)
292  {}
293  #endif
294 
295  //- Construct (shallow copy) from span character content
296  explicit ISpanStream
297  (
299  IOstreamOption streamOpt = IOstreamOption()
300  )
301  :
302  ISpanStream(s.data(), s.size(), streamOpt)
303  {}
304 
305  //- Construct (shallow copy) from span character content
306  explicit ISpanStream
307  (
309  IOstreamOption streamOpt = IOstreamOption()
310  )
311  :
312  ISpanStream(s.data(), s.size(), streamOpt)
313  {}
314 
315 
316  // Member Functions
317 
318  //- Position of the get buffer
319  std::streampos tellg() const { return stream_.input_pos(); }
320 
321  //- The current get position within the buffer (tellg)
322  std::streampos input_pos() const { return stream_.input_pos(); }
323 
324  //- The input list size. Same as capacity()
325  label size() const { return label(stream_.capacity()); }
326 
327  //- The get buffer capacity
328  std::streamsize capacity() const { return stream_.capacity(); }
329 
330  //- The number of characters remaining in the get area.
331  //- Same as (capacity() - input_pos())
332  std::streamsize remaining() const { return stream_.remaining(); }
333 
334  //- Span of the current input characters (is modifiable!)
335  UList<char> list() const { return stream_.list(); }
336 
337  //- A string_view (c++17) or span view (older c++) of buffer contents
338  auto view() const -> decltype(stream_.view())
339  {
340  return stream_.view();
341  }
342 
343  //- For IStringStream compatibility, return the buffer as string copy.
344  // Use sparingly - it creates a full copy!!
345  auto str() const -> decltype(stream_.str())
346  {
347  return stream_.str();
348  }
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);
361  syncState();
362  }
364  #if __cplusplus >= 201703L
365  //- Reset input area to use data from a std::string_view
366  void reset(std::string_view s)
367  {
368  stream_.reset(s.data(), s.size());
369  syncState();
370  }
371  #endif
372 
373  //- Reset input area to use data from span character content
375  {
376  stream_.reset(s.data(), s.size());
377  syncState();
378  }
379 
380  //- Reset input area to use data from span character content
382  {
383  stream_.reset(s.data(), s.size());
385  }
386 
387  //- Rewind the stream, clearing any old errors
388  virtual void rewind() override
389  {
390  stream_.rewind();
391  syncState();
392  }
393 
394  //- Print stream description to Ostream
395  virtual void print(Ostream& os) const override;
396 
397 
398  // Member Operators
399 
400  //- A non-const reference to const Istream
401  // Needed for read-constructors where the stream argument is temporary
402  Istream& operator()() const
403  {
404  // Could also rewind
405  return const_cast<ISpanStream&>(*this);
406  }
407 };
408 
409 
410 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411 
412 } // End namespace Foam
413 
414 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
415 
416 #endif
417 
418 // ************************************************************************* //
auto str() const -> decltype(stream_.str())
For IStringStream compatibility, return the buffer as string copy.
Definition: ISpanStream.H:404
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:478
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:412
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:378
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:201
void rewind()
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:213
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ISpanStream.H:154
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:171
std::streamsize capacity() const
The get buffer capacity.
Definition: ISpanStream.H:162
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:384
The base input streambuf with memory access.
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ISpanStream.H:179
std::streamsize span_remaining() const
The number of characters remaining in the get area.
void reset(const char *buffer, size_t nbytes)
Reset the get buffer area.
Definition: ISpanStream.H:222
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:459
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:240
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:363
UList< char > list() const
Span of the current input characters (is modifiable!)
Definition: ISpanStream.H:389
ISpanStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty), optionally with specified stream option.
Definition: ISpanStream.H:268
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:191
versionNumber version() const noexcept
Get the stream version.
Rudimentary functionality similar to std::span for holding memory view.
Definition: stdFoam.H:500
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:368
stdFoam::span< const char > view() const
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:251
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:394
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:373