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-2020 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  (
99  streamFormat fmt,
101  compressionType comp = compressionType::UNCOMPRESSED
102  )
103  :
104  Ostream(IOstreamOption(fmt, comp, ver))
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 level
175  unsigned short indentSize() const
176  {
177  return indentSize_;
178  }
179 
180  //- Access to indent size
181  unsigned short& indentSize()
182  {
183  return indentSize_;
184  }
185 
186  //- Return indent level
187  unsigned short indentLevel() const
188  {
189  return indentLevel_;
190  }
191 
192  //- Access to indent level
193  unsigned short& indentLevel()
194  {
195  return indentLevel_;
196  }
197 
198  //- Increment the indent level
199  void incrIndent()
200  {
201  ++indentLevel_;
202  }
203 
204  //- Decrement the indent level
205  void decrIndent();
206 
207  //- Write the keyword followed by an appropriate indentation
208  virtual Ostream& writeKeyword(const keyType& kw);
209 
210  //- Write begin block group with the given name
211  // Increments indentation, adds newline.
212  virtual Ostream& beginBlock(const keyType& kw);
213 
214  //- Write begin block group without a name
215  // Increments indentation, adds newline.
216  virtual Ostream& beginBlock();
217 
218  //- Write end block group
219  // Decrements indentation, adds newline.
220  virtual Ostream& endBlock();
221 
222  //- Write end entry (';') followed by newline.
223  virtual Ostream& endEntry();
224 
225  //- Write a keyword/value entry.
226  // The following two are functionally equivalent:
227  // \code
228  // os.writeEntry(key, value);
229  //
230  // os.writeKeyword(key) << value << endEntry;
231  // \endcode
232  template<class T>
233  Ostream& writeEntry(const keyType& key, const T& value)
234  {
235  writeKeyword(key) << value;
236  return endEntry();
237  }
238 
239  //- Write a keyword/value entry only when the two values differ.
240  // \param key the name of the entry
241  // \param value1 the reference value
242  // \param value2 the value to write if it differs from value1
243  template<class T>
245  (
246  const word& key,
247  const T& value1,
248  const T& value2
249  )
250  {
251  if (value1 != value2)
252  {
253  writeEntry(key, value2);
254  }
255 
256  return *this;
257  }
258 
259 
260  // Stream state functions
261 
262  //- Flush stream
263  virtual void flush() = 0;
264 
265  //- Add newline and flush stream
266  virtual void endl() = 0;
267 
268  //- Get padding character
269  virtual char fill() const = 0;
270 
271  //- Set padding character for formatted field up to field width
272  virtual char fill(const char fillch) = 0;
273 
274  //- Get width of output field
275  virtual int width() const = 0;
276 
277  //- Set width of output field (and return old width)
278  virtual int width(const int w) = 0;
279 
280  //- Get precision of output field
281  virtual int precision() const = 0;
282 
283  //- Set precision of output field (and return old precision)
284  virtual int precision(const int p) = 0;
285 
286 
287  // Member Operators
288 
289  //- Return a non-const reference to const Ostream
290  // Needed for write functions where the stream argument is temporary:
291  // e.g. thing thisThing(OFstream("thingFileName")());
292  Ostream& operator()() const
293  {
294  return const_cast<Ostream&>(*this);
295  }
296 };
297 
298 
299 // --------------------------------------------------------------------
300 // ------ Manipulators (not taking arguments)
301 // --------------------------------------------------------------------
302 
303 //- An Ostream manipulator
304 typedef Ostream& (*OstreamManip)(Ostream&);
305 
306 //- operator<< handling for manipulators without arguments
308 {
309  return f(os);
310 }
311 
312 //- operator<< handling for manipulators without arguments
314 {
315  f(os);
316  return os;
317 }
318 
319 
320 //- Indent stream
321 inline Ostream& indent(Ostream& os)
322 {
323  os.indent();
324  return os;
325 }
326 
327 //- Increment the indent level
328 inline Ostream& incrIndent(Ostream& os)
329 {
330  os.incrIndent();
331  return os;
332 }
333 
334 //- Decrement the indent level
335 inline Ostream& decrIndent(Ostream& os)
336 {
337  os.decrIndent();
338  return os;
339 }
340 
341 
342 //- Flush stream
343 inline Ostream& flush(Ostream& os)
344 {
345  os.flush();
346  return os;
347 }
348 
349 
350 //- Add newline and flush stream
351 inline Ostream& endl(Ostream& os)
352 {
353  os.endl();
354  return os;
355 }
356 
357 
358 //- Write begin block group without a name
359 // Increments indentation, adds newline.
360 inline Ostream& beginBlock(Ostream& os)
361 {
362  os.beginBlock();
363  return os;
364 }
365 
366 
367 //- Write end block group
368 // Decrements indentation, adds newline.
369 inline Ostream& endBlock(Ostream& os)
370 {
371  os.endBlock();
372  return os;
373 }
374 
375 
376 //- Write end entry (';') followed by newline.
377 inline Ostream& endEntry(Ostream& os)
378 {
379  os.endEntry();
380  return os;
381 }
382 
383 
384 // Useful aliases for tab and newline characters
385 constexpr char tab = '\t';
386 constexpr char nl = '\n';
387 
388 
389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
390 
391 } // End namespace Foam
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 #endif
396 
397 // ************************************************************************* //
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:244
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Ostream::indentLevel
unsigned short & indentLevel()
Access to indent level.
Definition: Ostream.H:192
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::Ostream::operator()
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:291
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:422
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
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:350
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:327
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:60
Foam::flush
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:342
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:303
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::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:368
Foam::Ostream::indentSize
unsigned short indentSize() const
Return indent level.
Definition: Ostream.H:174
Foam::beginBlock
Ostream & beginBlock(Ostream &os)
Write begin block group without a name.
Definition: Ostream.H:359
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:334
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
Foam::Ostream::indentLevel
unsigned short indentLevel() const
Return indent level.
Definition: Ostream.H:186
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:320
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::tab
constexpr char tab
Definition: Ostream.H:384
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Ostream::incrIndent
void incrIndent()
Increment the indent level.
Definition: Ostream.H:198
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:376
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
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::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
Foam::Ostream::indentSize
unsigned short & indentSize()
Access to indent size.
Definition: Ostream.H:180
keyType.H
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::Ostream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)=0
Low-level raw binary output.
Foam::Ostream::width
virtual int width() const =0
Get width of output field.