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-2018 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  // Public data types
99 
100  //- Enumeration defining the valid states of an IOobject
101  enum objectState
102  {
104  BAD
105  };
106 
107  //- Enumeration defining the read options
108  enum readOption
109  {
113  NO_READ
114  };
115 
116  //- Enumeration defining the write options
117  enum writeOption
118  {
120  NO_WRITE = 1
121  };
122 
123  //- Enumeration defining the file checking options
124  enum fileCheckTypes
125  {
130  };
133 
134 private:
135 
136  // Private data
137 
138  //- Name
139  word name_;
140 
141  //- Class name read from header
142  word headerClassName_;
143 
144  //- Optional note
145  string note_;
146 
147  //- Instance path component
148  fileName instance_;
149 
150  //- Local path component
151  fileName local_;
152 
153  //- objectRegistry reference
154  const objectRegistry& db_;
155 
156  //- Read option
157  readOption rOpt_;
158 
159  //- Write option
160  writeOption wOpt_;
161 
162  //- Register object created from this IOobject with registry if true
163  bool registerObject_;
164 
165  //- Is object same for all processors
166  bool globalObject_;
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  //- Type of file modification checking
216 
217 
218  // Constructors
219 
220  //- Construct from name, instance, registry, io options
221  IOobject
222  (
223  const word& name,
224  const fileName& instance,
225  const objectRegistry& registry,
226  readOption r=NO_READ,
228  bool registerObject=true
229  );
230 
231  //- Construct from name, instance, local, registry, io options
232  IOobject
233  (
234  const word& name,
235  const fileName& instance,
236  const fileName& local,
237  const objectRegistry& registry,
238  readOption r=NO_READ,
240  bool registerObject=true,
241  bool globalObject = false
242  );
243 
244  //- Construct from path, registry, io options
245  // Uses fileNameComponents() to split path into components.
246  IOobject
247  (
248  const fileName& path,
249  const objectRegistry& registry,
250  readOption r=NO_READ,
252  bool registerObject=true,
253  bool globalObject = false
254  );
255 
256  //- Construct from copy resetting registry
257  IOobject
258  (
259  const IOobject& io,
260  const objectRegistry& registry
261  );
262 
263  //- Construct from copy resetting name
264  IOobject
265  (
266  const IOobject& io,
267  const word& name
268  );
269 
270  //- Clone
271  autoPtr<IOobject> clone() const
272  {
273  return autoPtr<IOobject>(new IOobject(*this));
274  }
275 
276  //- Clone resetting registry
277  autoPtr<IOobject> clone(const objectRegistry& registry) const
278  {
279  return autoPtr<IOobject>(new IOobject(*this, registry));
280  }
281 
282 
283  //- Destructor
284  virtual ~IOobject();
285 
286 
287  // Member Functions
288 
289  // General access
290 
291  //- Return time
292  const Time& time() const;
293 
294  //- Return the local objectRegistry
295  const objectRegistry& db() const;
296 
297  //- Return name
298  const word& name() const
299  {
300  return name_;
301  }
302 
303  //- Return name of the class name read from header
304  const word& headerClassName() const
305  {
306  return headerClassName_;
307  }
308 
309  //- Return name of the class name read from header
311  {
312  return headerClassName_;
313  }
314 
315  //- Return non-constant access to the optional note
316  string& note()
317  {
318  return note_;
319  }
320 
321  //- Return the optional note
322  const string& note() const
323  {
324  return note_;
325  }
326 
327  //- Rename
328  virtual void rename(const word& newName)
329  {
330  name_ = newName;
331  }
332 
333  //- Register object created from this IOobject with registry if true
334  bool& registerObject()
335  {
336  return registerObject_;
337  }
338 
339  //- Register object created from this IOobject with registry if true
340  bool registerObject() const
341  {
342  return registerObject_;
343  }
344 
345  //- Is object same for all processors
346  bool& globalObject()
347  {
348  return globalObject_;
349  }
350 
351  //- Is object same for all processors
352  bool globalObject() const
353  {
354  return globalObject_;
355  }
356 
357 
358  // Read/write options
360  readOption readOpt() const
361  {
362  return rOpt_;
363  }
366  {
367  return rOpt_;
368  }
370  writeOption writeOpt() const
371  {
372  return wOpt_;
373  }
376  {
377  return wOpt_;
378  }
379 
380 
381  // Path components
382 
383  //- Return group (extension part of name)
384  word group() const;
385 
386  //- Return member (name without the extension)
387  word member() const;
388 
389  const fileName& rootPath() const;
390 
391  const fileName& caseName() const;
393  const fileName& instance() const
394  {
395  return instance_;
396  }
398  fileName& instance()
399  {
400  return instance_;
401  }
403  const fileName& local() const
404  {
405  return local_;
406  }
407 
408  //- Return complete path
409  fileName path() const;
410 
411  //- Return complete path with alternative instance and local
412  fileName path
413  (
414  const word& instance,
415  const fileName& local = ""
416  ) const;
417 
418  //- Return complete path + object name
419  fileName objectPath() const
420  {
421  return path()/name();
422  }
423 
424  //- Helper for filePath that searches locally.
425  fileName localFilePath(const word& typeName) const;
426 
427  //- Helper for filePath that searches up if in parallel
428  fileName globalFilePath(const word& typeName) const;
429 
430 
431  // Reading
432 
433  //- Read header
434  bool readHeader(Istream&);
435 
436  //- Read header (uses typeFilePath to find file) and check header
437  // info. Optionally checks headerClassName against type
438  template<class Type>
439  bool typeHeaderOk(const bool checkType = true);
440 
441  //- Helper: warn that type does not support re-reading
442  template<class Type>
443  void warnNoRereading() const;
444 
445  // Writing
446 
447  //- Write the standard OpenFOAM file/dictionary banner
448  // Optionally without -*- C++ -*- editor hint (eg, for logs)
449  template<class Stream>
450  static inline Stream& writeBanner(Stream& os, bool noHint=false);
451 
452  //- Write the standard file section divider
453  template<class Stream>
454  static inline Stream& writeDivider(Stream& os);
455 
456  //- Write the standard end file divider
457  template<class Stream>
458  static inline Stream& writeEndDivider(Stream& os);
459 
460  //- Write header
461  bool writeHeader(Ostream&) const;
462 
463  //- Write header. Allow override of type
464  bool writeHeader(Ostream&, const word& objectType) const;
465 
466 
467  // Error Handling
469  bool good() const
470  {
471  return objState_ == GOOD;
472  }
474  bool bad() const
475  {
476  return objState_ == BAD;
477  }
478 
479 
480  // Info
481 
482  //- Return info proxy.
483  // Used to print token information to a stream
484  InfoProxy<IOobject> info() const
485  {
486  return *this;
487  }
488 
489 
490  // Member operators
491 
492  void operator=(const IOobject&);
493 };
494 
495 
496 template<>
497 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
498 
499 
500 //- Template function for obtaining global status
501 template<class T>
502 inline bool typeGlobal()
503 {
504  return false;
505 }
506 
507 //- Template function for obtaining filePath
508 template<class T>
509 inline fileName typeFilePath(const IOobject& io)
510 {
511  return
512  (
513  typeGlobal<T>()
514  ? io.globalFilePath(T::typeName)
515  : io.localFilePath(T::typeName)
516  );
517 }
518 
519 
520 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
521 
522 } // End namespace Foam
523 
524 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
525 
526 #include "IOobjectI.H"
527 
528 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
529 
530 #ifdef NoRepository
531 # include "IOobjectTemplates.C"
532 #endif
533 
534 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
535 
536 #endif
537 
538 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:123
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:270
writeOption
Enumeration defining the write options.
Definition: IOobject.H:116
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:106
bool good() const
Definition: IOobject.H:468
const word & name() const
Return name.
Definition: IOobject.H:297
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:69
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:45
bool typeGlobal()
Template function for obtaining global status.
Definition: IOobject.H:501
void operator=(const IOobject &)
Definition: IOobject.C:454
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:93
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:209
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:327
word member() const
Return member (name without the extension)
Definition: IOobject.C:385
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:100
virtual ~IOobject()
Destructor.
Definition: IOobject.C:355
word group() const
Return group (extension part of name)
Definition: IOobject.C:379
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
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:397
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:421
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:315
bool bad() const
Definition: IOobject.H:473
A class for handling words, derived from string.
Definition: word.H:59
bool & globalObject()
Is object same for all processors.
Definition: IOobject.H:345
static word groupName(Name name, const word &group)
const fileName & local() const
Definition: IOobject.H:402
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:435
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:428
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:214
fileName typeFilePath(const IOobject &io)
Template function for obtaining filePath.
Definition: IOobject.H:508
const fileName & rootPath() const
Definition: IOobject.C:391
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:131
writeOption writeOpt() const
Definition: IOobject.H:369
const fileName & instance() const
Definition: IOobject.H:392
static Stream & writeEndDivider(Stream &os)
Write the standard end file divider.
Definition: IOobjectI.H:103
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
const Time & time() const
Return time.
Definition: IOobject.C:367
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
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:483
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:361
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:333
readOption readOpt() const
Definition: IOobject.H:359
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:303
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:373
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:418
void warnNoRereading() const
Helper: warn that type does not support re-reading.
Namespace for OpenFOAM.