objectRegistry.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 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::objectRegistry
26 
27 Description
28  Registry of regIOobjects
29 
30 SourceFiles
31  objectRegistry.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef objectRegistry_H
36 #define objectRegistry_H
37 
38 #include "HashTable.H"
39 #include "regIOobject.H"
40 #include "wordReList.H"
41 #include "HashSet.H"
42 #include "Pair.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class objectRegistry Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 class objectRegistry
54 :
55  public regIOobject,
56  public HashTable<regIOobject*>
57 {
58  // Private Data
59 
60  //- Master time objectRegistry
61  const Time& time_;
62 
63  //- Parent objectRegistry
64  const objectRegistry& parent_;
65 
66  //- Local directory path of this objectRegistry relative to time
67  fileName dbDir_;
68 
69  //- Current event
70  mutable uint64_t event_;
71 
72  //- Table of temporary object names with current state
73  mutable HashTable<Pair<bool>> cacheTemporaryObjects_;
74 
75  //- State of cacheTemporaryObjects_. 0 means as yet uninitialised. 1
76  // means initialised. -1 means deactivated (during destruction).
77  mutable label cacheTemporaryObjectsState_;
78 
79  //- Accumulated list of temporary objects available to cache
80  // Used to provide diagnostics in case the requested object is not
81  // available
82  mutable HashSet<word> temporaryObjects_;
83 
84  mutable List<regIOobject*> dependents_;
85 
86 
87  // Private Member Functions
88 
89  //- Is the objectRegistry parent_ different from time_
90  // Used to terminate searching within the ancestors
91  bool parentNotTime() const;
92 
93  //- Read the cacheTemporaryObjects list from controlDict
94  void readCacheTemporaryObjects() const;
95 
96  //- Delete the current cached object before caching a new object
97  void deleteCachedObject(regIOobject& cachedOb) const;
98 
99 
100 public:
101 
102  //- Declare type name for this IOobject
103  TypeName("objectRegistry");
104 
105 
106  // Constructors
107 
108  //- Construct the time objectRegistry given an initial estimate
109  // for the number of entries
111  (
112  const Time& db,
113  const label nIoObjects = 128
114  );
115 
116  //- Construct a sub-registry given an IObject to describe the registry
117  // and an initial estimate for the number of entries
119  (
120  const IOobject& io,
121  const fileName& dbDir,
122  const label nIoObjects = 128
123  );
124 
125  //- Construct a sub-registry given an IObject to describe the registry
126  // and an initial estimate for the number of entries
128  (
129  const IOobject& io,
130  const label nIoObjects = 128
131  );
132 
133  //- Move constructor
134  objectRegistry(objectRegistry&&) = default;
135 
136  //- Disallow default bitwise copy construction
137  objectRegistry(const objectRegistry&) = delete;
138 
139 
140  //- Destructor
141  virtual ~objectRegistry();
142 
143 
144  // Member Functions
145 
146  // Access
147 
148  //- Return time
149  const Time& time() const
150  {
151  return time_;
152  }
153 
154  //- Return the parent objectRegistry
155  const objectRegistry& parent() const
156  {
157  return parent_;
158  }
159 
160  //- Local directory path of this objectRegistry relative to the time
161  const fileName& dbDir() const
162  {
163  return dbDir_;
164  }
165 
166  using regIOobject::path;
167 
168  //- Return complete path with alternative instance and local
169  fileName path
170  (
171  const word& instance,
172  const fileName& local = ""
173  ) const;
174 
177 
178  //- Return the list of names of IOobjects of given class name
179  wordList toc(const word& className) const;
180 
181  //- Return the sorted list of names of IOobjects of given class name
182  wordList sortedToc(const word& className) const;
183 
184  //- Return the list of names and types of IOobjects
185  List<Pair<word>> tocTypes() const;
186 
187  //- Return the list of names of the IOobjects of given type
188  template<class Type>
189  wordList toc() const;
190 
191  //- Return the list of objects whose name matches the input regExp
192  template<class Type>
193  wordList toc(const wordRe& name) const;
194 
195  //- Return the list of objects whose name matches the input regExp
196  template<class Type>
197  wordList toc(const wordReList& name) const;
198 
199  //- Lookup and return a const sub-objectRegistry. Optionally create
200  // it if it does not exist.
202  (
203  const word& name,
204  const bool forceCreate = false
205  ) const;
206 
207  //- Lookup and return all objects of the given Type
208  template<class Type>
209  HashTable<const Type*> lookupClass(const bool strict = false) const;
210 
211  //- Lookup and return all objects of the given Type
212  template<class Type>
213  HashTable<Type*> lookupClass(const bool strict = false);
214 
215  //- Is the named Type in registry
216  template<class Type>
217  bool foundObject(const word& name) const;
218 
219  //- Lookup and return the object of the given Type and name
220  template<class Type>
221  const Type& lookupObject(const word& name) const;
222 
223  //- Lookup and return the object reference of the given Type
224  template<class Type>
225  Type& lookupObjectRef(const word& name) const;
226 
227  //- Is the Type in registry
228  // the name is derived from the type name and given group
229  template<class Type>
230  bool foundType(const word& group = word::null) const;
231 
232  //- Lookup and return the object of the given Type
233  // the name is derived from the type name and given group
234  template<class Type>
235  const Type& lookupType(const word& group = word::null) const;
236 
237  //- Return new event number.
238  uint64_t getEvent() const;
239 
240  //- Return the objectRegistry
241  // Required for consistent interface for DemandDrivenMeshObject
242  // instantiated on objectRegistry
243  const objectRegistry& db() const
244  {
245  return *this;
246  }
247 
248 
249  // Edit
250 
251  //- Rename
252  virtual void rename(const word& newName);
253 
254  //- Add an regIOobject to registry
255  bool checkIn(regIOobject&) const;
256 
257  //- Remove an regIOobject from registry
258  bool checkOut(regIOobject&) const;
259 
260  //- Remove all regIOobject owned by the registry
261  void clear();
262 
263  //- Add the given name to the cacheTemporaryObjects set
264  void cacheTemporary(const word& name) const;
265 
266  //- Cache the given object
267  template<class Object>
268  bool cacheTemporaryObject(Object& ob) const;
269 
270  //- Return true if given name is in the cacheTemporaryObjects set
271  bool temporaryObjectCached(const word& name) const;
272 
273  //- Reset the cache state of the given object
274  // in the cacheTemporaryObjects set
275  void resetCacheTemporaryObject(const regIOobject& ob) const;
276 
277  //- Check that all objects in the cacheTemporaryObjects set
278  // were cached
279  bool checkCacheTemporaryObjects() const;
280 
281 
282  // Reading
283 
284  //- Return true if any of the object's files have been modified
285  virtual bool modified() const;
286 
287  //- Cache pointers to objects who's dependencies have been modified
288  virtual bool dependenciesModified() const;
289 
290  //- Read object if modified
291  virtual bool readIfModified();
292 
293  //- Read dependent objects
294  virtual bool read();
295 
296  //- Read the objects that have been modified
297  void readModifiedObjects();
298 
299 
300  // Writing
301 
302  //- Print the list of object names and their type
303  // to the given stream
304  void printToc(Ostream& os) const;
305 
306  //- writeData function required by regIOobject but not used
307  // for this class, writeObject is used instead
308  virtual bool writeData(Ostream&) const
309  {
311  return false;
312  }
313 
314  //- Write the objects
315  virtual bool writeObject
316  (
320  const bool write
321  ) const;
322 
323 
324  // Member Operators
325 
326  //- Disallow default bitwise assignment
327  void operator=(const objectRegistry&) = delete;
328 };
329 
330 
331 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
332 
333 } // End namespace Foam
334 
335 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
336 
337 #ifdef NoRepository
338  #include "objectRegistryTemplates.C"
339 #endif
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 #endif
344 
345 // ************************************************************************* //
A HashTable with keys but without contents.
Definition: HashSet.H:62
An STL-conforming hash table.
Definition: HashTable.H:127
List< word > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:242
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const fileName & local() const
Definition: IOobject.H:400
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:352
word group() const
Return group (extension part of name)
Definition: IOobject.C:321
const word & name() const
Return name.
Definition: IOobject.H:307
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A class for handling file names.
Definition: fileName.H:82
Registry of regIOobjects.
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
const Time & time() const
Return time.
wordList toc() const
Return the list of names of the IOobjects of given type.
Type & lookupObjectRef(const word &name) const
Lookup and return the object reference of the given Type.
const objectRegistry & db() const
Return the objectRegistry.
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
void printToc(Ostream &os) const
Print the list of object names and their type.
bool temporaryObjectCached(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
virtual bool modified() const
Return true if any of the object's files have been modified.
objectRegistry(const Time &db, const label nIoObjects=128)
Construct the time objectRegistry given an initial estimate.
virtual bool dependenciesModified() const
Cache pointers to objects who's dependencies have been modified.
const objectRegistry & parent() const
Return the parent objectRegistry.
void operator=(const objectRegistry &)=delete
Disallow default bitwise assignment.
void cacheTemporary(const word &name) const
Add the given name to the cacheTemporaryObjects set.
virtual ~objectRegistry()
Destructor.
TypeName("objectRegistry")
Declare type name for this IOobject.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type and name.
bool foundType(const word &group=word::null) const
Is the Type in registry.
bool checkCacheTemporaryObjects() const
Check that all objects in the cacheTemporaryObjects set.
List< Pair< word > > tocTypes() const
Return the list of names and types of IOobjects.
uint64_t getEvent() const
Return new event number.
virtual bool readIfModified()
Read object if modified.
virtual bool writeData(Ostream &) const
writeData function required by regIOobject but not used
fileName path() const
Return complete path.
Definition: regIOobject.C:175
void clear()
Remove all regIOobject owned by the registry.
void resetCacheTemporaryObject(const regIOobject &ob) const
Reset the cache state of the given object.
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
void readModifiedObjects()
Read the objects that have been modified.
const Type & lookupType(const word &group=word::null) const
Lookup and return the object of the given Type.
bool cacheTemporaryObject(Object &ob) const
Cache the given object.
virtual void rename(const word &newName)
Rename.
virtual bool read()
Read dependent objects.
bool foundObject(const word &name) const
Is the named Type in registry.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
virtual bool write(const bool write=true) const
Write using setting from DB.
fileName path() const
Return complete path.
Definition: regIOobject.C:175
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:217
bool checkIn()
Add object to registry.
Definition: regIOobject.C:181
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:77
A class for handling words, derived from string.
Definition: word.H:63
static const word null
An empty word.
Definition: word.H:78
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:381
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59