Loading...
Searching...
No Matches
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,2024 OpenFOAM Foundation
9 Copyright (C) 2016-2023 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#include "stdFoam.H" // For span etc.
45
46// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47
48namespace Foam
51// Forward Declarations
52class token;
53
54constexpr char tab = '\t';
55constexpr char nl = '\n';
57/*---------------------------------------------------------------------------*\
58 Class Ostream Declaration
59\*---------------------------------------------------------------------------*/
60
61class Ostream
62:
63 public IOstream
64{
65protected:
66
67 // Protected Data
68
69 //- Indentation of the entry from the start of the keyword
70 static constexpr const unsigned short entryIndentation_ = 16;
71
72 //- Number of spaces per indent level
73 unsigned short indentSize_ = 4;
74
75 //- Current indent level
76 unsigned short indentLevel_ = 0;
78
79public:
80
81 // Generated Methods
82
83 //- Copy construct
84 Ostream(const Ostream&) = default;
85
86 //- Destructor
87 virtual ~Ostream() = default;
88
89
90 // Constructors
91
92 //- Default construct (ASCII, uncompressed),
93 //- construct with specified stream option
94 explicit Ostream(IOstreamOption streamOpt = IOstreamOption())
95 :
96 IOstream(streamOpt)
97 {}
98
99
100 //- Construct with format (uncompressed)
105 )
106 :
107 Ostream(IOstreamOption(fmt, cmp))
108 {}
109
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 content, with/without surrounding quotes
123 virtual Ostream& writeQuoted
124 (
125 const char* str,
126 std::streamsize len,
127 const bool quoted=true
128 ) = 0;
130 //- Write string content, with/without surrounding quotes
131 virtual Ostream& writeQuoted
132 (
133 const std::string& str,
134 const bool quoted=true
135 );
136
137 //- Write character string
138 virtual Ostream& write(const char* str) = 0;
140 //- Write word (unquoted)
141 virtual Ostream& write(const word& str) = 0;
142
143 //- Write string content (usually quoted)
144 virtual Ostream& write(const std::string& str) = 0;
145
146 //- Write keyType
147 // A plain word is written unquoted.
148 // A regular expression is written as a quoted string.
149 virtual Ostream& write(const keyType& kw);
150
151 //- Write int32_t
152 virtual Ostream& write(const int32_t val) = 0;
153
154 //- Write int64_t
155 virtual Ostream& write(const int64_t val) = 0;
156
157 //- Write uint32_t
158 virtual Ostream& write(const uint32_t val) = 0;
159
160 //- Write uint64_t
161 virtual Ostream& write(const uint64_t val) = 0;
162
163 //- Write float
164 virtual Ostream& write(const float val) = 0;
165
166 //- Write double
167 virtual Ostream& write(const double val) = 0;
169 //- Write binary block.
170 virtual Ostream& write(const char* data, std::streamsize count) = 0;
171
172 //- Low-level raw binary output.
173 virtual Ostream& writeRaw
174 (
175 const char* data,
176 std::streamsize count
177 ) = 0;
178
179 //- Emit begin marker for low-level raw binary output.
180 // The count indicates the number of bytes for subsequent
181 // writeRaw calls [currently only used by Pstream].
182 virtual bool beginRawWrite(std::streamsize count) = 0;
183
184 //- Emit end marker for low-level raw binary output.
185 virtual bool endRawWrite() = 0;
187 //- Add indentation characters
188 virtual void indent() = 0;
189
190 //- Return indent size (spaces per level)
191 unsigned short indentSize() const noexcept
192 {
193 return indentSize_;
194 }
195
196 //- Change indent size (spaces per level), return old value
197 unsigned short indentSize(unsigned short val) noexcept
198 {
199 auto old(indentSize_);
200 indentSize_ = val;
201 return old;
202 }
203
204 //- Return the indent level
205 unsigned short indentLevel() const noexcept
207 return indentLevel_;
208 }
209
210 //- Change the indent level, return old value
211 unsigned short indentLevel(unsigned short val) noexcept
212 {
213 auto old(indentLevel_);
214 indentLevel_ = val;
215 return old;
217
218 //- Increment the indent level
219 void incrIndent() noexcept
220 {
221 ++indentLevel_;
222 }
223
224 //- Decrement the indent level
225 void decrIndent();
226
227 //- Write the keyword followed by an appropriate indentation
228 virtual Ostream& writeKeyword(const keyType& kw);
229
230 //- Write begin block group with the given name
231 // Increments indentation, adds newline.
232 virtual Ostream& beginBlock(const keyType& kw);
234 //- Write begin block group without a name
235 // Increments indentation, adds newline.
236 virtual Ostream& beginBlock();
237
238 //- Write end block group
239 // Decrements indentation, adds newline.
240 virtual Ostream& endBlock();
241
242 //- Write end entry (';') followed by newline.
243 virtual Ostream& endEntry();
244
245 //- Write a keyword/value entry.
246 // The following two are functionally equivalent:
247 // \code
248 // os.writeEntry(key, value);
249 //
250 // os.writeKeyword(key) << value << endEntry;
251 // \endcode
252 template<class T>
253 Ostream& writeEntry(const keyType& key, const T& value)
254 {
255 writeKeyword(key) << value;
256 return endEntry();
257 }
258
259 //- Write a keyword/value entry only when the two values differ.
260 // \param key the name of the entry
261 // \param value1 the reference value
262 // \param value2 the value to write if it differs from value1
263 template<class T>
265 (
266 const word& key,
267 const T& value1,
268 const T& value2
270 {
271 if (value1 != value2)
272 {
273 writeEntry(key, value2);
274 }
275
276 return *this;
277 }
278
280 // Stream state functions
281
282 //- Flush stream
283 virtual void flush() = 0;
284
285 //- Add newline and flush stream
286 virtual void endl() = 0;
287
288 //- Get padding character
289 virtual char fill() const = 0;
290
291 //- Set padding character for formatted field up to field width
292 virtual char fill(const char fillch) = 0;
293
294 //- Get width of output field
295 virtual int width() const = 0;
296
297 //- Set width of output field (and return old width)
298 virtual int width(const int w) = 0;
299
300 //- Get precision of output field
301 virtual int precision() const = 0;
302
303 //- Set precision of output field (and return old precision)
304 virtual int precision(const int p) = 0;
305
306
307 // Member Operators
308
309 //- Return a non-const reference to const Ostream
310 // Needed for write functions where the stream argument is temporary:
311 // e.g. thing thisThing(OFstream("thingFileName")());
312 Ostream& operator()() const
313 {
314 return const_cast<Ostream&>(*this);
315 }
316
317
318 // Housekeeping
319
320 //- Access to indent level
321 unsigned short& indentLevel() noexcept
322 {
323 return indentLevel_;
324 }
325
326 //- Access to indent size
327 unsigned short& indentSize() noexcept
328 {
329 return indentSize_;
330 }
332
333
334// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
335
336//- Write operator for std::string_view
337inline Ostream& operator<<(Ostream& os, std::string_view s)
338{
339 os.writeQuoted(s.data(), s.size(), true); // quoted
340 os.check("Foam::operator<<(Ostream&, std::string_view)");
341 return os;
342}
343
344
345/*---------------------------------------------------------------------------*\
346 Manipulators (without arguments)
347\*---------------------------------------------------------------------------*/
348
349//- An Ostream manipulator
350typedef Ostream& (*OstreamManip)(Ostream&);
351
352//- operator<< handling for manipulators without arguments
354{
355 return f(os);
356}
357
358//- operator<< handling for manipulators without arguments
360{
361 f(os);
362 return os;
363}
364
365
366//- Indent stream
367inline Ostream& indent(Ostream& os)
368{
369 os.indent();
370 return os;
372
373//- Increment the indent level
375{
376 os.incrIndent();
377 return os;
378}
379
380//- Decrement the indent level
382{
383 os.decrIndent();
384 return os;
385}
387
388//- Flush stream
389inline Ostream& flush(Ostream& os)
390{
391 os.flush();
392 return os;
393}
394
395
396//- Add newline and flush stream
397inline Ostream& endl(Ostream& os)
398{
399 os.endl();
400 return os;
402
403
404//- Write begin block group without a name
405// Increments indentation, adds newline.
407{
408 os.beginBlock();
409 return os;
410}
411
413//- Write end block group
414// Decrements indentation, adds newline.
415inline Ostream& endBlock(Ostream& os)
416{
417 os.endBlock();
418 return os;
419}
420
421
422//- Write end entry (';') followed by newline.
424{
425 os.endEntry();
426 return os;
427}
428
429// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
430
431} // End namespace Foam
432
433// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
434
435#endif
436
437// ************************************************************************* //
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.
streamFormat
Data format (ascii | binary | coherent).
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED).
@ UNCOMPRESSED
compression = false
IOstream(const IOstream &)=default
Copy construct.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
static constexpr const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition Ostream.H:67
virtual Ostream & endBlock()
Write end block group.
Definition Ostream.C:108
virtual char fill(const char fillch)=0
Set padding character for formatted field up to field width.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition Ostream.H:331
Ostream(IOstreamOption streamOpt=IOstreamOption())
Default construct (ASCII, uncompressed), construct with specified stream option.
Definition Ostream.H:101
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition Ostream.C:60
unsigned short indentSize_
Number of spaces per indent level.
Definition Ostream.H:72
virtual char fill() const =0
Get padding character.
virtual Ostream & write(const double val)=0
Write double.
virtual void flush()=0
Flush stream.
unsigned short & indentSize() noexcept
Access to indent size.
Definition Ostream.H:431
virtual Ostream & write(const char c)=0
Write character.
virtual bool endRawWrite()=0
Emit end marker for low-level raw binary output.
virtual Ostream & write(const uint32_t val)=0
Write uint32_t.
virtual void indent()=0
Add indentation characters.
Ostream(const Ostream &)=default
Copy construct.
virtual int precision() const =0
Get precision of output field.
Ostream(IOstreamOption::streamFormat fmt, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
Construct with format (uncompressed).
Definition Ostream.H:111
virtual Ostream & beginBlock()
Write begin block group without a name.
Definition Ostream.C:99
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.
virtual Ostream & write(const float val)=0
Write float.
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:346
virtual Ostream & write(const int64_t val)=0
Write int64_t.
unsigned short & indentLevel() noexcept
Access to indent level.
Definition Ostream.H:423
virtual int width() const =0
Get width of output field.
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition Ostream.H:412
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:269
virtual Ostream & write(const word &str)=0
Write word (unquoted).
virtual Ostream & write(const int32_t val)=0
Write int32_t.
unsigned short indentLevel() const noexcept
Return the indent level.
Definition Ostream.H:261
virtual ~Ostream()=default
Destructor.
unsigned short indentSize(unsigned short val) noexcept
Change indent size (spaces per level), return old value.
Definition Ostream.H:251
unsigned short indentSize() const noexcept
Return indent size (spaces per level).
Definition Ostream.H:243
virtual Ostream & write(const char *str)=0
Write character string.
void decrIndent()
Decrement the indent level.
Definition Ostream.C:30
virtual int precision(const int p)=0
Set precision of output field (and return old precision).
void incrIndent() noexcept
Increment the indent level.
Definition Ostream.H:279
virtual Ostream & write(const std::string &str)=0
Write string content (usually quoted).
virtual Ostream & write(const char *data, std::streamsize count)=0
Write binary block.
virtual Ostream & write(const uint64_t val)=0
Write uint64_t.
unsigned short indentLevel_
Current indent level.
Definition Ostream.H:77
virtual Ostream & endEntry()
Write end entry (';') followed by newline.
Definition Ostream.C:117
A class for handling keywords in dictionaries.
Definition keyType.H:69
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
volScalarField & p
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
Ostream & beginBlock(Ostream &os)
Write begin block group without a name.
Definition Ostream.H:531
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition Ostream.H:490
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition IOstream.H:562
const direction noexcept
Definition scalarImpl.H:265
Ostream & endEntry(Ostream &os)
Write end entry (';') followed by newline.
Definition Ostream.H:553
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition Ostream.H:499
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Ostream &(* OstreamManip)(Ostream &)
An Ostream manipulator.
Definition Ostream.H:458
Ostream & flush(Ostream &os)
Flush stream.
Definition Ostream.H:509
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
Ostream & endBlock(Ostream &os)
Write end block group.
Definition Ostream.H:543
runTime write()
labelList f(nPoints)
Includes some common C++ headers, defines global macros and templates used in multiple places by Open...