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