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 -------------------------------------------------------------------------------
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 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43 
44 /*---------------------------------------------------------------------------*\
45  Class ocountstream Declaration
46 \*---------------------------------------------------------------------------*/
47 
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.
51 class 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 
144 public:
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 
174 
175 /*---------------------------------------------------------------------------*\
176  Class OCountStream Declaration
177 \*---------------------------------------------------------------------------*/
178 
179 //- An output stream for calculating byte counts
180 class OCountStream
181 :
182  public Foam::Detail::StreamAllocator<Foam::ocountstream>,
183  public Foam::OSstream
184 {
185  typedef
187  allocator_type;
188 
189 public:
190 
191  // Constructors
192 
193  //- Default construct
194  explicit OCountStream
195  (
196  IOstreamOption streamOpt = IOstreamOption()
197  )
198  :
199  allocator_type(),
200  OSstream(stream_, "count", streamOpt.format(), streamOpt.version())
201  {}
202 
203  //- Copy construct
204  OCountStream(const OCountStream& str)
205  :
206  allocator_type(),
207  OSstream(stream_, str.name(), static_cast<IOstreamOption>(str))
208  {
210  }
211 
212 
213  // Member Functions
214 
215  //- The number of bytes counted
216  std::streamsize count() const noexcept { return stream_.count(); }
217 
218  //- The number of bytes counted
219  std::streamsize size() const noexcept { return stream_.count(); }
220 
221  //- Reset the count
222  void reset(std::streamsize n = 0) noexcept { stream_.reset(n); }
223 
224  //- Rewind the stream, reset the count, clearing any old errors
225  virtual void rewind()
226  {
227  stream_.reset();
228  syncState();
229  }
230 
231  //- Print stream description to Ostream
232  virtual void print(Ostream& os) const override;
233 };
234 
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 } // End namespace Foam
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
242 #endif
243 
244 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:600
An output stream for calculating byte counts.
Definition: OCountStream.H:209
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:182
Trivial output stream for calculating byte counts.
Definition: OCountStream.H:47
OCountStream(IOstreamOption streamOpt=IOstreamOption())
Default construct.
Definition: OCountStream.H:226
void debug_info(Ostream &os) const
Some information about the output buffer position/capacity.
Definition: OCountStream.H:195
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:134
virtual void rewind()
Rewind the stream, reset the count, clearing any old errors.
Definition: OCountStream.H:266
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)
Foam::ocountstream stream_
The std::stream.
Definition: IOstream.H:609
void reset(std::streamsize n=0) noexcept
Reset the count.
Definition: OCountStream.H:186
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:176
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
The number of bytes counted.
Definition: OCountStream.H:251
void reset(std::streamsize n=0) noexcept
Reset the count.
Definition: OCountStream.H:261
ocountstream()
Default construct.
Definition: OCountStream.H:168
std::streamsize size() const noexcept
The number of bytes counted.
Definition: OCountStream.H:256
surface1 clear()
const dimensionedScalar c
Speed of light in a vacuum.
std::streamsize count() const noexcept
The number of bytes counted.
Definition: OCountStream.H:181
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))
OSstream(const OSstream &)=default
Copy construct.
Namespace for OpenFOAM.