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