OSstream.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) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2017-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::OSstream
29 
30 Description
31  Generic output stream using a standard (STL) stream.
32 
33 SourceFiles
34  OSstreamI.H
35  OSstream.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef OSstream_H
40 #define OSstream_H
41 
42 #include "Ostream.H"
43 #include "fileName.H"
44 #include <iostream>
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class OSstream Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class OSstream
56 :
57  public Ostream
58 {
59  // Private Data
60 
61  fileName name_;
62 
63  std::ostream& os_;
64 
65 
66 public:
67 
68  // Generated Methods
69 
70  //- Copy construct
71  OSstream(const OSstream&) = default;
72 
73  //- No copy assignment
74  void operator=(const OSstream&) = delete;
75 
76 
77  // Constructors
78 
79  //- Construct wrapper around std::ostream, set stream status
80  // Default stream options (ASCII, uncompressed)
81  inline OSstream
82  (
83  std::ostream& os,
84  const string& streamName,
85  IOstreamOption streamOpt = IOstreamOption()
86  );
87 
88  //- Construct wrapper around std::ostream, set stream status
90  (
91  std::ostream& os,
92  const string& streamName,
96  )
97  :
98  OSstream(os, streamName, IOstreamOption(fmt, ver, cmp))
99  {}
100 
101 
102  // Member Functions
103 
104  // Characteristics
105 
106  //- Get the name of the stream.
107  // Useful for Fstream to remember the filename
108  virtual const fileName& name() const
109  {
110  return name_;
111  }
112 
113  //- Return stream name for modification
114  virtual fileName& name()
115  {
116  return name_;
117  }
118 
119  //- Get stream flags
120  virtual ios_base::fmtflags flags() const;
121 
122 
123  // Write Functions
124 
125  //- Write token to stream or otherwise handle it.
126  // \return false if the token type was not handled by this method
127  virtual bool write(const token& tok);
128 
129  //- Write character
130  virtual Ostream& write(const char c);
131 
132  //- Write character string
133  virtual Ostream& write(const char* str);
134 
135  //- Write word
136  virtual Ostream& write(const word& str);
137 
138  //- Write string (quoted)
139  // In the rare case that the string contains a final trailing
140  // backslash, it will be dropped to the appearance of an escaped
141  // double-quote.
142  virtual Ostream& write(const string& str);
143 
144  //- Write std::string surrounded by quotes.
145  // Optional write without quotes.
146  virtual Ostream& writeQuoted
147  (
148  const std::string& str,
149  const bool quoted=true
150  );
151 
152  //- Write int32_t
153  virtual Ostream& write(const int32_t val);
154 
155  //- Write int64_t
156  virtual Ostream& write(const int64_t val);
157 
158  //- Write floatScalar
159  virtual Ostream& write(const floatScalar val);
160 
161  //- Write doubleScalar
162  virtual Ostream& write(const doubleScalar val);
163 
164  //- Write binary block
165  virtual Ostream& write(const char* data, std::streamsize count);
166 
167  //- Low-level raw binary output
168  virtual Ostream& writeRaw
169  (
170  const char* data,
171  std::streamsize count
172  );
173 
174  //- Begin marker for low-level raw binary output.
175  // The count indicates the number of bytes for subsequent
176  // writeRaw calls.
177  virtual bool beginRawWrite(std::streamsize count);
178 
179  //- End marker for low-level raw binary output.
180  virtual bool endRawWrite();
181 
182  //- Add indentation characters
183  virtual void indent();
184 
185 
186  // Stream state functions
187 
188  //- Set stream flags
189  virtual ios_base::fmtflags flags(const ios_base::fmtflags f);
190 
191  //- Flush stream
192  virtual void flush();
193 
194  //- Add newline and flush stream
195  virtual void endl();
196 
197  //- Get the current padding character
198  virtual char fill() const;
199 
200  //- Set padding character for formatted field up to field width
201  // \return previous padding character
202  virtual char fill(const char fillch);
203 
204  //- Get width of output field
205  virtual int width() const;
206 
207  //- Set width of output field
208  // \return previous width
209  virtual int width(const int w);
210 
211  //- Get precision of output field
212  virtual int precision() const;
213 
214  //- Set precision of output field
215  // \return old precision
216  virtual int precision(const int p);
217 
218 
219  // STL stream
220 
221  //- Access to underlying std::ostream
222  virtual std::ostream& stdStream()
223  {
224  return os_;
225  }
226 
227  //- Const access to underlying std::ostream
228  virtual const std::ostream& stdStream() const
229  {
230  return os_;
231  }
232 
233 
234  // Print
235 
236  //- Print stream description to Ostream
237  virtual void print(Ostream& os) const;
238 };
239 
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 } // End namespace Foam
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 #include "OSstreamI.H"
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:79
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::OSstream::write
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: OSstream.C:36
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::OSstream::stdStream
virtual const std::ostream & stdStream() const
Const access to underlying std::ostream.
Definition: OSstream.H:227
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::OSstream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OSstream.C:225
Foam::OSstream::width
virtual int width() const
Get width of output field.
Definition: OSstream.C:314
Foam::OSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:119
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::OSstream::operator=
void operator=(const OSstream &)=delete
No copy assignment.
Foam::OSstream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:251
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::OSstream::name
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
Foam::OSstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OSstream.H:221
Foam::OSstream::OSstream
OSstream(const OSstream &)=default
Copy construct.
OSstreamI.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
fileName.H
Foam::OSstream::fill
virtual char fill() const
Get the current padding character.
Definition: OSstream.C:302
Foam::OSstream::flags
virtual ios_base::fmtflags flags() const
Get stream flags.
Definition: OSstream.C:288
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::OSstream::name
virtual fileName & name()
Return stream name for modification.
Definition: OSstream.H:113
Foam::OSstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:241
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
Ostream.H
Foam::OSstream::indent
virtual void indent()
Add indentation characters.
Definition: OSstream.C:266
Foam::OSstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:281
f
labelList f(nPoints)
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::OSstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:45
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::OSstream::flush
virtual void flush()
Flush stream.
Definition: OSstream.C:275