initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / IOobject / IOobject.H
blob063af46c66118b07ab87b42faf9bb834d83a5d76
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::IOobject
28 Description
29     IOobject defines the attributes of an object for which implicit
30     objectRegistry management is supported, and provides the infrastructure
31     for performing stream I/O.
33     An IOobject is constructed with an object name, a class name, an instance
34     path, a reference to a objectRegistry, and parameters determining its
35     storage status.
37     @par Read options
39     Define what is done on object construction and explicit reads:
40     @param MUST_READ
41         Object must be read from Istream on construction. \n
42         Error if Istream does not exist or can't be read.
43     @param READ_IF_PRESENT
44         Read object from Istream if Istream exists, otherwise don't. \n
45         Error only if Istream exists but can't be read.
46     @param NO_READ
47           Don't read
49     @par Write options
51     Define what is done on object destruction and explicit writes:
52     @param AUTO_WRITE
53         Object is written automatically when requested to by the
54         objectRegistry.
55     @param NO_WRITE
56         No automatic write on destruction but can be written explicitly
58 SourceFiles
59     IOobject.C
60     IOobjectReadHeader.C
61     IOobjectWriteHeader.C
62     IOobjectPrint.C
64 \*---------------------------------------------------------------------------*/
66 #ifndef IOobject_H
67 #define IOobject_H
69 #include "fileName.H"
70 #include "typeInfo.H"
71 #include "autoPtr.H"
72 #include "InfoProxy.H"
74 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 namespace Foam
79 class Time;
80 class objectRegistry;
82 /*---------------------------------------------------------------------------*\
83                            Class IOobject Declaration
84 \*---------------------------------------------------------------------------*/
86 class IOobject
89 public:
91     // Public data types
93         //- Enumeration defining the valid states of an IOobject
94         enum objectState
95         {
96             GOOD,
97             BAD
98         };
100         //- Enumeration defining the read options
101         enum readOption
102         {
103             MUST_READ,
104             READ_IF_PRESENT,
105             NO_READ
106         };
108         //- Enumeration defining the write options
109         enum writeOption
110         {
111             AUTO_WRITE = 0,
112             NO_WRITE = 1
113         };
116 private:
118     // Private data
120         //- Name
121         word name_;
123         //- Class name read from header
124         word headerClassName_;
126         //- Optional note
127         string note_;
129         //- Instance path component
130         fileName instance_;
132         //- Local path component
133         fileName local_;
135         //- objectRegistry reference
136         const objectRegistry& db_;
138         //- Read option
139         readOption rOpt_;
141         //- Write option
142         writeOption wOpt_;
144         //- Register object created from this IOobject with registry if true
145         bool registerObject_;
147         //- IOobject state
148         objectState objState_;
150 protected:
152     // Protected member functions
154         //- Construct and return an IFstream for the object.
155         //  The results is NULL if the stream construction failed
156         Istream* objectStream();
158         //- Set the object state to bad
159         void setBad(const string&);
162 public:
164     //- Runtime type information
165     TypeName("IOobject");
168     // Static Member Functions
170         //- Split path into instance, local, name components
171         static bool fileNameComponents
172         (
173             const fileName& path,
174             fileName& instance,
175             fileName& local,
176             word& name
177         );
180     // Constructors
182         //- Construct from name, instance, registry, io options
183         IOobject
184         (
185             const word& name,
186             const fileName& instance,
187             const objectRegistry& registry,
188             readOption r=NO_READ,
189             writeOption w=NO_WRITE,
190             bool registerObject=true
191         );
193         //- Construct from name, instance, local, registry, io options
194         IOobject
195         (
196             const word& name,
197             const fileName& instance,
198             const fileName& local,
199             const objectRegistry& registry,
200             readOption r=NO_READ,
201             writeOption w=NO_WRITE,
202             bool registerObject=true
203         );
205         //- Construct from path, registry, io options
206         //  Uses fileNameComponents() to split path into components.
207         IOobject
208         (
209             const fileName& path,
210             const objectRegistry& registry,
211             readOption r=NO_READ,
212             writeOption w=NO_WRITE,
213             bool registerObject=true
214         );
216         //- Clone
217         Foam::autoPtr<IOobject> clone() const
218         {
219             return autoPtr<IOobject>(new IOobject(*this));
220         }
223     // Destructor
225         virtual ~IOobject();
228     // Member Functions
230         // General access
232             //- Return time
233             const Time& time() const;
235             //- Return the local objectRegistry
236             const objectRegistry& db() const;
238             //- Return name
239             const word& name() const
240             {
241                 return name_;
242             }
244             //- Return name of the class name read from header
245             const word& headerClassName() const
246             {
247                 return headerClassName_;
248             }
250             //- Return non-constant access to the optional note
251             string& note()
252             {
253                 return note_;
254             }
256             //- Return the optional note
257             const string& note() const
258             {
259                 return note_;
260             }
262             //- Rename
263             virtual void rename(const word& newName)
264             {
265                 name_ = newName;
266             }
268             //- Register object created from this IOobject with registry if true
269             bool registerObject() const
270             {
271                 return registerObject_;
272             }
275         // Read/write options
277             readOption readOpt() const
278             {
279                 return rOpt_;
280             }
282             readOption& readOpt()
283             {
284                 return rOpt_;
285             }
287             writeOption writeOpt() const
288             {
289                 return wOpt_;
290             }
292             writeOption& writeOpt()
293             {
294                 return wOpt_;
295             }
298         // Path components
300             const fileName& rootPath() const;
302             const fileName& caseName() const;
304             const fileName& instance() const
305             {
306                 return instance_;
307             }
309             fileName& instance()
310             {
311                 return instance_;
312             }
314             const fileName& local() const
315             {
316                 return local_;
317             }
319             //- Return complete path
320             fileName path() const;
322             //- Return complete path with alternative instance and local
323             fileName path
324             (
325                 const word& instance,
326                 const fileName& local = ""
327             ) const;
329             //- Return complete path + object name
330             fileName objectPath() const
331             {
332                 return path()/name();
333             }
335             //- Return complete path + object name if the file exists
336             //  either in the case/processor or case otherwise null
337             fileName filePath() const;
340         // Reading
342             //- Read header
343             bool readHeader(Istream&);
345             //- Read and check header info
346             bool headerOk();
349         // Writing
351             //- Write the standard OpenFOAM file/dictionary banner
352             //  Optionally without -*- C++ -*- editor hint (eg, for logs)
353             template<class Stream>
354             static inline Stream& writeBanner(Stream& os, bool noHint=false);
356             //- Write the standard file section divider
357             template<class Stream>
358             static inline Stream& writeDivider(Stream& os);
360             //- Write the standard end file divider
361             template<class Stream>
362             static inline Stream& writeEndDivider(Stream& os);
364             //- Write header
365             bool writeHeader(Ostream&) const;
368         // Error Handling
370             bool good() const
371             {
372                 return objState_ == GOOD;
373             }
375             bool bad() const
376             {
377                 return objState_ == BAD;
378             }
381         // Info
383             //- Return info proxy.
384             //  Used to print token information to a stream
385             InfoProxy<IOobject> info() const
386             {
387                 return *this;
388             }
391     // Member operators
393         void operator=(const IOobject&);
397 #if defined (__GNUC__)
398 template<>
399 #endif
400 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 } // End namespace Foam
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 #   include "IOobjectI.H"
411 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 #endif
415 // ************************************************************************* //