UIListStream.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-2021 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::UIListStream
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  UIListStream 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(UIListStream(buffer)());
55  \endcode
56 
57 See Also
58  Foam::IListStream
59  Foam::OListStream
60  Foam::UOListStream
61 
62 \*---------------------------------------------------------------------------*/
63 
64 #ifndef UIListStream_H
65 #define UIListStream_H
66 
67 #include "UList.H"
68 #include "ISstream.H"
69 #include "memoryStreamBuffer.H"
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 namespace Foam
74 {
75 
76 /*---------------------------------------------------------------------------*\
77  Class uiliststream Declaration
78 \*---------------------------------------------------------------------------*/
79 
80 //- Similar to std::istringstream, but with an externally managed input buffer.
81 // This allows the input buffer to be filled or refilled from various sources
82 // without copying.
83 class uiliststream
84 :
85  virtual public std::ios,
86  protected memorybuf::in,
87  public std::istream
88 {
89 public:
90 
91  //- Construct for character array and number of bytes
92  uiliststream(const char* buffer, size_t nbytes)
93  :
94  memorybuf::in(const_cast<char*>(buffer), nbytes),
95  std::istream(static_cast<memorybuf::in*>(this))
96  {}
97 
98  //- Reset buffer pointers
99  inline void reset(char *buffer, size_t nbytes)
100  {
101  resetg(buffer, nbytes);
102  }
103 
104  //- Rewind the stream, clearing any old errors
105  void rewind()
106  {
107  this->pubseekpos(0, std::ios_base::in);
108  clear(); // for safety, clear any old errors
109  }
110 };
111 
112 
113 namespace Detail
114 {
115 
116 /*---------------------------------------------------------------------------*\
117  Class Detail::UIListStreamAllocator Declaration
118 \*---------------------------------------------------------------------------*/
119 
120 //- An stream/stream-buffer input allocator for a externally allocated list
122 {
123 protected:
124 
125  // Protected Data
126 
127  typedef std::istream stream_type;
128 
129  //- The stream buffer
131 
132  //- The stream
134 
135 
136  // Constructors
137 
138  //- Construct for character array and number of bytes
139  UIListStreamAllocator(char* buffer, size_t nbytes)
140  :
141  buf_(buffer, nbytes),
142  stream_(&buf_)
143  {}
144 
145 
146  // Protected Member Functions
147 
148  //- Reset buffer pointers
149  inline void reset(char* buffer, size_t nbytes)
150  {
151  buf_.resetg(buffer, nbytes);
152  }
153 
154  void printBufInfo(Ostream& os) const
155  {
157  }
158 
159 public:
160 
161  // Member Functions
162 
163  //- Const UList access to the input characters (shallow copy).
164  inline const UList<char> list() const
165  {
166  return buf_.list();
167  }
168 
169  //- Non-const UList access to the input characters (shallow copy).
170  inline UList<char> list()
171  {
172  return buf_.list();
173  }
174 
175  //- The list size
176  inline label size() const
177  {
178  return buf_.capacity();
179  }
180 
181  //- Position of the get buffer
182  std::streampos tellg() const
183  {
184  return buf_.tellg();
185  }
186 
187  //- Move to buffer start, clear errors
188  void rewind()
189  {
190  buf_.pubseekpos(0, std::ios_base::in);
191  stream_.clear(); // for safety, clear any old errors
192  }
193 };
194 
195 } // End namespace Detail
196 
197 
198 /*---------------------------------------------------------------------------*\
199  Class UIListStream Declaration
200 \*---------------------------------------------------------------------------*/
201 
202 class UIListStream
203 :
205  public ISstream
206 {
208 
209 public:
210 
211  // Constructors
212 
213  //- Construct using specified buffer and number of bytes
215  (
216  const char* buffer,
217  size_t nbytes,
218  IOstreamOption streamOpt = IOstreamOption()
219  )
220  :
221  allocator_type(const_cast<char*>(buffer), nbytes),
222  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
223  {}
224 
225  //- Construct using data area from a List and number of bytes
227  (
228  const UList<char>& buffer,
229  size_t nbytes,
230  IOstreamOption streamOpt = IOstreamOption()
231  )
232  :
233  UIListStream(buffer.cdata(), nbytes, streamOpt)
234  {}
235 
236  //- Construct using data area from a List and its inherent storage size
237  // Uses addressed size, thus no special treatment for a DynamicList
238  explicit UIListStream
239  (
240  const UList<char>& buffer,
241  IOstreamOption streamOpt = IOstreamOption()
242  )
243  :
244  UIListStream(buffer.cdata(), buffer.size(), streamOpt)
245  {}
246 
247 
248  // Member Functions
249 
250  //- Return the current get position in the buffer
251  std::streampos pos() const
252  {
253  return allocator_type::tellg();
254  }
255 
256  //- Rewind the stream, clearing any old errors
257  virtual void rewind()
258  {
260  setGood(); // resynchronize with internal state
261  }
262 
263  //- Print stream description to Ostream
264  virtual void print(Ostream& os) const;
265 
266 
267  // Member Operators
268 
269  //- A non-const reference to const Istream
270  // Needed for read-constructors where the stream argument is temporary
271  Istream& operator()() const
272  {
273  // Could also rewind
274  return const_cast<UIListStream&>(*this);
275  }
276 
277 
278  // Additional constructors and methods (as per v2012 and earlier)
279  #ifdef Foam_IOstream_extras
280 
281  //- Construct using specified buffer and number of bytes
283  (
284  const char* buffer,
285  size_t nbytes,
288  )
289  :
290  UIListStream(buffer, nbytes, IOstreamOption(fmt, ver))
291  {}
292 
293  //- Construct using data area from a List and number of bytes
295  (
296  const UList<char>& buffer,
297  size_t nbytes,
299  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
300  )
301  :
302  UIListStream(buffer.cdata(), nbytes, IOstreamOption(fmt, ver))
303  {}
304 
305  //- Construct using data area from a List and its inherent storage size
306  // Uses addressed size, thus no special treatment for a DynamicList
308  (
309  const UList<char>& buf,
311  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
312  )
313  :
314  UIListStream(buf.cdata(), buf.size(), IOstreamOption(fmt, ver))
315  {}
316 
317  #endif /* Foam_IOstream_extras */
318 };
319 
320 
321 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322 
323 } // End namespace Foam
324 
325 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
326 
327 #endif
328 
329 // ************************************************************************* //
Foam::UIListStream
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:201
Foam::Detail::UIListStreamAllocator::buf_
memorybuf::in buf_
The stream buffer.
Definition: UIListStream.H:129
Foam::UIListStream::UIListStream
UIListStream(const char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
Definition: UIListStream.H:214
Foam::UList::cdata
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:230
Foam::memorybuf::in::printBufInfo
void printBufInfo(Ostream &os) const
Some information about the input buffer position/capacity.
Definition: memoryStreamBuffer.H:227
Foam::memorybuf::in::list
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: memoryStreamBuffer.H:215
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
ISstream.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::memorybuf::in::capacity
std::streamsize capacity() const
The buffer capacity.
Definition: memoryStreamBuffer.H:209
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::Detail::UIListStreamAllocator::list
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: UIListStream.H:163
Foam::IOstream::setGood
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
Foam::UIListStream::rewind
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UIListStream.H:256
Foam::memorybuf::in
An input streambuf for memory access.
Definition: memoryStreamBuffer.H:159
Foam::Detail::UIListStreamAllocator
An stream/stream-buffer input allocator for a externally allocated list.
Definition: UIListStream.H:120
Foam::UIListStream::operator()
Istream & operator()() const
A non-const reference to const Istream.
Definition: UIListStream.H:270
Foam::uiliststream::rewind
void rewind()
Rewind the stream, clearing any old errors.
Definition: UIListStream.H:104
Foam::Detail::UIListStreamAllocator::tellg
std::streampos tellg() const
Position of the get buffer.
Definition: UIListStream.H:181
Foam::memorybuf
A streambuf for memory.
Definition: memoryStreamBuffer.H:54
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::memorybuf::in::resetg
void resetg(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
Definition: memoryStreamBuffer.H:193
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
memoryStreamBuffer.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Detail::UIListStreamAllocator::reset
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:148
Foam::ISstream::ISstream
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition: ISstreamI.H:32
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::uiliststream::reset
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:98
Foam::Detail::UIListStreamAllocator::stream_
stream_type stream_
The stream.
Definition: UIListStream.H:132
Foam::memorybuf::in::tellg
std::streamsize tellg() const
The buffer get position.
Definition: memoryStreamBuffer.H:134
Foam::uiliststream::uiliststream
uiliststream(const char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UIListStream.H:91
Foam::Detail::UIListStreamAllocator::stream_type
std::istream stream_type
Definition: UIListStream.H:126
Foam::uiliststream
Similar to std::istringstream, but with an externally managed input buffer.
Definition: UIListStream.H:82
UList.H
clear
patchWriters clear()
Foam::UIListStream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:42
Foam::Detail::UIListStreamAllocator::UIListStreamAllocator
UIListStreamAllocator(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UIListStream.H:138
Foam::UList
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:103
Foam::UIListStream::pos
std::streampos pos() const
Return the current get position in the buffer.
Definition: UIListStream.H:250
Foam::Detail::UIListStreamAllocator::size
label size() const
The list size.
Definition: UIListStream.H:175
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Detail::UIListStreamAllocator::rewind
void rewind()
Move to buffer start, clear errors.
Definition: UIListStream.H:187
Foam::Detail::UIListStreamAllocator::printBufInfo
void printBufInfo(Ostream &os) const
Definition: UIListStream.H:153
Foam::Detail::UIListStreamAllocator::list
UList< char > list()
Non-const UList access to the input characters (shallow copy).
Definition: UIListStream.H:169