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