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-2019 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  // Constructors
78 
79  //- Construct and set stream status
81  (
85  )
86  :
88  {}
89 
90 
91  //- Destructor
92  virtual ~Ostream() = default;
93 
94 
95  // Member Functions
96 
97  // Write Functions
98 
99  //- Write token to stream or otherwise handle it.
100  // \return false if the token type was not handled by this method
101  virtual bool write(const token& tok) = 0;
102 
103  //- Write character
104  virtual Ostream& write(const char c) = 0;
105 
106  //- Write character string
107  virtual Ostream& write(const char* str) = 0;
108 
109  //- Write word
110  virtual Ostream& write(const word& str) = 0;
111 
112  //- Write keyType
113  // A plain word is written unquoted.
114  // A regular expression is written as a quoted string.
115  virtual Ostream& write(const keyType& kw);
116 
117  //- Write string
118  virtual Ostream& write(const string& str) = 0;
119 
120  //- Write std::string surrounded by quotes.
121  // Optional write without quotes.
122  virtual Ostream& writeQuoted
123  (
124  const std::string& str,
125  const bool quoted=true
126  ) = 0;
127 
128  //- Write int32_t
129  virtual Ostream& write(const int32_t val) = 0;
130 
131  //- Write int64_t
132  virtual Ostream& write(const int64_t val) = 0;
133 
134  //- Write floatScalar
135  virtual Ostream& write(const floatScalar val) = 0;
136 
137  //- Write doubleScalar
138  virtual Ostream& write(const doubleScalar val) = 0;
139 
140  //- Write binary block.
141  virtual Ostream& write(const char* data, std::streamsize count) = 0;
142 
143  //- Low-level raw binary output.
144  virtual Ostream& writeRaw
145  (
146  const char* data,
147  std::streamsize count
148  ) = 0;
149 
150  //- Emit begin marker for low-level raw binary output.
151  // The count indicates the number of bytes for subsequent
152  // writeRaw calls.
153  virtual bool beginRawWrite(std::streamsize count) = 0;
154 
155  //- Emit end marker for low-level raw binary output.
156  virtual bool endRawWrite() = 0;
157 
158  //- Add indentation characters
159  virtual void indent() = 0;
160 
161  //- Return indent level
162  unsigned short indentSize() const
163  {
164  return indentSize_;
165  }
166 
167  //- Access to indent size
168  unsigned short& indentSize()
169  {
170  return indentSize_;
171  }
172 
173  //- Return indent level
174  unsigned short indentLevel() const
175  {
176  return indentLevel_;
177  }
178 
179  //- Access to indent level
180  unsigned short& indentLevel()
181  {
182  return indentLevel_;
183  }
184 
185  //- Increment the indent level
186  void incrIndent()
187  {
188  ++indentLevel_;
189  }
190 
191  //- Decrement the indent level
192  void decrIndent();
193 
194  //- Write the keyword followed by an appropriate indentation
195  virtual Ostream& writeKeyword(const keyType& kw);
196 
197  //- Write begin block group with the given name
198  // Increments indentation, adds newline.
199  virtual Ostream& beginBlock(const keyType& kw);
200 
201  //- Write begin block group without a name
202  // Increments indentation, adds newline.
203  virtual Ostream& beginBlock();
204 
205  //- Write end block group
206  // Decrements indentation, adds newline.
207  virtual Ostream& endBlock();
208 
209  //- Write end entry (';') followed by newline.
210  virtual Ostream& endEntry();
211 
212  //- Write a keyword/value entry.
213  // The following two are functionally equivalent:
214  // \code
215  // os.writeEntry(key, value);
216  //
217  // os.writeKeyword(key) << value << endEntry;
218  // \endcode
219  template<class T>
220  Ostream& writeEntry(const keyType& key, const T& value)
221  {
222  writeKeyword(key) << value;
223  return endEntry();
224  }
225 
226  //- Write a keyword/value entry only when the two values differ.
227  // \param key the name of the entry
228  // \param value1 the reference value
229  // \param value2 the value to write if it differs from value1
230  template<class T>
232  (
233  const word& key,
234  const T& value1,
235  const T& value2
236  )
237  {
238  if (value1 != value2)
239  {
240  writeEntry(key, value2);
241  }
242 
243  return *this;
244  }
245 
246 
247  // Stream state functions
248 
249  //- Flush stream
250  virtual void flush() = 0;
251 
252  //- Add newline and flush stream
253  virtual void endl() = 0;
254 
255  //- Get padding character
256  virtual char fill() const = 0;
257 
258  //- Set padding character for formatted field up to field width
259  virtual char fill(const char fillch) = 0;
260 
261  //- Get width of output field
262  virtual int width() const = 0;
263 
264  //- Set width of output field (and return old width)
265  virtual int width(const int w) = 0;
266 
267  //- Get precision of output field
268  virtual int precision() const = 0;
269 
270  //- Set precision of output field (and return old precision)
271  virtual int precision(const int p) = 0;
272 
273 
274  // Member operators
275 
276  //- Return a non-const reference to const Ostream
277  // Needed for write functions where the stream argument is temporary:
278  // e.g. thing thisThing(OFstream("thingFileName")());
279  Ostream& operator()() const
280  {
281  return const_cast<Ostream&>(*this);
282  }
283 };
284 
285 
286 // --------------------------------------------------------------------
287 // ------ Manipulators (not taking arguments)
288 // --------------------------------------------------------------------
289 
290 //- An Ostream manipulator
291 typedef Ostream& (*OstreamManip)(Ostream&);
292 
293 //- operator<< handling for manipulators without arguments
295 {
296  return f(os);
297 }
298 
299 //- operator<< handling for manipulators without arguments
301 {
302  f(os);
303  return os;
304 }
305 
306 
307 //- Indent stream
308 inline Ostream& indent(Ostream& os)
309 {
310  os.indent();
311  return os;
312 }
313 
314 //- Increment the indent level
315 inline Ostream& incrIndent(Ostream& os)
316 {
317  os.incrIndent();
318  return os;
319 }
320 
321 //- Decrement the indent level
322 inline Ostream& decrIndent(Ostream& os)
323 {
324  os.decrIndent();
325  return os;
326 }
327 
328 
329 //- Flush stream
330 inline Ostream& flush(Ostream& os)
331 {
332  os.flush();
333  return os;
334 }
335 
336 
337 //- Add newline and flush stream
338 inline Ostream& endl(Ostream& os)
339 {
340  os.endl();
341  return os;
342 }
343 
344 
345 //- Write begin block group without a name
346 // Increments indentation, adds newline.
347 inline Ostream& beginBlock(Ostream& os)
348 {
349  os.beginBlock();
350  return os;
351 }
352 
353 
354 //- Write end block group
355 // Decrements indentation, adds newline.
356 inline Ostream& endBlock(Ostream& os)
357 {
358  os.endBlock();
359  return os;
360 }
361 
362 
363 //- Write end entry (';') followed by newline.
364 inline Ostream& endEntry(Ostream& os)
365 {
366  os.endEntry();
367  return os;
368 }
369 
370 
371 // Useful aliases for tab and newline characters
372 constexpr char tab = '\t';
373 constexpr char nl = '\n';
374 
375 
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
377 
378 } // End namespace Foam
379 
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 #endif
383 
384 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:73
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:231
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::doubleScalar
double doubleScalar
Floating-point double precision scalar type.
Definition: doubleScalar.H:52
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Ostream::indentLevel
unsigned short & indentLevel()
Access to indent level.
Definition: Ostream.H:179
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Ostream::operator()
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:278
Foam::Ostream::~Ostream
virtual ~Ostream()=default
Destructor.
Foam::floatScalar
float floatScalar
Floating-point single precision scalar type.
Definition: floatScalar.H:52
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:414
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number.
Definition: IOstreamOption.H:193
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:337
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
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:314
Foam::Ostream::precision
virtual int precision() const =0
Get precision of output field.
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::flush
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:329
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:79
Foam::Ostream::endl
virtual void endl()=0
Add newline and flush stream.
Foam::IOstream::IOstream
IOstream(const IOstreamOption option)
Construct with specified stream option.
Definition: IOstream.H:152
IOstream.H
Foam::OstreamManip
Ostream &(* OstreamManip)(Ostream &)
An Ostream manipulator.
Definition: Ostream.H:290
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::Ostream::endEntry
virtual Ostream & endEntry()
Write end entry (';') followed by newline.
Definition: Ostream.C:118
Foam::Ostream::Ostream
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct and set stream status.
Definition: Ostream.H:80
Foam::Ostream::decrIndent
void decrIndent()
Decrement the indent level.
Definition: Ostream.C:37
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
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:64
Foam::endBlock
Ostream & endBlock(Ostream &os)
Write end block group.
Definition: Ostream.H:355
Foam::Ostream::indentSize
unsigned short indentSize() const
Return indent level.
Definition: Ostream.H:161
Foam::beginBlock
Ostream & beginBlock(Ostream &os)
Write begin block group without a name.
Definition: Ostream.H:346
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:321
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:173
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:307
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::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::tab
constexpr char tab
Definition: Ostream.H:371
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::Ostream::incrIndent
void incrIndent()
Increment the indent level.
Definition: Ostream.H:185
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:74
Foam::endEntry
Ostream & endEntry(Ostream &os)
Write end entry (';') followed by newline.
Definition: Ostream.H:363
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:219
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::fill
virtual char fill() const =0
Get padding character.
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:297
Foam::Ostream::indentSize
unsigned short & indentSize()
Access to indent size.
Definition: Ostream.H:167
keyType.H
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
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.