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  //- The output stream path
62  fileName name_;
63 
64  //- The output stream
65  std::ostream& os_;
66 
67 
68 public:
69 
70  // Generated Methods
71 
72  //- Copy construct
73  OSstream(const OSstream&) = default;
74 
75  //- No copy assignment
76  void operator=(const OSstream&) = delete;
77 
78 
79  // Constructors
80 
81  //- Construct wrapper around std::ostream, set stream status
82  // Default stream options (ASCII, uncompressed)
83  inline OSstream
84  (
85  std::ostream& os,
86  const string& streamName,
87  IOstreamOption streamOpt = IOstreamOption()
88  );
89 
90  //- Construct wrapper around std::ostream, set stream status
91  OSstream
92  (
93  std::ostream& os,
94  const string& streamName,
97  )
98  :
99  OSstream(os, streamName, IOstreamOption(fmt, cmp))
100  {}
101 
102  //- Construct wrapper around std::ostream, set stream status
103  OSstream
104  (
105  std::ostream& os,
106  const string& streamName,
110  )
111  :
112  OSstream(os, streamName, IOstreamOption(fmt, ver, cmp))
113  {}
115 
116  // Member Functions
117 
118  // Characteristics
119 
120  //- Get the name of the output serial stream.
121  //- (eg, the name of the Fstream file name)
122  virtual const fileName& name() const override { return name_; }
123 
124  //- The name of the output serial stream, for modification.
125  // Use with caution since some classes (eg, Fstream)
126  // also use this for filesystem information!
127  virtual fileName& name() { return name_; }
128 
129 
130  // STL stream
131 
132  //- Const access to underlying std::ostream
133  virtual const std::ostream& stdStream() const { return os_; }
135  //- Access to underlying std::ostream
136  virtual std::ostream& stdStream() { return os_; }
137 
138 
139  // Stream State
140 
141  //- Get current stream flags
142  virtual std::ios_base::fmtflags flags() const override
143  {
144  return os_.flags();
145  }
146 
147  //- Set stream flags, return old stream flags
148  virtual std::ios_base::fmtflags flags
149  (
150  std::ios_base::fmtflags f
151  ) override
152  {
153  return os_.flags(f);
154  }
156  //- Set stream state to match that of the std::ostream
157  void syncState()
158  {
159  setState(os_.rdstate());
160  }
161 
162 
163  // Write Functions
164 
165  //- Inherit write methods from Ostream
166  using Ostream::writeQuoted;
167 
168  //- Write token to stream or otherwise handle it.
169  // \return false if the token type was not handled by this method
170  virtual bool write(const token& tok) override;
171 
172  //- Write character
173  virtual Ostream& write(const char c) override;
174 
175  //- Write character/string content, with/without surrounding quotes
176  virtual Ostream& writeQuoted
177  (
178  const char* str,
179  std::streamsize len,
180  const bool quoted=true
181  ) override;
183  //- Write character string
184  virtual Ostream& write(const char* str) override;
185 
186  //- Write word
187  virtual Ostream& write(const word& str) override;
188 
189  //- Write string (quoted)
190  // In the rare case that the string contains a final trailing
191  // backslash, it will be dropped to the appearance of an escaped
192  // double-quote.
193  virtual Ostream& write(const std::string& str) override;
194 
195  //- Write int32_t
196  virtual Ostream& write(const int32_t val) override;
197 
198  //- Write int64_t
199  virtual Ostream& write(const int64_t val) override;
200 
201  //- Write float
202  virtual Ostream& write(const float val) override;
203 
204  //- Write double
205  virtual Ostream& write(const double val) override;
206 
207  //- Write binary block
208  virtual Ostream& write
209  (
210  const char* data,
211  std::streamsize count
212  ) override;
213 
214  //- Low-level raw binary output
215  virtual Ostream& writeRaw
216  (
217  const char* data,
218  std::streamsize count
219  ) override;
220 
221  //- Begin marker for low-level raw binary output.
222  // The count indicates the number of bytes for subsequent
223  // writeRaw calls.
224  virtual bool beginRawWrite(std::streamsize count) override;
225 
226  //- End marker for low-level raw binary output.
227  virtual bool endRawWrite() override;
228 
229  //- Add indentation characters
230  virtual void indent() override;
231 
232 
233  // Stream state functions
234 
235  //- Flush stream
236  virtual void flush() override;
237 
238  //- Add newline and flush stream
239  virtual void endl() override;
240 
241  //- Get the current padding character
242  virtual char fill() const override;
243 
244  //- Set padding character for formatted field up to field width
245  // \return previous padding character
246  virtual char fill(const char fillch) override;
247 
248  //- Get width of output field
249  virtual int width() const override;
250 
251  //- Set width of output field
252  // \return previous width
253  virtual int width(const int w) override;
254 
255  //- Get precision of output field
256  virtual int precision() const override;
257 
258  //- Set precision of output field
259  // \return old precision
260  virtual int precision(const int p) override;
261 
262 
263  // Print
264 
265  //- Print stream description to Ostream
266  virtual void print(Ostream& os) const override;
267 };
268 
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 } // End namespace Foam
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #include "OSstreamI.H"
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 
280 #endif
281 
282 // ************************************************************************* //
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:182
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
virtual std::ios_base::fmtflags flags() const override
Get current stream flags.
Definition: OSstream.H:163
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:134
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:150
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.