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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::UIListStream
28
29Description
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
57See 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
73namespace 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.
83class uiliststream
84:
85 virtual public std::ios,
86 protected memorybuf::in,
87 public std::istream
88{
89public:
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
113namespace Detail
114{
115
116/*---------------------------------------------------------------------------*\
117 Class Detail::UIListStreamAllocator Declaration
118\*---------------------------------------------------------------------------*/
119
120//- An stream/stream-buffer input allocator for a externally allocated list
122{
123protected:
124
125 // Protected Data
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 }
154 void printBufInfo(Ostream& os) const
155 {
157 }
158
159public:
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\*---------------------------------------------------------------------------*/
202class UIListStream
203:
205 public ISstream
206{
208
209public:
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
294 UIListStream
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// ************************************************************************* //
An stream/stream-buffer input allocator for a externally allocated list.
Definition: UIListStream.H:121
void printBufInfo(Ostream &os) const
Definition: UIListStream.H:153
UList< char > list()
Non-const UList access to the input characters (shallow copy).
Definition: UIListStream.H:169
std::streampos tellg() const
Position of the get buffer.
Definition: UIListStream.H:181
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: UIListStream.H:163
memorybuf::in buf_
The stream buffer.
Definition: UIListStream.H:129
UIListStreamAllocator(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UIListStream.H:138
label size() const
The list size.
Definition: UIListStream.H:175
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:148
void rewind()
Move to buffer start, clear errors.
Definition: UIListStream.H:187
stream_type stream_
The stream.
Definition: UIListStream.H:132
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary)
static const versionNumber currentVersion
The current version number (2.0)
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:205
UIListStream(const UList< char > &buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a List and number of bytes.
Definition: UIListStream.H:226
Istream & operator()() const
A non-const reference to const Istream.
Definition: UIListStream.H:270
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:42
UIListStream(const char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
Definition: UIListStream.H:214
std::streampos pos() const
Return the current get position in the buffer.
Definition: UIListStream.H:250
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UIListStream.H:256
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
An input streambuf for memory access.
void printBufInfo(Ostream &os) const
Some information about the input buffer position/capacity.
void resetg(char *s, std::streamsize n)
Reset for character array (can be nullptr) and number of bytes.
const UList< char > list() const
Const UList access to the input characters (shallow copy).
std::streamsize capacity() const
The buffer capacity.
std::streamsize tellg() const
The buffer get position.
A streambuf for memory.
Similar to std::istringstream, but with an externally managed input buffer.
Definition: UIListStream.H:87
uiliststream(const char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UIListStream.H:91
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:98
void rewind()
Rewind the stream, clearing any old errors.
Definition: UIListStream.H:104
patchWriters clear()
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.