IOobject.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
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  - \par 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  - \par 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  - \par 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  - \par NO_READ
53  Don't read
54 
55  \par Write options
56 
57  Define what is done on object destruction and explicit writes:
58  - \par AUTO_WRITE
59  Object is written automatically when requested to by the
60  objectRegistry.
61  - \par NO_WRITE
62  No automatic write on destruction but can be written explicitly
63 
64 Class
65  Foam::typeIOobject
66 
67 Description
68  Templated form of IOobject providing type information for file reading and
69  header type checking.
70 
71 SourceFiles
72  IOobject.C
73  IOobjectReadHeader.C
74  IOobjectWriteHeader.C
75  IOobjectPrint.C
76 
77 \*---------------------------------------------------------------------------*/
78 
79 #ifndef IOobject_H
80 #define IOobject_H
81 
82 #include "fileName.H"
83 #include "typeInfo.H"
84 #include "autoPtr.H"
85 #include "InfoProxy.H"
86 #include "NamedEnum.H"
87 
88 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89 
90 namespace Foam
91 {
92 
93 class Time;
94 class objectRegistry;
95 
96 /*---------------------------------------------------------------------------*\
97  Class IOobject Declaration
98 \*---------------------------------------------------------------------------*/
99 
100 class IOobject
101 {
102 
103 public:
104 
105  //- Keyword for the FoamFile header sub-dictionary
106  static constexpr const char* foamFile = "FoamFile";
107 
108  // Public data types
109 
110  //- Enumeration defining the valid states of an IOobject
112  {
114  BAD
115  };
116 
117  //- Enumeration defining the read options
119  {
123  NO_READ
124  };
125 
126  //- Enumeration defining the write options
128  {
130  NO_WRITE = 1
131  };
132 
133  //- Enumeration defining the file checking options
135  {
140  };
141 
143 
144 
145 private:
146 
147  // Private Data
148 
149  //- Name
150  word name_;
151 
152  //- Class name read from header
153  word headerClassName_;
154 
155  //- Optional note
156  string note_;
157 
158  //- Instance path component
159  mutable fileName instance_;
160 
161  //- Local path component
162  fileName local_;
163 
164  //- objectRegistry reference
165  const objectRegistry& db_;
166 
167  //- Read option
168  readOption rOpt_;
169 
170  //- Write option
171  writeOption wOpt_;
172 
173  //- Register object created from this IOobject with registry if true
174  bool registerObject_;
175 
176  //- IOobject state
177  objectState objState_;
178 
179 
180 protected:
181 
182  // Protected Member Functions
183 
184  //- Read header using typeGlobalFile to find file
185  // and optionally check the headerClassName against Type
186  template<class Type>
187  bool typeHeaderOk(const bool checkType);
188 
189 
190 public:
191 
192  //- Runtime type information
193  TypeName("IOobject");
194 
195 
196  // Static Member Functions
197 
198  //- Split path into instance, local, name components
199  // input IOobject(instance, local, name)
200  // ----- ------
201  // "foo" ("", "", "foo")
202  // "foo/bar" ("foo", "", "bar")
203  // "/XXX/bar" ("/XXX", "", "bar")
204  // "foo/bar/" ERROR - no name
205  // "foo/xxx/bar" ("foo", "xxx", "bar")
206  // "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
207  static bool fileNameComponents
208  (
209  const fileName& path,
211  fileName& local,
212  word& name
213  );
214 
215  template<class Name>
216  static inline word groupName(Name name, const word& group);
217 
218  //- Return group (extension part of name)
219  static word group(const word& name);
220 
221  //- Return member (name without the extension)
222  static word member(const word& name);
223 
224  //- Type of file modification checking
226 
227 
228  // Constructors
229 
230  //- Construct from name, instance, registry, io options
231  IOobject
232  (
233  const word& name,
234  const fileName& instance,
235  const objectRegistry& registry,
238  bool registerObject=true
239  );
240 
241  //- Construct from name, instance, local, registry, io options
242  IOobject
243  (
244  const word& name,
245  const fileName& instance,
246  const fileName& local,
247  const objectRegistry& registry,
250  bool registerObject=true
251  );
252 
253  //- Construct from path, registry, io options
254  // Uses fileNameComponents() to split path into components.
255  IOobject
256  (
257  const fileName& path,
258  const objectRegistry& registry,
261  bool registerObject=true
262  );
263 
264  //- Construct from copy resetting registry
265  IOobject
266  (
267  const IOobject& io,
268  const objectRegistry& registry
269  );
270 
271  //- Construct from copy resetting name
272  IOobject
273  (
274  const IOobject& io,
275  const word& name
276  );
277 
278  //- Copy constructor
279  IOobject(const IOobject& io) = default;
280 
281  //- Clone
282  autoPtr<IOobject> clone() const
283  {
284  return autoPtr<IOobject>(new IOobject(*this));
285  }
286 
287  //- Clone resetting registry
288  autoPtr<IOobject> clone(const objectRegistry& registry) const
289  {
290  return autoPtr<IOobject>(new IOobject(*this, registry));
291  }
292 
293 
294  //- Destructor
295  virtual ~IOobject();
296 
297 
298  // Member Functions
299 
300  // General access
301 
302  //- Return time
303  const Time& time() const;
304 
305  //- Return the local objectRegistry
306  const objectRegistry& db() const;
307 
308  //- Return name
309  const word& name() const
310  {
311  return name_;
312  }
313 
314  //- Return name of the class name read from header
315  const word& headerClassName() const
316  {
317  return headerClassName_;
318  }
319 
320  //- Return name of the class name read from header
322  {
323  return headerClassName_;
324  }
325 
326  //- Return non-constant access to the optional note
327  string& note()
328  {
329  return note_;
330  }
331 
332  //- Return the optional note
333  const string& note() const
334  {
335  return note_;
336  }
337 
338  //- Rename
339  virtual void rename(const word& newName)
340  {
341  name_ = newName;
342  }
343 
344  //- Register object created from this IOobject with registry if true
345  bool& registerObject()
346  {
347  return registerObject_;
348  }
349 
350  //- Register object created from this IOobject with registry if true
351  bool registerObject() const
352  {
353  return registerObject_;
354  }
355 
356 
357  // Read/write options
358 
359  readOption readOpt() const
360  {
361  return rOpt_;
362  }
363 
365  {
366  return rOpt_;
367  }
368 
369  writeOption writeOpt() const
370  {
371  return wOpt_;
372  }
373 
375  {
376  return wOpt_;
377  }
378 
379 
380  // Path components
381 
382  //- Return group (extension part of name)
383  word group() const;
384 
385  //- Return member (name without the extension)
386  word member() const;
387 
388  const fileName& rootPath() const;
389 
390  const fileName& caseName(const bool global) const;
391 
392  //- Return the instance directory, constant, system, <time> etc.
393  // Allows modification of the instance
394  fileName& instance() const;
395 
396  //- If the instance is a time directory update to the current time
397  void updateInstance() const;
398 
399  //- Update instance to the current time
400  void updateTimeInstance() const;
401 
402  const fileName& local() const
403  {
404  return local_;
405  }
406 
407  //- Return complete path including the processor sub-directory
408  // for a parallel run if global is set false
409  fileName path(const bool global) const;
410 
411  //- Return complete path + object name including the processor
412  // sub-directory for a parallel run if global is set false
413  fileName objectPath(const bool global) const
414  {
415  return path(global)/name();
416  }
417 
418  //- Return the path relative to the case directory
419  fileName relativePath() const;
420 
421  //- Return complete relativePath + object name
423  {
424  return relativePath()/name();
425  }
426 
427  //- Return complete path + object name if the file exists
428  // in the case directory otherwise null.
429  // If global and parallel searches up into the global case
430  // directory.
431  fileName filePath(const bool global) const;
432 
433 
434  // Reading
435 
436  //- Read header
437  bool readHeader(Istream&);
438 
439  //- Read header of local object without type-checking
440  // Mainly used to create IOobjectLists
441  bool headerOk();
442 
443  //- Helper: warn that type does not support re-reading
444  template<class Type>
445  void warnNoRereading() const;
446 
447 
448  // Writing
449 
450  //- Write the standard OpenFOAM file/dictionary banner
451  // Optionally without -*- C++ -*- editor hint (eg, for logs)
452  template<class Stream>
453  static inline Stream& writeBanner(Stream& os, bool noHint=false);
454 
455  //- Write the standard file section divider
456  template<class Stream>
457  static inline Stream& writeDivider(Stream& os);
458 
459  //- Write the standard end file divider
460  template<class Stream>
461  static inline Stream& writeEndDivider(Stream& os);
462 
463  //- Write header
464  bool writeHeader(Ostream&) const;
465 
466  //- Write header. Allow override of type
467  bool writeHeader(Ostream&, const word& objectType) const;
468 
469 
470  // Error Handling
471 
472  bool good() const
473  {
474  return objState_ == GOOD;
475  }
476 
477  bool bad() const
478  {
479  return objState_ == BAD;
480  }
481 
482 
483  // Info
484 
485  //- Return info proxy.
486  // Used to print token information to a stream
487  InfoProxy<IOobject> info() const
488  {
489  return *this;
490  }
491 
492 
493  // Member Operators
494 
495  void operator=(const IOobject&);
496 };
497 
498 
499 template<>
500 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
501 
502 //- Trait for obtaining global status
503 template<class Type>
505 {
506  static const bool global = false;
507 };
508 
509 //- Trait for obtaining global write status
510 template<class Type>
512 {
513  static const bool global = typeGlobal<Type>::global;
514 };
515 
516 inline IOobject unregister(const IOobject& io)
517 {
518  IOobject uio(io);
519  uio.registerObject() = false;
520  return uio;
521 }
522 
523 
524 /*---------------------------------------------------------------------------*\
525  Class typeIOobject Declaration
526 \*---------------------------------------------------------------------------*/
527 
528 template<class Type>
529 class typeIOobject
530 :
531  public IOobject
532 {
533 
534 public:
535 
536  // Constructors
537 
539 
540  typeIOobject(const IOobject& io)
541  :
542  IOobject(io)
543  {}
544 
545 
546  // Member Functions
547 
548  // Reading
549 
550  //- Read header (uses typeGlobalFile to find file) and check
551  bool headerOk();
552 
553  using IOobject::objectPath;
554 
555  //- Return the object path for this Type
556  inline fileName objectPath() const
557  {
559  }
560 
561  using IOobject::filePath;
562 
563  //- Return the path for the file for this Type
564  inline fileName filePath() const
565  {
567  }
568 };
569 
570 
571 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
572 
573 } // End namespace Foam
574 
575 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
576 
577 #include "IOobjectI.H"
578 
579 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
580 
581 #ifdef NoRepository
582 # include "IOobjectTemplates.C"
583 #endif
584 
585 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
586 
587 #endif
588 
589 // ************************************************************************* //
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:45
const fileName & local() const
Definition: IOobject.H:400
bool typeHeaderOk(const bool checkType)
Read header using typeGlobalFile to find file.
const fileName & caseName(const bool global) const
Definition: IOobject.C:339
const Time & time() const
Return time.
Definition: IOobject.C:315
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
writeOption writeOpt() const
Definition: IOobject.H:367
fileName relativeObjectPath() const
Return complete relativePath + object name.
Definition: IOobject.H:420
void warnNoRereading() const
Helper: warn that type does not support re-reading.
static Stream & writeEndDivider(Stream &os)
Write the standard end file divider.
Definition: IOobjectI.H:103
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:343
fileName relativePath() const
Return the path relative to the case directory.
Definition: IOobject.C:396
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:352
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:309
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:164
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:325
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:61
readOption readOpt() const
Definition: IOobject.H:357
void updateInstance() const
If the instance is a time directory update to the current time.
Definition: IOobject.C:358
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:104
fileName filePath(const bool global) const
Return complete path + object name if the file exists.
Definition: IOobject.C:409
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:280
bool headerOk()
Read header of local object without type-checking.
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:313
bool bad() const
Definition: IOobject.H:475
word group() const
Return group (extension part of name)
Definition: IOobject.C:321
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:110
TypeName("IOobject")
Runtime type information.
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:223
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:337
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:93
bool good() const
Definition: IOobject.H:470
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:485
void operator=(const IOobject &)
Definition: IOobject.C:415
word member() const
Return member (name without the extension)
Definition: IOobject.C:327
bool readHeader(Istream &)
Read header.
const word & name() const
Return name.
Definition: IOobject.H:307
const fileName & rootPath() const
Definition: IOobject.C:333
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:140
static word groupName(Name name, const word &group)
void updateTimeInstance() const
Update instance to the current time.
Definition: IOobject.C:377
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:133
writeOption
Enumeration defining the write options.
Definition: IOobject.H:126
bool writeHeader(Ostream &) const
Write header.
virtual ~IOobject()
Destructor.
Definition: IOobject.C:303
fileName objectPath(const bool global) const
Return complete path + object name including the processor.
Definition: IOobject.H:411
fileName path(const bool global) const
Return complete path including the processor sub-directory.
Definition: IOobject.C:383
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A class for handling file names.
Definition: fileName.H:82
Registry of regIOobjects.
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:530
fileName objectPath() const
Return the object path for this Type.
Definition: IOobject.H:554
typeIOobject(const IOobject &io)
Definition: IOobject.H:538
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
fileName filePath() const
Return the path for the file for this Type.
Definition: IOobject.H:562
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
IOobject unregister(const IOobject &io)
Definition: IOobject.H:514
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
Trait for obtaining global write status.
Definition: IOobject.H:510
static const bool global
Definition: IOobject.H:511
Trait for obtaining global status.
Definition: IOobject.H:503
static const bool global
Definition: IOobject.H:504
Basic run-time type information using word as the type's name. Used to enhance the standard RTTI to c...