IOobject.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::IOobject
26 
27 Description
28  IOobject defines the attributes of an object for which implicit
29  objectRegistry management is supported, and provides the infrastructure
30  for performing stream I/O.
31 
32  An IOobject is constructed with an object name, a class name, an instance
33  path, a reference to a objectRegistry, and parameters determining its
34  storage status.
35 
36  \par Read options
37 
38  Define what is done on object construction and explicit reads:
39  \param MUST_READ
40  Object must be read from Istream on construction. \n
41  Error if Istream does not exist or can't be read.
42  Does not check timestamp or re-read.
43  \param MUST_READ_IF_MODIFIED
44  Object must be read from Istream on construction. \n
45  Error if Istream does not exist or can't be read. If object is
46  registered its timestamp will be checked every timestep and possibly
47  re-read.
48  \param READ_IF_PRESENT
49  Read object from Istream if Istream exists, otherwise don't. \n
50  Error only if Istream exists but can't be read.
51  Does not check timestamp or re-read.
52  \param NO_READ
53  Don't read
54 
55  \par Write options
56 
57  Define what is done on object destruction and explicit writes:
58  \param AUTO_WRITE
59  Object is written automatically when requested to by the
60  objectRegistry.
61  \param NO_WRITE
62  No automatic write on destruction but can be written explicitly
63 
64 SourceFiles
65  IOobject.C
66  IOobjectReadHeader.C
67  IOobjectWriteHeader.C
68  IOobjectPrint.C
69 
70 \*---------------------------------------------------------------------------*/
71 
72 #ifndef IOobject_H
73 #define IOobject_H
74 
75 #include "fileName.H"
76 #include "typeInfo.H"
77 #include "autoPtr.H"
78 #include "InfoProxy.H"
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 class Time;
86 class objectRegistry;
87 
88 /*---------------------------------------------------------------------------*\
89  Class IOobject Declaration
90 \*---------------------------------------------------------------------------*/
91 
92 class IOobject
93 {
94 
95 public:
96 
97  // Public data types
98 
99  //- Enumeration defining the valid states of an IOobject
100  enum objectState
101  {
103  BAD
104  };
105 
106  //- Enumeration defining the read options
107  enum readOption
108  {
112  NO_READ
113  };
114 
115  //- Enumeration defining the write options
116  enum writeOption
117  {
119  NO_WRITE = 1
120  };
121 
122 
123 private:
124 
125  // Private data
126 
127  //- Name
128  word name_;
129 
130  //- Class name read from header
132 
133  //- Optional note
134  string note_;
135 
136  //- Instance path component
138 
139  //- Local path component
141 
142  //- objectRegistry reference
143  const objectRegistry& db_;
144 
145  //- Read option
147 
148  //- Write option
150 
151  //- Register object created from this IOobject with registry if true
152  bool registerObject_;
153 
154  //- IOobject state
156 
157 private:
158 
159  // Private member functions
160 
161  //- Unique on disk file name.
162  const word& uniqueFileName() const;
163 
164 protected:
165 
166  // Protected Member Functions
167 
168  //- Construct and return an IFstream for the object.
169  // The results is NULL if the stream construction failed
171 
172  //- Construct and return an IFstream for the object given the
173  // exact file. The results is NULL if the stream construction failed
174  Istream* objectStream(const fileName&);
175 
176  //- Set the object state to bad
177  void setBad(const string&);
178 
179 
180 public:
181 
182  //- Runtime type information
183  TypeName("IOobject");
184 
185 
186  // Static Member Functions
187 
188  //- Split path into instance, local, name components
189  // input IOobject(instance, local, name)
190  // ----- ------
191  // "foo" ("", "", "foo")
192  // "foo/bar" ("foo", "", "bar")
193  // "/XXX/bar" ("/XXX", "", "bar")
194  // "foo/bar/" ERROR - no name
195  // "foo/xxx/bar" ("foo", "xxx", "bar")
196  // "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
197  static bool fileNameComponents
198  (
199  const fileName& path,
201  fileName& local,
202  word& name
203  );
204 
205  template<class Name>
206  static inline word groupName(Name name, const word& group);
207 
208 
209  // Constructors
210 
211  //- Construct from name, instance, registry, io options
212  IOobject
213  (
214  const word& name,
215  const fileName& instance,
216  const objectRegistry& registry,
219  bool registerObject=true
220  );
221 
222  //- Construct from name, instance, local, registry, io options
223  IOobject
224  (
225  const word& name,
226  const fileName& instance,
227  const fileName& local,
228  const objectRegistry& registry,
231  bool registerObject=true
232  );
233 
234  //- Construct from path, registry, io options
235  // Uses fileNameComponents() to split path into components.
236  IOobject
237  (
238  const fileName& path,
239  const objectRegistry& registry,
242  bool registerObject=true
243  );
244 
245  //- Construct as copy resetting name
246  IOobject
247  (
248  const IOobject& io,
249  const word& name
250  );
251 
252  //- Clone
254  {
255  return autoPtr<IOobject>(new IOobject(*this));
256  }
257 
258 
259  //- Destructor
260  virtual ~IOobject();
261 
262 
263  // Member Functions
264 
265  // General access
266 
267  //- Return time
268  const Time& time() const;
269 
270  //- Return the local objectRegistry
271  const objectRegistry& db() const;
272 
273  //- Return name
274  const word& name() const
275  {
276  return name_;
277  }
278 
279  //- Return name of the class name read from header
280  const word& headerClassName() const
281  {
282  return headerClassName_;
283  }
284 
285  //- Return non-constant access to the optional note
286  string& note()
287  {
288  return note_;
289  }
290 
291  //- Return the optional note
292  const string& note() const
293  {
294  return note_;
295  }
296 
297  //- Rename
298  virtual void rename(const word& newName)
299  {
300  name_ = newName;
301  }
302 
303  //- Register object created from this IOobject with registry if true
304  bool& registerObject()
305  {
306  return registerObject_;
307  }
308 
309  //- Register object created from this IOobject with registry if true
310  bool registerObject() const
311  {
312  return registerObject_;
313  }
314 
315 
316  // Read/write options
317 
318  readOption readOpt() const
319  {
320  return rOpt_;
321  }
322 
324  {
325  return rOpt_;
326  }
327 
328  writeOption writeOpt() const
329  {
330  return wOpt_;
331  }
332 
334  {
335  return wOpt_;
336  }
337 
338 
339  // Path components
340 
341  //- Return group (extension part of name)
342  word group() const;
343 
344  //- Return member (name without the extension)
345  word member() const;
346 
347  const fileName& rootPath() const;
348 
349  const fileName& caseName() const;
350 
351  const fileName& instance() const
352  {
353  return instance_;
354  }
355 
356  fileName& instance()
357  {
358  return instance_;
359  }
360 
361  const fileName& local() const
362  {
363  return local_;
364  }
365 
366  //- Return complete path
367  fileName path() const;
368 
369  //- Return complete path with alternative instance and local
370  fileName path
371  (
372  const word& instance,
373  const fileName& local = ""
374  ) const;
375 
376  //- Return complete path + object name
377  fileName objectPath() const
378  {
379  return path()/uniqueFileName();
380  }
381 
382  //- Return complete path + object name if the file exists
383  // either in the case/processor or case otherwise null
384  fileName filePath() const;
385 
386 
387  // Reading
388 
389  //- Read header
390  bool readHeader(Istream&);
391 
392  //- Read and check header info
393  bool headerOk();
394 
395 
396  // Writing
397 
398  //- Write the standard OpenFOAM file/dictionary banner
399  // Optionally without -*- C++ -*- editor hint (eg, for logs)
400  template<class Stream>
401  static inline Stream& writeBanner(Stream& os, bool noHint=false);
402 
403  //- Write the standard file section divider
404  template<class Stream>
405  static inline Stream& writeDivider(Stream& os);
406 
407  //- Write the standard end file divider
408  template<class Stream>
409  static inline Stream& writeEndDivider(Stream& os);
410 
411  //- Write header
412  bool writeHeader(Ostream&) const;
413 
414  //- Write header. Allow override of type
415  bool writeHeader(Ostream&, const word& objectType) const;
416 
417 
418  // Error Handling
419 
420  bool good() const
421  {
422  return objState_ == GOOD;
423  }
424 
425  bool bad() const
426  {
427  return objState_ == BAD;
428  }
429 
430 
431  // Info
432 
433  //- Return info proxy.
434  // Used to print token information to a stream
435  InfoProxy<IOobject> info() const
436  {
437  return *this;
438  }
439 
440 
441  // Member operators
442 
443  void operator=(const IOobject&);
444 
445  // Static functions
446 
447  //- Set unique on disk file name
448  //- e.g., b -> b_ to avoid upper/lowercase clash with B
449  static void replaceFileName(const word& from, const word& to);
450 };
451 
452 
453 template<>
454 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
455 
456 
457 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
458 
459 } // End namespace Foam
460 
461 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
462 
463 #include "IOobjectI.H"
464 
465 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
466 
467 #endif
468 
469 // ************************************************************************* //
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::IOobject::groupName
static word groupName(Name name, const word &group)
Foam::IOobject::filePath
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:336
Foam::IOobject::setBad
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:480
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
w
volScalarField w(IOobject("w", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE), mesh, dimensionedScalar("w", dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0))
Foam::IOobject::objectStream
Istream * objectStream()
Construct and return an IFstream for the object.
Definition: IOobject.C:410
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
Foam::IOobject::rOpt_
readOption rOpt_
Read option.
Definition: IOobject.H:145
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOobject::registerObject_
bool registerObject_
Register object created from this IOobject with registry if true.
Definition: IOobject.H:151
typeInfo.H
Foam::IOobject::AUTO_WRITE
@ AUTO_WRITE
Definition: IOobject.H:117
Foam::IOobject::instance_
fileName instance_
Instance path component.
Definition: IOobject.H:136
InfoProxy.H
Foam::IOobject::writeEndDivider
static Stream & writeEndDivider(Stream &os)
Write the standard end file divider.
Definition: IOobjectI.H:118
Foam::IOobject::rootPath
const fileName & rootPath() const
Definition: IOobject.C:287
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::IOobject::TypeName
TypeName("IOobject")
Runtime type information.
Foam::IOobject::note_
string note_
Optional note.
Definition: IOobject.H:133
Foam::IOobject::replaceFileName
static void replaceFileName(const word &from, const word &to)
Definition: IOobject.C:317
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobject.H:350
Foam::IOobject::writeOpt
writeOption writeOpt() const
Definition: IOobject.H:327
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:245
Foam::IOobject::info
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:434
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
Foam::IOobject::BAD
@ BAD
Definition: IOobject.H:102
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:239
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::IOobject::objectPath
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:376
Foam::IOobject::headerOk
bool headerOk()
Read and check header info.
Definition: IOobject.C:439
Foam::IOobject::MUST_READ_IF_MODIFIED
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:109
Foam::IOobject::writeOpt
writeOption & writeOpt()
Definition: IOobject.H:332
Foam::IOobject::writeHeader
bool writeHeader(Ostream &) const
Write header.
Definition: IOobjectWriteHeader.C:67
Foam::IOobject::registerObject
bool registerObject() const
Register object created from this IOobject with registry if true.
Definition: IOobject.H:309
Foam::IOobject::db_
const objectRegistry & db_
objectRegistry reference
Definition: IOobject.H:142
Foam::IOobject::headerClassName_
word headerClassName_
Class name read from header.
Definition: IOobject.H:130
Foam::IOobject::readOpt
readOption readOpt() const
Definition: IOobject.H:317
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:279
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:111
Foam::IOobject::registerObject
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:303
Foam::IOobject::caseName
const fileName & caseName() const
Definition: IOobject.C:251
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:115
fileName.H
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobject.H:273
Foam::IOobject::wOpt_
writeOption wOpt_
Write option.
Definition: IOobject.H:148
Foam::IOobject::fileNameComponents
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:43
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::IOobject::operator=
void operator=(const IOobject &)
Definition: IOobject.C:499
Foam::IOobject::GOOD
@ GOOD
Definition: IOobject.H:101
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::IOobject::good
bool good() const
Definition: IOobject.H:419
Foam::IOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:297
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::IOobject::writeBanner
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:45
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::IOobject::~IOobject
virtual ~IOobject()
Destructor.
Definition: IOobject.C:233
Foam::IOobject::instance
fileName & instance()
Definition: IOobject.H:355
Foam::IOobject::IOobject
IOobject(const word &name, const fileName &instance, const objectRegistry &registry, readOption r=NO_READ, writeOption w=NO_WRITE, bool registerObject=true)
Construct from name, instance, registry, io options.
Definition: IOobject.C:116
Foam::IOobject::clone
Foam::autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:252
IOobjectI.H
Foam::IOobject::member
word member() const
Return member (name without the extension)
Definition: IOobject.C:272
Foam::IOobject::objectState
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:99
Foam::IOobject::name_
word name_
Name.
Definition: IOobject.H:127
Foam::IOobject::group
word group() const
Return group (extension part of name)
Definition: IOobject.C:257
Foam::IOobject::uniqueFileName
const word & uniqueFileName() const
Unique on disk file name.
Definition: IOobject.C:324
Foam::IOobject::local_
fileName local_
Local path component.
Definition: IOobject.H:139
Foam::IOobject::readHeader
bool readHeader(Istream &)
Read header.
Definition: IOobjectReadHeader.C:31
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
Foam::IOobject::bad
bool bad() const
Definition: IOobject.H:424
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOobject::objState_
objectState objState_
IOobject state.
Definition: IOobject.H:154
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:110
Foam::IOobject::path
fileName path() const
Return complete path.
Definition: IOobject.C:293
Foam::IOobject::note
const string & note() const
Return the optional note.
Definition: IOobject.H:291
Foam::IOobject::readOpt
readOption & readOpt()
Definition: IOobject.H:322
Foam::IOobject::local
const fileName & local() const
Definition: IOobject.H:360
Foam::IOobject::note
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:285
autoPtr.H