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