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