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-2020 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,
93  streamFormat fmt,
95  compressionType comp = compressionType::UNCOMPRESSED
96  )
97  :
98  OSstream(os, streamName, IOstreamOption(fmt, comp, ver))
99  {}
100 
101 
102  // Member Functions
103 
104  // Enquiry
105 
106  //- Return the name of the stream
107  // Useful for Fstream to return the filename
108  virtual const fileName& name() const
109  {
110  return name_;
111  }
112 
113  //- Return non-const access to the name of the stream
114  // Useful to alter the stream name
115  virtual fileName& name()
116  {
117  return name_;
118  }
119 
120  //- Return flags of output stream
121  virtual ios_base::fmtflags flags() const;
122 
123 
124  // Write functions
125 
126  //- Write token to stream or otherwise handle it.
127  // \return false if the token type was not handled by this method
128  virtual bool write(const token& tok);
129 
130  //- Write character
131  virtual Ostream& write(const char c);
132 
133  //- Write character string
134  virtual Ostream& write(const char* str);
135 
136  //- Write word
137  virtual Ostream& write(const word& str);
138 
139  //- Write string (quoted)
140  // In the rare case that the string contains a final trailing
141  // backslash, it will be dropped to the appearance of an escaped
142  // double-quote.
143  virtual Ostream& write(const string& str);
144 
145  //- Write std::string surrounded by quotes.
146  // Optional write without quotes.
147  virtual Ostream& writeQuoted
148  (
149  const std::string& str,
150  const bool quoted=true
151  );
152 
153  //- Write int32_t
154  virtual Ostream& write(const int32_t val);
155 
156  //- Write int64_t
157  virtual Ostream& write(const int64_t val);
158 
159  //- Write floatScalar
160  virtual Ostream& write(const floatScalar val);
161 
162  //- Write doubleScalar
163  virtual Ostream& write(const doubleScalar val);
164 
165  //- Write binary block
166  virtual Ostream& write(const char* data, std::streamsize count);
167 
168  //- Low-level raw binary output
169  virtual Ostream& writeRaw
170  (
171  const char* data,
172  std::streamsize count
173  );
174 
175  //- Begin marker for low-level raw binary output.
176  // The count indicates the number of bytes for subsequent
177  // writeRaw calls.
178  virtual bool beginRawWrite(std::streamsize count);
179 
180  //- End marker for low-level raw binary output.
181  virtual bool endRawWrite();
182 
183  //- Add indentation characters
184  virtual void indent();
185 
186 
187  // Stream state functions
188 
189  //- Set stream flags
190  virtual ios_base::fmtflags flags(const ios_base::fmtflags f);
191 
192  //- Flush stream
193  virtual void flush();
194 
195  //- Add newline and flush stream
196  virtual void endl();
197 
198  //- Get the current padding character
199  virtual char fill() const;
200 
201  //- Set padding character for formatted field up to field width
202  // \return previous padding character
203  virtual char fill(const char fillch);
204 
205  //- Get width of output field
206  virtual int width() const;
207 
208  //- Set width of output field
209  // \return previous width
210  virtual int width(const int w);
211 
212  //- Get precision of output field
213  virtual int precision() const;
214 
215  //- Set precision of output field
216  // \return old precision
217  virtual int precision(const int p);
218 
219 
220  // STL stream
221 
222  //- Access to underlying std::ostream
223  virtual std::ostream& stdStream()
224  {
225  return os_;
226  }
227 
228  //- Const access to underlying std::ostream
229  virtual const std::ostream& stdStream() const
230  {
231  return os_;
232  }
233 
234 
235  // Print
236 
237  //- Print stream description to Ostream
238  virtual void print(Ostream& os) const;
239 };
240 
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 
244 } // End namespace Foam
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 #include "OSstreamI.H"
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 #endif
253 
254 // ************************************************************************* //
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:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::OSstream::stdStream
virtual const std::ostream & stdStream() const
Const access to underlying std::ostream.
Definition: OSstream.H:228
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:219
Foam::OSstream::width
virtual int width() const
Get width of output field.
Definition: OSstream.C:308
Foam::OSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:113
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
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:245
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::OSstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:107
Foam::OSstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OSstream.H:222
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:296
Foam::OSstream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:282
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::OSstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:235
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:320
Ostream.H
Foam::OSstream::indent
virtual void indent()
Add indentation characters.
Definition: OSstream.C:260
Foam::OSstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:275
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::Ostream
Ostream(const Ostream &)=default
Copy construct.
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:269