Ostream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Ostream
26 
27 Description
28  An Ostream is an abstract base class for all output systems
29  (streams, files, token lists, etc).
30 
31 SourceFiles
32  Ostream.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef Ostream_H
37 #define Ostream_H
38 
39 #include "IOstream.H"
40 #include "verbatimString.H"
41 #include "keyType.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward declaration of classes
49 class token;
50 
51 /*---------------------------------------------------------------------------*\
52  Class Ostream Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class Ostream
56 :
57  public IOstream
58 {
59 
60 protected:
61 
62  // Protected data
63 
64  //- Number of spaces per indent level
65  static const unsigned short indentSize_ = 4;
66 
67  //- Current indent level
68  unsigned short indentLevel_;
69 
70 
71 public:
72 
73  // Constructors
74 
75  //- Set stream status
76  Ostream
77  (
78  const streamFormat format = ASCII,
81  const bool global = false
82  )
83  :
85  indentLevel_(0)
86  {}
87 
88 
89  //- Destructor
90  virtual ~Ostream()
91  {}
92 
93 
94  // Member Functions
95 
96  // Write functions
97 
98  //- Write token
99  virtual Ostream& write(const token&);
100 
101  //- Write the compound token tag if the name is a compound token
102  virtual Ostream& writeCompoundTag(const word& typeName);
103 
104  //- Write character
105  virtual Ostream& write(const char) = 0;
106 
107  //- Write character string
108  virtual Ostream& write(const char*) = 0;
109 
110  //- Write word
111  virtual Ostream& write(const word&) = 0;
112 
113  //- Write string
114  virtual Ostream& write(const string&) = 0;
115 
116  //- Write keyType
117  virtual Ostream& write(const keyType&) = 0;
118 
119  //- Write verbatimString
120  virtual Ostream& write(const verbatimString&) = 0;
121 
122  //- Write std::string surrounded by quotes.
123  // Optional write without quotes.
124  virtual Ostream& writeQuoted
125  (
126  const std::string&,
127  const bool quoted=true
128  ) = 0;
129 
130  //- Write int32_t
131  virtual Ostream& write(const int32_t) = 0;
132 
133  //- Write int64_t
134  virtual Ostream& write(const int64_t) = 0;
135 
136  //- Write uint32_t
137  virtual Ostream& write(const uint32_t) = 0;
138 
139  //- Write uint64_t
140  virtual Ostream& write(const uint64_t) = 0;
141 
142  //- Write floatScalar
143  virtual Ostream& write(const floatScalar) = 0;
144 
145  //- Write doubleScalar
146  virtual Ostream& write(const doubleScalar) = 0;
147 
148  //- Write longDoubleScalar
149  virtual Ostream& write(const longDoubleScalar) = 0;
150 
151  //- Write binary block
152  virtual Ostream& write(const char*, std::streamsize) = 0;
153 
154  //- Add indentation characters
155  virtual void indent() = 0;
156 
157  //- Return indent level
158  unsigned short indentLevel() const
159  {
160  return indentLevel_;
161  }
162 
163  //- Access to indent level
164  unsigned short& indentLevel()
165  {
166  return indentLevel_;
167  }
168 
169  //- Return the number of characters in an indent
170  unsigned short indentSize() const
171  {
172  return indentLevel_*indentSize_;
173  }
174 
175  //- Incrememt the indent level
176  void incrIndent()
177  {
178  indentLevel_++;
179  }
180 
181  //- Decrememt the indent level
182  void decrIndent();
183 
184  //- Write the keyword followed by an appropriate indentation.
185  // Here for backward compatibility, replaced by
186  // writeKeyword(Foam::Ostream& os, const keyType& kw);
187  Ostream& writeKeyword(const keyType&);
188 
189 
190  // Stream state functions
191 
192  //- Flush stream
193  virtual void flush() = 0;
194 
195  //- Add newline and flush stream
196  virtual void endl() = 0;
197 
198  //- Get width of output field
199  virtual int width() const = 0;
200 
201  //- Set width of output field (and return old width)
202  virtual int width(const int w) = 0;
203 
204  //- Get precision of output field
205  virtual int precision() const = 0;
206 
207  //- Set precision of output field (and return old precision)
208  virtual int precision(const int p) = 0;
209 
210 
211  // Member Operators
212 
213  //- Return a non-const reference to const Ostream
214  // Needed for write functions where the stream argument is temporary:
215  // e.g. thing thisThing(OFstream("thingFileName")());
216  Ostream& operator()() const
217  {
218  return const_cast<Ostream&>(*this);
219  }
220 };
221 
222 
223 // --------------------------------------------------------------------
224 // ------ Manipulators (not taking arguments)
225 // --------------------------------------------------------------------
226 
227 typedef Ostream& (*OstreamManip)(Ostream&);
228 
229 //- operator<< handling for manipulators without arguments
231 {
232  return f(os);
233 }
234 
235 //- operator<< handling for manipulators without arguments
237 {
238  f(os);
239  return os;
240 }
241 
242 
243 //- Indent stream
244 inline Ostream& indent(Ostream& os)
245 {
246  os.indent();
247  return os;
248 }
249 
250 //- Indent stream or add newline if indent level == 0
251 inline Ostream& indentOrNl(Ostream& os)
252 {
253  if (os.indentLevel() == 0)
254  {
255  os.endl();
256  }
257  else
258  {
259  os.indent();
260  }
261 
262  return os;
263 }
264 
265 //- Increment the indent level
266 inline Ostream& incrIndent(Ostream& os)
267 {
268  os.incrIndent();
269  return os;
270 }
271 
272 //- Decrement the indent level
273 inline Ostream& decrIndent(Ostream& os)
274 {
275  os.decrIndent();
276  return os;
277 }
278 
279 
280 //- Flush stream
281 inline Ostream& flush(Ostream& os)
282 {
283  os.flush();
284  return os;
285 }
286 
287 
288 //- Add newline and flush stream
289 inline Ostream& endl(Ostream& os)
290 {
291  os.endl();
292  return os;
293 }
294 
295 
296 // Useful aliases for tab and newline characters
297 static const char tab = '\t';
298 static const char nl = '\n';
299 
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 } // End namespace Foam
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 #endif
308 
309 // ************************************************************************* //
Version number type.
Definition: IOstream.H:97
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
bool global() const
Return global state.
Definition: IOstream.H:438
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:416
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
static const unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:64
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:45
virtual void flush()=0
Flush stream.
unsigned short indentSize() const
Return the number of characters in an indent.
Definition: Ostream.H:169
virtual Ostream & write(const token &)
Write token.
Definition: Ostream.C:51
unsigned short indentLevel() const
Return indent level.
Definition: Ostream.H:157
virtual void indent()=0
Add indentation characters.
virtual int precision() const =0
Get precision of output field.
void incrIndent()
Incrememt the indent level.
Definition: Ostream.H:175
virtual void endl()=0
Add newline and flush stream.
virtual int width() const =0
Get width of output field.
virtual Ostream & writeCompoundTag(const word &typeName)
Write the compound token tag if the name is a compound token.
Definition: Ostream.C:135
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:215
Ostream(const streamFormat format=ASCII, const versionNumber version=currentVersion, const compressionType compression=UNCOMPRESSED, const bool global=false)
Set stream status.
Definition: Ostream.H:76
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
void decrIndent()
Decrememt the indent level.
Definition: Ostream.C:31
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:67
virtual ~Ostream()
Destructor.
Definition: Ostream.H:89
A class for handling keywords in dictionaries.
Definition: keyType.H:69
A token holds items read from Istream.
Definition: token.H:74
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:63
Namespace for OpenFOAM.
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:272
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
static const char tab
Definition: Ostream.H:296
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:265
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:570
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
Ostream & indentOrNl(Ostream &os)
Indent stream or add newline if indent level == 0.
Definition: Ostream.H:250
Ostream &(* OstreamManip)(Ostream &)
Definition: Ostream.H:226
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
static const char nl
Definition: Ostream.H:297
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:280
long double longDoubleScalar
Lang double precision floating point scalar type.
labelList f(nPoints)
volScalarField & p