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 -------------------------------------------------------------------------------
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  //- 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 
120 public:
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.
149 class ocountstream
150 :
151  virtual public std::ios,
152  protected countstreambuf,
153  public std::ostream
154 {
155 public:
156 
157  //- Default construct
158  ocountstream()
159  :
160  countstreambuf(),
161  std::ostream(static_cast<countstreambuf*>(this))
162  {}
163 
164 
165  //- \return The buffer put position == number of bytes counted.
166  using countstreambuf::tellp;
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 
183 namespace Detail
184 {
185 
186 /*---------------------------------------------------------------------------*\
187  Class Detail::OCountStreamAllocator Declaration
188 \*---------------------------------------------------------------------------*/
189 
190 //- An stream/stream-buffer allocator for counting
192 {
193 protected:
194 
195  // Protected Data
196 
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
217 
218  void printBufInfo(Ostream& os) const
219  {
221  }
222 
223 public:
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
249 class OCountStream
250 :
252  public OSstream
253 {
255 
256 public:
257 
258  // Constructors
259 
260  //- Default construct
261  explicit OCountStream
262  (
263  IOstreamOption streamOpt = IOstreamOption()
264  )
265  :
266  allocator_type(),
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  :
301  OCountStream(IOstreamOption(fmt, ver))
302  {}
303 
304 
305  #endif /* Foam_IOstream_extras */
306 };
307 
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 } // End namespace Foam
312 
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314 
315 #endif
316 
317 // ************************************************************************* //
Foam::countstreambuf::tellp
std::streamsize tellp() const
Definition: OCountStream.H:128
Foam::Detail::OCountStreamAllocator::OCountStreamAllocator
OCountStreamAllocator(std::streamsize precount=0)
Default construct, or with precount size.
Definition: OCountStream.H:208
Foam::Detail::OCountStreamAllocator::stream_type
std::ostream stream_type
Definition: OCountStream.H:196
s
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))
Definition: gmvOutputSpray.H:25
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
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::IOstream::setGood
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
Foam::Detail::OCountStreamAllocator::stream_
stream_type stream_
The output stream.
Definition: OCountStream.H:202
Foam::countstreambuf::printBufInfo
void printBufInfo(Ostream &os) const
Some information about the number of bytes counted.
Definition: OCountStream.H:134
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::ocountstream::ocountstream
ocountstream()
Default construct.
Definition: OCountStream.H:157
Foam::OSstream::name
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
Foam::OSstream::OSstream
OSstream(const OSstream &)=default
Copy construct.
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
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:338
Foam::countstreambuf::seekpos
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
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
os
OBJstream os(runTime.globalPath()/outputName)
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::countstreambuf::overflow
virtual int overflow(int c=EOF)
Handle output counting via overflow.
Definition: OCountStream.H:101
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OCountStream
An output stream for calculating byte counts.
Definition: OCountStream.H:248
Foam::countstreambuf::countstreambuf
countstreambuf(std::streamsize precount=0)
Default construct, or with precount size.
Definition: OCountStream.H:122
Foam::Detail::OCountStreamAllocator::printBufInfo
void printBufInfo(Ostream &os) const
Definition: OCountStream.H:217
Foam::ocountstream
Trivial output stream for calculating byte counts.
Definition: OCountStream.H:148
Foam::ocountstream::size
std::streamsize size() const
Definition: OCountStream.H:168
Foam::countstreambuf::seekoff
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
clear
patchWriters clear()
Foam::OCountStream::rewind
virtual void rewind()
Rewind the stream, reset the count, clearing any old errors.
Definition: OCountStream.H:280
Foam::OCountStream::OCountStream
OCountStream(const OCountStream &str)
Copy construct.
Definition: OCountStream.H:270
Foam::Detail::OCountStreamAllocator::size
std::streamsize size() const
The number of bytes counted.
Definition: OCountStream.H:227
Foam::Detail::OCountStreamAllocator::rewind
void rewind()
Rewind the stream, reset the count.
Definition: OCountStream.H:233
Foam::countstreambuf::xsputn
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Put sequence of characters.
Definition: OCountStream.H:112
Foam::OCountStream::OCountStream
OCountStream(IOstreamOption streamOpt=IOstreamOption())
Default construct.
Definition: OCountStream.H:261
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:174
Foam::Detail::OCountStreamAllocator
An stream/stream-buffer allocator for counting.
Definition: OCountStream.H:190
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:199
OSstream.H
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177