Ostream.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-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Ostream
29
30Description
31 An Ostream is an abstract base class for all output systems
32 (streams, files, token lists, etc).
33
34SourceFiles
35 Ostream.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_Ostream_H
40#define Foam_Ostream_H
41
42#include "IOstream.H"
43#include "keyType.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47namespace Foam
48{
49
50// Forward Declarations
51class token;
53constexpr char tab = '\t';
54constexpr char nl = '\n';
55
56/*---------------------------------------------------------------------------*\
57 Class Ostream Declaration
58\*---------------------------------------------------------------------------*/
60class Ostream
61:
62 public IOstream
63{
64protected:
65
66 // Protected Data
67
68 //- Indentation of the entry from the start of the keyword
69 static constexpr const unsigned short entryIndentation_ = 16;
70
71 //- Number of spaces per indent level
72 unsigned short indentSize_ = 4;
73
74 //- Current indent level
75 unsigned short indentLevel_ = 0;
76
77
78public:
79
80 // Generated Methods
81
82 //- Copy construct
83 Ostream(const Ostream&) = default;
84
85 //- Destructor
86 virtual ~Ostream() = default;
87
88
89 // Constructors
90
91 //- Default construct (ASCII, uncompressed),
92 //- construct with specified stream option
93 explicit Ostream(IOstreamOption streamOpt = IOstreamOption())
94 :
95 IOstream(streamOpt)
96 {}
97
98
99 //- Construct with format, version (compression)
100 explicit Ostream
101 (
105 )
106 :
107 Ostream(IOstreamOption(fmt, ver, cmp))
108 {}
109
110
111 // Member Functions
112
113 // Write Functions
114
115 //- Write token to stream or otherwise handle it.
116 // \return false if the token type was not handled by this method
117 virtual bool write(const token& tok) = 0;
118
119 //- Write character
120 virtual Ostream& write(const char c) = 0;
121
122 //- Write character string
123 virtual Ostream& write(const char* str) = 0;
124
125 //- Write word
126 virtual Ostream& write(const word& str) = 0;
127
128 //- Write keyType
129 // A plain word is written unquoted.
130 // A regular expression is written as a quoted string.
131 virtual Ostream& write(const keyType& kw);
132
133 //- Write string
134 virtual Ostream& write(const string& str) = 0;
135
136 //- Write std::string surrounded by quotes.
137 // Optional write without quotes.
138 virtual Ostream& writeQuoted
139 (
140 const std::string& str,
141 const bool quoted=true
142 ) = 0;
143
144 //- Write int32_t
145 virtual Ostream& write(const int32_t val) = 0;
146
147 //- Write int64_t
148 virtual Ostream& write(const int64_t val) = 0;
149
150 //- Write floatScalar
151 virtual Ostream& write(const floatScalar val) = 0;
152
153 //- Write doubleScalar
154 virtual Ostream& write(const doubleScalar val) = 0;
155
156 //- Write binary block.
157 virtual Ostream& write(const char* data, std::streamsize count) = 0;
158
159 //- Low-level raw binary output.
160 virtual Ostream& writeRaw
161 (
162 const char* data,
163 std::streamsize count
164 ) = 0;
165
166 //- Emit begin marker for low-level raw binary output.
167 // The count indicates the number of bytes for subsequent
168 // writeRaw calls.
169 virtual bool beginRawWrite(std::streamsize count) = 0;
170
171 //- Emit end marker for low-level raw binary output.
172 virtual bool endRawWrite() = 0;
173
174 //- Add indentation characters
175 virtual void indent() = 0;
176
177 //- Return indent size (spaces per level)
178 unsigned short indentSize() const noexcept
179 {
180 return indentSize_;
181 }
182
183 //- Change indent size (spaces per level), return old value
184 unsigned short indentSize(unsigned short val) noexcept
185 {
186 auto old(indentSize_);
187 indentSize_ = val;
188 return old;
189 }
190
191 //- Return the indent level
192 unsigned short indentLevel() const noexcept
193 {
194 return indentLevel_;
195 }
196
197 //- Change the indent level, return old value
198 unsigned short indentLevel(unsigned short val) noexcept
199 {
200 auto old(indentLevel_);
201 indentLevel_ = val;
202 return old;
203 }
204
205 //- Increment the indent level
206 void incrIndent() noexcept
207 {
208 ++indentLevel_;
209 }
210
211 //- Decrement the indent level
212 void decrIndent();
213
214 //- Write the keyword followed by an appropriate indentation
215 virtual Ostream& writeKeyword(const keyType& kw);
216
217 //- Write begin block group with the given name
218 // Increments indentation, adds newline.
219 virtual Ostream& beginBlock(const keyType& kw);
220
221 //- Write begin block group without a name
222 // Increments indentation, adds newline.
223 virtual Ostream& beginBlock();
224
225 //- Write end block group
226 // Decrements indentation, adds newline.
227 virtual Ostream& endBlock();
228
229 //- Write end entry (';') followed by newline.
230 virtual Ostream& endEntry();
231
232 //- Write a keyword/value entry.
233 // The following two are functionally equivalent:
234 // \code
235 // os.writeEntry(key, value);
236 //
237 // os.writeKeyword(key) << value << endEntry;
238 // \endcode
239 template<class T>
240 Ostream& writeEntry(const keyType& key, const T& value)
241 {
242 writeKeyword(key) << value;
243 return endEntry();
244 }
245
246 //- Write a keyword/value entry only when the two values differ.
247 // \param key the name of the entry
248 // \param value1 the reference value
249 // \param value2 the value to write if it differs from value1
250 template<class T>
252 (
253 const word& key,
254 const T& value1,
255 const T& value2
256 )
257 {
258 if (value1 != value2)
259 {
260 writeEntry(key, value2);
261 }
262
263 return *this;
264 }
265
266
267 // Stream state functions
268
269 //- Flush stream
270 virtual void flush() = 0;
271
272 //- Add newline and flush stream
273 virtual void endl() = 0;
274
275 //- Get padding character
276 virtual char fill() const = 0;
277
278 //- Set padding character for formatted field up to field width
279 virtual char fill(const char fillch) = 0;
280
281 //- Get width of output field
282 virtual int width() const = 0;
283
284 //- Set width of output field (and return old width)
285 virtual int width(const int w) = 0;
286
287 //- Get precision of output field
288 virtual int precision() const = 0;
289
290 //- Set precision of output field (and return old precision)
291 virtual int precision(const int p) = 0;
292
293
294 // Member Operators
295
296 //- Return a non-const reference to const Ostream
297 // Needed for write functions where the stream argument is temporary:
298 // e.g. thing thisThing(OFstream("thingFileName")());
299 Ostream& operator()() const
300 {
301 return const_cast<Ostream&>(*this);
302 }
303
304
305 // Housekeeping
306
307 //- Access to indent level
308 unsigned short& indentLevel() noexcept
309 {
310 return indentLevel_;
311 }
312
313 //- Access to indent size
314 unsigned short& indentSize() noexcept
315 {
316 return indentSize_;
317 }
318};
319
320
321// --------------------------------------------------------------------
322// ------ Manipulators (not taking arguments)
323// --------------------------------------------------------------------
324
325//- An Ostream manipulator
326typedef Ostream& (*OstreamManip)(Ostream&);
327
328//- operator<< handling for manipulators without arguments
330{
331 return f(os);
332}
333
334//- operator<< handling for manipulators without arguments
336{
337 f(os);
338 return os;
339}
340
341
342//- Indent stream
343inline Ostream& indent(Ostream& os)
344{
345 os.indent();
346 return os;
347}
348
349//- Increment the indent level
351{
352 os.incrIndent();
353 return os;
354}
355
356//- Decrement the indent level
358{
359 os.decrIndent();
360 return os;
361}
362
363
364//- Flush stream
365inline Ostream& flush(Ostream& os)
366{
367 os.flush();
368 return os;
369}
370
371
372//- Add newline and flush stream
373inline Ostream& endl(Ostream& os)
374{
375 os.endl();
376 return os;
377}
378
379
380//- Write begin block group without a name
381// Increments indentation, adds newline.
383{
384 os.beginBlock();
385 return os;
386}
387
388
389//- Write end block group
390// Decrements indentation, adds newline.
391inline Ostream& endBlock(Ostream& os)
392{
393 os.endBlock();
394 return os;
395}
396
397
398//- Write end entry (';') followed by newline.
399inline Ostream& endEntry(Ostream& os)
400{
401 os.endEntry();
402 return os;
403}
404
405// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
406
407} // End namespace Foam
408
409// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
410
411#endif
412
413// ************************************************************************* //
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
streamFormat
Data format (ascii | binary)
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
@ UNCOMPRESSED
compression = false
static const versionNumber currentVersion
The current version number (2.0)
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:281
virtual void indent()
Add indentation characters.
Definition: OSstream.C:266
virtual void flush()
Flush stream.
Definition: OSstream.C:275
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
static constexpr const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition: Ostream.H:68
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:105
virtual char fill(const char fillch)=0
Set padding character for formatted field up to field width.
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:239
Ostream(IOstreamOption streamOpt=IOstreamOption())
Definition: Ostream.H:92
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:71
virtual Ostream & write(const doubleScalar val)=0
Write doubleScalar.
virtual char fill() const =0
Get padding character.
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)=0
Write std::string surrounded by quotes.
virtual void flush()=0
Flush stream.
unsigned short & indentSize() noexcept
Access to indent size.
Definition: Ostream.H:313
virtual Ostream & write(const char c)=0
Write character.
virtual bool endRawWrite()=0
Emit end marker for low-level raw binary output.
virtual void indent()=0
Add indentation characters.
Ostream(const Ostream &)=default
Copy construct.
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:87
virtual int precision() const =0
Get precision of output field.
Ostream(IOstreamOption::streamFormat fmt, IOstreamOption::versionNumber ver=IOstreamOption::currentVersion, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
Construct with format, version (compression)
Definition: Ostream.H:100
virtual Ostream & beginBlock()
Write begin block group without a name.
Definition: Ostream.C:96
virtual void endl()=0
Add newline and flush stream.
virtual Ostream & writeRaw(const char *data, std::streamsize count)=0
Low-level raw binary output.
virtual bool beginRawWrite(std::streamsize count)=0
Emit begin marker for low-level raw binary output.
Ostream & writeEntryIfDifferent(const word &key, const T &value1, const T &value2)
Write a keyword/value entry only when the two values differ.
Definition: Ostream.H:251
virtual Ostream & write(const int64_t val)=0
Write int64_t.
unsigned short & indentLevel() noexcept
Access to indent level.
Definition: Ostream.H:307
virtual int width() const =0
Get width of output field.
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:298
virtual int width(const int w)=0
Set width of output field (and return old width)
unsigned short indentLevel(unsigned short val) noexcept
Change the indent level, return old value.
Definition: Ostream.H:197
virtual Ostream & write(const word &str)=0
Write word.
virtual Ostream & write(const int32_t val)=0
Write int32_t.
unsigned short indentLevel() const noexcept
Return the indent level.
Definition: Ostream.H:191
virtual ~Ostream()=default
Destructor.
unsigned short indentSize(unsigned short val) noexcept
Change indent size (spaces per level), return old value.
Definition: Ostream.H:183
unsigned short indentSize() const noexcept
Return indent size (spaces per level)
Definition: Ostream.H:177
virtual Ostream & write(const char *str)=0
Write character string.
void decrIndent()
Decrement the indent level.
Definition: Ostream.C:37
virtual int precision(const int p)=0
Set precision of output field (and return old precision)
virtual Ostream & write(const floatScalar val)=0
Write floatScalar.
void incrIndent() noexcept
Increment the indent level.
Definition: Ostream.H:205
virtual Ostream & write(const char *data, std::streamsize count)=0
Write binary block.
virtual Ostream & write(const string &str)=0
Write string.
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:74
virtual Ostream & endEntry()
Write end entry (';') followed by newline.
Definition: Ostream.C:114
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A class for handling keywords in dictionaries.
Definition: keyType.H:71
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & beginBlock(Ostream &os)
Write begin block group without a name.
Definition: Ostream.H:381
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:349
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:431
const direction noexcept
Definition: Scalar.H:223
Ostream & endEntry(Ostream &os)
Write end entry (';') followed by newline.
Definition: Ostream.H:398
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:356
Ostream &(* OstreamManip)(Ostream &)
An Ostream manipulator.
Definition: Ostream.H:325
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:364
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
constexpr char tab
The tab '\t' character(0x09)
Definition: Ostream.H:52
Ostream & endBlock(Ostream &os)
Write end block group.
Definition: Ostream.H:390
runTime write()
labelList f(nPoints)