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-2024 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 label event_;
71 
72  //- Table of temporary object names with current state
73  mutable HashTable<Pair<bool>> cacheTemporaryObjects_;
74 
75  //- State of cacheTemporaryObjects_, set true after reading
76  mutable bool cacheTemporaryObjectsSet_;
77 
78  //- Accumulated list of temporary objects available to cache
79  // Used to provide diagnostics in case the requested object is not
80  // available
81  mutable HashSet<word> temporaryObjects_;
82 
83  mutable List<regIOobject*> dependents_;
84 
85 
86  // Private Member Functions
87 
88  //- Is the objectRegistry parent_ different from time_
89  // Used to terminate searching within the ancestors
90  bool parentNotTime() const;
91 
92  //- Read the cacheTemporaryObjects list from controlDict
93  void readCacheTemporaryObjects() const;
94 
95  //- Delete the current cached object before caching a new object
96  void deleteCachedObject(regIOobject& cachedOb) const;
97 
98 
99 public:
100 
101  //- Declare type name for this IOobject
102  TypeName("objectRegistry");
103 
104 
105  // Constructors
106 
107  //- Construct the time objectRegistry given an initial estimate
108  // for the number of entries
109  explicit objectRegistry
110  (
111  const Time& db,
112  const label nIoObjects = 128
113  );
114 
115  //- Construct a sub-registry given an IObject to describe the registry
116  // and an initial estimate for the number of entries
117  explicit objectRegistry
118  (
119  const IOobject& io,
120  const label nIoObjects = 128
121  );
122 
123  //- Move constructor
124  objectRegistry(objectRegistry&&) = default;
125 
126  //- Disallow default bitwise copy construction
127  objectRegistry(const objectRegistry&) = delete;
128 
129 
130  //- Destructor
131  virtual ~objectRegistry();
132 
133 
134  // Member Functions
135 
136  // Access
137 
138  //- Return time
139  const Time& time() const
140  {
141  return time_;
142  }
143 
144  //- Return the parent objectRegistry
145  const objectRegistry& parent() const
146  {
147  return parent_;
148  }
149 
150  //- Local directory path of this objectRegistry relative to the time
151  virtual const fileName& dbDir() const
152  {
153  return dbDir_;
154  }
155 
156  using regIOobject::path;
157 
158  //- Return complete path with alternative instance and local
159  fileName path
160  (
161  const word& instance,
162  const fileName& local = ""
163  ) const;
164 
167 
168  //- Return the list of names of IOobjects of given class name
169  wordList toc(const word& className) const;
170 
171  //- Return the sorted list of names of IOobjects of given class name
172  wordList sortedToc(const word& className) const;
173 
174  //- Return the list of names of the IOobjects of given type
175  template<class Type>
176  wordList toc() const;
177 
178  //- Return the list of objects whose name matches the input regExp
179  template<class Type>
180  wordList toc(const wordRe& name) const;
181 
182  //- Return the list of objects whose name matches the input regExp
183  template<class Type>
184  wordList toc(const wordReList& name) const;
185 
186  //- Lookup and return a const sub-objectRegistry. Optionally create
187  // it if it does not exist.
189  (
190  const word& name,
191  const bool forceCreate = false
192  ) const;
193 
194  //- Lookup and return all objects of the given Type
195  template<class Type>
196  HashTable<const Type*> lookupClass(const bool strict = false) const;
197 
198  //- Lookup and return all objects of the given Type
199  template<class Type>
200  HashTable<Type*> lookupClass(const bool strict = false);
201 
202  //- Is the named Type in registry
203  template<class Type>
204  bool foundObject(const word& name) const;
205 
206  //- Lookup and return the object of the given Type and name
207  template<class Type>
208  const Type& lookupObject(const word& name) const;
209 
210  //- Lookup and return the object reference of the given Type
211  template<class Type>
212  Type& lookupObjectRef(const word& name) const;
213 
214  //- Is the Type in registry
215  // the name is derived from the type name and given group
216  template<class Type>
217  bool foundType(const word& group = word::null) const;
218 
219  //- Lookup and return the object of the given Type
220  // the name is derived from the type name and given group
221  template<class Type>
222  const Type& lookupType(const word& group = word::null) const;
223 
224  //- Return new event number.
225  label getEvent() const;
226 
227  //- Return the object registry
228  const objectRegistry& thisDb() const
229  {
230  return *this;
231  }
232 
233 
234  // Edit
235 
236  //- Rename
237  virtual void rename(const word& newName);
238 
239  //- Add an regIOobject to registry
240  bool checkIn(regIOobject&) const;
241 
242  //- Remove an regIOobject from registry
243  bool checkOut(regIOobject&) const;
244 
245  //- Remove all regIOobject owned by the registry
246  void clear();
247 
248  //- Add the given name to the set of temporary objects to cache
249  void addTemporaryObject(const word& name) const;
250 
251  //- Return true if given name is in the cacheTemporaryObjects set
252  bool cacheTemporaryObject(const word& name) const;
253 
254  //- Cache the given object
255  template<class Object>
256  bool cacheTemporaryObject(Object& ob) const;
257 
258  //- Reset the cache state of the given object
259  // in the cacheTemporaryObjects set
260  void resetCacheTemporaryObject(const regIOobject& ob) const;
261 
262  //- Check that all objects in the cacheTemporaryObjects set
263  // were cached
264  bool checkCacheTemporaryObjects() const;
265 
266 
267  // Reading
268 
269  //- Return true if any of the object's files have been modified
270  virtual bool modified() const;
271 
272  //- Cache pointers to objects who's dependencies have been modified
273  virtual bool dependenciesModified() const;
274 
275  //- Read object if modified
276  virtual bool readIfModified();
277 
278  //- Read dependent objects
279  virtual bool read();
280 
281  //- Read the objects that have been modified
282  void readModifiedObjects();
283 
284 
285  // Writing
286 
287  //- Print the list of object names and their type
288  // to the given stream
289  void printToc(Ostream& os) const;
290 
291  //- writeData function required by regIOobject but not used
292  // for this class, writeObject is used instead
293  virtual bool writeData(Ostream&) const
294  {
296  return false;
297  }
298 
299  //- Write the objects
300  virtual bool writeObject
301  (
305  const bool write
306  ) const;
307 
308 
309  // Member Operators
310 
311  //- Disallow default bitwise assignment
312  void operator=(const objectRegistry&) = delete;
313 };
314 
315 
316 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 
318 } // End namespace Foam
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 #ifdef NoRepository
323  #include "objectRegistryTemplates.C"
324 #endif
325 
326 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
327 
328 #endif
329 
330 // ************************************************************************* //
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:355
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
word group() const
Return group (extension part of name)
Definition: IOobject.C:324
const word & name() const
Return name.
Definition: IOobject.H:310
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 & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
void printToc(Ostream &os) const
Print the list of object names and their type.
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 & thisDb() const
Return the object registry.
const objectRegistry & parent() const
Return the parent objectRegistry.
void operator=(const objectRegistry &)=delete
Disallow default bitwise assignment.
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.
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
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:199
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.
void addTemporaryObject(const word &name) const
Add the given name to the set of temporary objects to cache.
const Type & lookupType(const word &group=word::null) const
Lookup and return the object of the given Type.
virtual void rename(const word &newName)
Rename.
virtual bool read()
Read dependent objects.
label getEvent() const
Return new event number.
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:199
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:241
bool checkIn()
Add object to registry.
Definition: regIOobject.C:205
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:62
static const word null
An empty word.
Definition: word.H:77
#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