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 | Copyright (C) 2011-2016 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 binary block
140  virtual Ostream& write(const char*, std::streamsize) = 0;
141 
142  //- Add indentation characters
143  virtual void indent() = 0;
144 
145  //- Return indent level
146  unsigned short indentLevel() const
147  {
148  return indentLevel_;
149  }
150 
151  //- Access to indent level
152  unsigned short& indentLevel()
153  {
154  return indentLevel_;
155  }
156 
157  //- Incrememt the indent level
158  void incrIndent()
159  {
160  indentLevel_++;
161  }
162 
163  //- Decrememt the indent level
164  void decrIndent();
165 
166  //- Write the keyword followed by an appropriate indentation
167  Ostream& writeKeyword(const keyType&);
168 
169 
170  // Stream state functions
171 
172  //- Flush stream
173  virtual void flush() = 0;
174 
175  //- Add newline and flush stream
176  virtual void endl() = 0;
177 
178  //- Get width of output field
179  virtual int width() const = 0;
180 
181  //- Set width of output field (and return old width)
182  virtual int width(const int w) = 0;
183 
184  //- Get precision of output field
185  virtual int precision() const = 0;
186 
187  //- Set precision of output field (and return old precision)
188  virtual int precision(const int p) = 0;
189 
190 
191  // Member operators
192 
193  //- Return a non-const reference to const Ostream
194  // Needed for write functions where the stream argument is temporary:
195  // e.g. thing thisThing(OFstream("thingFileName")());
196  Ostream& operator()() const
197  {
198  return const_cast<Ostream&>(*this);
199  }
200 };
201 
202 
203 // --------------------------------------------------------------------
204 // ------ Manipulators (not taking arguments)
205 // --------------------------------------------------------------------
207 typedef Ostream& (*OstreamManip)(Ostream&);
208 
209 //- operator<< handling for manipulators without arguments
211 {
212  return f(os);
213 }
214 
215 //- operator<< handling for manipulators without arguments
217 {
218  f(os);
219  return os;
220 }
221 
222 
223 //- Indent stream
224 inline Ostream& indent(Ostream& os)
225 {
226  os.indent();
227  return os;
228 }
229 
230 //- Increment the indent level
231 inline Ostream& incrIndent(Ostream& os)
232 {
233  os.incrIndent();
234  return os;
235 }
236 
237 //- Decrement the indent level
238 inline Ostream& decrIndent(Ostream& os)
239 {
240  os.decrIndent();
241  return os;
242 }
243 
244 
245 //- Flush stream
246 inline Ostream& flush(Ostream& os)
247 {
248  os.flush();
249  return os;
250 }
251 
252 
253 //- Add newline and flush stream
254 inline Ostream& endl(Ostream& os)
255 {
256  os.endl();
257  return os;
258 }
259 
260 
261 // Useful aliases for tab and newline characters
262 static const char tab = '\t';
263 static const char nl = '\n';
264 
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 } // End namespace Foam
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #endif
273 
274 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:69
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual void flush()=0
Flush stream.
static const char tab
Definition: Ostream.H:261
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:206
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:49
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:49
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
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:157
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:262
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
labelList f(nPoints)
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:195
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
unsigned short indentLevel() const
Return indent level.
Definition: Ostream.H:145
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
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:416
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.