OCountStream.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::OSCountStream
28
29Description
30 An output stream for calculating byte counts.
31
32\*---------------------------------------------------------------------------*/
33
34#ifndef OScountStream_H
35#define OScountStream_H
36
37#include "OSstream.H"
38#include <iostream>
39
40// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41
42namespace Foam
43{
44
45/*---------------------------------------------------------------------------*\
46 Class countstreambuf Declaration
47\*---------------------------------------------------------------------------*/
48
49//- A streambuf class for determining byte counts
51:
52 public std::streambuf
53{
54 //- The number of bytes counted
55 std::streamsize size_;
56
57protected:
58
59 //- Set position pointer to relative position
60 virtual std::streampos seekoff
61 (
62 std::streamoff off,
63 std::ios_base::seekdir way,
64 std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
65 )
66 {
67 if (which & std::ios_base::out)
68 {
69 if (way == std::ios_base::beg)
70 {
71 size_ = off;
72 }
73 else if (way == std::ios_base::cur)
74 {
75 size_ += off;
76 }
77 else if (way == std::ios_base::end)
78 {
79 // not really possible
80 }
81
82 return size_; // tellp()
83 }
84
85 return -1;
86 }
87
88
89 //- Set position pointer to absolute position
90 // For the counter, adjust the count accordingly.
91 virtual std::streampos seekpos
92 (
93 std::streampos pos,
94 std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
95 )
96 {
97 return seekoff(pos, std::ios_base::beg, which);
98 }
99
100
101 //- Handle output counting via overflow
102 virtual int overflow(int c = EOF)
103 {
104 if (c != EOF)
105 {
106 ++size_;
107 }
108 return c;
109 }
110
111
112 //- Put sequence of characters
113 virtual std::streamsize xsputn(const char* s, std::streamsize n)
114 {
115 size_ += n;
116 return n;
117 }
118
119
120public:
121
122 //- Default construct, or with precount size
123 explicit countstreambuf(std::streamsize precount=0)
124 :
125 size_(precount)
126 {}
127
128 //- \return The buffer put position == number of bytes counted.
129 std::streamsize tellp() const
130 {
131 return size_;
132 }
133
134 //- Some information about the number of bytes counted
135 void printBufInfo(Ostream& os) const
136 {
137 os << "count=" << size_;
138 }
139};
140
141
142/*---------------------------------------------------------------------------*\
143 Class ocountstream Declaration
144\*---------------------------------------------------------------------------*/
145
146//- Trivial output stream for calculating byte counts.
147// Since all output values are discarded, it can be used as a /dev/null
148// output buffer as well.
149class ocountstream
150:
151 virtual public std::ios,
152 protected countstreambuf,
153 public std::ostream
154{
155public:
156
157 //- Default construct
159 :
161 std::ostream(static_cast<countstreambuf*>(this))
162 {}
163
164
165 //- \return The buffer put position == number of bytes counted.
167
168 //- \return The number of bytes counted
169 std::streamsize size() const
170 {
171 return countstreambuf::tellp();
172 }
173
174 //- Rewind the stream, reset the count
175 void rewind()
176 {
177 this->pubseekpos(0, std::ios_base::out);
178 clear(); // for safety, clear any old errors
179 }
180};
181
182
183namespace Detail
184{
185
186/*---------------------------------------------------------------------------*\
187 Class Detail::OCountStreamAllocator Declaration
188\*---------------------------------------------------------------------------*/
189
190//- An stream/stream-buffer allocator for counting
192{
193protected:
194
195 // Protected Data
197 typedef std::ostream stream_type;
198
199 //- The stream buffer
201
202 //- The output stream
204
205
206 // Constructors
207
208 //- Default construct, or with precount size
209 OCountStreamAllocator(std::streamsize precount=0)
210 :
211 buf_(precount),
212 stream_(&buf_)
213 {}
214
215
216 // Protected Member Functions
218 void printBufInfo(Ostream& os) const
219 {
221 }
222
223public:
224
225 // Member Functions
226
227 //- The number of bytes counted
228 std::streamsize size() const
229 {
230 return buf_.tellp();
231 }
232
233 //- Rewind the stream, reset the count
234 void rewind()
235 {
236 buf_.pubseekpos(0);
237 stream_.clear(); // for safety, clear any old errors
238 }
239};
240
241} // End namespace Detail
242
243
244/*---------------------------------------------------------------------------*\
245 Class OCountStream Declaration
246\*---------------------------------------------------------------------------*/
247
248//- An output stream for calculating byte counts
249class OCountStream
250:
252 public OSstream
253{
255
256public:
257
258 // Constructors
259
260 //- Default construct
261 explicit OCountStream
262 (
263 IOstreamOption streamOpt = IOstreamOption()
264 )
265 :
267 OSstream(stream_, "count", streamOpt.format(), streamOpt.version())
268 {}
269
270 //- Copy construct
271 OCountStream(const OCountStream& str)
272 :
273 allocator_type(str.size()),
274 OSstream(stream_, str.name(), str.format(), str.version())
275 {}
276
277
278 // Member Functions
279
280 //- Rewind the stream, reset the count, clearing any old errors
281 virtual void rewind()
282 {
284 setGood(); // resynchronize with internal state
285 }
286
287 //- Print stream description to Ostream
288 virtual void print(Ostream& os) const;
289
290
291 // Additional constructors and methods (as per v2012 and earlier)
292 #ifdef Foam_IOstream_extras
293
294 //- Default construct
295 explicit OCountStream
296 (
299 )
300 :
302 {}
303
304
305 #endif /* Foam_IOstream_extras */
306};
307
308
309// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310
311} // End namespace Foam
312
313// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314
315#endif
316
317// ************************************************************************* //
label n
An stream/stream-buffer allocator for counting.
Definition: OCountStream.H:191
void printBufInfo(Ostream &os) const
Definition: OCountStream.H:217
std::streamsize size() const
The number of bytes counted.
Definition: OCountStream.H:227
OCountStreamAllocator(std::streamsize precount=0)
Default construct, or with precount size.
Definition: OCountStream.H:208
void rewind()
Rewind the stream, reset the count.
Definition: OCountStream.H:233
countstreambuf buf_
The stream buffer.
Definition: OCountStream.H:199
stream_type stream_
The output stream.
Definition: OCountStream.H:202
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.
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
An output stream for calculating byte counts.
Definition: OCountStream.H:252
OCountStream(IOstreamOption streamOpt=IOstreamOption())
Default construct.
Definition: OCountStream.H:261
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:66
OCountStream(const OCountStream &str)
Copy construct.
Definition: OCountStream.H:270
virtual void rewind()
Rewind the stream, reset the count, clearing any old errors.
Definition: OCountStream.H:280
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A streambuf class for determining byte counts.
Definition: OCountStream.H:52
void printBufInfo(Ostream &os) const
Some information about the number of bytes counted.
Definition: OCountStream.H:134
std::streamsize tellp() const
Definition: OCountStream.H:128
virtual std::streampos seekpos(std::streampos pos, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Set position pointer to absolute position.
Definition: OCountStream.H:91
virtual int overflow(int c=EOF)
Handle output counting via overflow.
Definition: OCountStream.H:101
countstreambuf(std::streamsize precount=0)
Default construct, or with precount size.
Definition: OCountStream.H:122
virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Set position pointer to relative position.
Definition: OCountStream.H:60
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Put sequence of characters.
Definition: OCountStream.H:112
Trivial output stream for calculating byte counts.
Definition: OCountStream.H:153
std::streamsize size() const
Definition: OCountStream.H:168
ocountstream()
Default construct.
Definition: OCountStream.H:157
void rewind()
Rewind the stream, reset the count.
Definition: OCountStream.H:174
patchWriters clear()
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)