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

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-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::IOobject
00026 
00027 Description
00028     IOobject defines the attributes of an object for which implicit
00029     objectRegistry management is supported, and provides the infrastructure
00030     for performing stream I/O.
00031 
00032     An IOobject is constructed with an object name, a class name, an instance
00033     path, a reference to a objectRegistry, and parameters determining its
00034     storage status.
00035 
00036     @par Read options
00037 
00038     Define what is done on object construction and explicit reads:
00039     @param MUST_READ
00040         Object must be read from Istream on construction. \n
00041         Error if Istream does not exist or can't be read.
00042     @param READ_IF_PRESENT
00043         Read object from Istream if Istream exists, otherwise don't. \n
00044         Error only if Istream exists but can't be read.
00045     @param NO_READ
00046           Don't read
00047 
00048     @par Write options
00049 
00050     Define what is done on object destruction and explicit writes:
00051     @param AUTO_WRITE
00052         Object is written automatically when requested to by the
00053         objectRegistry.
00054     @param 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 <OpenFOAM/fileName.H>
00069 #include <OpenFOAM/typeInfo.H>
00070 #include <OpenFOAM/autoPtr.H>
00071 #include <OpenFOAM/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 protected:
00150 
00151     // Protected member functions
00152 
00153         //- Construct and return an IFstream for the object.
00154         //  The results is NULL if the stream construction failed
00155         Istream* objectStream();
00156 
00157         //- Set the object state to bad
00158         void setBad(const string&);
00159 
00160         static const char* getBannerString(bool noHint);
00161 
00162 
00163 public:
00164 
00165     //- Runtime type information
00166     TypeName("IOobject");
00167 
00168 
00169     // Static Member Functions
00170 
00171         //- Split path into instance, local, name components
00172         static bool fileNameComponents
00173         (
00174             const fileName& path,
00175             fileName& instance,
00176             fileName& local,
00177             word& name
00178         );
00179 
00180 
00181     // Constructors
00182 
00183         //- Construct from name, instance, registry, io options
00184         IOobject
00185         (
00186             const word& name,
00187             const fileName& instance,
00188             const objectRegistry& registry,
00189             readOption r=NO_READ,
00190             writeOption w=NO_WRITE,
00191             bool registerObject=true
00192         );
00193 
00194         //- Construct from name, instance, local, registry, io options
00195         IOobject
00196         (
00197             const word& name,
00198             const fileName& instance,
00199             const fileName& local,
00200             const objectRegistry& registry,
00201             readOption r=NO_READ,
00202             writeOption w=NO_WRITE,
00203             bool registerObject=true
00204         );
00205 
00206         //- Construct from path, registry, io options
00207         //  Uses fileNameComponents() to split path into components.
00208         IOobject
00209         (
00210             const fileName& path,
00211             const objectRegistry& registry,
00212             readOption r=NO_READ,
00213             writeOption w=NO_WRITE,
00214             bool registerObject=true
00215         );
00216 
00217         //- Clone
00218         Foam::autoPtr<IOobject> clone() const
00219         {
00220             return autoPtr<IOobject>(new IOobject(*this));
00221         }
00222 
00223 
00224     // Destructor
00225 
00226         virtual ~IOobject();
00227 
00228 
00229     // Member Functions
00230 
00231         // General access
00232 
00233             //- Return time
00234             const Time& time() const;
00235 
00236             //- Return the local objectRegistry
00237             const objectRegistry& db() const;
00238 
00239             //- Return name
00240             const word& name() const
00241             {
00242                 return name_;
00243             }
00244 
00245             //- Return name of the class name read from header
00246             const word& headerClassName() const
00247             {
00248                 return headerClassName_;
00249             }
00250 
00251             //- Return non-constant access to the optional note
00252             string& note()
00253             {
00254                 return note_;
00255             }
00256 
00257             //- Return the optional note
00258             const string& note() const
00259             {
00260                 return note_;
00261             }
00262 
00263             //- Rename
00264             virtual void rename(const word& newName)
00265             {
00266                 name_ = newName;
00267             }
00268 
00269             //- Register object created from this IOobject with registry if true
00270             bool registerObject() const
00271             {
00272                 return registerObject_;
00273             }
00274 
00275 
00276         // Read/write options
00277 
00278             readOption readOpt() const
00279             {
00280                 return rOpt_;
00281             }
00282 
00283             readOption& readOpt()
00284             {
00285                 return rOpt_;
00286             }
00287 
00288             writeOption writeOpt() const
00289             {
00290                 return wOpt_;
00291             }
00292 
00293             writeOption& writeOpt()
00294             {
00295                 return wOpt_;
00296             }
00297 
00298 
00299         // Path components
00300 
00301             const fileName& rootPath() const;
00302 
00303             const fileName& caseName() const;
00304 
00305             const fileName& instance() const
00306             {
00307                 return instance_;
00308             }
00309 
00310             fileName& instance()
00311             {
00312                 return instance_;
00313             }
00314 
00315             const fileName& local() const
00316             {
00317                 return local_;
00318             }
00319 
00320             //- Return complete path
00321             fileName path() const;
00322 
00323             //- Return complete path with alternative instance and local
00324             fileName path
00325             (
00326                 const word& instance,
00327                 const fileName& local = ""
00328             ) const;
00329 
00330             //- Return complete path + object name
00331             fileName objectPath() const
00332             {
00333                 return path()/name();
00334             }
00335 
00336             //- Return complete path + object name if the file exists
00337             //  either in the case/processor or case otherwise null
00338             fileName filePath() const;
00339 
00340 
00341         // Reading
00342 
00343             //- Read header
00344             bool readHeader(Istream&);
00345 
00346             //- Read and check header info
00347             bool headerOk();
00348 
00349 
00350         // Writing
00351 
00352             //- Write the standard OpenFOAM file/dictionary banner
00353             //  Optionally without -*- C++ -*- editor hint (eg, for logs)
00354             template<class Stream>
00355             static inline Stream& writeBanner(Stream& os, bool noHint=false);
00356 
00357             //- Write the standard file section divider
00358             template<class Stream>
00359             static inline Stream& writeDivider(Stream& os);
00360 
00361             //- Write the standard end file divider
00362             template<class Stream>
00363             static inline Stream& writeEndDivider(Stream& os);
00364 
00365             //- Write header
00366             bool writeHeader(Ostream&) const;
00367 
00368 
00369         // Error Handling
00370 
00371             bool good() const
00372             {
00373                 return objState_ == GOOD;
00374             }
00375 
00376             bool bad() const
00377             {
00378                 return objState_ == BAD;
00379             }
00380 
00381 
00382         // Info
00383 
00384             //- Return info proxy.
00385             //  Used to print token information to a stream
00386             InfoProxy<IOobject> info() const
00387             {
00388                 return *this;
00389             }
00390 
00391 
00392     // Member operators
00393 
00394         void operator=(const IOobject&);
00395 };
00396 
00397 
00398 #if defined (__GNUC__)
00399 template<>
00400 #endif
00401 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
00402 
00403 
00404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00405 
00406 } // End namespace Foam
00407 
00408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00409 
00410 #   include <OpenFOAM/IOobjectI.H>
00411 
00412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00413 
00414 #endif
00415 
00416 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines