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-2014 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  virtual Ostream& write(const keyType&);
113 
114  //- Write string
115  virtual Ostream& write(const string&) = 0;
116 
117  //- Write std::string surrounded by quotes.
118  // Optional write without quotes.
119  virtual Ostream& writeQuoted
120  (
121  const std::string&,
122  const bool quoted=true
123  ) = 0;
124 
125  //- Write int32_t
126  virtual Ostream& write(const int32_t) = 0;
127 
128  //- Write int64_t
129  virtual Ostream& write(const int64_t) = 0;
130 
131  //- Write floatScalar
132  virtual Ostream& write(const floatScalar) = 0;
133 
134  //- Write doubleScalar
135  virtual Ostream& write(const doubleScalar) = 0;
136 
137  //- Write binary block
138  virtual Ostream& write(const char*, std::streamsize) = 0;
139 
140  //- Add indentation characters
141  virtual void indent() = 0;
142 
143  //- Return indent level
144  unsigned short indentLevel() const
145  {
146  return indentLevel_;
147  }
148 
149  //- Access to indent level
150  unsigned short& indentLevel()
151  {
152  return indentLevel_;
153  }
154 
155  //- Incrememt the indent level
156  void incrIndent()
157  {
158  indentLevel_++;
159  }
160 
161  //- Decrememt the indent level
162  void decrIndent();
163 
164  //- Write the keyword followed by an appropriate indentation
165  Ostream& writeKeyword(const keyType&);
166 
167 
168  // Stream state functions
169 
170  //- Flush stream
171  virtual void flush() = 0;
172 
173  //- Add newline and flush stream
174  virtual void endl() = 0;
175 
176  //- Get width of output field
177  virtual int width() const = 0;
178 
179  //- Set width of output field (and return old width)
180  virtual int width(const int w) = 0;
181 
182  //- Get precision of output field
183  virtual int precision() const = 0;
184 
185  //- Set precision of output field (and return old precision)
186  virtual int precision(const int p) = 0;
187 
188 
189  // Member operators
190 
191  //- Return a non-const reference to const Ostream
192  // Needed for write functions where the stream argument is temporary:
193  // e.g. thing thisThing(OFstream("thingFileName")());
194  Ostream& operator()() const
195  {
196  return const_cast<Ostream&>(*this);
197  }
198 };
199 
200 
201 // --------------------------------------------------------------------
202 // ------ Manipulators (not taking arguments)
203 // --------------------------------------------------------------------
204 
205 typedef Ostream& (*OstreamManip)(Ostream&);
206 
207 //- operator<< handling for manipulators without arguments
209 {
210  return f(os);
211 }
212 
213 //- operator<< handling for manipulators without arguments
215 {
216  f(os);
217  return os;
218 }
219 
220 
221 //- Indent stream
222 inline Ostream& indent(Ostream& os)
223 {
224  os.indent();
225  return os;
226 }
227 
228 //- Increment the indent level
229 inline Ostream& incrIndent(Ostream& os)
230 {
231  os.incrIndent();
232  return os;
233 }
234 
235 //- Decrement the indent level
236 inline Ostream& decrIndent(Ostream& os)
237 {
238  os.decrIndent();
239  return os;
240 }
241 
242 
243 //- Flush stream
244 inline Ostream& flush(Ostream& os)
245 {
246  os.flush();
247  return os;
248 }
249 
250 
251 //- Add newline and flush stream
252 inline Ostream& endl(Ostream& os)
253 {
254  os.endl();
255  return os;
256 }
257 
258 
259 // Useful aliases for tab and newline characters
260 static const char tab = '\t';
261 static const char nl = '\n';
262 
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 } // End namespace Foam
267 
268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269 
270 #endif
271 
272 // ************************************************************************* //
Foam::IOstream::format
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
Foam::doubleScalar
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
Foam::Ostream::indentLevel
unsigned short & indentLevel()
Access to indent level.
Definition: Ostream.H:149
w
volScalarField w(IOobject("w", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE), mesh, dimensionedScalar("w", dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0))
p
p
Definition: pEqn.H:62
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::Ostream::operator()
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:193
Foam::IOstream::compressionType
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Foam::floatScalar
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
Foam::IOstream::compression
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:416
Foam::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:549
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:71
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::Ostream::entryIndentation_
static const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition: Ostream.H:66
Foam::IOstream::currentVersion
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
Foam::incrIndent
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:228
Foam::Ostream::precision
virtual int precision() const =0
Get precision of output field.
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:56
Foam::flush
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:243
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::Ostream::endl
virtual void endl()=0
Add newline and flush stream.
Foam::Ostream::write
virtual Ostream & write(const token &)=0
Write next token to stream.
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::IOstream::IOstream
IOstream(streamFormat format, versionNumber version, compressionType compression=UNCOMPRESSED)
Construct setting format and version.
Definition: IOstream.H:269
Foam::Ostream::indentSize_
static const unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:63
IOstream.H
Foam::OstreamManip
Ostream &(* OstreamManip)(Ostream &)
Definition: Ostream.H:204
Foam::Ostream::Ostream
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Ostream.H:78
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::Ostream::decrIndent
void decrIndent()
Decrememt the indent level.
Definition: Ostream.C:35
Foam::Ostream::~Ostream
virtual ~Ostream()
Destructor.
Definition: Ostream.H:90
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:235
Foam::Ostream::indentLevel
unsigned short indentLevel() const
Return indent level.
Definition: Ostream.H:143
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
Foam::Ostream::flush
virtual void flush()=0
Flush stream.
Foam::Ostream::indentLevel_
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:69
Foam::tab
static const char tab
Definition: Ostream.H:259
Foam::Ostream::incrIndent
void incrIndent()
Incrememt the indent level.
Definition: Ostream.H:155
Foam::IOstream::version
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
f
labelList f(nPoints)
Foam::Ostream::writeQuoted
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
Foam::Ostream::writeKeyword
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
Foam::Ostream::indent
virtual void indent()=0
Add indentation characters.
Foam::IOstream::UNCOMPRESSED
@ UNCOMPRESSED
Definition: IOstream.H:195
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
keyType.H
Foam::Ostream::width
virtual int width() const =0
Get width of output field.
Foam::IOstream::streamFormat
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86