Loading...
Searching...
No Matches
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-2024 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 Foam_OScountStream_H
35#define Foam_OScountStream_H
36
37#include "OSstream.H"
38
39// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40
41namespace Foam
42{
43
44/*---------------------------------------------------------------------------*\
45 Class ocountstream Declaration
46\*---------------------------------------------------------------------------*/
48//- Trivial output stream for calculating byte counts.
49// Since all output values are discarded, it can be used as a /dev/null
50// output buffer as well.
51class ocountstream
52:
53 virtual public std::ios,
54 public std::ostream
55{
56 //- A streambuf class for determining byte counts
57 class countbuf : public std::streambuf
58 {
59 //- The number of bytes counted
60 std::streamsize size_;
61
62 protected:
63
64 //- Set position pointer to relative position
65 virtual std::streampos seekoff
66 (
67 std::streamoff off,
68 std::ios_base::seekdir way,
69 std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
70 )
71 {
72 if (which & std::ios_base::out)
73 {
74 if (way == std::ios_base::beg)
75 {
76 size_ = off;
77 }
78 else if (way == std::ios_base::cur)
79 {
80 size_ += off;
81 }
82 else if (way == std::ios_base::end)
83 {
84 // not really possible
85 }
86
87 return size_; // Like span_tellp()
88 }
89
90 return -1;
91 }
92
93 //- Set position pointer to absolute position
94 // For the counter, adjust the count accordingly.
95 virtual std::streampos seekpos
96 (
97 std::streampos pos,
98 std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
99 )
100 {
101 return seekoff(pos, std::ios_base::beg, which);
102 }
103
104 //- Output overflow handling - increment counter
105 virtual int overflow(int_type c = traits_type::eof())
106 {
107 if (c != traits_type::eof()) ++size_;
108 return c;
109 }
110
111 //- Put sequence of characters - increment counter
112 virtual std::streamsize xsputn(const char* s, std::streamsize n)
113 {
114 size_ += n;
115 return n;
116 }
117
118 public:
119
120 // Constructors
121
122 //- Default construct, count = 0
123 countbuf() : size_(0) {}
124
125
126 // Member Functions
127
128 //- The number of bytes counted
129 std::streamsize count() const noexcept { return size_; }
130
131 //- Reset the count
132 void reset(std::streamsize n = 0) noexcept { size_ = n; }
133 };
134
135
136 // Private Data
137
138 typedef countbuf buffer_type;
139 typedef std::ostream stream_type;
140
141 //- Reference to the underlying buffer
142 buffer_type buf_;
143
144public:
145
146 // Constructors
147
148 //- Default construct
149 ocountstream() : stream_type(&buf_) {}
150
151
152 // Member Functions
153
154 //- This hides both signatures of std::basic_ios::rdbuf()
155 countbuf* rdbuf() { return &buf_; }
156
157 //- The number of bytes counted
158 std::streamsize count() const noexcept { return buf_.count(); }
159
160 //- Reset the count
161 void reset(std::streamsize n = 0) noexcept
162 {
163 buf_.reset(n);
164 stream_type::clear(); // Clear old errors
165 }
166
167 //- Some information about the output buffer position/capacity
168 void debug_info(Ostream& os) const
169 {
170 os << "count=" << buf_.count();
171 }
172
173 //- Information about stream
174 void print(Ostream& os) const { debug_info(os); }
175};
177
178/*---------------------------------------------------------------------------*\
179 Class OCountStream Declaration
180\*---------------------------------------------------------------------------*/
182//- An output stream for calculating byte counts
183class OCountStream
184:
185 public Foam::Detail::StreamAllocator<Foam::ocountstream>,
187{
188 typedef
190 allocator_type;
191
192public:
193
194 // Constructors
196 //- Default construct
197 explicit OCountStream
198 (
199 IOstreamOption streamOpt = IOstreamOption()
200 )
201 :
202 allocator_type(),
203 OSstream(stream_, "count", streamOpt.format(), streamOpt.version())
204 {}
205
206 //- Copy construct
207 OCountStream(const OCountStream& str)
208 :
209 allocator_type(),
210 OSstream(stream_, str.name(), static_cast<IOstreamOption>(str))
211 {
212 stream_.reset(str.count());
213 }
215
216 // Member Functions
217
218 //- The number of bytes counted
219 std::streamsize count() const noexcept { return stream_.count(); }
220
221 //- The number of bytes counted
222 std::streamsize size() const noexcept { return stream_.count(); }
223
224 //- Reset the count
225 void reset(std::streamsize n = 0) noexcept { stream_.reset(n); }
226
227 //- Rewind the stream, reset the count, clearing any old errors
228 virtual void rewind()
229 {
230 stream_.reset();
231 syncState();
232 }
233
234 //- Print stream description
235 virtual void print(Ostream& os) const override
236 {
237 os << "ocountstream: ";
239 os << '\n';
240 }
241};
243
244// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245
246} // End namespace Foam
247
248// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249
250#endif
251
252// ************************************************************************* //
label n
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition IOstream.H:620
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
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression.
streamFormat format() const noexcept
Get the current stream format.
An output stream for calculating byte counts.
OCountStream(IOstreamOption streamOpt=IOstreamOption())
Default construct.
std::streamsize size() const noexcept
The number of bytes counted.
OCountStream(const OCountStream &str)
Copy construct.
void reset(std::streamsize n=0) noexcept
Reset the count.
virtual void print(Ostream &os) const override
Print stream description.
virtual void rewind()
Rewind the stream, reset the count, clearing any old errors.
std::streamsize count() const noexcept
The number of bytes counted.
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
virtual const fileName & name() const override
Get the name of the output serial stream. (eg, the name of the Fstream file name).
Definition OSstream.H:134
OSstream(const OSstream &)=default
Copy construct.
void syncState()
Set stream state to match that of the std::ostream.
Definition OSstream.H:182
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
void print(Ostream &os) const
Information about stream.
void debug_info(Ostream &os) const
Some information about the output buffer position/capacity.
ocountstream()
Default construct.
countbuf * rdbuf()
This hides both signatures of std::basic_ios::rdbuf().
void reset(std::streamsize n=0) noexcept
Reset the count.
std::streamsize count() const noexcept
The number of bytes counted.
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)
const direction noexcept
Definition scalarImpl.H:265
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
word format(conversionProperties.get< word >("format"))