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-2016 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 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 class Time;
86 class objectRegistry;
87 
88 /*---------------------------------------------------------------------------*\
89  Class IOobject Declaration
90 \*---------------------------------------------------------------------------*/
91 
92 class IOobject
93 {
94 
95 public:
96 
97  // Public data types
98 
99  //- Enumeration defining the valid states of an IOobject
100  enum objectState
101  {
103  BAD
104  };
105 
106  //- Enumeration defining the read options
107  enum readOption
108  {
112  NO_READ
113  };
114 
115  //- Enumeration defining the write options
116  enum writeOption
117  {
119  NO_WRITE = 1
120  };
121 
122 
123 private:
124 
125  // Private data
126 
127  //- Name
128  word name_;
129 
130  //- Class name read from header
131  word headerClassName_;
132 
133  //- Optional note
134  string note_;
135 
136  //- Instance path component
137  fileName instance_;
138 
139  //- Local path component
140  fileName local_;
141 
142  //- objectRegistry reference
143  const objectRegistry& db_;
144 
145  //- Read option
146  readOption rOpt_;
147 
148  //- Write option
149  writeOption wOpt_;
150 
151  //- Register object created from this IOobject with registry if true
152  bool registerObject_;
153 
154  //- IOobject state
155  objectState objState_;
156 
157 
158 protected:
159 
160  // Protected Member Functions
161 
162  //- Construct and return an IFstream for the object.
163  // The results is NULL if the stream construction failed
165 
166  //- Construct and return an IFstream for the object given the
167  // exact file. The results is NULL if the stream construction failed
168  Istream* objectStream(const fileName&);
169 
170  //- Set the object state to bad
171  void setBad(const string&);
172 
173 
174 public:
175 
176  //- Runtime type information
177  TypeName("IOobject");
178 
179 
180  // Static Member Functions
181 
182  //- Split path into instance, local, name components
183  // input IOobject(instance, local, name)
184  // ----- ------
185  // "foo" ("", "", "foo")
186  // "foo/bar" ("foo", "", "bar")
187  // "/XXX/bar" ("/XXX", "", "bar")
188  // "foo/bar/" ERROR - no name
189  // "foo/xxx/bar" ("foo", "xxx", "bar")
190  // "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
191  static bool fileNameComponents
192  (
193  const fileName& path,
195  fileName& local,
196  word& name
197  );
198 
199  template<class Name>
200  static inline word groupName(Name name, const word& group);
201 
202 
203  // Constructors
204 
205  //- Construct from name, instance, registry, io options
206  IOobject
207  (
208  const word& name,
209  const fileName& instance,
210  const objectRegistry& registry,
211  readOption r=NO_READ,
213  bool registerObject=true
214  );
215 
216  //- Construct from name, instance, local, registry, io options
217  IOobject
218  (
219  const word& name,
220  const fileName& instance,
221  const fileName& local,
222  const objectRegistry& registry,
223  readOption r=NO_READ,
225  bool registerObject=true
226  );
227 
228  //- Construct from path, registry, io options
229  // Uses fileNameComponents() to split path into components.
230  IOobject
231  (
232  const fileName& path,
233  const objectRegistry& registry,
234  readOption r=NO_READ,
236  bool registerObject=true
237  );
238 
239  //- Clone
241  {
242  return autoPtr<IOobject>(new IOobject(*this));
243  }
244 
245 
246  //- Destructor
247  virtual ~IOobject();
248 
249 
250  // Member Functions
251 
252  // General access
253 
254  //- Return time
255  const Time& time() const;
256 
257  //- Return the local objectRegistry
258  const objectRegistry& db() const;
259 
260  //- Return name
261  const word& name() const
262  {
263  return name_;
264  }
265 
266  //- Return name of the class name read from header
267  const word& headerClassName() const
268  {
269  return headerClassName_;
270  }
271 
272  //- Return non-constant access to the optional note
273  string& note()
274  {
275  return note_;
276  }
277 
278  //- Return the optional note
279  const string& note() const
280  {
281  return note_;
282  }
283 
284  //- Rename
285  virtual void rename(const word& newName)
286  {
287  name_ = newName;
288  }
289 
290  //- Register object created from this IOobject with registry if true
291  bool& registerObject()
292  {
293  return registerObject_;
294  }
295 
296  //- Register object created from this IOobject with registry if true
297  bool registerObject() const
298  {
299  return registerObject_;
300  }
301 
302 
303  // Read/write options
305  readOption readOpt() const
306  {
307  return rOpt_;
308  }
311  {
312  return rOpt_;
313  }
315  writeOption writeOpt() const
316  {
317  return wOpt_;
318  }
321  {
322  return wOpt_;
323  }
324 
325 
326  // Path components
327 
328  //- Return group (extension part of name)
329  word group() const;
330 
331  //- Return member (name without the extension)
332  word member() const;
333 
334  const fileName& rootPath() const;
335 
336  const fileName& caseName() const;
338  const fileName& instance() const
339  {
340  return instance_;
341  }
343  fileName& instance()
344  {
345  return instance_;
346  }
348  const fileName& local() const
349  {
350  return local_;
351  }
352 
353  //- Return complete path
354  fileName path() const;
355 
356  //- Return complete path with alternative instance and local
357  fileName path
358  (
359  const word& instance,
360  const fileName& local = ""
361  ) const;
362 
363  //- Return complete path + object name
364  fileName objectPath() const
365  {
366  return path()/name();
367  }
368 
369  //- Return complete path + object name if the file exists
370  // either in the case/processor or case otherwise null
371  fileName filePath() const;
372 
373 
374  // Reading
375 
376  //- Read header
377  bool readHeader(Istream&);
378 
379  //- Read and check header info
380  bool headerOk();
381 
382 
383  // Writing
384 
385  //- Write the standard OpenFOAM file/dictionary banner
386  // Optionally without -*- C++ -*- editor hint (eg, for logs)
387  template<class Stream>
388  static inline Stream& writeBanner(Stream& os, bool noHint=false);
389 
390  //- Write the standard file section divider
391  template<class Stream>
392  static inline Stream& writeDivider(Stream& os);
393 
394  //- Write the standard end file divider
395  template<class Stream>
396  static inline Stream& writeEndDivider(Stream& os);
397 
398  //- Write header
399  bool writeHeader(Ostream&) const;
400 
401  //- Write header. Allow override of type
402  bool writeHeader(Ostream&, const word& objectType) const;
403 
404 
405  // Error Handling
407  bool good() const
408  {
409  return objState_ == GOOD;
410  }
412  bool bad() const
413  {
414  return objState_ == BAD;
415  }
416 
417 
418  // Info
419 
420  //- Return info proxy.
421  // Used to print token information to a stream
422  InfoProxy<IOobject> info() const
423  {
424  return *this;
425  }
426 
427 
428  // Member operators
429 
430  void operator=(const IOobject&);
431 };
432 
433 
434 template<>
435 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
436 
437 
438 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
439 
440 } // End namespace Foam
441 
442 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
443 
444 #include "IOobjectI.H"
445 
446 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
447 
448 #endif
449 
450 // ************************************************************************* //
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
writeOption
Enumeration defining the write options.
Definition: IOobject.H:115
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:41
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
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:221
void operator=(const IOobject &)
Definition: IOobject.C:459
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:114
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:284
bool writeHeader(Ostream &) const
Write header.
objectState
Enumeration defining the valid states of an IOobject.
Definition: IOobject.H:99
virtual ~IOobject()
Destructor.
Definition: IOobject.C:215
word member() const
Return member (name without the extension)
Definition: IOobject.C:254
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:266
writeOption writeOpt() const
Definition: IOobject.H:314
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:275
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:299
const fileName & caseName() const
Definition: IOobject.C:233
A class for handling words, derived from string.
Definition: word.H:59
const Time & time() const
Return time.
Definition: IOobject.C:227
static word groupName(Name name, const word &group)
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:421
Foam::autoPtr< IOobject > clone() const
Clone.
Definition: IOobject.H:239
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:440
const fileName & local() const
Definition: IOobject.H:347
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool bad() const
Definition: IOobject.H:411
Istream * objectStream()
Construct and return an IFstream for the object.
Definition: IOobject.C:371
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
bool good() const
Definition: IOobject.H:406
readOption readOpt() const
Definition: IOobject.H:304
TypeName("IOobject")
Runtime type information.
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
const fileName & rootPath() const
Definition: IOobject.C:269
Registry of regIOobjects.
word group() const
Return group (extension part of name)
Definition: IOobject.C:239
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:290
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
const fileName & instance() const
Definition: IOobject.H:337
const word & name() const
Return name.
Definition: IOobject.H:260
Namespace for OpenFOAM.