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-2024 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 character
99  virtual Ostream& write(const char) = 0;
100 
101  //- Write character string
102  virtual Ostream& write(const char*) = 0;
103 
104  //- Write word
105  virtual Ostream& write(const word&) = 0;
106 
107  //- Write string
108  virtual Ostream& write(const string&) = 0;
109 
110  //- Write verbatimString
111  virtual Ostream& write(const verbatimString&) = 0;
112 
113  //- Write std::string surrounded by quotes.
114  // Optional write without quotes.
115  virtual Ostream& writeQuoted
116  (
117  const std::string&,
118  const bool quoted=true
119  ) = 0;
120 
121  //- Write int32_t
122  virtual Ostream& write(const int32_t) = 0;
123 
124  //- Write int64_t
125  virtual Ostream& write(const int64_t) = 0;
126 
127  //- Write uint32_t
128  virtual Ostream& write(const uint32_t) = 0;
129 
130  //- Write uint64_t
131  virtual Ostream& write(const uint64_t) = 0;
132 
133  //- Write floatScalar
134  virtual Ostream& write(const floatScalar) = 0;
135 
136  //- Write doubleScalar
137  virtual Ostream& write(const doubleScalar) = 0;
138 
139  //- Write longDoubleScalar
140  virtual Ostream& write(const longDoubleScalar) = 0;
141 
142  //- Write binary block
143  virtual Ostream& write(const char*, std::streamsize) = 0;
144 
145  //- Add indentation characters
146  virtual void indent() = 0;
147 
148  //- Return indent level
149  unsigned short indentLevel() const
150  {
151  return indentLevel_;
152  }
153 
154  //- Access to indent level
155  unsigned short& indentLevel()
156  {
157  return indentLevel_;
158  }
159 
160  //- Incrememt the indent level
161  void incrIndent()
162  {
163  indentLevel_++;
164  }
165 
166  //- Decrememt the indent level
167  void decrIndent();
168 
169  //- Write the keyword followed by an appropriate indentation.
170  // Here for backward compatibility, replaced by
171  // writeKeyword(Foam::Ostream& os, const keyType& kw);
172  Ostream& writeKeyword(const keyType&);
173 
174 
175  // Stream state functions
176 
177  //- Flush stream
178  virtual void flush() = 0;
179 
180  //- Add newline and flush stream
181  virtual void endl() = 0;
182 
183  //- Get width of output field
184  virtual int width() const = 0;
185 
186  //- Set width of output field (and return old width)
187  virtual int width(const int w) = 0;
188 
189  //- Get precision of output field
190  virtual int precision() const = 0;
191 
192  //- Set precision of output field (and return old precision)
193  virtual int precision(const int p) = 0;
194 
195 
196  // Member Operators
197 
198  //- Return a non-const reference to const Ostream
199  // Needed for write functions where the stream argument is temporary:
200  // e.g. thing thisThing(OFstream("thingFileName")());
201  Ostream& operator()() const
202  {
203  return const_cast<Ostream&>(*this);
204  }
205 };
206 
207 
208 // --------------------------------------------------------------------
209 // ------ Manipulators (not taking arguments)
210 // --------------------------------------------------------------------
211 
212 typedef Ostream& (*OstreamManip)(Ostream&);
213 
214 //- operator<< handling for manipulators without arguments
216 {
217  return f(os);
218 }
219 
220 //- operator<< handling for manipulators without arguments
222 {
223  f(os);
224  return os;
225 }
226 
227 
228 //- Indent stream
229 inline Ostream& indent(Ostream& os)
230 {
231  os.indent();
232  return os;
233 }
234 
235 //- Increment the indent level
236 inline Ostream& incrIndent(Ostream& os)
237 {
238  os.incrIndent();
239  return os;
240 }
241 
242 //- Decrement the indent level
243 inline Ostream& decrIndent(Ostream& os)
244 {
245  os.decrIndent();
246  return os;
247 }
248 
249 
250 //- Flush stream
251 inline Ostream& flush(Ostream& os)
252 {
253  os.flush();
254  return os;
255 }
256 
257 
258 //- Add newline and flush stream
259 inline Ostream& endl(Ostream& os)
260 {
261  os.endl();
262  return os;
263 }
264 
265 
266 // Useful aliases for tab and newline characters
267 static const char tab = '\t';
268 static const char nl = '\n';
269 
270 
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272 
273 } // End namespace Foam
274 
275 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 
277 #endif
278 
279 // ************************************************************************* //
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:44
virtual void flush()=0
Flush stream.
unsigned short indentLevel() const
Return indent level.
Definition: Ostream.H:148
virtual void indent()=0
Add indentation characters.
virtual Ostream & write(const char)=0
Write character.
virtual int precision() const =0
Get precision of output field.
void incrIndent()
Incrememt the indent level.
Definition: Ostream.H:160
virtual void endl()=0
Add newline and flush stream.
virtual int width() const =0
Get width of output field.
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:200
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:30
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 class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:242
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
static const char tab
Definition: Ostream.H:266
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:235
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:561
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
Ostream &(* OstreamManip)(Ostream &)
Definition: Ostream.H:211
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:228
static const char nl
Definition: Ostream.H:267
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:250
long double longDoubleScalar
Lang double precision floating point scalar type.
labelList f(nPoints)
volScalarField & p