OpenFOAM logo
Open Source CFD Toolkit

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-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     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 given file.
00155         //  The results is NULL if the stream constuction failed
00156         Istream* objectStream(const fileName&);
00157 
00158         //- Construct and return an IFstream for the object.
00159         //  The results is NULL if the stream constuction failed
00160         Istream* objectStream();
00161 
00162         //- Set the object state to bad
00163         void setBad(const string&);
00164 
00165 
00166 public:
00167 
00168     //- Runtime type information
00169     TypeName("IOobject");
00170 
00171 
00172     // Constructors
00173 
00174         //- Construct from name, instance, registry, io options
00175         IOobject
00176         (
00177             const word& name,
00178             const fileName& instance,
00179             const objectRegistry& registry,
00180             readOption r=NO_READ,
00181             writeOption w=NO_WRITE,
00182             bool registerObject=true
00183         );
00184 
00185         //- Construct from name, instance, local, registry, io options
00186         IOobject
00187         (
00188             const word& name,
00189             const fileName& instance,
00190             const fileName& local,
00191             const objectRegistry& registry,
00192             readOption r=NO_READ,
00193             writeOption w=NO_WRITE,
00194             bool registerObject=true
00195         );
00196 
00197         //- Clone
00198         autoPtr<IOobject> clone() const
00199         {
00200             return autoPtr<IOobject>(new IOobject(*this));
00201         }
00202 
00203 
00204     // Destructor
00205 
00206         virtual ~IOobject()
00207         {}
00208 
00209 
00210     // Member functions
00211 
00212         // General access
00213 
00214             //- Return time
00215             const Time& time() const;
00216 
00217             //- Return the local objectRegistry
00218             const objectRegistry& db() const;
00219 
00220             //- Return name
00221             const word& name() const
00222             {
00223                 return name_;
00224             }
00225 
00226             //- Return name of the class name read from header
00227             const word& headerClassName() const
00228             {
00229                 return headerClassName_;
00230             }
00231 
00232             //- Return non-constant access to the optional note
00233             string& note()
00234             {
00235                 return note_;
00236             }
00237 
00238             //- Return the optional note
00239             const string& note() const
00240             {
00241                 return note_;
00242             }
00243 
00244             //- Rename
00245             virtual void rename(const word& newName)
00246             {
00247                 name_ = newName;
00248             }
00249 
00250             //- Register object created from this IOobject with registry if true
00251             bool registerObject() const
00252             {
00253                 return registerObject_;
00254             }
00255 
00256 
00257         // Read/write options
00258 
00259             readOption readOpt() const
00260             {
00261                 return rOpt_;
00262             }
00263 
00264             readOption& readOpt()
00265             {
00266                 return rOpt_;
00267             }
00268 
00269             writeOption writeOpt() const
00270             {
00271                 return wOpt_;
00272             }
00273 
00274             writeOption& writeOpt()
00275             {
00276                 return wOpt_;
00277             }
00278 
00279 
00280         // Path components
00281 
00282             const fileName& rootPath() const;
00283 
00284             const fileName& caseName() const;
00285 
00286             const fileName& instance() const
00287             {
00288                 return instance_;
00289             }
00290 
00291             fileName& instance()
00292             {
00293                 return instance_;
00294             }
00295 
00296             const fileName& local() const
00297             {
00298                 return local_;
00299             }
00300 
00301             //- Return complete path
00302             fileName path() const;
00303 
00304             //- Return complete path with alternative instance and local
00305             fileName path
00306             (
00307                 const word& instance,
00308                 const fileName& local = ""
00309             ) const;
00310 
00311             //- Return complete path + object name
00312             fileName objectPath() const
00313             {
00314                 return path()/name();
00315             }
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 header
00330             bool writeHeader(Ostream&) const;
00331 
00332 
00333         // Error Handling
00334 
00335             bool good() const
00336             {
00337                 return objState_ == GOOD;
00338             }
00339 
00340             bool bad() const
00341             {
00342                 return objState_ == BAD;
00343             }
00344 
00345 
00346         // Info
00347 
00348             //- Return info proxy.
00349             //  Used to print token information to a stream
00350             InfoProxy<IOobject> info() const
00351             {
00352                 return *this;
00353             }
00354 
00355 
00356     // Member operators
00357 
00358         void operator=(const IOobject&);
00359 };
00360 
00361 
00362 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00363 
00364 } // End namespace Foam
00365 
00366 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00367 
00368 #endif
00369 
00370 // ************************************************************************* //
For further information go to www.openfoam.org