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-2019 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  //- Runtime type information
202  TypeName("IOobject");
203 
204 
205  // Static Data Members
206 
207  //- Type of file modification checking
209 
210 
211  // Static Member Functions
212 
213  //- Split path into instance, local, name components
214  //
215  // The splitting behaviour is as follows:
216  // \verbatim
217  // input | instance | local | name
218  // ----------- | ---------- | ----- | ----
219  // a | | | a
220  // a/b | a | | b
221  // a/b/c/d | a | b/c | d
222  // /a/b/c | /a/b | | c
223  // ./a/b/c | PWD/a/b | | c
224  // ../a/b/c | PWD/../a/b | | c
225  // a/b/ | ERROR | |
226  // \endverbatim
227  // where PWD is the Foam::cwd() current working directory
228  static bool fileNameComponents
229  (
230  const fileName& path,
232  fileName& local,
233  word& name
234  );
235 
236  //- Create dot-delimited name.group
237  // If the group is an empty string, it is ignored.
238  template<class StringType>
239  static inline word groupName(StringType name, const word& group);
240 
241 
242  //- Return group (extension part of name)
243  static word group(const word& name);
244 
245  //- Return member (name without the extension)
246  static word member(const word& name);
247 
248  //- Return the IOobject, but also consider an alternative file name.
249  //
250  // \param io The expected IOobject to use
251  // \param altFile Alternative fileName (ignored if empty).
252  // \param ioName The alternative name for the IOobject when
253  // the altFile resolves to a directory.
254  //
255  // \note If the alternative fileName is a non-empty string,
256  // it defines the location but uses all other properties of the
257  // expected IOobject.
258  // The location may be an absolute or a relative path.
259  // If it corresponds to a directory, the name of the
260  // expected IOobject will be used in its resolution.
261  // This expected name can provided via the ioName parameter.
262  static IOobject selectIO
263  (
264  const IOobject& io,
265  const fileName& altFile,
266  const word& ioName = ""
267  );
268 
269 
270  // Constructors
271 
272  //- Construct from name, instance, registry, io options
273  IOobject
274  (
275  const word& name,
276  const fileName& instance,
277  const objectRegistry& registry,
280  bool registerObject=true
281  );
282 
283  //- Construct from name, instance, local, registry, io options
284  IOobject
285  (
286  const word& name,
287  const fileName& instance,
288  const fileName& local,
289  const objectRegistry& registry,
292  bool registerObject=true,
293  bool globalObject = false
294  );
295 
296  //- Construct from path, registry, io options.
297  // Uses fileNameComponents() to split path into components.
298  // A path that starts with a '/' is regarded as a file system path.
299  // Paths starting with either './' or '../' are relative to
300  // current working directory (and replaced with absolute equivalents).
301  // All other paths are considered to be relative to the case.
302  IOobject
303  (
304  const fileName& path,
305  const objectRegistry& registry,
308  bool registerObject=true,
309  bool globalObject = false
310  );
311 
312  //- Construct as copy resetting registry
313  IOobject
314  (
315  const IOobject& io,
316  const objectRegistry& registry
317  );
318 
319  //- Construct as copy resetting name
320  IOobject
321  (
322  const IOobject& io,
323  const word& name
324  );
325 
326  //- Clone
327  autoPtr<IOobject> clone() const
328  {
329  return autoPtr<IOobject>::New(*this);
330  }
331 
332  //- Clone resetting registry
333  autoPtr<IOobject> clone(const objectRegistry& registry) const
334  {
335  return autoPtr<IOobject>::New(*this, registry);
336  }
337 
338 
339  //- Destructor
340  virtual ~IOobject() = default;
341 
342 
343  // Member Functions
344 
345  // General access
346 
347  //- Return the local objectRegistry
348  const objectRegistry& db() const;
349 
350  //- Return time
351  const Time& time() const;
352 
353  //- Return name
354  inline const word& name() const;
355 
356  //- Return name of the class name read from header
357  inline const word& headerClassName() const;
358 
359  //- Return non-constant access to the class name read from header
360  inline word& headerClassName();
361 
362  //- Return the optional note
363  inline const string& note() const;
364 
365  //- Return non-constant access to the optional note
366  inline string& note();
367 
368  //- Rename
369  virtual void rename(const word& newName)
370  {
371  name_ = newName;
372  }
373 
374  //- Should object created with this IOobject be registered?
375  inline bool registerObject() const;
376 
377  //- Should object created with this IOobject be registered?
378  inline bool& registerObject();
379 
380  //- Is object same for all processors?
381  inline bool globalObject() const;
382 
383  //- Is object same for all processors?
384  inline bool& globalObject();
385 
386  //- The label byte-size, possibly read from the header
387  inline unsigned labelByteSize() const;
388 
389  //- The scalar byte-size, possibly read from the header
390  inline unsigned scalarByteSize() const;
391 
392 
393  // Checks
394 
395  //- Test if headerClassName() equals the given class name
396  inline bool isHeaderClassName(const word& clsName) const;
397 
398  //- Test if headerClassName() equals Type::typeName
399  template<class Type>
400  inline bool isHeaderClassName() const;
401 
402 
403  // Read/write options
404 
405  //- The read option
406  inline readOption readOpt() const;
407 
408  //- Non-constant access to the read option
409  inline readOption& readOpt();
410 
411  //- The write option
412  inline writeOption writeOpt() const;
413 
414  //- Non-constant access to the write option
415  inline writeOption& writeOpt();
416 
417 
418  // Path components
419 
420  //- Return group (extension part of name)
421  inline word group() const;
422 
423  //- Return member (name without the extension)
424  inline word member() const;
425 
426  const fileName& rootPath() const;
427 
428  const fileName& caseName() const;
429 
430  inline const fileName& instance() const;
431 
432  inline fileName& instance();
433 
434  inline const fileName& local() const;
435 
436  //- The complete path
437  fileName path() const;
438 
439  //- The complete path with alternative instance and local
440  fileName path
441  (
442  const word& instance,
443  const fileName& local = fileName::null
444  ) const;
445 
446  //- The complete path + object name
447  inline fileName objectPath() const;
448 
449  //- Helper for filePath that searches locally.
450  // When search is false, simply use the current instance,
451  // otherwise search previous instances.
453  (
454  const word& typeName,
455  const bool search=true
456  ) const;
457 
458  //- Helper for filePath that searches up if in parallel
459  // When search is false, simply use the current instance,
460  // otherwise search previous instances.
462  (
463  const word& typeName,
464  const bool search=true
465  ) const;
466 
467 
468  // Reading
469 
470  //- Read header
471  bool readHeader(Istream& is);
472 
473  //- Read header (uses typeFilePath to find file) and check its info.
474  // Optionally checks headerClassName against the type-name.
475  // When search is false, simply use the current instance,
476  // otherwise search previous instances.
477  template<class Type>
478  bool typeHeaderOk
479  (
480  const bool checkType = true,
481  const bool search = true,
482  const bool verbose = true
483  );
484 
485  //- Helper: warn that type does not support re-reading
486  template<class Type>
487  void warnNoRereading() const;
488 
489 
490  // Writing
491 
492  //- Write the standard OpenFOAM file/dictionary banner
493  // Optionally without -*- C++ -*- editor hint (eg, for logs)
494  static Ostream& writeBanner(Ostream& os, bool noHint=false);
495 
496  //- Write the standard file section divider
497  static Ostream& writeDivider(Ostream& os);
498 
499  //- Write the standard end file divider
500  static Ostream& writeEndDivider(Ostream& os);
501 
502  //- Write header
503  bool writeHeader(Ostream& os) const;
504 
505  //- Write header. Allow override of type
506  bool writeHeader(Ostream& os, const word& objectType) const;
507 
508 
509  // Error Handling
510 
511  inline bool good() const;
512 
513  inline bool bad() const;
514 
515 
516  // Info
517 
518  //- Return info proxy.
519  // Used to print token information to a stream
520  InfoProxy<IOobject> info() const
521  {
522  return *this;
523  }
524 
525 
526  // Member Operators
527 
528  void operator=(const IOobject& io);
529 };
530 
531 
532 //- Specialization for \c void always returns true (no headerClassName check).
533 template<>
534 inline bool IOobject::isHeaderClassName<void>() const
535 {
536  return true;
537 }
538 
539 
540 template<>
542 
543 
544 //- Template function for obtaining global vs. local status
545 template<class T>
546 inline bool typeGlobal()
547 {
548  return false;
549 }
550 
551 
552 //- Template function for obtaining local or global filePath
553 template<class T>
554 inline fileName typeFilePath(const IOobject& io, const bool search = true)
555 {
556  return
557  (
558  typeGlobal<T>()
559  ? io.globalFilePath(T::typeName, search)
560  : io.localFilePath(T::typeName, search)
561  );
562 }
563 
564 
565 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
566 
567 } // End namespace Foam
568 
569 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
570 
571 #include "IOobjectI.H"
572 
573 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
574 
575 #ifdef NoRepository
576 # include "IOobjectTemplates.C"
577 #endif
578 
579 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
580 
581 #endif
582 
583 // ************************************************************************* //
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::name
const word & name() const
Return name.
Definition: IOobjectI.H:46
Foam::IOobject::scalarByteSize
unsigned scalarByteSize() const
The scalar byte-size, possibly read from the header.
Definition: IOobjectI.H:118
Foam::IOobject::localFilePath
fileName localFilePath(const word &typeName, const bool search=true) const
Helper for filePath that searches locally.
Definition: IOobject.C:482
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:444
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobjectI.H:167
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:133
Foam::IOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:207
Foam::IOobject::TypeName
TypeName("IOobject")
Runtime type information.
Foam::IOobject::labelByteSize
unsigned labelByteSize() const
The label byte-size, possibly read from the header.
Definition: IOobjectI.H:112
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:438
Foam::IOobject::info
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:519
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:432
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
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:88
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:179
Foam::IOobject::clone
autoPtr< IOobject > clone(const objectRegistry &registry) const
Clone resetting registry.
Definition: IOobject.H:332
Foam::IOobject::writeOpt
writeOption writeOpt() const
The write option.
Definition: IOobjectI.H:153
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:450
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
Foam::IOobject::globalObject
bool globalObject() const
Is object same for all processors?
Definition: IOobjectI.H:100
fileName.H
Foam::IOobject::clone
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:326
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:203
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:112
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:493
Foam::IOobject::BAD
Definition: IOobject.H:114
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOobject::good
bool good() const
Definition: IOobjectI.H:193
Foam::IOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:368
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:524
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobjectI.H:64
Foam::typeFilePath
fileName typeFilePath(const IOobject &io, const bool search=true)
Template function for obtaining local or global filePath.
Definition: IOobject.H:553
Foam::IOobject::note
const string & note() const
Return the optional note.
Definition: IOobjectI.H:76
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
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:276
IOobjectI.H
Foam::IOobject::groupName
static word groupName(StringType name, const word &group)
Create dot-delimited name.group.
Foam::IOobject::member
word member() const
Return member (name without the extension)
Definition: IOobjectI.H:58
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:141
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:52
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:545
Foam::IOobject::setBad
void setBad(const string &s)
Set the object state to bad.
Definition: IOobject.C:503
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header.
Definition: IOobjectWriteHeader.C:156
Foam::IOobject::bad
bool bad() const
Definition: IOobjectI.H:199
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:118
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:185
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:456
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::IOobject::MUST_READ
Definition: IOobject.H:120
autoPtr.H
Enum.H