FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/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
78  Ostream
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 label
126  virtual Ostream& write(const label) = 0;
127 
128  //- Write floatScalar
129  virtual Ostream& write(const floatScalar) = 0;
130 
131  //- Write doubleScalar
132  virtual Ostream& write(const doubleScalar) = 0;
133 
134  //- Write binary block
135  virtual Ostream& write(const char*, std::streamsize) = 0;
136 
137  //- Add indentation characters
138  virtual void indent() = 0;
139 
140  //- Return indent level
141  unsigned short indentLevel() const
142  {
143  return indentLevel_;
144  }
145 
146  //- Access to indent level
147  unsigned short& indentLevel()
148  {
149  return indentLevel_;
150  }
151 
152  //- Incrememt the indent level
153  void incrIndent()
154  {
155  indentLevel_++;
156  }
157 
158  //- Decrememt the indent level
159  void decrIndent();
160 
161  //- Write the keyword followed by an appropriate indentation
162  Ostream& writeKeyword(const keyType&);
163 
164 
165  // Stream state functions
166 
167  //- Flush stream
168  virtual void flush() = 0;
169 
170  //- Add newline and flush stream
171  virtual void endl() = 0;
172 
173  //- Get width of output field
174  virtual int width() const = 0;
175 
176  //- Set width of output field (and return old width)
177  virtual int width(const int w) = 0;
178 
179  //- Get precision of output field
180  virtual int precision() const = 0;
181 
182  //- Set precision of output field (and return old precision)
183  virtual int precision(const int p) = 0;
184 
185 
186  // Member operators
187 
188  //- Return a non-const reference to const Ostream
189  // Needed for write functions where the stream argument is temporary:
190  // e.g. thing thisThing(OFstream("thingFileName")());
192  {
193  return const_cast<Ostream&>(*this);
194  }
195 };
196 
197 
198 // --------------------------------------------------------------------
199 // ------ Manipulators (not taking arguments)
200 // --------------------------------------------------------------------
201 
202 typedef Ostream& (*OstreamManip)(Ostream&);
203 
204 //- operator<< handling for manipulators without arguments
206 {
207  return f(os);
208 }
209 
210 //- operator<< handling for manipulators without arguments
212 {
213  f(os);
214  return os;
215 }
216 
217 
218 //- Indent stream
219 inline Ostream& indent(Ostream& os)
220 {
221  os.indent();
222  return os;
223 }
224 
225 //- Increment the indent level
227 {
228  os.incrIndent();
229  return os;
230 }
231 
232 //- Decrement the indent level
234 {
235  os.decrIndent();
236  return os;
237 }
238 
239 
240 //- Flush stream
241 inline Ostream& flush(Ostream& os)
242 {
243  os.flush();
244  return os;
245 }
246 
247 
248 //- Add newline and flush stream
249 inline Ostream& endl(Ostream& os)
250 {
251  os.endl();
252  return os;
253 }
254 
255 
256 // Useful aliases for tab and newline characters
257 static const char tab = '\t';
258 static const char nl = '\n';
259 
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 } // End namespace Foam
264 
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 
267 #endif
268 
269 // ************************ vim: set sw=4 sts=4 et: ************************ //