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-2019 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
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 floatScalar
127  virtual Ostream& write(const floatScalar) = 0;
128 
129  //- Write doubleScalar
130  virtual Ostream& write(const doubleScalar) = 0;
131 
132  //- Write longDoubleScalar
133  virtual Ostream& write(const longDoubleScalar) = 0;
134 
135  //- Write binary block
136  virtual Ostream& write(const char*, std::streamsize) = 0;
137 
138  //- Add indentation characters
139  virtual void indent() = 0;
140 
141  //- Return indent level
142  unsigned short indentLevel() const
143  {
144  return indentLevel_;
145  }
146 
147  //- Access to indent level
148  unsigned short& indentLevel()
149  {
150  return indentLevel_;
151  }
152 
153  //- Incrememt the indent level
154  void incrIndent()
155  {
156  indentLevel_++;
157  }
158 
159  //- Decrememt the indent level
160  void decrIndent();
161 
162  //- Write the keyword followed by an appropriate indentation.
163  // Here for backward compatibility, replaced by
164  // writeKeyword(Foam::Ostream& os, const keyType& kw);
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 // --------------------------------------------------------------------
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 // ************************************************************************* //
A class for handling verbatimStrings, derived from string.
A class for handling keywords in dictionaries.
Definition: keyType.H:66
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:67
virtual Ostream & write(const char)=0
Write character.
virtual void flush()=0
Flush stream.
static const char tab
Definition: Ostream.H:259
virtual ~Ostream()
Destructor.
Definition: Ostream.H:88
Ostream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Ostream.H:76
Ostream &(* OstreamManip)(Ostream &)
Definition: Ostream.H:204
Ostream & operator()() const
Return a non-const reference to const Ostream.
Definition: Ostream.H:193
versionNumber version() const
Return the stream version.
Definition: IOstream.H:396
void decrIndent()
Decrememt the indent level.
Definition: Ostream.C:30
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
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:374
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:153
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
virtual int width() const =0
Get width of output field.
static const char nl
Definition: Ostream.H:260
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:44
labelList f(nPoints)
long double longDoubleScalar
Lang double precision floating point scalar type.
compressionType compression() const
Return the stream compression.
Definition: IOstream.H:413
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:203
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:64
volScalarField & p
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.H:546
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:141