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-2018 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 "keyType.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // Forward declaration of classes
48 class token;
49 
50 /*---------------------------------------------------------------------------*\
51  Class Ostream Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class Ostream
55 :
56  public IOstream
57 {
58 
59 protected:
60 
61  // Protected data
62 
63  //- Number of spaces per indent level
64  static const unsigned short indentSize_ = 4;
65 
66  //- Indentation of the entry from the start of the keyword
67  static const unsigned short entryIndentation_ = 16;
68 
69  //- Current indent level
70  unsigned short indentLevel_;
71 
72 
73 public:
74 
75  // Constructors
76 
77  //- Set stream status
79  (
83  )
84  :
86  indentLevel_(0)
87  {}
88 
89 
90  //- Destructor
91  virtual ~Ostream()
92  {}
93 
94 
95  // Member functions
96 
97  // Write functions
98 
99  //- Write next token to stream
100  virtual Ostream& write(const token&) = 0;
101 
102  //- Write character
103  virtual Ostream& write(const char) = 0;
104 
105  //- Write character string
106  virtual Ostream& write(const char*) = 0;
107 
108  //- Write word
109  virtual Ostream& write(const word&) = 0;
110 
111  //- Write keyType
112  // write regular expression as quoted string
113  // write plain word as word (unquoted)
114  virtual Ostream& write(const keyType&);
115 
116  //- Write string
117  virtual Ostream& write(const string&) = 0;
118 
119  //- Write std::string surrounded by quotes.
120  // Optional write without quotes.
121  virtual Ostream& writeQuoted
122  (
123  const std::string&,
124  const bool quoted=true
125  ) = 0;
126 
127  //- Write int32_t
128  virtual Ostream& write(const int32_t) = 0;
129 
130  //- Write int64_t
131  virtual Ostream& write(const int64_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  Ostream& writeKeyword(const keyType&);
171 
172 
173  // Stream state functions
174 
175  //- Flush stream
176  virtual void flush() = 0;
177 
178  //- Add newline and flush stream
179  virtual void endl() = 0;
180 
181  //- Get width of output field
182  virtual int width() const = 0;
183 
184  //- Set width of output field (and return old width)
185  virtual int width(const int w) = 0;
186 
187  //- Get precision of output field
188  virtual int precision() const = 0;
189 
190  //- Set precision of output field (and return old precision)
191  virtual int precision(const int p) = 0;
192 
193 
194  // Member operators
195 
196  //- Return a non-const reference to const Ostream
197  // Needed for write functions where the stream argument is temporary:
198  // e.g. thing thisThing(OFstream("thingFileName")());
199  Ostream& operator()() const
200  {
201  return const_cast<Ostream&>(*this);
202  }
203 };
204 
205 
206 // --------------------------------------------------------------------
207 // ------ Manipulators (not taking arguments)
208 // --------------------------------------------------------------------
210 typedef Ostream& (*OstreamManip)(Ostream&);
211 
212 //- operator<< handling for manipulators without arguments
214 {
215  return f(os);
216 }
217 
218 //- operator<< handling for manipulators without arguments
220 {
221  f(os);
222  return os;
223 }
224 
225 
226 //- Indent stream
227 inline Ostream& indent(Ostream& os)
228 {
229  os.indent();
230  return os;
231 }
232 
233 //- Increment the indent level
234 inline Ostream& incrIndent(Ostream& os)
235 {
236  os.incrIndent();
237  return os;
238 }
239 
240 //- Decrement the indent level
241 inline Ostream& decrIndent(Ostream& os)
242 {
243  os.decrIndent();
244  return os;
245 }
246 
247 
248 //- Flush stream
249 inline Ostream& flush(Ostream& os)
250 {
251  os.flush();
252  return os;
253 }
254 
255 
256 //- Add newline and flush stream
257 inline Ostream& endl(Ostream& os)
258 {
259  os.endl();
260  return os;
261 }
262 
263 
264 // Useful aliases for tab and newline characters
265 static const char tab = '\t';
266 static const char nl = '\n';
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 } // End namespace Foam
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 #endif
276 
277 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:69
virtual void flush()=0
Flush stream.
static const char tab
Definition: Ostream.H:264
virtual ~Ostream()
Destructor.
Definition: Ostream.H:90
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Ostream.H:78
A token holds items read from Istream.
Definition: token.H:69
Ostream &(* OstreamManip)(Ostream &)
Definition: Ostream.H:209
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:198
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
void decrIndent()
Decrememt the indent level.
Definition: Ostream.C:34
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
static const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition: Ostream.H:66
A class for handling words, derived from string.
Definition: word.H:59
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual void endl()=0
Add newline and flush stream.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
void incrIndent()
Incrememt the indent level.
Definition: Ostream.H:160
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual int width() const =0
Get width of output field.
static const char nl
Definition: Ostream.H:265
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
labelList f(nPoints)
long double longDoubleScalar
Lang double precision floating point scalar type.
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:416
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
Version number type.
Definition: IOstream.H:96
friend Ostream & operator<<(Ostream &os, const streamFormat &sf)
Ostream operator.
static const unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:63
virtual Ostream & write(const token &)=0
Write next token to stream.
volScalarField & p
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:549
virtual void indent()=0
Add indentation characters.
Namespace for OpenFOAM.
virtual int precision() const =0
Get precision of output field.
unsigned short indentLevel() const
Return indent level.
Definition: Ostream.H:148