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