UOListStream.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::UOListStream
28
29Description
30 Similar to OStringStream but using an externally managed buffer for
31 its output.
32
33 This allows the output buffer to be reused and can make it easier when
34 writing out data. It is the user's responsibility to ensure proper
35 synchronization in the sizes. Provided that the external buffer is large
36 enough that overflow does not occur, the following usage pattern
37 works.
38
39 \code
40 DynamicList<char> buffer(4096); // allocate some large buffer
41
42 {
43 UOListStream os(buffer);
44 os << "content1" << " and more content";
45 buffer.resize(os.size()); // synchronize sizes
46 }
47
48 something.write(buffer, buffer.size());
49 \endcode
50
51 Although the UOListStream is quite lightweight, there may be cases
52 where it is preferable to reuse the stream as well.
53 \code
54 DynamicList<char> buffer(4096); // allocate some large buffer
55
56 UOListStream os(buffer);
57 os << "content1" << " and more content";
58 buffer.resize(os.size()); // synchronize sizes
59
60 something.write(buffer, buffer.size());
61
62 os.rewind();
63 os << "content2";
64 buffer.resize(os.size()); // synchronize sizes
65
66 something.write(buffer, buffer.size());
67
68 // or simply using the output size directly (without sync)
69 os.rewind();
70 os << "content3";
71
72 something.write(buffer, os.size());
73 \endcode
74
75See Also
76 Foam::IListStream
77 Foam::OListStream
78 Foam::UIListStream
79
80\*---------------------------------------------------------------------------*/
81
82#ifndef UOListStream_H
83#define UOListStream_H
84
85#include "DynamicList.H"
86#include "FixedList.H"
87#include "OSstream.H"
88#include "memoryStreamBuffer.H"
89
90// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
91
92namespace Foam
93{
94
95namespace Detail
96{
97
98/*---------------------------------------------------------------------------*\
99 Class Detail::UOListStreamAllocator Declaration
100\*---------------------------------------------------------------------------*/
101
102//- An stream/stream-buffer allocator for external buffers
104{
105protected:
106
107 // Protected Data
109 typedef std::ostream stream_type;
110
111 //- The stream buffer
113
114 //- The stream
116
117
118 // Constructors
119
120 //- Construct for character array and number of bytes
121 UOListStreamAllocator(char* buffer, size_t nbytes)
122 :
123 buf_(buffer, nbytes),
124 stream_(&buf_)
125 {}
127 void printBufInfo(Ostream& os) const
128 {
130 }
131
132public:
133
134 // Member Functions
135
136 //- Const UList access to the characters written (shallow copy).
137 inline const UList<char> list() const
138 {
139 return buf_.list();
140 }
141
142 //- Non-const UList access to the characters written (shallow copy).
143 inline UList<char> list()
144 {
145 return buf_.list();
146 }
147
148 //- The current list output capacity
149 inline label capacity() const
150 {
151 return buf_.capacity();
152 }
153
154 //- The current output position in the buffer,
155 //- which is also the addressed list size
156 inline label size() const
157 {
158 return buf_.tellp();
159 }
160
161 //- Move to buffer start, clear errors
162 void rewind()
163 {
164 buf_.pubseekpos(0, std::ios_base::out);
165 stream_.clear(); // for safety, clear any old errors
166 }
167};
168
169} // End namespace Detail
170
171
172/*---------------------------------------------------------------------------*\
173 Class UOListStream Declaration
174\*---------------------------------------------------------------------------*/
175
176//- An OSstream attached to an unallocated external buffer
177class UOListStream
178:
180 public OSstream
181{
183
184public:
185
186 // Constructors
187
188 //- Construct using specified buffer and number of bytes
190 (
191 char* buffer,
192 size_t nbytes,
193 IOstreamOption streamOpt = IOstreamOption()
194 )
195 :
196 allocator_type(buffer, nbytes),
197 OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
198 {}
199
200 //- Construct using data area from a List and number of bytes
202 (
203 UList<char>& buffer,
204 size_t nbytes,
205 IOstreamOption streamOpt = IOstreamOption()
206 )
207 :
208 UOListStream(buffer.data(), nbytes, streamOpt)
209 {}
210
211 //- Construct using data area from a List and its inherent storage size
212 explicit UOListStream
213 (
214 UList<char>& buffer,
215 IOstreamOption streamOpt = IOstreamOption()
216 )
217 :
218 UOListStream(buffer.data(), buffer.size(), streamOpt)
219 {}
220
221 //- Construct using data area from a FixedList
222 template<unsigned N>
223 explicit UOListStream
224 (
225 FixedList<char, N>& buffer,
226 IOstreamOption streamOpt = IOstreamOption()
227 )
228 :
229 UOListStream(buffer.data(), N, streamOpt)
230 {}
231
232 //- Construct using data area from a DynamicList and its capacity
233 template<int SizeMin>
234 explicit UOListStream
235 (
237 IOstreamOption streamOpt = IOstreamOption()
238 )
239 :
240 UOListStream(buffer.data(), buffer.capacity(), streamOpt)
241 {}
242
243
244 // Member Functions
245
246 //- Rewind the stream, clearing any old errors
247 virtual void rewind()
248 {
250 setGood(); // resynchronize with internal state
251 }
252
253 //- Print stream description to Ostream
254 virtual void print(Ostream& os) const;
255
256
257 // Additional constructors and methods (as per v2012 and earlier)
258 #ifdef Foam_IOstream_extras
259
260 //- Construct using specified buffer and number of bytes
262 (
263 char* buffer,
264 size_t nbytes,
267 )
268 :
269 UOListStream(buffer, nbytes, IOstreamOption(fmt, ver))
270 {}
271
272 //- Construct using data area from a List and number of bytes
273 UOListStream
274 (
275 UList<char>& buffer,
276 size_t nbytes,
278 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
279 )
280 :
281 UOListStream(buffer.data(), nbytes, IOstreamOption(fmt, ver))
282 {}
283
284 //- Construct using data area from a List and its inherent storage size
286 (
287 UList<char>& buffer,
289 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
290 )
291 :
292 UOListStream(buffer.data(), buffer.size(), IOstreamOption(fmt, ver))
293 {}
294
295 //- Construct using data area from a FixedList
296 template<unsigned N>
298 (
299 FixedList<char, N>& buffer,
301 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
302 )
303 :
304 UOListStream(buffer.data(), N, IOstreamOption(fmt, ver))
305 {}
306
307 //- Construct using data area from a DynamicList and its capacity
308 template<int SizeMin>
310 (
311 DynamicList<char,SizeMin>& buf,
313 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
314 )
315 :
316 UOListStream(buf.data(), buf.capacity(), IOstreamOption(fmt, ver))
317 {}
318
319 #endif /* Foam_IOstream_extras */
320};
321
322
323// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
324
325} // End namespace Foam
326
327// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328
329#endif
330
331// ************************************************************************* //
An stream/stream-buffer allocator for external buffers.
Definition: UOListStream.H:103
void printBufInfo(Ostream &os) const
Definition: UOListStream.H:126
UOListStreamAllocator(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UOListStream.H:120
UList< char > list()
Non-const UList access to the characters written (shallow copy).
Definition: UOListStream.H:142
const UList< char > list() const
Const UList access to the characters written (shallow copy).
Definition: UOListStream.H:136
label capacity() const
The current list output capacity.
Definition: UOListStream.H:148
void rewind()
Move to buffer start, clear errors.
Definition: UOListStream.H:161
memorybuf::out buf_
The stream buffer.
Definition: UOListStream.H:111
stream_type stream_
The stream.
Definition: UOListStream.H:114
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
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 output stream using a standard (STL) stream.
Definition: OSstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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 OSstream attached to an unallocated external buffer.
Definition: UOListStream.H:180
UOListStream(DynamicList< char, SizeMin > &buffer, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a DynamicList and its capacity.
Definition: UOListStream.H:234
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:58
UOListStream(UList< char > &buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a List and number of bytes.
Definition: UOListStream.H:201
UOListStream(UList< char > &buffer, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a List and its inherent storage size.
Definition: UOListStream.H:212
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UOListStream.H:246
UOListStream(FixedList< char, N > &buffer, IOstreamOption streamOpt=IOstreamOption())
Construct using data area from a FixedList.
Definition: UOListStream.H:223
UOListStream(char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
Definition: UOListStream.H:189
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
An output streambuf for memory access.
void printBufInfo(Ostream &os) const
Some information about the output buffer position/capacity.
std::streamsize tellp() const
The buffer put position.
const UList< char > list() const
Const UList access to the characters written (shallow copy).
std::streamsize capacity() const
The buffer capacity.
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
const Vector< label > N(dict.get< Vector< label > >("N"))