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-2020 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 "FixedList.H"
68 #include "UList.H"
69 #include "ISstream.H"
70 #include "memoryStreamBuffer.H"
71 
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
73 
74 namespace Foam
75 {
76 
77 /*---------------------------------------------------------------------------*\
78  Class uiliststream Declaration
79 \*---------------------------------------------------------------------------*/
80 
81 //- Similar to std::istringstream, but with an externally managed input buffer.
82 // This allows the input buffer to be filled or refilled from various sources
83 // without copying.
84 class uiliststream
85 :
86  virtual public std::ios,
87  protected memorybuf::in,
88  public std::istream
89 {
90 public:
91 
92  //- Construct for character array and number of bytes
93  uiliststream(const char* buffer, size_t nbytes)
94  :
95  memorybuf::in(const_cast<char*>(buffer), nbytes),
96  std::istream(static_cast<memorybuf::in*>(this))
97  {}
98 
99  //- Reset buffer pointers
100  inline void reset(char *buffer, size_t nbytes)
101  {
102  resetg(buffer, nbytes);
103  }
104 
105  //- Rewind the stream, clearing any old errors
106  void rewind()
107  {
108  this->pubseekpos(0, std::ios_base::in);
109  clear(); // for safety, clear any old errors
110  }
111 };
112 
113 
114 namespace Detail
115 {
116 
117 /*---------------------------------------------------------------------------*\
118  Class Detail::UIListStreamAllocator Declaration
119 \*---------------------------------------------------------------------------*/
120 
121 //- An stream/stream-buffer input allocator for a externally allocated list
123 {
124 protected:
125 
126  // Protected Data
127 
128  typedef std::istream stream_type;
129 
130  //- The stream buffer
132 
133  //- The stream
135 
136 
137  // Constructors
138 
139  //- Construct for character array and number of bytes
140  UIListStreamAllocator(char* buffer, size_t nbytes)
141  :
142  buf_(buffer, nbytes),
143  stream_(&buf_)
144  {}
145 
146 
147  // Protected Member Functions
148 
149  //- Reset buffer pointers
150  inline void reset(char* buffer, size_t nbytes)
151  {
152  buf_.resetg(buffer, nbytes);
153  }
154 
155  void printBufInfo(Ostream& os) const
156  {
157  buf_.printBufInfo(os);
158  }
159 
160 public:
161 
162  // Member Functions
163 
164  //- Const UList access to the input characters (shallow copy).
165  inline const UList<char> list() const
166  {
167  return buf_.list();
168  }
169 
170  //- Non-const UList access to the input characters (shallow copy).
171  inline UList<char> list()
172  {
173  return buf_.list();
174  }
175 
176  //- The list size
177  inline label size() const
178  {
179  return buf_.capacity();
180  }
181 
182  //- Position of the get buffer
183  std::streampos tellg() const
184  {
185  return buf_.tellg();
186  }
187 
188  //- Move to buffer start, clear errors
189  void rewind()
190  {
191  buf_.pubseekpos(0, std::ios_base::in);
192  stream_.clear(); // for safety, clear any old errors
193  }
194 };
195 
196 } // End namespace Detail
197 
198 
199 /*---------------------------------------------------------------------------*\
200  Class UIListStream Declaration
201 \*---------------------------------------------------------------------------*/
202 
203 class UIListStream
204 :
206  public ISstream
207 {
209 
210 public:
211 
212  // Constructors
213 
214  //- Construct using specified buffer and number of bytes
216  (
217  const char* buffer,
218  size_t nbytes,
221  const Foam::string& name="input"
222  )
223  :
224  allocator_type(const_cast<char*>(buffer), nbytes),
226  {}
227 
228 
229  //- Construct using data area from a FixedList
230  template<unsigned N>
231  explicit UIListStream
232  (
233  const FixedList<char, N>& buffer,
236  const Foam::string& name="input"
237  )
238  :
239  UIListStream(buffer.cdata(), N, format, version, name)
240  {}
241 
242  //- Construct using data area from a List and number of bytes
244  (
245  const UList<char>& buffer,
246  label size,
249  const Foam::string& name="input"
250  )
251  :
252  UIListStream(buffer.cdata(), size, format, version, name)
253  {}
254 
255 
256  //- Construct using data area from a List and its inherent storage size
257  // Uses addressed size, thus no special treatment for a DynamicList
258  explicit UIListStream
259  (
260  const UList<char>& buffer,
263  const Foam::string& name="input"
264  )
265  :
266  UIListStream(buffer.cdata(), buffer.size(), format, version, name)
267  {}
268 
269 
270  // Member Functions
271 
272  //- Return the current get position in the buffer
273  std::streampos pos() const
274  {
275  return allocator_type::tellg();
276  }
277 
278  //- Rewind the stream, clearing any old errors
279  virtual void rewind()
280  {
282  setGood(); // resynchronize with internal state
283  }
284 
285  //- Print stream description to Ostream
286  virtual void print(Ostream& os) const;
287 
288 
289  // Member Operators
290 
291  //- A non-const reference to const Istream
292  // Needed for read-constructors where the stream argument is temporary
293  Istream& operator()() const
294  {
295  return const_cast<Istream&>(static_cast<const Istream&>(*this));
296  }
297 };
298 
299 
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301 
302 } // End namespace Foam
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 #endif
307 
308 // ************************************************************************* //
Foam::UIListStream
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:202
Foam::Detail::UIListStreamAllocator::buf_
memorybuf::in buf_
The stream buffer.
Definition: UIListStream.H:130
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
ISstream.H
Foam::FixedList::cdata
const T * cdata() const noexcept
Return a const pointer to the first data element.
Definition: FixedListI.H:179
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
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::UIListStream::UIListStream
UIListStream(const char *buffer, size_t nbytes, streamFormat format=ASCII, versionNumber version=currentVersion, const Foam::string &name="input")
Construct using specified buffer and number of bytes.
Definition: UIListStream.H:215
Foam::Detail::UIListStreamAllocator::list
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: UIListStream.H:164
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::UIListStream::rewind
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UIListStream.H:278
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:121
Foam::UIListStream::operator()
Istream & operator()() const
A non-const reference to const Istream.
Definition: UIListStream.H:292
Foam::uiliststream::rewind
void rewind()
Rewind the stream, clearing any old errors.
Definition: UIListStream.H:105
Foam::Detail::UIListStreamAllocator::tellg
std::streampos tellg() const
Position of the get buffer.
Definition: UIListStream.H:182
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::ISstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:124
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:341
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
memoryStreamBuffer.H
Foam::Detail::UIListStreamAllocator::reset
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:149
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:99
Foam::Detail::UIListStreamAllocator::stream_
stream_type stream_
The stream.
Definition: UIListStream.H:133
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:92
Foam::Detail::UIListStreamAllocator::stream_type
std::istream stream_type
Definition: UIListStream.H:127
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::uiliststream
Similar to std::istringstream, but with an externally managed input buffer.
Definition: UIListStream.H:83
UList.H
clear
patchWriters clear()
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
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:139
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
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::UList::cdata
const T * cdata() const
Return a const pointer to the first data element.
Definition: UListI.H:198
Foam::UIListStream::pos
std::streampos pos() const
Return the current get position in the buffer.
Definition: UIListStream.H:272
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::Detail::UIListStreamAllocator::size
label size() const
The list size.
Definition: UIListStream.H:176
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
N
const Vector< label > N(dict.get< Vector< label >>("N"))
FixedList.H
Foam::Detail::UIListStreamAllocator::rewind
void rewind()
Move to buffer start, clear errors.
Definition: UIListStream.H:188
Foam::Detail::UIListStreamAllocator::printBufInfo
void printBufInfo(Ostream &os) const
Definition: UIListStream.H:154
Foam::Detail::UIListStreamAllocator::list
UList< char > list()
Non-const UList access to the input characters (shallow copy).
Definition: UIListStream.H:170