FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::Ostream
00026 
00027 Description
00028     An Ostream is an abstract base class for all output systems
00029     (streams, files, token lists, etc).
00030 
00031 SourceFiles
00032     Ostream.C
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #ifndef Ostream_H
00037 #define Ostream_H
00038 
00039 #include "IOstream.H"
00040 #include <OpenFOAM/keyType.H>
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 // Forward declaration of classes
00048 class token;
00049 
00050 /*---------------------------------------------------------------------------*\
00051                            Class Ostream Declaration
00052 \*---------------------------------------------------------------------------*/
00053 
00054 class Ostream
00055 :
00056     public IOstream
00057 {
00058 
00059 protected:
00060 
00061     // Protected data
00062 
00063         //- Number of spaces per indent level
00064         static const unsigned short indentSize_ = 4;
00065 
00066         //- Indentation of the entry from the start of the keyword
00067         static const unsigned short entryIndentation_ = 16;
00068 
00069         //- Current indent level
00070         unsigned short indentLevel_;
00071 
00072 
00073 public:
00074 
00075     // Constructors
00076 
00077         //- Set stream status
00078         Ostream
00079         (
00080             streamFormat format=ASCII,
00081             versionNumber version=currentVersion,
00082             compressionType compression=UNCOMPRESSED
00083         )
00084         :
00085             IOstream(format, version, compression),
00086             indentLevel_(0)
00087         {}
00088 
00089 
00090     //- Destructor
00091     virtual ~Ostream()
00092     {}
00093 
00094 
00095     // Member functions
00096 
00097         // Write functions
00098 
00099             //- Write next token to stream
00100             virtual Ostream& write(const token&) = 0;
00101 
00102             //- Write character
00103             virtual Ostream& write(const char) = 0;
00104 
00105             //- Write character string
00106             virtual Ostream& write(const char*) = 0;
00107 
00108             //- Write word
00109             virtual Ostream& write(const word&) = 0;
00110 
00111             //- Write keyType
00112             virtual Ostream& write(const keyType&);
00113 
00114             //- Write string
00115             virtual Ostream& write(const string&) = 0;
00116 
00117             //- Write std::string surrounded by quotes.
00118             //  Optional write without quotes.
00119             virtual Ostream& writeQuoted
00120             (
00121                 const std::string&,
00122                 const bool quoted=true
00123             ) = 0;
00124 
00125             //- Write label
00126             virtual Ostream& write(const label) = 0;
00127 
00128             //- Write floatScalar
00129             virtual Ostream& write(const floatScalar) = 0;
00130 
00131             //- Write doubleScalar
00132             virtual Ostream& write(const doubleScalar) = 0;
00133 
00134             //- Write binary block
00135             virtual Ostream& write(const char*, std::streamsize) = 0;
00136 
00137             //- Add indentation characters
00138             virtual void indent() = 0;
00139 
00140             //- Return indent level
00141             unsigned short indentLevel() const
00142             {
00143                 return indentLevel_;
00144             }
00145 
00146             //- Access to indent level
00147             unsigned short& indentLevel()
00148             {
00149                 return indentLevel_;
00150             }
00151 
00152             //- Incrememt the indent level
00153             void incrIndent()
00154             {
00155                 indentLevel_++;
00156             }
00157 
00158             //- Decrememt the indent level
00159             void decrIndent();
00160 
00161             //- Write the keyword followed by an appropriate indentation
00162             Ostream& writeKeyword(const keyType&);
00163 
00164 
00165         // Stream state functions
00166 
00167             //- Flush stream
00168             virtual void flush() = 0;
00169 
00170             //- Add newline and flush stream
00171             virtual void endl() = 0;
00172 
00173             //- Get width of output field
00174             virtual int width() const = 0;
00175 
00176             //- Set width of output field (and return old width)
00177             virtual int width(const int w) = 0;
00178 
00179             //- Get precision of output field
00180             virtual int precision() const = 0;
00181 
00182             //- Set precision of output field (and return old precision)
00183             virtual int precision(const int p) = 0;
00184 
00185 
00186     // Member operators
00187 
00188         //- Return a non-const reference to const Ostream
00189         //  Needed for write functions where the stream argument is temporary:
00190         //  e.g. thing thisThing(OFstream("thingFileName")());
00191         Ostream& operator()() const
00192         {
00193             return const_cast<Ostream&>(*this);
00194         }
00195 };
00196 
00197 
00198 // --------------------------------------------------------------------
00199 // ------ Manipulators (not taking arguments)
00200 // --------------------------------------------------------------------
00201 
00202 typedef Ostream& (*OstreamManip)(Ostream&);
00203 
00204 //- operator<< handling for manipulators without arguments
00205 inline Ostream& operator<<(Ostream& os, OstreamManip f)
00206 {
00207     return f(os);
00208 }
00209 
00210 //- operator<< handling for manipulators without arguments
00211 inline Ostream& operator<<(Ostream& os, IOstreamManip f)
00212 {
00213     f(os);
00214     return os;
00215 }
00216 
00217 
00218 //- Indent stream
00219 inline Ostream& indent(Ostream& os)
00220 {
00221     os.indent();
00222     return os;
00223 }
00224 
00225 //- Increment the indent level
00226 inline Ostream& incrIndent(Ostream& os)
00227 {
00228     os.incrIndent();
00229     return os;
00230 }
00231 
00232 //- Decrement the indent level
00233 inline Ostream& decrIndent(Ostream& os)
00234 {
00235     os.decrIndent();
00236     return os;
00237 }
00238 
00239 
00240 //- Flush stream
00241 inline Ostream& flush(Ostream& os)
00242 {
00243     os.flush();
00244     return os;
00245 }
00246 
00247 
00248 //- Add newline and flush stream
00249 inline Ostream& endl(Ostream& os)
00250 {
00251     os.endl();
00252     return os;
00253 }
00254 
00255 
00256 // Useful aliases for tab and newline characters
00257 static const char tab = '\t';
00258 static const char nl = '\n';
00259 
00260 
00261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00262 
00263 } // End namespace Foam
00264 
00265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00266 
00267 #endif
00268 
00269 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines