OpenFOAM logo
Open Source CFD Toolkit

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-2005 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     Ostream
00027 
00028 Description
00029     An Ostream is an abstract base class for all output systems;
00030     be they streams, files, token lists etc.  The basic operations
00031     are construct, close, read token, read primitive and read binary
00032     block.  In addition version control and line number counting is
00033     incorporated.  Usually one would use the read primitive member
00034     functions,  but if one were reading a stream on unknown data
00035     sequence one can read token by token, and then analyse.
00036 
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifndef Ostream_H
00040 #define Ostream_H
00041 
00042 #include "IOstream.H"
00043 
00044 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00045 
00046 namespace Foam
00047 {
00048 
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         //- Current indent level
00068         unsigned short indentLevel_;
00069 
00070         //- Indentation of the entry from the start of the keyword
00071         static const unsigned short entryIndentation_ = 16;
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 
00093         virtual ~Ostream()
00094         {}
00095 
00096 
00097     // Member functions
00098 
00099         // Write functions
00100 
00101             //- Write next token to stream
00102             virtual Ostream& write(const token&) = 0;
00103 
00104             //- Write character
00105             virtual Ostream& write(const char) = 0;
00106 
00107             //- Write character string
00108             virtual Ostream& write(const char*) = 0;
00109 
00110             //- Write word
00111             virtual Ostream& write(const word&) = 0;
00112 
00113             //- Write string
00114             virtual Ostream& write(const string&) = 0;
00115 
00116             //- Write label
00117             virtual Ostream& write(const label) = 0;
00118 
00119             //- Write floatScalar
00120             virtual Ostream& write(const floatScalar) = 0;
00121 
00122             //- Write doubleScalar
00123             virtual Ostream& write(const doubleScalar) = 0;
00124 
00125             //- Write binary block
00126             virtual Ostream& write(const char*, std::streamsize) = 0;
00127 
00128             //- Add indentation characters
00129             virtual void indent() = 0;
00130 
00131             //- Return indent level
00132             unsigned short indentLevel() const
00133             {
00134                 return indentLevel_;
00135             }
00136 
00137             //- Access to indent level
00138             unsigned short& indentLevel()
00139             {
00140                 return indentLevel_;
00141             }
00142 
00143             //- Incrememt the indent level
00144             void incrIndent()
00145             {
00146                 indentLevel_++;
00147             }
00148 
00149             //- Decrememt the indent level
00150             void decrIndent();
00151 
00152             //- Write the keyword to the Ostream followed by 
00153             //  appropriate indentation
00154             Ostream& writeKeyword(const word& keyword);
00155 
00156 
00157         // Stream state functions
00158 
00159             //- Flush stream
00160             virtual void flush() = 0;
00161 
00162             //- Add '\n' and flush stream
00163             virtual void endl() = 0;
00164 
00165             //- Get width of output field
00166             virtual int width() const = 0;
00167 
00168             //- Set width of output field (and return old width)
00169             virtual int width(const int w) = 0;
00170 
00171             //- Get precision of output field
00172             virtual int precision() const = 0;
00173 
00174             //- Set precision of output field (and return old precision)
00175             virtual int precision(const int p) = 0;
00176 
00177 
00178     // Member operators
00179 
00180         //- Return a non-const reference to const Ostream
00181         //  Needed for write functions where the stream argument is temporary:
00182         //  e.g. thing thisThing(OFstream("thingFileName")());
00183         Ostream& operator()() const
00184         {
00185             return const_cast<Ostream&>(*this);
00186         }
00187 };
00188 
00189 
00190 // --------------------------------------------------------------------
00191 // ------ Manipulators (not taking arguments)
00192 // --------------------------------------------------------------------
00193 
00194 typedef Ostream& (*OstreamManip)(Ostream&);
00195 
00196 //- operator<< handling for manipulators without arguments
00197 inline Ostream& operator<<(Ostream& os, OstreamManip f)
00198 {
00199     return f(os);
00200 }
00201 
00202 //- operator<< handling for manipulators without arguments
00203 inline Ostream& operator<<(Ostream& os, IOstreamManip f)
00204 {
00205     f(os);
00206     return os;
00207 }
00208 
00209 
00210 //- Indent stream
00211 inline Ostream& indent(Ostream& os)
00212 {
00213     os.indent();
00214     return os;
00215 }
00216 
00217 //- Increment the indent level
00218 inline Ostream& incrIndent(Ostream& os)
00219 {
00220     os.incrIndent();
00221     return os;
00222 }
00223 
00224 //- Decrement the indent level
00225 inline Ostream& decrIndent(Ostream& os)
00226 {
00227     os.decrIndent();
00228     return os;
00229 }
00230 
00231 
00232 //- Flush stream
00233 inline Ostream& flush(Ostream& os)
00234 {
00235     os.flush();
00236     return os;
00237 }
00238 
00239 
00240 //- Add '\n' and flush stream
00241 inline Ostream& endl(Ostream& os)
00242 {
00243     os.endl();
00244     return os;
00245 }
00246 
00247 
00248 // Useful aliases for tab and newline characters
00249 static const char tab = '\t';
00250 static const char nl = '\n';
00251 
00252 
00253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00254 
00255 } // End namespace Foam
00256 
00257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00258 
00259 #endif
00260 
00261 // ************************************************************************* //
For further information go to www.openfoam.org