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-2021 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  //- Disallow default bitwise copy construction
123 
124 
125  //- Destructor
126  virtual ~objectRegistry();
127 
128 
129  // Member Functions
130 
131  // Access
132 
133  //- Return time
134  const Time& time() const
135  {
136  return time_;
137  }
138 
139  //- Return the parent objectRegistry
140  const objectRegistry& parent() const
141  {
142  return parent_;
143  }
144 
145  //- Local directory path of this objectRegistry relative to the time
146  virtual const fileName& dbDir() const
147  {
148  return dbDir_;
149  }
150 
151  using regIOobject::path;
152 
153  //- Return complete path with alternative instance and local
154  fileName path
155  (
156  const word& instance,
157  const fileName& local = ""
158  ) const;
159 
160  //- Return the list of names of the IOobjects
161  wordList names() const;
162 
163  //- Return the sorted list of names of the IOobjects
164  wordList sortedNames() const;
165 
166  //- Return the list of names of IOobjects of given class name
167  wordList names(const word& className) const;
168 
169  //- Return the sorted list of names of IOobjects of given class name
170  wordList sortedNames(const word& className) const;
171 
172  //- Return the list of names of the IOobjects of given type
173  template<class Type>
174  wordList names() const;
175 
176  //- Return the list of objects whose name matches the input regExp
177  template<class Type>
178  wordList names(const wordRe& name) const;
179 
180  //- Return the list of objects whose name matches the input regExp
181  template<class Type>
182  wordList names(const wordReList& name) const;
183 
184  //- Lookup and return a const sub-objectRegistry. Optionally create
185  // it if it does not exist.
187  (
188  const word& name,
189  const bool forceCreate = false
190  ) const;
191 
192  //- Lookup and return all objects of the given Type
193  template<class Type>
194  HashTable<const Type*> lookupClass(const bool strict = false) const;
195 
196  //- Lookup and return all objects of the given Type
197  template<class Type>
198  HashTable<Type*> lookupClass(const bool strict = false);
199 
200  //- Is the named Type found?
201  template<class Type>
202  bool foundObject(const word& name) const;
203 
204  //- Lookup and return the object of the given Type
205  template<class Type>
206  const Type& lookupObject(const word& name) const;
207 
208  //- Lookup and return the object reference of the given Type
209  template<class Type>
210  Type& lookupObjectRef(const word& name) const;
211 
212  //- Return new event number.
213  label getEvent() const;
214 
215  //- Return the object registry
216  const objectRegistry& thisDb() const
217  {
218  return *this;
219  }
220 
221 
222  // Edit
223 
224  //- Rename
225  virtual void rename(const word& newName);
226 
227  //- Add an regIOobject to registry
228  bool checkIn(regIOobject&) const;
229 
230  //- Remove an regIOobject from registry
231  bool checkOut(regIOobject&) const;
232 
233  //- Remove all regIOobject owned by the registry
234  void clear();
235 
236  //- Add the given name to the set of temporary objects to cache
237  void addTemporaryObject(const word& name) const;
238 
239  //- Return true if given name is in the cacheTemporaryObjects set
240  bool cacheTemporaryObject(const word& name) const;
241 
242  //- Cache the given object
243  template<class Object>
244  bool cacheTemporaryObject(Object& ob) const;
245 
246  //- Reset the cache state of the given object
247  // in the cacheTemporaryObjects set
248  void resetCacheTemporaryObject(const regIOobject& ob) const;
249 
250  //- Check that all objects in the cacheTemporaryObjects set
251  // were cached
252  bool checkCacheTemporaryObjects() const;
253 
254 
255  // Reading
256 
257  //- Return true if any of the object's files have been modified
258  virtual bool modified() const;
259 
260  //- Read the objects that have been modified
261  void readModifiedObjects();
262 
263  //- Read object if modified
264  virtual bool readIfModified();
265 
266 
267  // Writing
268 
269  //- writeData function required by regIOobject but not used
270  // for this class, write is used instead
271  virtual bool writeData(Ostream&) const
272  {
274 
275  return false;
276  }
277 
278  //- Write the objects
279  virtual bool writeObject
280  (
284  const bool write
285  ) const;
286 
287 
288  // Member Operators
289 
290  //- Disallow default bitwise assignment
291  void operator=(const objectRegistry&) = delete;
292 };
293 
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 } // End namespace Foam
298 
299 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300 
301 #ifdef NoRepository
302  #include "objectRegistryTemplates.C"
303 #endif
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 #endif
308 
309 // ************************************************************************* //
A HashTable with keys but without contents.
Definition: HashSet.H:59
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
void addTemporaryObject(const word &name) const
Add the given name to the set of temporary objects to cache.
const word & name() const
Return name.
Definition: IOobject.H:315
A class for handling file names.
Definition: fileName.H:79
void readModifiedObjects()
Read the objects that have been modified.
const objectRegistry & thisDb() const
Return the object registry.
void resetCacheTemporaryObject(const regIOobject &ob) const
Reset the cache state of the given object.
Type & lookupObjectRef(const word &name) const
Lookup and return the object reference of the given Type.
wordList names() const
Return the list of names of the IOobjects.
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
void operator=(const objectRegistry &)=delete
Disallow default bitwise assignment.
bool foundObject(const word &name) const
Is the named Type found?
const objectRegistry & parent() const
Return the parent objectRegistry.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
objectRegistry(const Time &db, const label nIoObjects=128)
Construct the time objectRegistry given an initial estimate.
A class for handling words, derived from string.
Definition: word.H:59
virtual bool writeData(Ostream &) const
writeData function required by regIOobject but not used
wordList sortedNames() const
Return the sorted list of names of the IOobjects.
virtual ~objectRegistry()
Destructor.
label getEvent() const
Return new event number.
const fileName & local() const
Definition: IOobject.H:409
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
virtual bool readIfModified()
Read object if modified.
An STL-conforming hash table.
Definition: HashTable.H:61
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
bool checkCacheTemporaryObjects() const
Check that all objects in the cacheTemporaryObjects set.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:241
TypeName("objectRegistry")
Declare type name for this IOobject.
const Time & time() const
Return time.
void clear()
Remove all regIOobject owned by the registry.
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:355
virtual bool write(const bool write=true) const
Write using setting from DB.
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
fileName path() const
Return complete path.
Definition: regIOobject.C:199
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:353
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
bool checkIn()
Add object to registry.
Definition: regIOobject.C:205
virtual void rename(const word &newName)
Rename.
Namespace for OpenFOAM.