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-2023 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 Foam_regIOobject_H
42 #define Foam_regIOobject_H
43 
44 #include "IOobject.H"
45 #include "OSspecific.H"
46 #include "DynamicList.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 class dictionary;
56 class regIOobject;
57 
58 namespace functionEntries
59 {
60  class codeStream;
61 }
62 namespace fileOperations
63 {
64  class uncollatedFileOperation;
65 }
66 
67 /*---------------------------------------------------------------------------*\
68  Class regIOobject Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class regIOobject
72 :
73  public IOobject
74 {
75 protected:
76 
77  //- Helper: check readOpt flags and read if necessary
78  bool readHeaderOk
79  (
81  const word& typeName
82  );
83 
84  //- To flag master-only reading of objects
85  static bool masterOnlyReading;
86 
87 
88 private:
89 
90  // Private Data
91 
92  //- Is this object registered with the registry
93  bool registered_;
94 
95  //- Is this object owned by the registry
96  bool ownedByRegistry_;
97 
98  //- The eventNo of last update
99  label eventNo_;
100 
101  //- List of additional files to watch
102  mutable DynamicList<fileName> watchFiles_;
103 
104  //- List of modification watch indices
105  mutable DynamicList<label> watchIndices_;
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 readOnProc);
118 
119 
120 public:
121 
122  //- Friendship with classes needing access to masterOnlyReading
123  friend class functionEntries::codeStream;
125 
126 
127  // Generated Methods
128 
129  //- No copy assignment
130  void operator=(const regIOobject&) = delete;
131 
132 
133  // Static Data
134 
135  //- Runtime type information
136  TypeName("regIOobject");
137 
138 
139  // Constructors
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 isTimeObject = 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 if 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  //- Query the registered state (ie, has been checked in).
181  //- This is not necessarily the same as registerObject(),
182  //- which is just a stated preference.
183  inline bool registered() const noexcept;
184 
185  //- Is this object owned by the registry?
186  inline bool ownedByRegistry() const noexcept;
187 
188  //- Register object with its registry
189  //- and transfer ownership to the registry.
190  // \return true if now ownedByRegistry
191  inline bool store();
192 
193  //- Transfer pointer ownership to its registry.
194  // \return reference to the stored object
195  template<class Type>
196  inline static Type& store(Type* p);
197 
198  //- Transfer pointer ownership to its registry.
199  // Resets (clears) the parameter.
200  // \return reference to the stored object
201  template<class Type>
202  inline static Type& store(autoPtr<Type>& ptr);
203 
204  //- Transfer pointer ownership to its registry.
205  // Resets (clears) the parameter.
206  // \return reference to the stored object
207  template<class Type>
208  inline static Type& store(autoPtr<Type>&& ptr);
209 
210  //- Transfer pointer ownership to its registry.
211  // Changes parameter from PTR to CREF (do not rely on this).
212  // \return reference to the stored object
213  template<class Type>
214  inline static Type& store(refPtr<Type>& ptr);
215 
216  //- Transfer pointer ownership to its registry.
217  // Changes parameter from PTR to CREF (do not rely on this).
218  // \return reference to the stored object
219  template<class Type>
220  inline static Type& store(refPtr<Type>&& ptr);
221 
222  //- Transfer pointer ownership to its registry.
223  // Changes parameter from PTR to CREF (do not rely on this).
224  // \return reference to the stored object
225  template<class Type>
226  inline static Type& store(tmp<Type>& ptr);
227 
228  //- Transfer pointer ownership to its registry.
229  // Changes parameter from PTR to CREF (do not rely on this).
230  // \return reference to the stored object
231  template<class Type>
232  inline static Type& store(tmp<Type>&& ptr);
233 
234  //- Set object as \b not ownedByRegistry
235  // \param unregister optionally set as non-registered too
236  inline void release(const bool unregister = false) noexcept;
237 
238 
239  // Dependency Checking
240 
241  //- Event number at last update.
242  inline label eventNo() const noexcept;
243 
244  //- Event number at last update.
245  inline label& eventNo() noexcept;
246 
247  //- Return true if up-to-date with respect to given object
248  bool upToDate(const regIOobject&) const;
249 
250  //- Return true if up-to-date with respect to given objects
251  bool upToDate
252  (
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;
264 
265  //- Return true if up-to-date with respect to given objects
266  bool upToDate
267  (
268  const regIOobject&,
269  const regIOobject&,
270  const regIOobject&,
271  const regIOobject&
272  ) const;
273 
274 
275  //- Set as up-to-date
276  void setUpToDate();
277 
278 
279  // Edit
280 
281  //- Rename
282  virtual void rename(const word& newName);
283 
284 
285  // Meta-data
286 
287  //- Return pointer to meta-data or nullptr
288  const dictionary* findMetaData() const noexcept;
289 
290  //- Get or create meta-data
292 
293  //- Remove meta-data
294  void removeMetaData();
295 
296  //- Update internal meta-data (eg, prior to writing)
297  virtual void updateMetaData();
298 
299 
300  // Reading
301 
302  //- Return complete path + object name if the file exists
303  // in the case directory otherwise null. Does not search
304  // up if parallel. Can be overridden to provide this functionality
305  // (e.g. IOdictionary)
306  virtual fileName filePath() const;
307 
308  //- Read and check header info. Does not check the headerClassName.
309  bool headerOk();
310 
311  //- Return Istream and check object type against that given
312  Istream& readStream(const word&, const bool readOnProc = true);
313 
314  //- Close Istream
315  void close();
316 
317  //- Virtual readData function.
318  // Must be defined in derived types for which
319  // re-reading is required
320  virtual bool readData(Istream&);
321 
322  //- Read object
323  virtual bool read();
324 
325  //- Add file watch for fileName on object if not yet watched.
326  // \return index of watch
327  virtual label addWatch(const fileName&);
328 
329  //- Read access to file-monitoring handles
330  inline const labelList& watchIndices() const noexcept;
331 
332  //- Write access to file-monitoring handles
333  inline labelList& watchIndices() noexcept;
334 
335  //- Return true if the object's file (or files for objectRegistry)
336  //- have been modified. (modified state is cached by Time)
337  virtual bool modified() const;
338 
339  //- Read object if modified (as set by call to modified)
340  virtual bool readIfModified();
341 
342 
343  // Writing
344 
345  //- Pure virtual writeData function.
346  // Must be defined in derived types
347  virtual bool writeData(Ostream&) const = 0;
348 
349  //- Write using stream options
350  virtual bool writeObject
351  (
352  IOstreamOption streamOpt,
353  const bool writeOnProc
354  ) const;
355 
356  //- Write using setting from DB
357  virtual bool write(const bool writeOnProc = true) const;
358 
359 
360  // Other
361 
362  //- Is object global
363  virtual bool global() const
364  {
365  return false;
366  }
367 
368 
369  // Member Operators
370 
371  //- Copy assignment
372  FOAM_DEPRECATED_STRICT(2023-12, "possibly remove in the future")
373  void operator=(const IOobject& io);
374 
375 
376  // Housekeeping
377 
378  //- Write using given format, version and compression
380  virtual bool writeObject
381  (
382  IOstreamOption::streamFormat fmt,
383  IOstreamOption::versionNumber ver,
384  IOstreamOption::compressionType cmp,
385  const bool writeOnProc
386  ) const
387  {
388  return writeObject(IOstreamOption(fmt, ver, cmp), writeOnProc);
389  }
390 };
391 
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 } // End namespace Foam
396 
397 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
398 
399 #include "regIOobjectI.H"
400 
401 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402 
403 #endif
404 
405 // ************************************************************************* //
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:406
A class for handling file names.
Definition: fileName.H:72
void release(const bool unregister=false) noexcept
Set object as not ownedByRegistry.
Definition: regIOobjectI.H:176
dictionary & getMetaData() noexcept
Get or create meta-data.
virtual bool read()
Read object.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void setUpToDate()
Set as up-to-date.
Definition: regIOobject.C:478
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label eventNo() const noexcept
Event number at last update.
Definition: regIOobjectI.H:186
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition: regIOobject.C:43
virtual void updateMetaData()
Update internal meta-data (eg, prior to writing)
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:505
TypeName("regIOobject")
Runtime type information.
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:499
A simple container for options an IOstream can normally have.
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
void removeMetaData()
Remove meta-data.
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
fileOperation that assumes file operations are local.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself...
Definition: codeStream.H:115
void close()
Close Istream.
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool readIfModified()
Read object if modified (as set by call to modified)
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
const labelList & watchIndices() const noexcept
Read access to file-monitoring handles.
Definition: regIOobjectI.H:197
bool checkOut()
Remove all file watches and remove object from registry.
Definition: regIOobject.C:221
const dictionary * findMetaData() const noexcept
Return pointer to meta-data or nullptr.
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:277
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:484
virtual bool global() const
Is object global.
Definition: regIOobject.H:493
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:84
virtual bool readData(Istream &)
Virtual readData function.
void operator=(const regIOobject &)=delete
No copy assignment.
bool ownedByRegistry() const noexcept
Is this object owned by the registry?
Definition: regIOobjectI.H:30
virtual bool modified() const
Return true if the object&#39;s file (or files for objectRegistry) have been modified. (modified state is cached by Time)
streamFormat
Data format (ascii | binary)
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
volScalarField & p
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
A class for managing temporary objects.
Definition: HashPtrTable.H:50
virtual bool writeData(Ostream &) const =0
Pure virtual writeData function.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
bool checkIn()
Add object to registry, if not already registered.
Definition: regIOobject.C:184
bool readHeaderOk(const IOstreamOption::streamFormat fmt, const word &typeName)
Helper: check readOpt flags and read if necessary.
bool registered() const noexcept
Query the registered state (ie, has been checked in). This is not necessarily the same as registerObj...
Definition: regIOobjectI.H:24
Namespace for OpenFOAM.
virtual ~regIOobject()
Destructor.
Definition: regIOobject.C:142