OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Ostream.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::Ostream
00027 
00028 Description
00029     An Ostream is an abstract base class for all output systems
00030     (streams, files, token lists, etc).
00031 
00032 SourceFiles
00033     Ostream.C
00034 
00035 \*---------------------------------------------------------------------------*/
00036 
00037 #ifndef Ostream_H
00038 #define Ostream_H
00039 
00040 #include "IOstream.H"
00041 #include "keyType.H"
00042 
00043 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00044 
00045 namespace Foam
00046 {
00047 
00048 // Forward declaration of classes
00049 class token;
00050 
00051 /*---------------------------------------------------------------------------*\
00052                            Class Ostream Declaration
00053 \*---------------------------------------------------------------------------*/
00054 
00055 class Ostream
00056 :
00057     public IOstream
00058 {
00059 
00060 protected:
00061 
00062     // Protected data
00063 
00064         //- Number of spaces per indent level
00065         static const unsigned short indentSize_ = 4;
00066 
00067         //- Indentation of the entry from the start of the keyword
00068         static const unsigned short entryIndentation_ = 16;
00069 
00070         //- Current indent level
00071         unsigned short indentLevel_;
00072 
00073 
00074 public:
00075 
00076     // Constructors
00077 
00078         //- Set stream status
00079         Ostream
00080         (
00081             streamFormat format=ASCII,
00082             versionNumber version=currentVersion,
00083             compressionType compression=UNCOMPRESSED
00084         )
00085         :
00086             IOstream(format, version, compression),
00087             indentLevel_(0)
00088         {}
00089 
00090 
00091     //- Destructor
00092     virtual ~Ostream()
00093     {}
00094 
00095 
00096     // Member functions
00097 
00098         // Write functions
00099 
00100             //- Write next token to stream
00101             virtual Ostream& write(const token&) = 0;
00102 
00103             //- Write character
00104             virtual Ostream& write(const char) = 0;
00105 
00106             //- Write character string
00107             virtual Ostream& write(const char*) = 0;
00108 
00109             //- Write word
00110             virtual Ostream& write(const word&) = 0;
00111 
00112             //- Write keyType
00113             virtual Ostream& write(const keyType&);
00114 
00115             //- Write string
00116             virtual Ostream& write(const string&) = 0;
00117 
00118             //- Write std::string surrounded by quotes.
00119             //  Optional write without quotes.
00120             virtual Ostream& writeQuoted
00121             (
00122                 const std::string&,
00123                 const bool quoted=true
00124             ) = 0;
00125 
00126             //- Write label
00127             virtual Ostream& write(const label) = 0;
00128 
00129             //- Write floatScalar
00130             virtual Ostream& write(const floatScalar) = 0;
00131 
00132             //- Write doubleScalar
00133             virtual Ostream& write(const doubleScalar) = 0;
00134 
00135             //- Write binary block
00136             virtual Ostream& write(const char*, std::streamsize) = 0;
00137 
00138             //- Add indentation characters
00139             virtual void indent() = 0;
00140 
00141             //- Return indent level
00142             unsigned short indentLevel() const
00143             {
00144                 return indentLevel_;
00145             }
00146 
00147             //- Access to indent level
00148             unsigned short& indentLevel()
00149             {
00150                 return indentLevel_;
00151             }
00152 
00153             //- Incrememt the indent level
00154             void incrIndent()
00155             {
00156                 indentLevel_++;
00157             }
00158 
00159             //- Decrememt the indent level
00160             void decrIndent();
00161 
00162             //- Write the keyword followed by an appropriate indentation
00163             Ostream& writeKeyword(const keyType&);
00164 
00165 
00166         // Stream state functions
00167 
00168             //- Flush stream
00169             virtual void flush() = 0;
00170 
00171             //- Add newline and flush stream
00172             virtual void endl() = 0;
00173 
00174             //- Get width of output field
00175             virtual int width() const = 0;
00176 
00177             //- Set width of output field (and return old width)
00178             virtual int width(const int w) = 0;
00179 
00180             //- Get precision of output field
00181             virtual int precision() const = 0;
00182 
00183             //- Set precision of output field (and return old precision)
00184             virtual int precision(const int p) = 0;
00185 
00186 
00187     // Member operators
00188 
00189         //- Return a non-const reference to const Ostream
00190         //  Needed for write functions where the stream argument is temporary:
00191         //  e.g. thing thisThing(OFstream("thingFileName")());
00192         Ostream& operator()() const
00193         {
00194             return const_cast<Ostream&>(*this);
00195         }
00196 };
00197 
00198 
00199 // --------------------------------------------------------------------
00200 // ------ Manipulators (not taking arguments)
00201 // --------------------------------------------------------------------
00202 
00203 typedef Ostream& (*OstreamManip)(Ostream&);
00204 
00205 //- operator<< handling for manipulators without arguments
00206 inline Ostream& operator<<(Ostream& os, OstreamManip f)
00207 {
00208     return f(os);
00209 }
00210 
00211 //- operator<< handling for manipulators without arguments
00212 inline Ostream& operator<<(Ostream& os, IOstreamManip f)
00213 {
00214     f(os);
00215     return os;
00216 }
00217 
00218 
00219 //- Indent stream
00220 inline Ostream& indent(Ostream& os)
00221 {
00222     os.indent();
00223     return os;
00224 }
00225 
00226 //- Increment the indent level
00227 inline Ostream& incrIndent(Ostream& os)
00228 {
00229     os.incrIndent();
00230     return os;
00231 }
00232 
00233 //- Decrement the indent level
00234 inline Ostream& decrIndent(Ostream& os)
00235 {
00236     os.decrIndent();
00237     return os;
00238 }
00239 
00240 
00241 //- Flush stream
00242 inline Ostream& flush(Ostream& os)
00243 {
00244     os.flush();
00245     return os;
00246 }
00247 
00248 
00249 //- Add newline and flush stream
00250 inline Ostream& endl(Ostream& os)
00251 {
00252     os.endl();
00253     return os;
00254 }
00255 
00256 
00257 // Useful aliases for tab and newline characters
00258 static const char tab = '\t';
00259 static const char nl = '\n';
00260 
00261 
00262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00263 
00264 } // End namespace Foam
00265 
00266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00267 
00268 #endif
00269 
00270 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd