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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::IOobject
29 
30 Description
31  Defines the attributes of an object for which implicit
32  objectRegistry management is supported, and provides the infrastructure
33  for performing stream I/O.
34 
35  An IOobject is constructed with an object name, a class name, an instance
36  path, a reference to a objectRegistry, and parameters determining its
37  storage status.
38 
39  \par Read options
40 
41  Define what is done on object construction and explicit reads:
42  - \par MUST_READ
43  Object must be read from Istream on construction. \n
44  Error if Istream does not exist or can't be read.
45  Does not check timestamp or re-read.
46  - \par MUST_READ_IF_MODIFIED
47  Object must be read from Istream on construction. \n
48  Error if Istream does not exist or can't be read. If object is
49  registered its timestamp will be checked every timestep and possibly
50  re-read.
51  - \par READ_IF_PRESENT
52  Read object from Istream if Istream exists, otherwise don't. \n
53  Error only if Istream exists but can't be read.
54  Does not check timestamp or re-read.
55  - \par NO_READ
56  Don't read
57 
58  \par Write options
59 
60  Define what is done on object destruction and explicit writes:
61  - \par AUTO_WRITE
62  Object is written automatically when requested to by the
63  objectRegistry.
64  - \par NO_WRITE
65  No automatic write on destruction but can be written explicitly
66 
67 Note
68  Specifying registered does not result in the IOobject itself being
69  registered. It is only serves as guidance for a regIOobject using it.
70 
71 See also
72  Foam::objectRegistry
73  Foam::regIOobject
74 
75 SourceFiles
76  IOobject.C
77  IOobjectReadHeader.C
78  IOobjectWriteHeader.C
79  IOobjectPrint.C
80 
81 \*---------------------------------------------------------------------------*/
82 
83 #ifndef IOobject_H
84 #define IOobject_H
85 
86 #include "fileName.H"
87 #include "typeInfo.H"
88 #include "autoPtr.H"
89 #include "InfoProxy.H"
90 #include "Enum.H"
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 namespace Foam
95 {
96 
97 // Forward Declarations
98 class Time;
99 class objectRegistry;
100 
101 /*---------------------------------------------------------------------------*\
102  Class IOobject Declaration
103 \*---------------------------------------------------------------------------*/
104 
105 class IOobject
106 {
107 public:
108 
109  // Public Data Types
110 
111  //- Enumeration defining the valid states of an IOobject
112  enum objectState : char
113  {
115  BAD
116  };
117 
118  //- Enumeration defining the read options
119  enum readOption : char
120  {
124  NO_READ
125  };
126 
127  //- Enumeration defining the write options
128  enum writeOption : char
129  {
131  NO_WRITE = 1
132  };
133 
134  //- Enumeration defining the file checking options
135  enum fileCheckTypes : char
136  {
141  };
142 
143  //- Names for the fileCheckTypes
145 
146 
147 private:
148 
149  // Private Data
150 
151  //- Name
152  word name_;
153 
154  //- Class name read from header
155  word headerClassName_;
156 
157  //- Optional note
158  string note_;
159 
160  //- Instance path component
161  fileName instance_;
162 
163  //- Local path component
164  fileName local_;
165 
166  //- Reference to the objectRegistry
167  const objectRegistry& db_;
168 
169  //- Read option
170  readOption rOpt_;
171 
172  //- Write option
173  writeOption wOpt_;
174 
175  //- Should object created with this IOobject be registered?
176  bool registerObject_;
177 
178  //- Is object same for all processors?
179  bool globalObject_;
180 
181  //- IOobject state
182  objectState objState_;
183 
184  //- The label byte-size (could also be stored as byte)
185  unsigned short labelByteSize_;
186 
187  //- The scalar byte-size (could also be stored as byte)
188  unsigned short scalarByteSize_;
189 
190 
191 protected:
192 
193  // Protected Member Functions
194 
195  //- Set the object state to bad
196  void setBad(const string& s);
197 
198 
199 public:
200 
201  //- Declare type-name, virtual type (with debug switch)
202  TypeName("IOobject");
203 
204 
205  // Static Data Members
206 
207  //- Character for scoping object names (':' or '_')
208  // Change with caution.
209  static char scopeSeparator;
210 
211  //- Type of file modification checking
213 
214 
215  // Static Functions
216 
217  //- Split path into instance, local, name components
218  //
219  // The splitting behaviour is as follows:
220  // \verbatim
221  // input | instance | local | name
222  // ----------- | ---------- | ----- | ----
223  // a | | | a
224  // a/b | a | | b
225  // a/b/c/d | a | b/c | d
226  // /a/b/c | /a/b | | c
227  // ./a/b/c | PWD/a/b | | c
228  // ../a/b/c | PWD/../a/b | | c
229  // a/b/ | ERROR | |
230  // \endverbatim
231  // where PWD is the Foam::cwd() current working directory
232  static bool fileNameComponents
233  (
234  const fileName& path,
236  fileName& local,
237  word& name
238  );
239 
240  //- Create dot-delimited name.group string
241  // An empty group is ignored.
242  template<class StringType>
243  static inline word groupName(StringType base, const word& group);
244 
245  //- Return group (extension part of name)
246  static word group(const word& name);
247 
248  //- Return member (name without the extension)
249  static word member(const word& name);
250 
251  //- Create scope:name or scope_name string
252  // An empty scope or name is ignored.
253  template<class StringType>
254  static inline word scopedName(StringType scope, const word& name);
255 
256  //- Return the IOobject, but also consider an alternative file name.
257  //
258  // \param io The expected IOobject to use
259  // \param altFile Alternative fileName (ignored if empty).
260  // \param ioName The alternative name for the IOobject when
261  // the altFile resolves to a directory.
262  //
263  // \note If the alternative fileName is a non-empty string,
264  // it defines the location but uses all other properties of the
265  // expected IOobject.
266  // The location may be an absolute or a relative path.
267  // If it corresponds to a directory, the name of the
268  // expected IOobject will be used in its resolution.
269  // This expected name can provided via the ioName parameter.
270  static IOobject selectIO
271  (
272  const IOobject& io,
273  const fileName& altFile,
274  const word& ioName = ""
275  );
276 
277 
278  // Generated Methods
279 
280  //- Copy construct
281  IOobject(const IOobject&) = default;
282 
283  //- Destructor
284  virtual ~IOobject() = default;
285 
286 
287  // Constructors
288 
289  //- Construct from name, instance, registry, io options
290  IOobject
291  (
292  const word& name,
293  const fileName& instance,
294  const objectRegistry& registry,
297  bool registerObject=true
298  );
299 
300  //- Construct from name, instance, local, registry, io options
301  IOobject
302  (
303  const word& name,
304  const fileName& instance,
305  const fileName& local,
306  const objectRegistry& registry,
309  bool registerObject=true,
310  bool globalObject = false
311  );
312 
313  //- Construct from path, registry, io options.
314  // Uses fileNameComponents() to split path into components.
315  // A path that starts with a '/' is regarded as a file system path.
316  // Paths starting with either './' or '../' are relative to
317  // current working directory (and replaced with absolute equivalents).
318  // All other paths are considered to be relative to the case.
319  IOobject
320  (
321  const fileName& path,
322  const objectRegistry& registry,
325  bool registerObject=true,
326  bool globalObject = false
327  );
328 
329  //- Copy construct, resetting registry
330  IOobject
331  (
332  const IOobject& io,
333  const objectRegistry& registry
334  );
335 
336  //- Copy construct, resetting name
337  IOobject
338  (
339  const IOobject& io,
340  const word& name
341  );
342 
343  //- Clone
344  autoPtr<IOobject> clone() const
345  {
346  return autoPtr<IOobject>::New(*this);
347  }
348 
349  //- Clone resetting registry
350  autoPtr<IOobject> clone(const objectRegistry& registry) const
351  {
352  return autoPtr<IOobject>::New(*this, registry);
353  }
354 
355 
356  // Member Functions
357 
358  // General access
359 
360  //- Return the local objectRegistry
361  const objectRegistry& db() const;
362 
363  //- Return time
364  const Time& time() const;
365 
366  //- Return name
367  inline const word& name() const;
368 
369  //- Return name of the class name read from header
370  inline const word& headerClassName() const;
371 
372  //- Return non-constant access to the class name read from header
373  inline word& headerClassName();
374 
375  //- Return the optional note
376  inline const string& note() const;
377 
378  //- Return non-constant access to the optional note
379  inline string& note();
380 
381  //- Rename
382  virtual void rename(const word& newName)
383  {
384  name_ = newName;
385  }
386 
387  //- Should object created with this IOobject be registered?
388  inline bool registerObject() const;
389 
390  //- Should object created with this IOobject be registered?
391  inline bool& registerObject();
392 
393  //- Is object same for all processors?
394  inline bool globalObject() const;
395 
396  //- Is object same for all processors?
397  inline bool& globalObject();
398 
399  //- The label byte-size, possibly read from the header
400  inline unsigned labelByteSize() const;
401 
402  //- The scalar byte-size, possibly read from the header
403  inline unsigned scalarByteSize() const;
404 
405 
406  // Checks
407 
408  //- Test if headerClassName() equals the given class name
409  inline bool isHeaderClassName(const word& clsName) const;
410 
411  //- Test if headerClassName() equals Type::typeName
412  template<class Type>
413  inline bool isHeaderClassName() const;
414 
415 
416  // Read/write options
417 
418  //- The read option
419  inline readOption readOpt() const;
420 
421  //- Non-constant access to the read option
422  inline readOption& readOpt();
423 
424  //- The write option
425  inline writeOption writeOpt() const;
426 
427  //- Non-constant access to the write option
428  inline writeOption& writeOpt();
429 
430 
431  // Path components
432 
433  //- Return group (extension part of name)
434  inline word group() const;
435 
436  //- Return member (name without the extension)
437  inline word member() const;
438 
439  const fileName& rootPath() const;
440 
441  const fileName& caseName() const;
442 
443  inline const fileName& instance() const;
444 
445  inline fileName& instance();
446 
447  inline const fileName& local() const;
448 
449  //- The complete path
450  fileName path() const;
451 
452  //- The complete path with alternative instance and local
453  fileName path
454  (
455  const word& instance,
456  const fileName& local = fileName::null
457  ) const;
458 
459  //- The complete path + object name
460  inline fileName objectPath() const;
461 
462  //- Helper for filePath that searches locally.
463  // When search is false, simply use the current instance,
464  // otherwise search previous instances.
466  (
467  const word& typeName,
468  const bool search=true
469  ) const;
470 
471  //- Helper for filePath that searches up if in parallel
472  // When search is false, simply use the current instance,
473  // otherwise search previous instances.
475  (
476  const word& typeName,
477  const bool search=true
478  ) const;
479 
480 
481  // Reading
482 
483  //- Read header
484  bool readHeader(Istream& is);
485 
486  //- Read header (uses typeFilePath to find file) and check its info.
487  // Optionally checks headerClassName against the type-name.
488  // When search is false, simply use the current instance,
489  // otherwise search previous instances.
490  template<class Type>
491  bool typeHeaderOk
492  (
493  const bool checkType = true,
494  const bool search = true,
495  const bool verbose = true
496  );
497 
498  //- Helper: warn that type does not support re-reading
499  template<class Type>
500  void warnNoRereading() const;
501 
502 
503  // Writing
504 
505  //- Write the standard OpenFOAM file/dictionary banner
506  // Optionally without -*- C++ -*- editor hint (eg, for logs)
507  static Ostream& writeBanner(Ostream& os, bool noHint=false);
508 
509  //- Write the standard file section divider
510  static Ostream& writeDivider(Ostream& os);
511 
512  //- Write the standard end file divider
513  static Ostream& writeEndDivider(Ostream& os);
514 
515  //- Write header
516  bool writeHeader(Ostream& os) const;
517 
518  //- Write header. Allow override of type
519  bool writeHeader(Ostream& os, const word& objectType) const;
520 
521 
522  // Error Handling
523 
524  inline bool good() const;
525 
526  inline bool bad() const;
527 
528 
529  // Info
530 
531  //- Return info proxy.
532  // Used to print token information to a stream
533  InfoProxy<IOobject> info() const
534  {
535  return *this;
536  }
537 
538 
539  // Member Operators
540 
541  void operator=(const IOobject& io);
542 };
543 
544 
545 //- Specialization for \c void always returns true (no headerClassName check).
546 template<>
547 inline bool IOobject::isHeaderClassName<void>() const
548 {
549  return true;
550 }
551 
552 
553 template<>
555 
556 
557 //- Template function for obtaining global vs. local status
558 template<class T>
559 inline bool typeGlobal()
560 {
561  return false;
562 }
563 
564 
565 //- Template function for obtaining local or global filePath
566 template<class T>
567 inline fileName typeFilePath(const IOobject& io, const bool search = true)
568 {
569  return
570  (
571  typeGlobal<T>()
572  ? io.globalFilePath(T::typeName, search)
573  : io.localFilePath(T::typeName, search)
574  );
575 }
576 
577 
578 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
579 
580 } // End namespace Foam
581 
582 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
583 
584 #include "IOobjectI.H"
585 
586 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
587 
588 #ifdef NoRepository
589 # include "IOobjectTemplates.C"
590 #endif
591 
592 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
593 
594 #endif
595 
596 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::IOobject::fileCheckTypes
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:134
Foam::Enum< fileCheckTypes >
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:129
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::IOobject::scopedName
static word scopedName(StringType scope, const word &name)
Create scope:name or scope_name string.
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::IOobject::scalarByteSize
unsigned scalarByteSize() const
The scalar byte-size, possibly read from the header.
Definition: IOobjectI.H:142
Foam::IOobject::localFilePath
fileName localFilePath(const word &typeName, const bool search=true) const
Helper for filePath that searches locally.
Definition: IOobject.C:502
Foam::IOobject::timeStamp
Definition: IOobject.H:136
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOobject::fileCheckTypesNames
static const Enum< fileCheckTypes > fileCheckTypesNames
Names for the fileCheckTypes.
Definition: IOobject.H:143
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:47
IOobjectTemplates.C
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
typeInfo.H
Foam::IOobject::~IOobject
virtual ~IOobject()=default
Destructor.
Foam::IOobject::writeBanner
static Ostream & writeBanner(Ostream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectWriteHeader.C:45
Foam::IOobject::typeHeaderOk
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
Definition: IOobjectTemplates.C:39
InfoProxy.H
Foam::IOobject::writeEndDivider
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
Definition: IOobjectWriteHeader.C:109
Foam::IOobject::rootPath
const fileName & rootPath() const
Definition: IOobject.C:455
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobjectI.H:191
Foam::IOobject::inotifyMaster
Definition: IOobject.H:139
Foam::IOobject::timeStampMaster
Definition: IOobject.H:137
Foam::IOobject::isHeaderClassName
bool isHeaderClassName() const
Test if headerClassName() equals Type::typeName.
Definition: IOobjectI.H:157
Foam::IOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:211
Foam::IOobject::scopeSeparator
static char scopeSeparator
Character for scoping object names (':' or '_')
Definition: IOobject.H:208
Foam::IOobject::IOobject
IOobject(const IOobject &)=default
Copy construct.
Foam::IOobject::TypeName
TypeName("IOobject")
Declare type-name, virtual type (with debug switch)
Foam::IOobject::labelByteSize
unsigned labelByteSize() const
The label byte-size, possibly read from the header.
Definition: IOobjectI.H:136
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:449
Foam::IOobject::info
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:532
Foam::IOobject::inotify
Definition: IOobject.H:138
Foam::IOobject::objectState
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:111
Foam::IOobject::readHeader
bool readHeader(Istream &is)
Read header.
Definition: IOobjectReadHeader.C:35
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:443
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:127
Foam::IOobject::registerObject
bool registerObject() const
Should object created with this IOobject be registered?
Definition: IOobjectI.H:112
Foam::IOobject::warnNoRereading
void warnNoRereading() const
Helper: warn that type does not support re-reading.
Definition: IOobjectTemplates.C:88
Foam::IOobject::local
const fileName & local() const
Definition: IOobjectI.H:203
Foam::IOobject::clone
autoPtr< IOobject > clone(const objectRegistry &registry) const
Clone resetting registry.
Definition: IOobject.H:349
Foam::IOobject::writeOpt
writeOption writeOpt() const
The write option.
Definition: IOobjectI.H:177
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOobject::caseName
const fileName & caseName() const
Definition: IOobject.C:461
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
Foam::IOobject::globalObject
bool globalObject() const
Is object same for all processors?
Definition: IOobjectI.H:124
fileName.H
Foam::IOobject::clone
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:343
Foam::IOobject::selectIO
static IOobject selectIO(const IOobject &io, const fileName &altFile, const word &ioName="")
Return the IOobject, but also consider an alternative file name.
Definition: IOobject.C:214
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:123
Foam::IOobject::globalFilePath
fileName globalFilePath(const word &typeName, const bool search=true) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:513
Foam::IOobject::BAD
Definition: IOobject.H:114
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOobject::good
bool good() const
Definition: IOobjectI.H:217
Foam::IOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:381
Foam::fileName::null
static const fileName null
An empty fileName.
Definition: fileName.H:97
Foam::IOobject::operator=
void operator=(const IOobject &io)
Definition: IOobject.C:544
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobjectI.H:88
Foam::typeFilePath
fileName typeFilePath(const IOobject &io, const bool search=true)
Template function for obtaining local or global filePath.
Definition: IOobject.H:566
Foam::IOobject::note
const string & note() const
Return the optional note.
Definition: IOobjectI.H:100
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::IOobject::GOOD
Definition: IOobject.H:113
Foam::IOobject::writeDivider
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
Definition: IOobjectWriteHeader.C:99
IOobjectI.H
Foam::IOobject::member
word member() const
Return member (name without the extension)
Definition: IOobjectI.H:82
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:165
Foam::search
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:576
Foam::IOobject::group
word group() const
Return group (extension part of name)
Definition: IOobjectI.H:76
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:121
Foam::typeGlobal
bool typeGlobal()
Template function for obtaining global vs. local status.
Definition: IOobject.H:558
Foam::IOobject::setBad
void setBad(const string &s)
Set the object state to bad.
Definition: IOobject.C:523
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header.
Definition: IOobjectWriteHeader.C:156
Foam::IOobject::bad
bool bad() const
Definition: IOobjectI.H:223
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:118
Foam::IOobject::groupName
static word groupName(StringType base, const word &group)
Create dot-delimited name.group string.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:209
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:467
Foam::IOobject::MUST_READ
Definition: IOobject.H:120
autoPtr.H
Enum.H