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:200
Foam::Detail::UIListStreamAllocator::buf_
memorybuf::in buf_
Definition: UIListStream.H:128
Foam::UIListStream::UIListStream
UIListStream(const char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Definition: UIListStream.H:213
Foam::UList::cdata
const T * cdata() const noexcept
Definition: UListI.H:223
Foam::memorybuf::in::printBufInfo
void printBufInfo(Ostream &os) const
Definition: memoryStreamBuffer.H:225
Foam::memorybuf::in::list
const UList< char > list() const
Definition: memoryStreamBuffer.H:213
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:189
ISstream.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Definition: IOstreamOption.H:282
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
Definition: IOstreamOption.H:161
Foam::memorybuf::in::capacity
std::streamsize capacity() const
Definition: memoryStreamBuffer.H:207
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
Foam::Detail::UIListStreamAllocator::list
const UList< char > list() const
Definition: UIListStream.H:162
Foam::IOstream::setGood
void setGood() noexcept
Definition: IOstream.H:143
Foam::UIListStream::rewind
virtual void rewind()
Definition: UIListStream.H:255
Foam::memorybuf::in
Definition: memoryStreamBuffer.H:157
Foam::Detail::UIListStreamAllocator
Definition: UIListStream.H:119
Foam::UIListStream::operator()
Istream & operator()() const
Definition: UIListStream.H:269
Foam::uiliststream::rewind
void rewind()
Definition: UIListStream.H:101
Foam::Detail::UIListStreamAllocator::tellg
std::streampos tellg() const
Definition: UIListStream.H:180
Foam::memorybuf
A std::streambuf used for memory buffer streams such as UIListStream, UOListStream,...
Definition: memoryStreamBuffer.H:50
Foam::IOstreamOption::versionNumber
Definition: IOstreamOption.H:81
Foam::memorybuf::in::resetg
void resetg(char *s, std::streamsize n)
Definition: memoryStreamBuffer.H:191
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:59
Foam::IOstreamOption::version
versionNumber version() const noexcept
Definition: IOstreamOption.H:334
Foam::IOstreamOption::streamFormat
streamFormat
Definition: IOstreamOption.H:66
memoryStreamBuffer.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Detail::UIListStreamAllocator::reset
void reset(char *buffer, size_t nbytes)
Definition: UIListStream.H:147
Foam::ISstream::ISstream
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Definition: ISstreamI.H:25
Foam
Definition: atmBoundaryLayer.C:26
Foam::uiliststream::reset
void reset(char *buffer, size_t nbytes)
Definition: UIListStream.H:95
Foam::Detail::UIListStreamAllocator::stream_
stream_type stream_
Definition: UIListStream.H:131
Foam::memorybuf::in::tellg
std::streamsize tellg() const
Definition: memoryStreamBuffer.H:130
Foam::uiliststream::uiliststream
uiliststream(const char *buffer, size_t nbytes)
Definition: UIListStream.H:88
Foam::Detail::UIListStreamAllocator::stream_type
std::istream stream_type
Definition: UIListStream.H:125
Foam::uiliststream
Definition: UIListStream.H:79
UList.H
clear
patchWriters clear()
Foam::UIListStream::print
virtual void print(Ostream &os) const
Definition: ListStream.C:35
Foam::Detail::UIListStreamAllocator::UIListStreamAllocator
UIListStreamAllocator(char *buffer, size_t nbytes)
Definition: UIListStream.H:137
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:99
Foam::UIListStream::pos
std::streampos pos() const
Definition: UIListStream.H:249
Foam::Detail::UIListStreamAllocator::size
label size() const
Definition: UIListStream.H:174
Foam::UList::size
void size(const label n)
Definition: UList.H:110
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::Detail::UIListStreamAllocator::rewind
void rewind()
Definition: UIListStream.H:186
Foam::Detail::UIListStreamAllocator::printBufInfo
void printBufInfo(Ostream &os) const
Definition: UIListStream.H:152
Foam::Detail::UIListStreamAllocator::list
UList< char > list()
Definition: UIListStream.H:168