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-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::OSCountStream
28 
29 Description
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 
42 namespace Foam
43 {
44 
45 /*---------------------------------------------------------------------------*\
46  Class countstreambuf Declaration
47 \*---------------------------------------------------------------------------*/
48 
49 //- A streambuf class for determining byte counts
50 class countstreambuf
51 :
52  public std::streambuf
53 {
54  //- The number of bytes counted
55  std::streamsize size_;
56 
57 protected:
58 
59  //- Handle output counting via overflow
60  virtual int overflow(int c = EOF)
61  {
62  if (c != EOF)
63  {
64  ++size_;
65  }
66  return c;
67  }
68 
69  //- Set position pointer to absolute position
70  // For the counter, any positioning is ignored and it always acts like
71  // seekpos(0), which resets the count.
72  virtual std::streampos seekpos
73  (
74  std::streampos,
75  std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
76  )
77  {
78  size_ = 0;
79  return 0;
80  }
81 
82 public:
83 
84  //- Default construct, or with precount size
85  countstreambuf(std::streamsize precount=0)
86  :
87  size_(precount)
88  {}
89 
90  //- \return The number of bytes counted
91  std::streamsize size() const
92  {
93  return size_;
94  }
95 
96  //- Some information about the number of bytes counted
97  inline void printBufInfo(Ostream& os) const
98  {
99  os << "count=" << size_;
100  }
101 };
102 
103 
104 /*---------------------------------------------------------------------------*\
105  Class ocountstream Declaration
106 \*---------------------------------------------------------------------------*/
107 
108 //- Trivial output stream for calculating byte counts.
109 // Since all output values are discarded, it can be used as a /dev/null
110 // output buffer as well.
111 class ocountstream
112 :
113  virtual public std::ios,
114  protected countstreambuf,
115  public std::ostream
116 {
117 public:
118 
119  //- Default construct
120  ocountstream()
121  :
122  countstreambuf(),
123  std::ostream(static_cast<countstreambuf*>(this))
124  {}
125 
126 
127  //- \return The number of bytes counted
128  using countstreambuf::size;
129 
130  //- Rewind the stream, reset the count
131  void rewind()
132  {
133  this->pubseekpos(0, std::ios_base::out);
134  clear(); // for safety, clear any old errors
135  }
136 };
137 
138 
139 namespace Detail
140 {
141 
142 /*---------------------------------------------------------------------------*\
143  Class Detail::OCountStreamAllocator Declaration
144 \*---------------------------------------------------------------------------*/
145 
146 //- An stream/stream-buffer allocator for counting
148 {
149 protected:
150 
151  // Protected Data
152 
153  typedef std::ostream stream_type;
154 
155  //- The stream buffer
157 
158  //- The output stream
160 
161 
162  // Constructors
163 
164  //- Default construct, or with precount size
165  OCountStreamAllocator(std::streamsize precount=0)
166  :
167  buf_(precount),
168  stream_(&buf_)
169  {}
170 
171 
172  // Protected Member Functions
173 
174  void printBufInfo(Ostream& os) const
175  {
176  buf_.printBufInfo(os);
177  }
178 
179 public:
180 
181  // Member Functions
182 
183  //- The number of bytes counted
184  std::streamsize size() const
185  {
186  return buf_.size();
187  }
188 
189  //- Rewind the stream, reset the count
190  void rewind()
191  {
192  buf_.pubseekpos(0);
193  stream_.clear(); // for safety, clear any old errors
194  }
195 };
196 
197 } // End namespace Detail
198 
199 
200 /*---------------------------------------------------------------------------*\
201  Class OCountStream Declaration
202 \*---------------------------------------------------------------------------*/
203 
204 //- An output stream for calculating byte counts
205 class OCountStream
206 :
208  public OSstream
209 {
211 
212 public:
213 
214  // Constructors
215 
216  //- Default construct
217  explicit OCountStream
218  (
221  )
222  :
223  allocator_type(),
224  OSstream(stream_, "count", format, version)
225  {}
226 
227  //- Copy construct
228  OCountStream(const OCountStream& str)
229  :
230  allocator_type(str.size()),
231  OSstream(stream_, str.name(), str.format(), str.version())
232  {}
233 
234 
235  // Member Functions
236 
237  //- Rewind the stream, reset the count, clearing any old errors
238  virtual void rewind()
239  {
241  setGood(); // resynchronize with internal state
242  }
243 
244  //- Print stream description to Ostream
245  virtual void print(Ostream& os) const;
246 };
247 
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 } // End namespace Foam
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 #endif
256 
257 // ************************************************************************* //
Foam::Detail::OCountStreamAllocator::OCountStreamAllocator
OCountStreamAllocator(std::streamsize precount=0)
Default construct, or with precount size.
Definition: OCountStream.H:164
Foam::Detail::OCountStreamAllocator::stream_type
std::ostream stream_type
Definition: OCountStream.H:152
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::OCountStream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:66
Foam::countstreambuf
A streambuf class for determining byte counts.
Definition: OCountStream.H:49
Foam::Detail::OCountStreamAllocator::stream_
stream_type stream_
The output stream.
Definition: OCountStream.H:158
Foam::countstreambuf::printBufInfo
void printBufInfo(Ostream &os) const
Some information about the number of bytes counted.
Definition: OCountStream.H:96
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::ocountstream::ocountstream
ocountstream()
Default construct.
Definition: OCountStream.H:119
Foam::OSstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:107
Foam::OSstream::OSstream
OSstream(const OSstream &)=default
Copy construct.
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
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
Foam::countstreambuf::overflow
virtual int overflow(int c=EOF)
Handle output counting via overflow.
Definition: OCountStream.H:59
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OCountStream
An output stream for calculating byte counts.
Definition: OCountStream.H:204
Foam::OCountStream::OCountStream
OCountStream(streamFormat format=ASCII, versionNumber version=currentVersion)
Default construct.
Definition: OCountStream.H:217
Foam::countstreambuf::seekpos
virtual std::streampos seekpos(std::streampos, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Set position pointer to absolute position.
Definition: OCountStream.H:72
Foam::countstreambuf::countstreambuf
countstreambuf(std::streamsize precount=0)
Default construct, or with precount size.
Definition: OCountStream.H:84
Foam::Detail::OCountStreamAllocator::printBufInfo
void printBufInfo(Ostream &os) const
Definition: OCountStream.H:173
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::ocountstream
Trivial output stream for calculating byte counts.
Definition: OCountStream.H:110
clear
patchWriters clear()
Foam::OCountStream::rewind
virtual void rewind()
Rewind the stream, reset the count, clearing any old errors.
Definition: OCountStream.H:237
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
Foam::OCountStream::OCountStream
OCountStream(const OCountStream &str)
Copy construct.
Definition: OCountStream.H:227
Foam::Detail::OCountStreamAllocator::size
std::streamsize size() const
The number of bytes counted.
Definition: OCountStream.H:183
Foam::Detail::OCountStreamAllocator::rewind
void rewind()
Rewind the stream, reset the count.
Definition: OCountStream.H:189
Foam::countstreambuf::size
std::streamsize size() const
Definition: OCountStream.H:90
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::ocountstream::rewind
void rewind()
Rewind the stream, reset the count.
Definition: OCountStream.H:130
Foam::Detail::OCountStreamAllocator
An stream/stream-buffer allocator for counting.
Definition: OCountStream.H:146
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Detail::OCountStreamAllocator::buf_
countstreambuf buf_
The stream buffer.
Definition: OCountStream.H:155
OSstream.H