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-2023 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 override { 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 override
136  {
137  return os_.flags();
138  }
139 
140  //- Set stream flags
141  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) override
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  //- Inherit write methods from Ostream
156  using Ostream::writeQuoted;
158  //- Write token to stream or otherwise handle it.
159  // \return false if the token type was not handled by this method
160  virtual bool write(const token& tok) override;
161 
162  //- Write character
163  virtual Ostream& write(const char c) override;
164 
165  //- Write character/string content, with/without surrounding quotes
166  virtual Ostream& writeQuoted
167  (
168  const char* str,
169  std::streamsize len,
170  const bool quoted=true
171  ) override;
172 
173  //- Write character string
174  virtual Ostream& write(const char* str) override;
175 
176  //- Write word
177  virtual Ostream& write(const word& str) override;
178 
179  //- Write string (quoted)
180  // In the rare case that the string contains a final trailing
181  // backslash, it will be dropped to the appearance of an escaped
182  // double-quote.
183  virtual Ostream& write(const std::string& str) override;
184 
185  //- Write int32_t
186  virtual Ostream& write(const int32_t val) override;
187 
188  //- Write int64_t
189  virtual Ostream& write(const int64_t val) override;
190 
191  //- Write float
192  virtual Ostream& write(const float val) override;
193 
194  //- Write double
195  virtual Ostream& write(const double val) override;
196 
197  //- Write binary block
198  virtual Ostream& write
199  (
200  const char* data,
201  std::streamsize count
202  ) override;
203 
204  //- Low-level raw binary output
205  virtual Ostream& writeRaw
206  (
207  const char* data,
208  std::streamsize count
209  ) override;
210 
211  //- Begin marker for low-level raw binary output.
212  // The count indicates the number of bytes for subsequent
213  // writeRaw calls.
214  virtual bool beginRawWrite(std::streamsize count) override;
215 
216  //- End marker for low-level raw binary output.
217  virtual bool endRawWrite() override;
218 
219  //- Add indentation characters
220  virtual void indent() override;
221 
222 
223  // Stream state functions
224 
225  //- Flush stream
226  virtual void flush() override;
227 
228  //- Add newline and flush stream
229  virtual void endl() override;
230 
231  //- Get the current padding character
232  virtual char fill() const override;
233 
234  //- Set padding character for formatted field up to field width
235  // \return previous padding character
236  virtual char fill(const char fillch) override;
237 
238  //- Get width of output field
239  virtual int width() const override;
240 
241  //- Set width of output field
242  // \return previous width
243  virtual int width(const int w) override;
244 
245  //- Get precision of output field
246  virtual int precision() const override;
247 
248  //- Set precision of output field
249  // \return old precision
250  virtual int precision(const int p) override;
251 
252 
253  // Print
254 
255  //- Print stream description to Ostream
256  virtual void print(Ostream& os) const override;
257 };
258 
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 } // End namespace Foam
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 #include "OSstreamI.H"
267 
268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269 
270 #endif
271 
272 // ************************************************************************* //
virtual ios_base::fmtflags flags() const override
Get stream flags.
Definition: OSstream.H:149
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A class for handling file names.
Definition: fileName.H:72
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition: OSstream.C:101
void syncState()
Set stream state to match that of the std::ostream.
Definition: OSstream.H:165
A token holds an item read from Istream.
Definition: token.H:65
virtual void endl() override
Add newline and flush stream.
Definition: OSstream.C:301
A simple container for options an IOstream can normally have.
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition: OSstream.C:271
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 Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
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
virtual int precision() const override
Get precision of output field.
Definition: OSstream.C:334
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
Definition: OSstream.C:247
virtual bool endRawWrite() override
End marker for low-level raw binary output.
Definition: OSstream.C:262
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:56
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition: OSstream.C:29
OBJstream os(runTime.globalPath()/outputName)
labelList f(nPoints)
virtual const std::ostream & stdStream() const
Const access to underlying std::ostream.
Definition: OSstream.H:136
virtual void indent() override
Add indentation characters.
Definition: OSstream.C:285
const dimensionedScalar c
Speed of light in a vacuum.
streamFormat
Data format (ascii | binary)
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SstreamsPrint.C:39
virtual void flush() override
Flush stream.
Definition: OSstream.C:295
void operator=(const OSstream &)=delete
No copy assignment.
virtual int width() const override
Get width of output field.
Definition: OSstream.C:322
virtual char fill() const override
Get the current padding character.
Definition: OSstream.C:310
volScalarField & p
OSstream(const OSstream &)=default
Copy construct.
Namespace for OpenFOAM.