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-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::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 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 
48 namespace Foam
49 {
50 
51 // Forward Declarations
52 class token;
53 
54 constexpr char tab = '\t';
55 constexpr char nl = '\n';
56 
57 /*---------------------------------------------------------------------------*\
58  Class Ostream Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class Ostream
62 :
63  public IOstream
64 {
65 protected:
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;
77 
78 
79 public:
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)
101  explicit Ostream
102  (
105  )
106  :
107  Ostream(IOstreamOption(fmt, 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 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;
129 
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;
139 
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 float
158  virtual Ostream& write(const float val) = 0;
159 
160  //- Write double
161  virtual Ostream& write(const double val) = 0;
162 
163  //- Write binary block.
164  virtual Ostream& write(const char* data, std::streamsize count) = 0;
165 
166  //- Low-level raw binary output.
167  virtual Ostream& writeRaw
168  (
169  const char* data,
170  std::streamsize count
171  ) = 0;
172 
173  //- Emit begin marker for low-level raw binary output.
174  // The count indicates the number of bytes for subsequent
175  // writeRaw calls.
176  virtual bool beginRawWrite(std::streamsize count) = 0;
177 
178  //- Emit end marker for low-level raw binary output.
179  virtual bool endRawWrite() = 0;
180 
181  //- Add indentation characters
182  virtual void indent() = 0;
183 
184  //- Return indent size (spaces per level)
185  unsigned short indentSize() const noexcept
186  {
187  return indentSize_;
188  }
189 
190  //- Change indent size (spaces per level), return old value
191  unsigned short indentSize(unsigned short val) noexcept
192  {
193  auto old(indentSize_);
194  indentSize_ = val;
195  return old;
196  }
197 
198  //- Return the indent level
199  unsigned short indentLevel() const noexcept
200  {
201  return indentLevel_;
202  }
203 
204  //- Change the indent level, return old value
205  unsigned short indentLevel(unsigned short val) noexcept
206  {
207  auto old(indentLevel_);
208  indentLevel_ = val;
209  return old;
210  }
211 
212  //- Increment the indent level
213  void incrIndent() noexcept
214  {
215  ++indentLevel_;
216  }
217 
218  //- Decrement the indent level
219  void decrIndent();
220 
221  //- Write the keyword followed by an appropriate indentation
222  virtual Ostream& writeKeyword(const keyType& kw);
223 
224  //- Write begin block group with the given name
225  // Increments indentation, adds newline.
226  virtual Ostream& beginBlock(const keyType& kw);
227 
228  //- Write begin block group without a name
229  // Increments indentation, adds newline.
230  virtual Ostream& beginBlock();
231 
232  //- Write end block group
233  // Decrements indentation, adds newline.
234  virtual Ostream& endBlock();
235 
236  //- Write end entry (';') followed by newline.
237  virtual Ostream& endEntry();
238 
239  //- Write a keyword/value entry.
240  // The following two are functionally equivalent:
241  // \code
242  // os.writeEntry(key, value);
243  //
244  // os.writeKeyword(key) << value << endEntry;
245  // \endcode
246  template<class T>
247  Ostream& writeEntry(const keyType& key, const T& value)
248  {
249  writeKeyword(key) << value;
250  return endEntry();
251  }
252 
253  //- Write a keyword/value entry only when the two values differ.
254  // \param key the name of the entry
255  // \param value1 the reference value
256  // \param value2 the value to write if it differs from value1
257  template<class T>
259  (
260  const word& key,
261  const T& value1,
262  const T& value2
263  )
264  {
265  if (value1 != value2)
266  {
267  writeEntry(key, value2);
268  }
270  return *this;
271  }
272 
273 
274  // Stream state functions
275 
276  //- Flush stream
277  virtual void flush() = 0;
278 
279  //- Add newline and flush stream
280  virtual void endl() = 0;
281 
282  //- Get padding character
283  virtual char fill() const = 0;
284 
285  //- Set padding character for formatted field up to field width
286  virtual char fill(const char fillch) = 0;
287 
288  //- Get width of output field
289  virtual int width() const = 0;
290 
291  //- Set width of output field (and return old width)
292  virtual int width(const int w) = 0;
293 
294  //- Get precision of output field
295  virtual int precision() const = 0;
296 
297  //- Set precision of output field (and return old precision)
298  virtual int precision(const int p) = 0;
299 
300 
301  // Member Operators
302 
303  //- Return a non-const reference to const Ostream
304  // Needed for write functions where the stream argument is temporary:
305  // e.g. thing thisThing(OFstream("thingFileName")());
306  Ostream& operator()() const
307  {
308  return const_cast<Ostream&>(*this);
309  }
310 
311 
312  // Housekeeping
313 
314  //- Access to indent level
315  unsigned short& indentLevel() noexcept
316  {
317  return indentLevel_;
318  }
319 
320  //- Access to indent size
321  unsigned short& indentSize() noexcept
322  {
323  return indentSize_;
324  }
325 };
326 
327 
328 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
329 
330 #if __cplusplus >= 201703L
331 //- Write operator for std::string_view
332 inline Ostream& operator<<(Ostream& os, std::string_view s)
333 {
334  os.writeQuoted(s.data(), s.size(), true); // quoted
335  os.check("Foam::operator<<(Ostream&, std::string_view)");
336  return os;
337 }
338 #endif
339 
340 //- Write operator for character span. Output like string data
341 inline Ostream& operator<<(Ostream& os, stdFoam::span<char> s)
342 {
343  os.writeQuoted(s.data(), s.size(), true); // quoted
344  os.check("Foam::operator<<(Ostream&, stdFoam::span<char>)");
345  return os;
346 }
347 
348 //- Write operator for const character span. Output like string data
349 inline Ostream& operator<<(Ostream& os, stdFoam::span<const char> s)
350 {
351  os.writeQuoted(s.data(), s.size(), true); // quoted
352  os.check("Foam::operator<<(Ostream&, stdFoam::span<const char>)");
353  return os;
354 }
355 
356 
357 /*---------------------------------------------------------------------------*\
358  Manipulators (without arguments)
359 \*---------------------------------------------------------------------------*/
360 
361 //- An Ostream manipulator
362 typedef Ostream& (*OstreamManip)(Ostream&);
363 
364 //- operator<< handling for manipulators without arguments
365 inline Ostream& operator<<(Ostream& os, OstreamManip f)
366 {
367  return f(os);
368 }
369 
370 //- operator<< handling for manipulators without arguments
371 inline Ostream& operator<<(Ostream& os, IOstreamManip f)
372 {
373  f(os);
374  return os;
375 }
376 
377 
378 //- Indent stream
379 inline Ostream& indent(Ostream& os)
380 {
381  os.indent();
382  return os;
383 }
384 
385 //- Increment the indent level
386 inline Ostream& incrIndent(Ostream& os)
387 {
388  os.incrIndent();
389  return os;
390 }
391 
392 //- Decrement the indent level
393 inline Ostream& decrIndent(Ostream& os)
394 {
395  os.decrIndent();
396  return os;
397 }
398 
399 
400 //- Flush stream
401 inline Ostream& flush(Ostream& os)
402 {
403  os.flush();
404  return os;
405 }
406 
407 
408 //- Add newline and flush stream
409 inline Ostream& endl(Ostream& os)
410 {
411  os.endl();
412  return os;
413 }
414 
415 
416 //- Write begin block group without a name
417 // Increments indentation, adds newline.
418 inline Ostream& beginBlock(Ostream& os)
419 {
420  os.beginBlock();
421  return os;
422 }
423 
424 
425 //- Write end block group
426 // Decrements indentation, adds newline.
427 inline Ostream& endBlock(Ostream& os)
428 {
429  os.endBlock();
430  return os;
431 }
432 
433 
434 //- Write end entry (';') followed by newline.
435 inline Ostream& endEntry(Ostream& os)
436 {
437  os.endEntry();
438  return os;
439 }
440 
441 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
442 
443 } // End namespace Foam
444 
445 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
446 
447 #endif
448 
449 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:77
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
virtual void flush()=0
Flush stream.
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
virtual Ostream & writeRaw(const char *data, std::streamsize count)=0
Low-level raw binary output.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual Ostream & beginBlock()
Write begin block group without a name.
Definition: Ostream.C:99
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Ostream &(* OstreamManip)(Ostream &)
An Ostream manipulator.
Definition: Ostream.H:470
virtual bool endRawWrite()=0
Emit end marker for low-level raw binary output.
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:402
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:321
Ostream & endEntry(Ostream &os)
Write end entry (&#39;;&#39;) followed by newline.
Definition: Ostream.H:565
A simple container for options an IOstream can normally have.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
void decrIndent()
Decrement the indent level.
Definition: Ostream.C:30
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
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition: OBJstream.C:78
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:108
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
virtual ~Ostream()=default
Destructor.
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:336
virtual void endl()=0
Add newline and flush stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual int width() const =0
Get width of output field.
const direction noexcept
Definition: Scalar.H:258
Ostream(IOstreamOption streamOpt=IOstreamOption())
Default construct (ASCII, uncompressed), construct with specified stream option.
Definition: Ostream.H:101
virtual bool beginRawWrite(std::streamsize count)=0
Emit begin marker for low-level raw binary output.
OBJstream os(runTime.globalPath()/outputName)
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:511
labelList f(nPoints)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
virtual char fill() const =0
Get padding character.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
virtual void indent() override
Add indentation characters.
Definition: OSstream.C:285
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:521
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
const dimensionedScalar c
Speed of light in a vacuum.
Ostream & endBlock(Ostream &os)
Write end block group.
Definition: Ostream.H:555
streamFormat
Data format (ascii | binary)
unsigned short indentSize() const noexcept
Return indent size (spaces per level)
Definition: Ostream.H:233
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:90
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
virtual void flush() override
Flush stream.
Definition: OSstream.C:295
unsigned short indentLevel() const noexcept
Return the indent level.
Definition: Ostream.H:251
Ostream & beginBlock(Ostream &os)
Write begin block group without a name.
Definition: Ostream.H:543
unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:72
volScalarField & p
virtual Ostream & endEntry()
Write end entry (&#39;;&#39;) followed by newline.
Definition: Ostream.C:117
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))
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:502
Ostream(const Ostream &)=default
Copy construct.
static constexpr const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition: Ostream.H:67
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:528
virtual void indent()=0
Add indentation characters.
void incrIndent() noexcept
Increment the indent level.
Definition: Ostream.H:269
Namespace for OpenFOAM.
virtual int precision() const =0
Get precision of output field.
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:60