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