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 | Copyright (C) 2011-2017 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  //- Type of file modification checking
210 
211 
212  // Constructors
213 
214  //- Construct from name, instance, registry, io options
215  IOobject
216  (
217  const word& name,
218  const fileName& instance,
219  const objectRegistry& registry,
220  readOption r=NO_READ,
222  bool registerObject=true
223  );
224 
225  //- Construct from name, instance, local, registry, io options
226  IOobject
227  (
228  const word& name,
229  const fileName& instance,
230  const fileName& local,
231  const objectRegistry& registry,
232  readOption r=NO_READ,
234  bool registerObject=true,
235  bool globalObject = false
236  );
237 
238  //- Construct from path, registry, io options
239  // Uses fileNameComponents() to split path into components.
240  IOobject
241  (
242  const fileName& path,
243  const objectRegistry& registry,
244  readOption r=NO_READ,
246  bool registerObject=true,
247  bool globalObject = false
248  );
249 
250  //- Construct from copy resetting registry
251  IOobject
252  (
253  const IOobject& io,
254  const objectRegistry& registry
255  );
256 
257  //- Construct from copy resetting name
258  IOobject
259  (
260  const IOobject& io,
261  const word& name
262  );
263 
264  //- Clone
265  autoPtr<IOobject> clone() const
266  {
267  return autoPtr<IOobject>(new IOobject(*this));
268  }
269 
270  //- Clone resetting registry
271  autoPtr<IOobject> clone(const objectRegistry& registry) const
272  {
273  return autoPtr<IOobject>(new IOobject(*this, registry));
274  }
275 
276 
277  //- Destructor
278  virtual ~IOobject();
279 
280 
281  // Member Functions
282 
283  // General access
284 
285  //- Return time
286  const Time& time() const;
287 
288  //- Return the local objectRegistry
289  const objectRegistry& db() const;
290 
291  //- Return name
292  const word& name() const
293  {
294  return name_;
295  }
296 
297  //- Return name of the class name read from header
298  const word& headerClassName() const
299  {
300  return headerClassName_;
301  }
302 
303  //- Return name of the class name read from header
305  {
306  return headerClassName_;
307  }
308 
309  //- Return non-constant access to the optional note
310  string& note()
311  {
312  return note_;
313  }
314 
315  //- Return the optional note
316  const string& note() const
317  {
318  return note_;
319  }
320 
321  //- Rename
322  virtual void rename(const word& newName)
323  {
324  name_ = newName;
325  }
326 
327  //- Register object created from this IOobject with registry if true
328  bool& registerObject()
329  {
330  return registerObject_;
331  }
332 
333  //- Register object created from this IOobject with registry if true
334  bool registerObject() const
335  {
336  return registerObject_;
337  }
338 
339  //- Is object same for all processors
340  bool& globalObject()
341  {
342  return globalObject_;
343  }
344 
345  //- Is object same for all processors
346  bool globalObject() const
347  {
348  return globalObject_;
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  const fileName& rootPath() const;
384 
385  const fileName& caseName() const;
387  const fileName& instance() const
388  {
389  return instance_;
390  }
392  fileName& instance()
393  {
394  return instance_;
395  }
397  const fileName& local() const
398  {
399  return local_;
400  }
401 
402  //- Return complete path
403  fileName path() const;
404 
405  //- Return complete path with alternative instance and local
406  fileName path
407  (
408  const word& instance,
409  const fileName& local = ""
410  ) const;
411 
412  //- Return complete path + object name
413  fileName objectPath() const
414  {
415  return path()/name();
416  }
417 
418  //- Helper for filePath that searches locally.
419  fileName localFilePath(const word& typeName) const;
420 
421  //- Helper for filePath that searches up if in parallel
422  fileName globalFilePath(const word& typeName) const;
423 
424 
425  // Reading
426 
427  //- Read header
428  bool readHeader(Istream&);
429 
430  //- Read header (uses typeFilePath to find file) and check header
431  // info. Optionally checks headerClassName against type
432  template<class Type>
433  bool typeHeaderOk(const bool checkType = true);
434 
435  //- Helper: warn that type does not support re-reading
436  template<class Type>
437  void warnNoRereading() const;
438 
439  // Writing
440 
441  //- Write the standard OpenFOAM file/dictionary banner
442  // Optionally without -*- C++ -*- editor hint (eg, for logs)
443  template<class Stream>
444  static inline Stream& writeBanner(Stream& os, bool noHint=false);
445 
446  //- Write the standard file section divider
447  template<class Stream>
448  static inline Stream& writeDivider(Stream& os);
449 
450  //- Write the standard end file divider
451  template<class Stream>
452  static inline Stream& writeEndDivider(Stream& os);
453 
454  //- Write header
455  bool writeHeader(Ostream&) const;
456 
457  //- Write header. Allow override of type
458  bool writeHeader(Ostream&, const word& objectType) const;
459 
460 
461  // Error Handling
463  bool good() const
464  {
465  return objState_ == GOOD;
466  }
468  bool bad() const
469  {
470  return objState_ == BAD;
471  }
472 
473 
474  // Info
475 
476  //- Return info proxy.
477  // Used to print token information to a stream
478  InfoProxy<IOobject> info() const
479  {
480  return *this;
481  }
482 
483 
484  // Member operators
485 
486  void operator=(const IOobject&);
487 };
488 
489 
490 template<>
491 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
492 
493 
494 //- Template function for obtaining global status
495 template<class T>
496 inline bool typeGlobal()
497 {
498  return false;
499 }
500 
501 //- Template function for obtaining filePath
502 template<class T>
503 inline fileName typeFilePath(const IOobject& io)
504 {
505  return
506  (
507  typeGlobal<T>()
508  ? io.globalFilePath(T::typeName)
509  : io.localFilePath(T::typeName)
510  );
511 }
512 
513 
514 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
515 
516 } // End namespace Foam
517 
518 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
519 
520 #include "IOobjectI.H"
521 
522 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
523 
524 #ifdef NoRepository
525 # include "IOobjectTemplates.C"
526 #endif
527 
528 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
529 
530 #endif
531 
532 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:123
autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:264
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:462
const word & name() const
Return name.
Definition: IOobject.H:291
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:495
void operator=(const IOobject &)
Definition: IOobject.C:442
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:98
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:179
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:321
word member() const
Return member (name without the extension)
Definition: IOobject.C:364
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:100
virtual ~IOobject()
Destructor.
Definition: IOobject.C:325
word group() const
Return group (extension part of name)
Definition: IOobject.C:349
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:385
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:409
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:309
bool bad() const
Definition: IOobject.H:467
A class for handling words, derived from string.
Definition: word.H:59
bool & globalObject()
Is object same for all processors.
Definition: IOobject.H:339
static word groupName(Name name, const word &group)
const fileName & local() const
Definition: IOobject.H:396
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:423
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:416
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:208
fileName typeFilePath(const IOobject &io)
Template function for obtaining filePath.
Definition: IOobject.H:502
const fileName & rootPath() const
Definition: IOobject.C:379
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:131
writeOption writeOpt() const
Definition: IOobject.H:363
const fileName & instance() const
Definition: IOobject.H:386
static Stream & writeEndDivider(Stream &os)
Write the standard end file divider.
Definition: IOobjectI.H:108
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
const Time & time() const
Return time.
Definition: IOobject.C:337
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:477
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:331
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:327
readOption readOpt() const
Definition: IOobject.H:353
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:297
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:343
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:412
void warnNoRereading() const
Helper: warn that type does not support re-reading.
Namespace for OpenFOAM.