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-2022 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 Foam_OSstream_H
40 #define Foam_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
89  OSstream
90  (
91  std::ostream& os,
92  const string& streamName,
95  )
96  :
97  OSstream(os, streamName, IOstreamOption(fmt, cmp))
98  {}
99 
100  //- Construct wrapper around std::ostream, set stream status
101  OSstream
102  (
103  std::ostream& os,
104  const string& streamName,
108  )
109  :
110  OSstream(os, streamName, IOstreamOption(fmt, ver, cmp))
111  {}
112 
113 
114  // Member Functions
115 
116  // Characteristics
117 
118  //- Get the name of the output serial stream.
119  //- (eg, the name of the Fstream file name)
120  virtual const fileName& name() const { return name_; }
121 
122 
123  // STL stream
124 
125  //- Const access to underlying std::ostream
126  virtual const std::ostream& stdStream() const { return os_; }
127 
128  //- Access to underlying std::ostream
129  virtual std::ostream& stdStream() { return os_; }
130 
131 
132  // Stream State
133 
134  //- Get stream flags
135  virtual ios_base::fmtflags flags() const
136  {
137  return os_.flags();
138  }
139 
140  //- Set stream flags
141  virtual ios_base::fmtflags flags(const ios_base::fmtflags f)
142  {
143  return os_.flags(f);
144  }
145 
146  //- Set stream state to match that of the std::ostream
147  void syncState()
148  {
149  setState(os_.rdstate());
150  }
151 
152 
153  // Write Functions
154 
155  //- Write token to stream or otherwise handle it.
156  // \return false if the token type was not handled by this method
157  virtual bool write(const token& tok);
158 
159  //- Write character
160  virtual Ostream& write(const char c);
161 
162  //- Write character string
163  virtual Ostream& write(const char* str);
164 
165  //- Write word
166  virtual Ostream& write(const word& str);
167 
168  //- Write string (quoted)
169  // In the rare case that the string contains a final trailing
170  // backslash, it will be dropped to the appearance of an escaped
171  // double-quote.
172  virtual Ostream& write(const string& str);
173 
174  //- Write std::string surrounded by quotes.
175  // Optional write without quotes.
176  virtual Ostream& writeQuoted
177  (
178  const std::string& str,
179  const bool quoted=true
180  );
181 
182  //- Write int32_t
183  virtual Ostream& write(const int32_t val);
184 
185  //- Write int64_t
186  virtual Ostream& write(const int64_t val);
187 
188  //- Write float
189  virtual Ostream& write(const float val);
190 
191  //- Write double
192  virtual Ostream& write(const double val);
193 
194  //- Write binary block
195  virtual Ostream& write(const char* data, std::streamsize count);
196 
197  //- Low-level raw binary output
198  virtual Ostream& writeRaw
199  (
200  const char* data,
201  std::streamsize count
202  );
203 
204  //- Begin marker for low-level raw binary output.
205  // The count indicates the number of bytes for subsequent
206  // writeRaw calls.
207  virtual bool beginRawWrite(std::streamsize count);
208 
209  //- End marker for low-level raw binary output.
210  virtual bool endRawWrite();
211 
212  //- Add indentation characters
213  virtual void indent();
214 
215 
216  // Stream state functions
217 
218  //- Flush stream
219  virtual void flush();
220 
221  //- Add newline and flush stream
222  virtual void endl();
223 
224  //- Get the current padding character
225  virtual char fill() const;
226 
227  //- Set padding character for formatted field up to field width
228  // \return previous padding character
229  virtual char fill(const char fillch);
230 
231  //- Get width of output field
232  virtual int width() const;
233 
234  //- Set width of output field
235  // \return previous width
236  virtual int width(const int w);
237 
238  //- Get precision of output field
239  virtual int precision() const;
240 
241  //- Set precision of output field
242  // \return old precision
243  virtual int precision(const int p);
244 
245 
246  // Print
247 
248  //- Print stream description to Ostream
249  virtual void print(Ostream& os) const;
250 };
251 
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 } // End namespace Foam
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 #include "OSstreamI.H"
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 #endif
264 
265 // ************************************************************************* //
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:233
virtual const fileName & name() const
Get the name of the output serial stream. (eg, the name of the Fstream file name) ...
Definition: OSstream.H:128
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
virtual char fill() const
Get the current padding character.
Definition: OSstream.C:281
A class for handling file names.
Definition: fileName.H:71
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:305
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:159
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:165
virtual ios_base::fmtflags flags() const
Get stream flags.
Definition: OSstream.H:149
A token holds an item read from Istream.
Definition: token.H:64
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:272
virtual void indent()
Add indentation characters.
Definition: OSstream.C:256
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:39
A simple container for options an IOstream can normally have.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual void flush()
Flush stream.
Definition: OSstream.C:266
Representation of a major/minor version number.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: OSstream.C:29
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:112
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
labelList f(nPoints)
virtual const std::ostream & stdStream() const
Const access to underlying std::ostream.
Definition: OSstream.H:136
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OSstream.C:218
virtual int width() const
Get width of output field.
Definition: OSstream.C:293
const dimensionedScalar c
Speed of light in a vacuum.
streamFormat
Data format (ascii | binary)
void operator=(const OSstream &)=delete
No copy assignment.
volScalarField & p
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:242
OSstream(const OSstream &)=default
Copy construct.
Namespace for OpenFOAM.