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-2021 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:
105  //- Keyword for the FoamFile header sub-dictionary
106  static constexpr const char* foamFile = "FoamFile";
107 
108  // Public data types
110  //- Enumeration defining the valid states of an IOobject
112  {
113  GOOD,
114  BAD
115  };
117  //- Enumeration defining the read options
119  {
123  NO_READ
124  };
126  //- Enumeration defining the write options
128  {
129  AUTO_WRITE = 0,
130  NO_WRITE = 1
131  };
133  //- Enumeration defining the file checking options
135  {
138  inotify,
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  //- Set the object state to bad
185  void setBad(const string&);
186 
187  //- Read header using typeGlobalFile to find file
188  // and optionally check the headerClassName against Type
189  template<class Type>
190  bool typeHeaderOk(const bool checkType);
191 
192 
193 public:
194 
195  //- Runtime type information
196  TypeName("IOobject");
197 
198 
199  // Static Member Functions
200 
201  //- Split path into instance, local, name components
202  // input IOobject(instance, local, name)
203  // ----- ------
204  // "foo" ("", "", "foo")
205  // "foo/bar" ("foo", "", "bar")
206  // "/XXX/bar" ("/XXX", "", "bar")
207  // "foo/bar/" ERROR - no name
208  // "foo/xxx/bar" ("foo", "xxx", "bar")
209  // "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
210  static bool fileNameComponents
211  (
212  const fileName& path,
214  fileName& local,
215  word& name
216  );
217 
218  template<class Name>
219  static inline word groupName(Name name, const word& group);
220 
221  //- Return group (extension part of name)
222  static word group(const word& name);
223 
224  //- Return member (name without the extension)
225  static word member(const word& name);
226 
227  //- Return the name of the object within the given model
228  // as <model>:<name>
229  template<class Name>
230  static inline word modelName(Name name, const word& model);
232  //- Type of file modification checking
234 
235 
236  // Constructors
237 
238  //- Construct from name, instance, registry, io options
239  IOobject
240  (
241  const word& name,
242  const fileName& instance,
243  const objectRegistry& registry,
244  readOption r=NO_READ,
246  bool registerObject=true
247  );
248 
249  //- Construct from name, instance, local, registry, io options
250  IOobject
251  (
252  const word& name,
253  const fileName& instance,
254  const fileName& local,
255  const objectRegistry& registry,
256  readOption r=NO_READ,
258  bool registerObject=true
259  );
260 
261  //- Construct from path, registry, io options
262  // Uses fileNameComponents() to split path into components.
263  IOobject
264  (
265  const fileName& path,
266  const objectRegistry& registry,
267  readOption r=NO_READ,
269  bool registerObject=true
270  );
271 
272  //- Construct from copy resetting registry
273  IOobject
274  (
275  const IOobject& io,
276  const objectRegistry& registry
277  );
278 
279  //- Construct from copy resetting name
280  IOobject
281  (
282  const IOobject& io,
283  const word& name
284  );
285 
286  //- Copy constructor
287  IOobject(const IOobject& io) = default;
289  //- Clone
290  autoPtr<IOobject> clone() const
291  {
292  return autoPtr<IOobject>(new IOobject(*this));
293  }
295  //- Clone resetting registry
296  autoPtr<IOobject> clone(const objectRegistry& registry) const
297  {
298  return autoPtr<IOobject>(new IOobject(*this, registry));
299  }
300 
301 
302  //- Destructor
303  virtual ~IOobject();
304 
305 
306  // Member Functions
307 
308  // General access
309 
310  //- Return time
311  const Time& time() const;
312 
313  //- Return the local objectRegistry
314  const objectRegistry& db() const;
316  //- Return name
317  const word& name() const
318  {
319  return name_;
320  }
322  //- Return name of the class name read from header
323  const word& headerClassName() const
324  {
325  return headerClassName_;
326  }
328  //- Return name of the class name read from header
330  {
331  return headerClassName_;
332  }
334  //- Return non-constant access to the optional note
335  string& note()
336  {
337  return note_;
338  }
340  //- Return the optional note
341  const string& note() const
342  {
343  return note_;
344  }
346  //- Rename
347  virtual void rename(const word& newName)
348  {
349  name_ = newName;
350  }
352  //- Register object created from this IOobject with registry if true
353  bool& registerObject()
354  {
355  return registerObject_;
356  }
358  //- Register object created from this IOobject with registry if true
359  bool registerObject() const
360  {
361  return registerObject_;
362  }
363 
364 
365  // Read/write options
366 
367  readOption readOpt() const
368  {
369  return rOpt_;
370  }
371 
373  {
374  return rOpt_;
375  }
376 
377  writeOption writeOpt() const
378  {
379  return wOpt_;
380  }
381 
383  {
384  return wOpt_;
385  }
386 
387 
388  // Path components
389 
390  //- Return group (extension part of name)
391  word group() const;
392 
393  //- Return member (name without the extension)
394  word member() const;
395 
396  //- Return the name of the object within this model
397  // as <model>:<name>
398  inline word modelName(const char* name) const;
399 
400  const fileName& rootPath() const;
401 
402  const fileName& caseName(const bool global) const;
403 
404  //- Return the instance directory, constant, system, <time> etc.
405  // Allows modification of the instance
406  fileName& instance() const;
407 
408  //- If the instance is a time directory update to the current time
409  void updateInstance() const;
410 
411  const fileName& local() const
412  {
413  return local_;
414  }
415 
416  //- Return complete path including the processor sub-directory
417  // for a parallel run if global is set false
418  fileName path(const bool global) const;
419 
420  //- Return complete path + object name including the processor
421  // sub-directory for a parallel run if global is set false
422  fileName objectPath(const bool global) const
423  {
424  return path(global)/name();
425  }
426 
427  //- Return the path relative to the case directory
428  fileName relativePath() const;
430  //- Return complete relativePath + object name
432  {
433  return relativePath()/name();
434  }
435 
436  //- Return complete path + object name if the file exists
437  // in the case directory otherwise null.
438  //
439  // If global and parallel searches up into the global case
440  // directory.
441  fileName filePath(const word& typeName, const bool global) const;
442 
443 
444  // Reading
445 
446  //- Read header
447  bool readHeader(Istream&);
448 
449  //- Read header of local object without type-checking
450  // Mainly used to create IOobjectLists
451  bool headerOk();
452 
453  //- Helper: warn that type does not support re-reading
454  template<class Type>
455  void warnNoRereading() const;
456 
457 
458  // Writing
459 
460  //- Write the standard OpenFOAM file/dictionary banner
461  // Optionally without -*- C++ -*- editor hint (eg, for logs)
462  template<class Stream>
463  static inline Stream& writeBanner(Stream& os, bool noHint=false);
464 
465  //- Write the standard file section divider
466  template<class Stream>
467  static inline Stream& writeDivider(Stream& os);
468 
469  //- Write the standard end file divider
470  template<class Stream>
471  static inline Stream& writeEndDivider(Stream& os);
472 
473  //- Write header
474  bool writeHeader(Ostream&) const;
475 
476  //- Write header. Allow override of type
477  bool writeHeader(Ostream&, const word& objectType) const;
478 
479 
480  // Error Handling
481 
482  bool good() const
483  {
484  return objState_ == GOOD;
485  }
486 
487  bool bad() const
488  {
489  return objState_ == BAD;
490  }
491 
492 
493  // Info
494 
495  //- Return info proxy.
496  // Used to print token information to a stream
497  InfoProxy<IOobject> info() const
498  {
499  return *this;
500  }
501 
502 
503  // Member Operators
504 
505  void operator=(const IOobject&);
506 };
507 
508 
509 template<>
510 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
511 
512 //- Template function for obtaining global status
513 template<class Type>
514 inline bool typeGlobal()
515 {
516  return false;
517 }
518 
519 //- Template function for obtaining global write status
520 template<class Type>
521 inline bool typeGlobalFile()
522 {
523  return typeGlobal<Type>();
524 }
525 
526 inline IOobject unregister(const IOobject& io)
527 {
528  IOobject uio(io);
529  uio.registerObject() = false;
530  return uio;
531 }
532 
533 
534 /*---------------------------------------------------------------------------*\
535  Class typeIOobject Declaration
536 \*---------------------------------------------------------------------------*/
538 template<class Type>
539 class typeIOobject
540 :
541  public IOobject
542 {
543 
544 public:
545 
546  // Constructors
547 
549 
550  typeIOobject(const IOobject& io)
551  :
552  IOobject(io)
553  {}
554 
555 
556  // Member Functions
557 
558  // Reading
559 
560  //- Read header (uses typeGlobalFile to find file) and check
561  bool headerOk();
562 
563  using IOobject::objectPath;
565  //- Return the object path for this Type
566  inline fileName objectPath() const
567  {
568  return objectPath(typeGlobalFile<Type>());
569  }
570 
571  using IOobject::filePath;
573  //- Return the path for the file for this Type
574  inline fileName filePath() const
575  {
576  return filePath(Type::typeName, typeGlobalFile<Type>());
577  }
578 };
579 
580 
581 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
582 
583 } // End namespace Foam
584 
585 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
586 
587 #include "IOobjectI.H"
588 
589 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
590 
591 #ifdef NoRepository
592 # include "IOobjectTemplates.C"
593 #endif
594 
595 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
596 
597 #endif
598 
599 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:132
bool typeHeaderOk(const bool checkType)
Read header using typeGlobalFile to find file.
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:288
writeOption
Enumeration defining the write options.
Definition: IOobject.H:125
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:64
bool good() const
Definition: IOobject.H:480
const word & name() const
Return name.
Definition: IOobject.H:315
A class for handling file names.
Definition: fileName.H:79
void updateInstance() const
If the instance is a time directory update to the current time.
Definition: IOobject.C:361
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:65
fileName relativeObjectPath() const
Return complete relativePath + object name.
Definition: IOobject.H:429
bool typeGlobal()
Template function for obtaining global status.
Definition: IOobject.H:512
void operator=(const IOobject &)
Definition: IOobject.C:435
Templated form of IOobject providing type information for file reading and header type checking...
Definition: IOobject.H:537
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:113
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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:167
bool typeGlobalFile()
Template function for obtaining global write status.
Definition: IOobject.H:519
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:345
const fileName & caseName(const bool global) const
Definition: IOobject.C:342
word member() const
Return member (name without the extension)
Definition: IOobject.C:330
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:109
virtual ~IOobject()
Destructor.
Definition: IOobject.C:306
word group() const
Return group (extension part of name)
Definition: IOobject.C:324
fileName filePath(const word &typeName, const bool global) const
Return complete path + object name if the file exists.
Definition: IOobject.C:407
readOption
Enumeration defining the read options.
Definition: IOobject.H:116
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:333
bool bad() const
Definition: IOobject.H:485
A class for handling words, derived from string.
Definition: word.H:59
static word groupName(Name name, const word &group)
fileName relativePath() const
Return the path relative to the case directory.
Definition: IOobject.C:393
const fileName & local() const
Definition: IOobject.H:409
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:416
fileName path(const bool global) const
Return complete path including the processor sub-directory.
Definition: IOobject.C:380
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:104
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:231
const fileName & rootPath() const
Definition: IOobject.C:336
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:140
writeOption writeOpt() const
Definition: IOobject.H:375
static Stream & writeEndDivider(Stream &os)
Write the standard end file divider.
Definition: IOobjectI.H:123
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
const Time & time() const
Return time.
Definition: IOobject.C:318
TypeName("IOobject")
Runtime type information.
bool writeHeader(Ostream &) const
Write header.
bool headerOk()
Read header of local object without type-checking.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:355
static word modelName(Name name, const word &model)
Return the name of the object within the given model.
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:495
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:351
readOption readOpt() const
Definition: IOobject.H:365
IOobject unregister(const IOobject &io)
Definition: IOobject.H:524
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:321
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
fileName objectPath(const bool global) const
Return complete path + object name including the processor.
Definition: IOobject.H:420
void warnNoRereading() const
Helper: warn that type does not support re-reading.
Namespace for OpenFOAM.