regIOobject.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) 2018-2020 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::regIOobject
29 
30 Description
31  regIOobject is an abstract class derived from IOobject to handle
32  automatic object registration with the objectRegistry.
33 
34 SourceFiles
35  regIOobject.C
36  regIOobjectRead.C
37  regIOobjectWrite.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef regIOobject_H
42 #define regIOobject_H
43 
44 #include "IOobject.H"
45 #include "typeInfo.H"
46 #include "stdFoam.H"
47 #include "OSspecific.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 
52 namespace Foam
53 {
54 
55 namespace functionEntries
56 {
57  class codeStream;
58 }
59 namespace fileOperations
60 {
61  class uncollatedFileOperation;
62  class masterUncollatedFileOperation;
63 }
64 
65 /*---------------------------------------------------------------------------*\
66  Class regIOobject Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class regIOobject
70 :
71  public IOobject
72 {
73 protected:
74 
75  //- Helper: check readOpt flags and read if necessary
76  bool readHeaderOk
77  (
78  const IOstream::streamFormat PstreamFormat,
79  const word& typeName
80  );
81 
82  //- Construct and return an IFstream for the object.
83  // The results is NULL if the stream construction failed
85 
86  //- To flag master-only reading of objects
87  static bool masterOnlyReading;
88 
89 
90 private:
91 
92  // Private Data
93 
94  //- Is this object registered with the registry
95  bool registered_;
96 
97  //- Is this object owned by the registry
98  bool ownedByRegistry_;
99 
100  //- List of modification watch indices
101  mutable labelList watchIndices_;
102 
103  //- eventNo of last update
104  label eventNo_;
105 
106  //- Istream for reading
107  autoPtr<ISstream> isPtr_;
108 
109 
110  // Private Member Functions
111 
112  //- Return Istream
113  Istream& readStream(const bool valid = true);
114 
115  //- No copy assignment
116  void operator=(const regIOobject&) = delete;
117 
118 
119 public:
120 
121  //- Declare friendship with classes that need access to
122  //- masterOnlyReading
126 
127 
128  // Static data
129 
130  //- Runtime type information
131  TypeName("regIOobject");
132 
133  static float fileModificationSkew;
134 
135  static int maxFileModificationPolls;
136 
137 
138  // Constructors
139 
140  //- Construct from IOobject. The optional flag adds special handling
141  //- if the object is the top-level regIOobject (eg, Time).
142  regIOobject(const IOobject& io, const bool isTime = false);
143 
144  //- Copy construct
145  regIOobject(const regIOobject& rio);
146 
147  //- Copy construct, transferring registry registration to the copy
148  //- if registerCopy is true
149  regIOobject(const regIOobject& rio, bool registerCopy);
150 
151  //- Copy construct with new name, transferring registry registration
152  //- to the copy f registerCopy is true
153  regIOobject(const word& newName, const regIOobject&, bool registerCopy);
154 
155  //- Copy construct with new IO parameters
156  regIOobject(const IOobject& io, const regIOobject& rio);
157 
158 
159  //- Destructor
160  virtual ~regIOobject();
161 
162 
163  // Member Functions
164 
165  // Registration
166 
167  //- Add object to registry, if not already registered
168  // \return true if object was already registered,
169  // or was newly registered
170  bool checkIn();
171 
172  //- Remove all file watches and remove object from registry
173  // \return true if object was registered and was removed
174  bool checkOut();
175 
176  //- Add file watch on object (if registered and READ_IF_MODIFIED)
177  virtual void addWatch();
178 
179  //- Is this object owned by the registry?
180  inline bool ownedByRegistry() const;
181 
182  //- Register object with its registry
183  //- and transfer ownership to the registry.
184  // \return true if now ownedByRegistry
185  inline bool store();
186 
187  //- Register object pointer with its registry
188  //- and transfer ownership to the registry.
189  // \return reference to the object.
190  template<class Type>
191  inline static Type& store(Type* p);
192 
193  //- Register object pointer with its registry
194  //- and transfer ownership to the registry.
195  // Clears the autoPtr parameter.
196  // \return reference to the object.
197  template<class Type>
198  inline static Type& store(autoPtr<Type>& aptr);
199 
200  //- Register object pointer with its registry
201  //- and transfer ownership to the registry.
202  // Clears the autoPtr parameter.
203  // \return reference to the object.
204  template<class Type>
205  inline static Type& store(autoPtr<Type>&& aptr);
206 
207  //- Register temporary pointer with its registry
208  //- and transfer ownership of pointer to the registry.
209  // After the call, tmp parameter changes from PTR to CREF.
210  //
211  // \return reference to the object.
212  template<class Type>
213  inline static Type& store(tmp<Type>& tptr);
214 
215  //- Register temporary pointer with its registry
216  //- and transfer ownership of pointer to the registry.
217  // After the call, the tmp parameter content is \em unspecified.
218  //
219  // \return reference to the object.
220  template<class Type>
221  inline static Type& store(tmp<Type>&& tptr);
222 
223  //- Release ownership of this object from its registry
224  // \param unregister optionally set as non-registered
225  inline void release(const bool unregister = false);
226 
227 
228  // Dependency Checking
229 
230  //- Event number at last update.
231  inline label eventNo() const;
232 
233  //- Event number at last update.
234  inline label& eventNo();
235 
236  //- Return true if up-to-date with respect to given object
237  bool upToDate(const regIOobject&) const;
238 
239  //- Return true if up-to-date with respect to given objects
240  bool upToDate
241  (
242  const regIOobject&,
243  const regIOobject&
244  ) const;
245 
246  //- Return true if up-to-date with respect to given objects
247  bool upToDate
248  (
249  const regIOobject&,
250  const regIOobject&,
251  const regIOobject&
252  ) const;
253 
254  //- Return true if up-to-date with respect to given objects
255  bool upToDate
256  (
257  const regIOobject&,
258  const regIOobject&,
259  const regIOobject&,
260  const regIOobject&
261  ) const;
262 
263 
264  //- Set as up-to-date
265  void setUpToDate();
266 
267 
268  // Edit
269 
270  //- Rename
271  virtual void rename(const word& newName);
272 
273 
274  // Reading
275 
276  //- Return complete path + object name if the file exists
277  // in the case directory otherwise null. Does not search
278  // up if parallel. Can be overridden to provide this functionality
279  // (e.g. IOdictionary)
280  virtual fileName filePath() const;
281 
282  //- Read and check header info
283  bool headerOk();
284 
285  //- Return Istream and check object type against that given
286  Istream& readStream(const word&, const bool valid = true);
287 
288  //- Close Istream
289  void close();
290 
291  //- Virtual readData function.
292  // Must be defined in derived types for which
293  // re-reading is required
294  virtual bool readData(Istream&);
295 
296  //- Read object
297  virtual bool read();
298 
299  //- Add file watch for fileName on object if not yet watched.
300  // \return index of watch
301  virtual label addWatch(const fileName&);
302 
303  //- Return file-monitoring handles
304  inline const labelList& watchIndices() const;
305 
306  //- Return file-monitoring handles
307  inline labelList& watchIndices();
308 
309  //- Return true if the object's file (or files for objectRegistry)
310  //- have been modified. (modified state is cached by Time)
311  virtual bool modified() const;
312 
313  //- Read object if modified (as set by call to modified)
314  virtual bool readIfModified();
315 
316 
317  // Writing
318 
319  //- Pure virtual writeData function.
320  // Must be defined in derived types
321  virtual bool writeData(Ostream&) const = 0;
322 
323  //- Write using stream options
324  virtual bool writeObject
325  (
326  IOstreamOption streamOpt,
327  const bool valid
328  ) const;
329 
330  //- Write using setting from DB
331  virtual bool write(const bool valid = true) const;
332 
333 
334  // Other
335 
336  //- Is object global
337  virtual bool global() const
338  {
339  return false;
340  }
341 
342 
343  // Member Operators
344 
345  //- Copy assignment
346  void operator=(const IOobject& io);
347 
348 
349  // Housekeeping
350 
351  //- Write using given format, version and compression
352  FOAM_DEPRECATED_FOR(2020-02, "writeObject(IOstreamOption, bool)")
353  virtual bool writeObject
354  (
355  IOstream::streamFormat fmt,
356  IOstream::versionNumber ver,
357  IOstream::compressionType comp,
358  const bool valid
359  ) const;
360 };
361 
362 
363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364 
365 } // End namespace Foam
366 
367 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
368 
369 #include "regIOobjectI.H"
370 
371 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
372 
373 #endif
374 
375 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::regIOobject::objectStream
Istream * objectStream()
Construct and return an IFstream for the object.
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
typeInfo.H
Foam::regIOobject::release
void release(const bool unregister=false)
Release ownership of this object from its registry.
Definition: regIOobjectI.H:134
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::regIOobject::upToDate
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:354
Foam::regIOobject::watchIndices
const labelList & watchIndices() const
Return file-monitoring handles.
Definition: regIOobjectI.H:155
Foam::regIOobject::writeData
virtual bool writeData(Ostream &) const =0
Pure virtual writeData function.
Foam::regIOobject::fileModificationSkew
static float fileModificationSkew
Definition: regIOobject.H:132
Foam::regIOobject::addWatch
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:284
Foam::regIOobject::regIOobject
regIOobject(const IOobject &io, const bool isTime=false)
Definition: regIOobject.C:70
Foam::regIOobject::read
virtual bool read()
Read object.
Definition: regIOobjectRead.C:202
Foam::regIOobject::checkIn
bool checkIn()
Add object to registry, if not already registered.
Definition: regIOobject.C:205
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::regIOobject::maxFileModificationPolls
static int maxFileModificationPolls
Definition: regIOobject.H:134
Foam::regIOobject::global
virtual bool global() const
Is object global.
Definition: regIOobject.H:336
Foam::regIOobject::eventNo
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:144
Foam::regIOobject::store
bool store()
Definition: regIOobjectI.H:37
Foam::regIOobject::readHeaderOk
bool readHeaderOk(const IOstream::streamFormat PstreamFormat, const word &typeName)
Helper: check readOpt flags and read if necessary.
Definition: regIOobjectRead.C:39
Foam::regIOobject::modified
virtual bool modified() const
Definition: regIOobjectRead.C:248
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:165
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::regIOobject::readIfModified
virtual bool readIfModified()
Read object if modified (as set by call to modified)
Definition: regIOobjectRead.C:262
IOobject.H
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::regIOobject::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: regIOobjectWrite.C:37
Foam::regIOobject::ownedByRegistry
bool ownedByRegistry() const
Is this object owned by the registry?
Definition: regIOobjectI.H:31
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::regIOobject::masterOnlyReading
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:86
Foam::regIOobject::setUpToDate
void setUpToDate()
Set as up-to-date.
Definition: regIOobject.C:426
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
Foam::regIOobject::TypeName
TypeName("regIOobject")
Runtime type information.
Foam::fileOperations::masterUncollatedFileOperation
fileOperations that performs all file operations on the master processor. Requires the calls to be pa...
Definition: masterUncollatedFileOperation.H:85
Foam::regIOobject::checkOut
bool checkOut()
Remove all file watches and remove object from registry.
Definition: regIOobject.C:241
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:182
Foam::fileOperations::uncollatedFileOperation
fileOperation that assumes file operations are local.
Definition: uncollatedFileOperation.H:51
Foam::regIOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:432
Foam::List< label >
stdFoam.H
Foam::regIOobject::filePath
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:447
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
Foam::functionEntries::codeStream
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself....
Definition: codeStream.H:116
Foam::regIOobject::readData
virtual bool readData(Istream &)
Virtual readData function.
Definition: regIOobjectRead.C:196
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::regIOobject::~regIOobject
virtual ~regIOobject()
Destructor.
Definition: regIOobject.C:166
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:453