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