OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

IOobject.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-2007 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     IOobject
00027 
00028 Description
00029     IOobject defines the attributes of an object for which implicit
00030     objectRegistry management is supported, and provides the infrastructure for
00031     performing stream I/O. An IOobject is constructed with an object name, a
00032     class name, an instance path, a reference to a objectRegistry, and
00033     parameters determining its storage status.
00034 
00035     Read options define what is done on object construction & explicit reads:
00036 
00037         MUST_READ
00038             Object must be read from Istream on construction.
00039             Error if Istream does not exist or can't be read.
00040 
00041         READ_IF_PRESENT
00042             Read object from Istream if Istream exists, otherwise don't.
00043             Error only if Istream exists but can't be read.
00044 
00045         NO_READ
00046             Don't read
00047 
00048     Write options define what is done on object destruction & explicit writes:
00049 
00050         AUTO_WRITE
00051             Object is written automatically when requested to by the
00052             objectRegistry.
00053 
00054         NO_WRITE
00055             No automatic write on destruction but can be written explicitly
00056 
00057 SourceFiles
00058     IOobject.C
00059     IOobjectReadHeader.C
00060     IOobjectWriteHeader.C
00061     IOobjectPrint.C
00062 
00063 \*---------------------------------------------------------------------------*/
00064 
00065 #ifndef IOobject_H
00066 #define IOobject_H
00067 
00068 #include "fileName.H"
00069 #include "typeInfo.H"
00070 #include "autoPtr.H"
00071 #include "InfoProxy.H"
00072 
00073 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00074 
00075 namespace Foam
00076 {
00077 
00078 class Time;
00079 class objectRegistry;
00080 
00081 /*---------------------------------------------------------------------------*\
00082                            Class IOobject Declaration
00083 \*---------------------------------------------------------------------------*/
00084 
00085 class IOobject
00086 {
00087 
00088 public:
00089 
00090     // Public data types
00091 
00092         //- Enumeration defining the valid states of an IOobject
00093         enum objectState
00094         {
00095             GOOD,
00096             BAD
00097         };
00098 
00099         //- Enumeration defining the read options
00100         enum readOption
00101         {
00102             MUST_READ,
00103             READ_IF_PRESENT,
00104             NO_READ
00105         };
00106 
00107         //- Enumeration defining the write options
00108         enum writeOption
00109         {
00110             AUTO_WRITE = 0,
00111             NO_WRITE = 1
00112         };
00113 
00114 
00115 private:
00116 
00117     // Private data
00118 
00119         //- Name
00120         word name_;
00121 
00122         //- Class name read from header
00123         word headerClassName_;
00124 
00125         //- Optional note
00126         string note_;
00127 
00128         //- Instance path component
00129         fileName instance_;
00130 
00131         //- Local path component
00132         fileName local_;
00133 
00134         //- objectRegistry reference
00135         const objectRegistry& db_;
00136 
00137         //- Read option
00138         readOption rOpt_;
00139 
00140         //- Write option
00141         writeOption wOpt_;
00142 
00143         //- Register object created from this IOobject with registry if true
00144         bool registerObject_;
00145 
00146         //- IOobject state
00147         objectState objState_;
00148 
00149 
00150 protected:
00151 
00152     // Protected member functions
00153 
00154         //- Construct and return an IFstream for the object.
00155         //  The results is NULL if the stream constuction failed
00156         Istream* objectStream();
00157 
00158         //- Set the object state to bad
00159         void setBad(const string&);
00160 
00161 
00162 public:
00163 
00164     //- Runtime type information
00165     TypeName("IOobject");
00166 
00167 
00168     // Constructors
00169 
00170         //- Construct from name, instance, registry, io options
00171         IOobject
00172         (
00173             const word& name,
00174             const fileName& instance,
00175             const objectRegistry& registry,
00176             readOption r=NO_READ,
00177             writeOption w=NO_WRITE,
00178             bool registerObject=true
00179         );
00180 
00181         //- Construct from name, instance, local, registry, io options
00182         IOobject
00183         (
00184             const word& name,
00185             const fileName& instance,
00186             const fileName& local,
00187             const objectRegistry& registry,
00188             readOption r=NO_READ,
00189             writeOption w=NO_WRITE,
00190             bool registerObject=true
00191         );
00192 
00193         //- Clone
00194         autoPtr<IOobject> clone() const
00195         {
00196             return autoPtr<IOobject>(new IOobject(*this));
00197         }
00198 
00199 
00200     // Destructor
00201 
00202         virtual ~IOobject()
00203         {}
00204 
00205 
00206     // Member functions
00207 
00208         // General access
00209 
00210             //- Return time
00211             const Time& time() const;
00212 
00213             //- Return the local objectRegistry
00214             const objectRegistry& db() const;
00215 
00216             //- Return name
00217             const word& name() const
00218             {
00219                 return name_;
00220             }
00221 
00222             //- Return name of the class name read from header
00223             const word& headerClassName() const
00224             {
00225                 return headerClassName_;
00226             }
00227 
00228             //- Return non-constant access to the optional note
00229             string& note()
00230             {
00231                 return note_;
00232             }
00233 
00234             //- Return the optional note
00235             const string& note() const
00236             {
00237                 return note_;
00238             }
00239 
00240             //- Rename
00241             virtual void rename(const word& newName)
00242             {
00243                 name_ = newName;
00244             }
00245 
00246             //- Register object created from this IOobject with registry if true
00247             bool registerObject() const
00248             {
00249                 return registerObject_;
00250             }
00251 
00252 
00253         // Read/write options
00254 
00255             readOption readOpt() const
00256             {
00257                 return rOpt_;
00258             }
00259 
00260             readOption& readOpt()
00261             {
00262                 return rOpt_;
00263             }
00264 
00265             writeOption writeOpt() const
00266             {
00267                 return wOpt_;
00268             }
00269 
00270             writeOption& writeOpt()
00271             {
00272                 return wOpt_;
00273             }
00274 
00275 
00276         // Path components
00277 
00278             const fileName& rootPath() const;
00279 
00280             const fileName& caseName() const;
00281 
00282             const fileName& instance() const
00283             {
00284                 return instance_;
00285             }
00286 
00287             fileName& instance()
00288             {
00289                 return instance_;
00290             }
00291 
00292             const fileName& local() const
00293             {
00294                 return local_;
00295             }
00296 
00297             //- Return complete path
00298             fileName path() const;
00299 
00300             //- Return complete path with alternative instance and local
00301             fileName path
00302             (
00303                 const word& instance,
00304                 const fileName& local = ""
00305             ) const;
00306 
00307             //- Return complete path + object name
00308             fileName objectPath() const
00309             {
00310                 return path()/name();
00311             }
00312 
00313             //- Return complete path + object name if the file exists 
00314             //  either in the case/processor or case otherwise null
00315             fileName filePath() const;
00316 
00317 
00318         // Reading
00319 
00320             //- Read header
00321             bool readHeader(Istream&);
00322 
00323             //- Read and check header info
00324             bool headerOk();
00325 
00326 
00327         // Writing
00328 
00329             //- Write the standard OpenFOAM file/dictionary banner
00330             template<class Stream>
00331             static inline void writeBanner(Stream& os);
00332 
00333             //- Write the standard OpenFOAM log banner
00334             template<class Stream>
00335             static inline void writeLogBanner(Stream& os);
00336 
00337             //- Write the standard file section divider
00338             template<class Stream>
00339             static inline void writeDivider(Stream& os);
00340 
00341             //- Write header
00342             bool writeHeader(Ostream&) const;
00343 
00344 
00345         // Error Handling
00346 
00347             bool good() const
00348             {
00349                 return objState_ == GOOD;
00350             }
00351 
00352             bool bad() const
00353             {
00354                 return objState_ == BAD;
00355             }
00356 
00357 
00358         // Info
00359 
00360             //- Return info proxy.
00361             //  Used to print token information to a stream
00362             InfoProxy<IOobject> info() const
00363             {
00364                 return *this;
00365             }
00366 
00367 
00368     // Member operators
00369 
00370         void operator=(const IOobject&);
00371 };
00372 
00373 
00374 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00375 
00376 } // End namespace Foam
00377 
00378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00379 
00380 #   include "IOobjectI.H"
00381 
00382 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00383 
00384 #endif
00385 
00386 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd