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-2020 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 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 #include "NamedEnum.H"
80 
81 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
82 
83 namespace Foam
84 {
85 
86 class Time;
87 class objectRegistry;
88 
89 /*---------------------------------------------------------------------------*\
90  Class IOobject Declaration
91 \*---------------------------------------------------------------------------*/
92 
93 class IOobject
94 {
95 
96 public:
97 
98  //- Keyword for the FoamFile header sub-dictionary
99  static constexpr const char* foamFile = "FoamFile";
100 
101  // Public data types
102 
103  //- Enumeration defining the valid states of an IOobject
104  enum objectState
105  {
107  BAD
108  };
109 
110  //- Enumeration defining the read options
111  enum readOption
112  {
116  NO_READ
117  };
118 
119  //- Enumeration defining the write options
120  enum writeOption
121  {
123  NO_WRITE = 1
124  };
125 
126  //- Enumeration defining the file checking options
127  enum fileCheckTypes
128  {
133  };
136 
137 private:
138 
139  // Private Data
140 
141  //- Name
142  word name_;
143 
144  //- Class name read from header
145  word headerClassName_;
146 
147  //- Optional note
148  string note_;
149 
150  //- Instance path component
151  fileName instance_;
152 
153  //- Local path component
154  fileName local_;
155 
156  //- objectRegistry reference
157  const objectRegistry& db_;
158 
159  //- Read option
160  readOption rOpt_;
161 
162  //- Write option
163  writeOption wOpt_;
164 
165  //- Register object created from this IOobject with registry if true
166  bool registerObject_;
167 
168  //- IOobject state
169  objectState objState_;
170 
171 
172 protected:
173 
174  // Protected Member Functions
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  //- Return group (extension part of name)
209  static word group(const word& name);
210 
211  //- Return member (name without the extension)
212  static word member(const word& name);
213 
214  //- Return the name of the object within the given model
215  // as <model>:<name>
216  template<class Name>
217  static inline word modelName(Name name, const word& model);
218 
219  //- Type of file modification checking
221 
222 
223  // Constructors
224 
225  //- Construct from name, instance, registry, io options
226  IOobject
227  (
228  const word& name,
229  const fileName& instance,
230  const objectRegistry& registry,
231  readOption r=NO_READ,
233  bool registerObject=true
234  );
235 
236  //- Construct from name, instance, local, registry, io options
237  IOobject
238  (
239  const word& name,
240  const fileName& instance,
241  const fileName& local,
242  const objectRegistry& registry,
243  readOption r=NO_READ,
245  bool registerObject=true
246  );
247 
248  //- Construct from path, registry, io options
249  // Uses fileNameComponents() to split path into components.
250  IOobject
251  (
252  const fileName& path,
253  const objectRegistry& registry,
254  readOption r=NO_READ,
256  bool registerObject=true
257  );
258 
259  //- Construct from copy resetting registry
260  IOobject
261  (
262  const IOobject& io,
263  const objectRegistry& registry
264  );
265 
266  //- Construct from copy resetting name
267  IOobject
268  (
269  const IOobject& io,
270  const word& name
271  );
272 
273  //- Copy constructor
274  IOobject(const IOobject& io) = default;
275 
276  //- Clone
277  autoPtr<IOobject> clone() const
278  {
279  return autoPtr<IOobject>(new IOobject(*this));
280  }
281 
282  //- Clone resetting registry
283  autoPtr<IOobject> clone(const objectRegistry& registry) const
284  {
285  return autoPtr<IOobject>(new IOobject(*this, registry));
286  }
287 
288 
289  //- Destructor
290  virtual ~IOobject();
291 
292 
293  // Member Functions
294 
295  // General access
296 
297  //- Return time
298  const Time& time() const;
299 
300  //- Return the local objectRegistry
301  const objectRegistry& db() const;
302 
303  //- Return name
304  const word& name() const
305  {
306  return name_;
307  }
308 
309  //- Return name of the class name read from header
310  const word& headerClassName() const
311  {
312  return headerClassName_;
313  }
314 
315  //- Return name of the class name read from header
317  {
318  return headerClassName_;
319  }
320 
321  //- Return non-constant access to the optional note
322  string& note()
323  {
324  return note_;
325  }
326 
327  //- Return the optional note
328  const string& note() const
329  {
330  return note_;
331  }
332 
333  //- Rename
334  virtual void rename(const word& newName)
335  {
336  name_ = newName;
337  }
338 
339  //- Register object created from this IOobject with registry if true
340  bool& registerObject()
341  {
342  return registerObject_;
343  }
344 
345  //- Register object created from this IOobject with registry if true
346  bool registerObject() const
347  {
348  return registerObject_;
349  }
350 
351 
352  // Read/write options
354  readOption readOpt() const
355  {
356  return rOpt_;
357  }
360  {
361  return rOpt_;
362  }
364  writeOption writeOpt() const
365  {
366  return wOpt_;
367  }
370  {
371  return wOpt_;
372  }
373 
374 
375  // Path components
376 
377  //- Return group (extension part of name)
378  word group() const;
379 
380  //- Return member (name without the extension)
381  word member() const;
382 
383  //- Return the name of the object within this model
384  // as <model>:<name>
385  inline word modelName(const char* name) const;
386 
387  const fileName& rootPath() const;
388 
389  const fileName& caseName() const;
391  const fileName& instance() const
392  {
393  return instance_;
394  }
396  fileName& instance()
397  {
398  return instance_;
399  }
401  const fileName& local() const
402  {
403  return local_;
404  }
405 
406  //- Return complete path
407  fileName path() const;
408 
409  //- Return complete path with alternative instance and local
410  fileName path
411  (
412  const word& instance,
413  const fileName& local = ""
414  ) const;
415 
416  //- Return the path relative to the case
417  fileName localPath() const;
418 
419  //- Return complete path + object name
420  fileName objectPath() const
421  {
422  return path()/name();
423  }
424 
425  //- Return complete localPath + object name
426  fileName localObjectPath() const
427  {
428  return localPath()/name();
429  }
430 
431  //- Helper for filePath that searches locally.
432  fileName localFilePath(const word& typeName) const;
433 
434  //- Helper for filePath that searches up if in parallel
435  fileName globalFilePath(const word& typeName) const;
436 
437 
438  // Reading
439 
440  //- Read header
441  bool readHeader(Istream&);
442 
443  //- Read header (uses typeFilePath to find file) and check header
444  // info. Optionally checks headerClassName against type
445  template<class Type>
446  bool typeHeaderOk(const bool checkType = true);
447 
448  //- Helper: warn that type does not support re-reading
449  template<class Type>
450  void warnNoRereading() const;
451 
452  // Writing
453 
454  //- Write the standard OpenFOAM file/dictionary banner
455  // Optionally without -*- C++ -*- editor hint (eg, for logs)
456  template<class Stream>
457  static inline Stream& writeBanner(Stream& os, bool noHint=false);
458 
459  //- Write the standard file section divider
460  template<class Stream>
461  static inline Stream& writeDivider(Stream& os);
462 
463  //- Write the standard end file divider
464  template<class Stream>
465  static inline Stream& writeEndDivider(Stream& os);
466 
467  //- Write header
468  bool writeHeader(Ostream&) const;
469 
470  //- Write header. Allow override of type
471  bool writeHeader(Ostream&, const word& objectType) const;
472 
473 
474  // Error Handling
476  bool good() const
477  {
478  return objState_ == GOOD;
479  }
481  bool bad() const
482  {
483  return objState_ == BAD;
484  }
485 
486 
487  // Info
488 
489  //- Return info proxy.
490  // Used to print token information to a stream
491  InfoProxy<IOobject> info() const
492  {
493  return *this;
494  }
495 
496 
497  // Member Operators
498 
499  void operator=(const IOobject&);
500 };
501 
502 
503 template<>
504 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
505 
506 
507 //- Template function for obtaining global status
508 template<class T>
509 inline bool typeGlobal()
510 {
511  return false;
512 }
513 
514 //- Template function for obtaining filePath
515 template<class T>
516 inline fileName typeFilePath(const IOobject& io)
517 {
518  return
519  (
520  typeGlobal<T>()
521  ? io.globalFilePath(T::typeName)
522  : io.localFilePath(T::typeName)
523  );
524 }
526 inline IOobject unregister(const IOobject& io)
527 {
528  IOobject uio(io);
529  uio.registerObject() = false;
530  return uio;
531 }
532 
533 
534 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
535 
536 } // End namespace Foam
537 
538 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
539 
540 #include "IOobjectI.H"
541 
542 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
543 
544 #ifdef NoRepository
545 # include "IOobjectTemplates.C"
546 #endif
547 
548 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
549 
550 #endif
551 
552 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:126
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:276
writeOption
Enumeration defining the write options.
Definition: IOobject.H:119
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:475
const word & name() const
Return name.
Definition: IOobject.H:303
bool typeHeaderOk(const bool checkType=true)
Read header (uses typeFilePath to find file) and check header.
A class for handling file names.
Definition: fileName.H:79
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:65
bool typeGlobal()
Template function for obtaining global status.
Definition: IOobject.H:508
void operator=(const IOobject &)
Definition: IOobject.C:418
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
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:333
word member() const
Return member (name without the extension)
Definition: IOobject.C:336
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:103
virtual ~IOobject()
Destructor.
Definition: IOobject.C:306
word group() const
Return group (extension part of name)
Definition: IOobject.C:330
readOption
Enumeration defining the read options.
Definition: IOobject.H:110
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
fileName path() const
Return complete path.
Definition: IOobject.C:348
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:385
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:321
bool bad() const
Definition: IOobject.H:480
A class for handling words, derived from string.
Definition: word.H:59
static word groupName(Name name, const word &group)
const fileName & local() const
Definition: IOobject.H:400
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:399
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:392
fileName localObjectPath() const
Return complete localPath + object name.
Definition: IOobject.H:425
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:98
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:219
fileName typeFilePath(const IOobject &io)
Template function for obtaining filePath.
Definition: IOobject.H:515
const fileName & rootPath() const
Definition: IOobject.C:342
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:134
writeOption writeOpt() const
Definition: IOobject.H:363
const fileName & instance() const
Definition: IOobject.H:390
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.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
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:490
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:339
readOption readOpt() const
Definition: IOobject.H:353
IOobject unregister(const IOobject &io)
Definition: IOobject.H:525
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:309
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
const fileName & caseName() const
Definition: IOobject.C:324
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:419
void warnNoRereading() const
Helper: warn that type does not support re-reading.
fileName localPath() const
Return the path relative to the case.
Definition: IOobject.C:372
Namespace for OpenFOAM.