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-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::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 "UList.H"
69 #include "ISstream.H"
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 namespace Foam
74 {
75 
76 // Forward Declarations
77 class ispanstream;
78 class ISpanStream;
79 
80 // Older names (prior to 2023-08)
83 
84 
85 /*---------------------------------------------------------------------------*\
86  Class ispanstream Declaration
87 \*---------------------------------------------------------------------------*/
88 
89 //- Similar to std::istringstream, but with an externally managed input buffer
90 //- which makes it most similar to std::ispanstream (C++23)
91 // This allows the input buffer to be filled or refilled from various sources
92 // without copying.
93 class ispanstream
94 :
95  virtual public std::ios,
96  protected Foam::memorybuf::in_base,
97  public std::istream
98 {
100  typedef std::istream stream_type;
101 
102 public:
103 
104  // Constructors
106  //- Default construct - empty
107  ispanstream()
108  :
109  buffer_type(),
110  stream_type(static_cast<buffer_type*>(this))
111  {}
112 
113  //- Construct (shallow copy) for character array and number of bytes
114  ispanstream(const char* buffer, size_t nbytes)
115  :
116  buffer_type(const_cast<char*>(buffer), nbytes),
117  stream_type(static_cast<buffer_type*>(this))
118  {}
119 
120  #if __cplusplus >= 201703L
121  //- Construct (shallow copy) from std::string_view content
122  explicit ispanstream(std::string_view s)
123  {
124  buffer_type(const_cast<char*>(s.data()), s.size()),
125  stream_type(static_cast<buffer_type*>(this));
126  }
127  #endif
128 
129  //- Construct (shallow copy) from span character content
131  :
132  buffer_type(const_cast<char*>(s.data()), s.size()),
133  stream_type(static_cast<buffer_type*>(this))
134  {}
135 
136  //- Construct (shallow copy) from span character content
138  :
139  buffer_type(const_cast<char*>(s.data()), s.size()),
140  stream_type(static_cast<buffer_type*>(this))
141  {}
142 
144  // Member Functions
145 
146  //- The current get position within the buffer (tellg)
147  std::streampos input_pos() const
148  {
149  return buffer_type::span_tellg();
150  }
151 
152  //- The get buffer capacity
153  std::streamsize capacity() const
154  {
156  }
157 
158  //- The number of characters remaining in the get area.
159  //- Same as (capacity() - input_pos())
160  std::streamsize remaining() const
161  {
163  }
164 
165  //- Span of the input characters (is modifiable!)
166  UList<char> list() const
167  {
168  return UList<char>
169  (
171  label(buffer_type::size_bytes())
172  );
173  }
174 
175  //- A string_view (c++17) or span view (older c++) of buffer contents
176  auto view() const -> decltype(buffer_type::view())
177  {
178  return buffer_type::view();
179  }
181  //- For istringstream compatibility, return the buffer as string copy.
182  // Use sparingly - it creates a full copy!!
183  std::string str() const
184  {
185  return std::string
186  (
189  );
190  }
191 
192  //- Rewind the stream, clearing any old errors
193  void rewind()
194  {
195  buffer_type::pubseekpos(0, std::ios_base::in);
196  stream_type::clear(); // Clear old errors
197  }
198 
199  //- Reset the get buffer area
200  void reset(const char* buffer, size_t nbytes)
201  {
202  buffer_type::resetg(const_cast<char*>(buffer), nbytes);
203  stream_type::clear(); // Clear old errors
204  }
205 
206  //- Reset the get buffer area to use the data from a string
207  void reset(const std::string& s)
208  {
209  buffer_type::resetg(const_cast<char*>(&s[0]), s.size());
210  stream_type::clear(); // Clear old errors
211  }
212 
213  //- Some information about the input buffer position/capacity
214  void debug_info(Ostream& os) const
215  {
216  os << "get="
217  << input_pos() << '/' << capacity();
218  }
219 };
220 
221 
222 namespace Detail
223 {
224 
225 /*---------------------------------------------------------------------------*\
226  Class Detail::ISpanStreamAllocator Declaration
227 \*---------------------------------------------------------------------------*/
228 
229 //- An allocator for holding Foam::ispanstream
230 class ISpanStreamAllocator
231 {
232 protected:
233 
234  // Protected Data
235 
236  //- The stream
238 
239 
240  // Constructors
242  //- Default construct (empty)
243  ISpanStreamAllocator() = default;
244 };
245 
246 } // End namespace Detail
247 
248 
249 /*---------------------------------------------------------------------------*\
250  Class ISpanStream Declaration
251 \*---------------------------------------------------------------------------*/
252 
253 class ISpanStream
254 :
255  public Detail::ISpanStreamAllocator,
256  public Foam::ISstream
257 {
258  typedef Detail::ISpanStreamAllocator allocator_type;
260 public:
261 
262  // Constructors
263 
264  //- Default construct (empty), optionally with specified stream option
265  explicit ISpanStream
266  (
267  IOstreamOption streamOpt = IOstreamOption()
268  )
269  :
270  allocator_type(),
271  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
272  {}
273 
274  //- Construct using specified buffer and number of bytes
276  (
277  const char* buffer,
278  size_t nbytes,
279  IOstreamOption streamOpt = IOstreamOption()
280  )
281  :
282  ISpanStream(streamOpt)
283  {
284  reset(buffer, nbytes);
285  }
287  //- Use data area from string content
288  explicit ISpanStream
289  (
290  const std::string& buffer,
291  IOstreamOption streamOpt = IOstreamOption()
292  )
293  :
294  ISpanStream(streamOpt)
295  {
296  reset(buffer);
297  }
298 
299  //- Construct using data area from a List and its inherent storage size
300  // Uses addressed size, thus no special treatment for a DynamicList
301  explicit ISpanStream
302  (
303  const ::Foam::UList<char>& buffer,
304  IOstreamOption streamOpt = IOstreamOption()
305  )
306  :
307  ISpanStream(buffer.cdata(), buffer.size(), streamOpt)
308  {}
309 
310  #if __cplusplus >= 201703L
311  //- Construct (shallow copy) from std::string_view content
312  explicit ISpanStream
313  (
314  std::string_view s,
315  IOstreamOption streamOpt = IOstreamOption()
316  )
317  :
318  ISpanStream(s.data(), s.size(), streamOpt)
319  {}
320  #endif
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  //- Construct (shallow copy) from span character content
333  explicit ISpanStream
334  (
336  IOstreamOption streamOpt = IOstreamOption()
337  )
338  :
339  ISpanStream(s.data(), s.size(), streamOpt)
340  {}
341 
342 
343  // Member Functions
345  //- Position of the get buffer
346  std::streampos tellg() const { return stream_.input_pos(); }
347 
348  //- The current get position within the buffer (tellg)
349  std::streampos input_pos() const { return stream_.input_pos(); }
350 
351  //- The input list size. Same as capacity()
352  label size() const { return label(stream_.capacity()); }
353 
354  //- The get buffer capacity
355  std::streamsize capacity() const { return stream_.capacity(); }
356 
357  //- The number of characters remaining in the get area.
358  //- Same as (capacity() - input_pos())
359  std::streamsize remaining() const { return stream_.remaining(); }
360 
361  //- Span of the current input characters (is modifiable!)
362  UList<char> list() const { return stream_.list(); }
363 
364  //- A string_view (c++17) or span view (older c++) of buffer contents
365  auto view() const -> decltype(stream_.view())
366  {
367  return stream_.view();
368  }
369 
370  //- For IStringStream compatibility, return the buffer as string copy.
371  // Use sparingly - it creates a full copy!!
372  auto str() const -> decltype(stream_.str())
373  {
374  return stream_.str();
375  }
376 
377  //- Reset input area, position to buffer start and clear errors
378  void reset(const char* buffer, size_t nbytes)
379  {
380  stream_.reset(buffer, nbytes);
381  syncState();
382  }
383 
384  //- Reset input area to use data from a string
385  void reset(const std::string& s)
386  {
387  stream_.reset(s);
388  syncState();
389  }
390 
391  #if __cplusplus >= 201703L
392  //- Reset input area to use data from a std::string_view
393  void reset(std::string_view s)
394  {
395  stream_.reset(s.data(), s.size());
397  }
398  #endif
399 
400  //- Reset input area to use data from span character content
402  {
403  stream_.reset(s.data(), s.size());
404  syncState();
405  }
407  //- Reset input area to use data from span character content
409  {
410  stream_.reset(s.data(), s.size());
412  }
413 
414  //- Rewind the stream, clearing any old errors
415  virtual void rewind() override
416  {
418  syncState();
419  }
420 
421  //- Print stream description to Ostream
422  virtual void print(Ostream& os) const override;
423 
424 
425  // Member Operators
426 
427  //- A non-const reference to const Istream
428  // Needed for read-constructors where the stream argument is temporary
429  Istream& operator()() const
430  {
431  // Could also rewind
432  return const_cast<ISpanStream&>(*this);
433  }
434 };
435 
436 
437 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
438 
439 } // End namespace Foam
440 
441 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
442 
443 #endif
444 
445 // ************************************************************************* //
auto str() const -> decltype(stream_.str())
For IStringStream compatibility, return the buffer as string copy.
Definition: ISpanStream.H:437
ispanstream uiliststream
Definition: ISpanStream.H:71
void resetg(char *s, std::streamsize n)
Reset get buffer with character data (can be nullptr) and count.
Istream & operator()() const
A non-const reference to const Istream.
Definition: ISpanStream.H:511
Similar to std::istringstream, but with an externally managed input buffer which makes it most simila...
Definition: ISpanStream.H:89
void reset(const char *buffer, size_t nbytes)
Reset input area, position to buffer start and clear errors.
Definition: ISpanStream.H:445
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
ISpanStreamAllocator()=default
Default construct (empty)
void syncState()
Set stream state to match that of the std::istream.
Definition: ISstream.H:183
ispanstream(const char *buffer, size_t nbytes)
Construct (shallow copy) for character array and number of bytes.
Definition: ISpanStream.H:114
std::streamsize capacity() const
The get buffer capacity.
Definition: ISpanStream.H:411
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:202
void rewind()
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:214
std::streampos input_pos() const
The current get position within the buffer (tellg)
Definition: ISpanStream.H:155
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:172
std::streamsize capacity() const
The get buffer capacity.
Definition: ISpanStream.H:163
std::streamsize remaining() const
The number of characters remaining in the get area. Same as (capacity() - input_pos()) ...
Definition: ISpanStream.H:417
The base input streambuf with memory access.
UList< char > list() const
Span of the input characters (is modifiable!)
Definition: ISpanStream.H:180
std::streamsize span_remaining() const
The number of characters remaining in the get area.
patchWriters clear()
void reset(const char *buffer, size_t nbytes)
Reset the get buffer area.
Definition: ISpanStream.H:223
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:492
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:241
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:396
UList< char > list() const
Span of the current input characters (is modifiable!)
Definition: ISpanStream.H:422
ISpanStream(IOstreamOption streamOpt=IOstreamOption())
Default construct (empty), optionally with specified stream option.
Definition: ISpanStream.H:301
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
auto view() const -> decltype(buffer_type::view())
A string_view (c++17) or span view (older c++) of buffer contents.
Definition: ISpanStream.H:192
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:401
stdFoam::span< const char > view() const
ISpanStream UIListStream
Definition: ISpanStream.H:75
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))
Foam::ispanstream stream_
The stream.
Definition: ISpanStream.H:268
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: ISpanStream.H:286
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:427
ispanstream()
Default construct - empty.
Definition: ISpanStream.H:105
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:406