objectRegistry.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-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2021 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::objectRegistry
29 
30 Description
31  Registry of regIOobjects
32 
33 SourceFiles
34  objectRegistry.C
35  objectRegistryTemplates.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef objectRegistry_H
40 #define objectRegistry_H
41 
42 #include "HashTable.H"
43 #include "HashSet.H"
44 #include "regIOobject.H"
45 #include "wordRes.H"
46 
47 // Historically included by objectRegistryTemplates (until NOV-2018),
48 // but not used by objectRegistry directly.
49 // Leave here for now to avoid a missing include in other bits of code.
50 #include "stringListOps.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 /*---------------------------------------------------------------------------*\
58  Class objectRegistry Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class objectRegistry
62 :
63  public regIOobject,
64  public HashTable<regIOobject*>
65 {
66  // Private Data
67 
68  //- Master time objectRegistry
69  const Time& time_;
70 
71  //- Parent objectRegistry
72  const objectRegistry& parent_;
73 
74  //- Local directory path of this objectRegistry relative to time
75  fileName dbDir_;
76 
77  //- Current event
78  mutable label event_;
79 
80 
81  // Private Member Functions
82 
83  //- Is the objectRegistry parent_ different from time_
84  // Used to terminate searching within the ancestors
85  bool parentNotTime() const noexcept;
86 
87  //- Templated implementation for count()
88  // The number of items with a matching class
89  template<class MatchPredicate1, class MatchPredicate2>
90  static label countImpl
91  (
92  const objectRegistry& list,
93  const MatchPredicate1& matchClass,
94  const MatchPredicate2& matchName
95  );
96 
97  //- Templated implementation for count()
98  // The number of items with a matching class
99  template<class Type, class MatchPredicate>
100  static label countTypeImpl
101  (
102  const objectRegistry& list,
103  const MatchPredicate& matchName
104  );
105 
106  //- Templated implementation for classes()
107  template<class MatchPredicate>
108  static HashTable<wordHashSet> classesImpl
109  (
110  const objectRegistry& list,
111  const MatchPredicate& matchName
112  );
113 
114  //- Templated implementation for names(), sortedNames()
115  template<class MatchPredicate1, class MatchPredicate2>
116  static wordList namesImpl
117  (
118  const objectRegistry& list,
119  const MatchPredicate1& matchClass,
120  const MatchPredicate2& matchName,
121  const bool doSort
122  );
123 
124  //- Templated implementation for names(), sortedNames()
125  template<class Type, class MatchPredicate>
126  static wordList namesTypeImpl
127  (
128  const objectRegistry& list,
129  const MatchPredicate& matchName,
130  const bool doSort
131  );
132 
133 
134  //- No copy construct
135  objectRegistry(const objectRegistry&) = delete;
136 
137  //- No copy assignment
138  void operator=(const objectRegistry&) = delete;
139 
140 
141 public:
142 
143  //- Declare type name for this IOobject
144  TypeName("objectRegistry");
145 
146 
147  // Constructors
148 
149  //- Construct the time objectRegistry,
150  //- with estimated table capacity (default: 128)
151  explicit objectRegistry(const Time& db, const label nObjects=128);
152 
153  //- Construct sub-registry given an IObject to describe the registry,
154  //- with estimated table capacity (default: 128)
155  explicit objectRegistry(const IOobject& io, const label nObjects=128);
156 
157 
158  //- Destructor, with checkOut() for all objects that are ownedByRegistry
159  virtual ~objectRegistry();
160 
161 
162  // Member Functions
163 
164  // Access
165 
166  //- Return the object registry
167  const objectRegistry& thisDb() const noexcept
168  {
169  return *this;
170  }
171 
172  //- Return the parent objectRegistry
173  const objectRegistry& parent() const noexcept
174  {
175  return parent_;
176  }
177 
178  //- Return time registry
179  const Time& time() const noexcept
180  {
181  return time_;
182  }
183 
184  //- True if the registry is Time
185  bool isTimeDb() const noexcept;
186 
187  //- Local directory path of this objectRegistry relative to the time
188  virtual const fileName& dbDir() const
189  {
190  return dbDir_;
191  }
192 
193 
194  // Summary of classes
195 
196  //- A summary hash of classes used and their associated object names.
197  // Behaviour and usage as per IOobjectList::classes
199 
200  //- A summary hash of classes used and their associated object names,
201  //- restricted to objects that have a matching object name.
202  template<class MatchPredicate>
203  HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
204 
205 
206  // Number of items
207 
208  //- The number of objects of the given class name
209  // \note uses the class type() method
210  label count(const char* clsName) const;
211 
212  //- The number of objects of the given class name
213  // \note uses the class type() method
214  template<class MatchPredicate>
215  label count(const MatchPredicate& matchClass) const;
216 
217  //- The number of objects of the given class name
218  // \note uses the class type() method
219  template<class MatchPredicate1, class MatchPredicate2>
220  label count
221  (
222  const MatchPredicate1& matchClass,
223  const MatchPredicate2& matchName
224  ) const;
225 
226  //- The names of objects with a class satisfying \c isA<Type>
227  //
228  // \param strict use \c isType<Type> instead of \c isA<Type>
229  //
230  // \note The values of \c count<Type>() and \c count(Type::typeName)
231  // may be inconsistent, since they use different mechanisms for
232  // testing the class type.
233  // \note If \a Type is \c void, no isA check is used (always true).
234  template<class Type>
235  label count(const bool strict = false) const;
236 
237  //- The names of objects with a class satisfying \c isA<Type>
238  //- that also have a matching object name.
239  //
240  // \note If \a Type is \c void, no isA check is used (always true).
241  template<class Type, class MatchPredicate>
242  label count(const MatchPredicate& matchName) const;
243 
244 
245  // Summary of names
246 
247  //- The names of all objects
248  wordList names() const;
249 
250  //- The names of objects with the given class name.
251  // \note uses the class type() method
252  wordList names(const char* clsName) const;
253 
254  //- The names of objects with a matching class name
255  // \note uses the class type() method
256  template<class MatchPredicate>
257  wordList names(const MatchPredicate& matchClass) const;
258 
259  //- The names of objects with a matching class name
260  //- that also have a matching object name.
261  // \note uses the class type() method
262  template<class MatchPredicate1, class MatchPredicate2>
264  (
265  const MatchPredicate1& matchClass,
266  const MatchPredicate2& matchName
267  ) const;
268 
269  //- The names of objects with a class satisfying \c isA<Type>.
270  //
271  // \note If \a Type is \c void, no isA check is used (always true).
272  template<class Type>
273  wordList names() const;
274 
275  //- The names of objects with a class satisfying \c isA<Type>
276  //- that also have a matching object name.
277  //
278  // \note If \a Type is \c void, no isA check is used (always true).
279  template<class Type, class MatchPredicate>
280  wordList names(const MatchPredicate& matchName) const;
281 
282 
283  // Summary of names (sorted)
284 
285  //- The sorted names of all objects
286  wordList sortedNames() const;
287 
288  //- The sorted names of objects with the given class name.
289  // \note uses the class type() method
290  wordList sortedNames(const char* clsName) const;
291 
292  //- The sorted names objects with a matching class name
293  // \note uses the class type() method
294  template<class MatchPredicate>
295  wordList sortedNames(const MatchPredicate& matchClass) const;
296 
297  //- The sorted names of objects with a matching class name
298  //- that also have a matching object name.
299  // \note uses the class type() method
300  template<class MatchPredicate1, class MatchPredicate2>
302  (
303  const MatchPredicate1& matchClass,
304  const MatchPredicate2& matchName
305  ) const;
306 
307  //- The sorted names of objects with a class satisfying \c isA<Type>
308  //
309  // \note If \a Type is \c void, no isA check is used (always true).
310  template<class Type>
311  wordList sortedNames() const;
312 
313  //- The sorted names of objects with a class satisfying \c isA<Type>
314  //- that also have a matching object name.
315  //
316  // \note If \a Type is \c void, no isA check is used (always true).
317  template<class Type, class MatchPredicate>
318  wordList sortedNames(const MatchPredicate& matchName) const;
319 
320 
321  // Lookup
322 
323  //- Lookup and return a const sub-objectRegistry.
324  //
325  // \param forceCreate create it if it does not exist.
326  // \param recursive search parent registries.
328  (
329  const word& name,
330  const bool forceCreate = false,
331  const bool recursive = false
332  ) const;
333 
334 
335  //- Return all objects with a class satisfying \c isA<Type>
336  //
337  // \param strict use \c isType<Type> instead of \c isA<Type>
338  template<class Type>
339  HashTable<const Type*> lookupClass(const bool strict = false) const;
340 
341  //- Return all objects with a class satisfying \c isA<Type>
342  //
343  // \param strict use \c isType<Type> instead of \c isA<Type>
344  template<class Type>
345  HashTable<Type*> lookupClass(const bool strict = false);
346 
347  //- Return const pointer to the regIOobject.
348  //
349  // \param recursive search parent registries
350  //
351  // \return nullptr if the object was not found.
353  (
354  const word& name,
355  const bool recursive = false
356  ) const;
357 
358  //- Can the regIOobject object be found (by name).
359  //
360  // \param recursive search parent registries
361  bool found(const word& name, const bool recursive = false) const;
362 
363 
364  //- Is the named Type found?
365  //
366  // \param recursive search parent registries
367  template<class Type>
368  bool foundObject
369  (
370  const word& name,
371  const bool recursive = false
372  ) const;
373 
374  //- Return const pointer to the object of the given Type.
375  //
376  // \param recursive search parent registries
377  //
378  // \return nullptr if the object was not found or had incorrect type.
379  template<class Type>
380  const Type* cfindObject
381  (
382  const word& name,
383  const bool recursive = false
384  ) const;
385 
386  //- Return const pointer to the object of the given Type.
387  //
388  // \param recursive search parent registries
389  //
390  // \return nullptr if the object was not found or had incorrect type.
391  template<class Type>
392  const Type* findObject
393  (
394  const word& name,
395  const bool recursive = false
396  ) const;
397 
398  //- Return non-const pointer to the object of the given Type.
399  //
400  // \param recursive search parent registries
401  //
402  // \return nullptr if the object was not found or had incorrect type.
403  template<class Type>
404  Type* findObject
405  (
406  const word& name,
407  const bool recursive = false
408  );
409 
410  //- Return non-const pointer to the object of the given Type,
411  //- using a const-cast to have it behave like a mutable.
412  // Exercise caution when using.
413  //
414  // \param recursive search parent registries.
415  //
416  // \return nullptr if the object was not found or had incorrect type.
417  template<class Type>
418  Type* getObjectPtr
419  (
420  const word& name,
421  const bool recursive = false
422  ) const;
423 
424  //- Lookup and return const reference to the object
425  //- of the given Type. Fatal if not found or the wrong type.
426  //
427  // \param recursive search parent registries.
428  template<class Type>
429  const Type& lookupObject
430  (
431  const word& name,
432  const bool recursive = false
433  ) const;
434 
435  //- Lookup and return non-const reference to the object
436  //- of the given Type. Fatal if not found or the wrong type.
437  //
438  // \param recursive search parent registries.
439  template<class Type>
440  Type& lookupObjectRef
441  (
442  const word& name,
443  const bool recursive = false
444  ) const;
445 
446 
447  // Events
448 
449  //- Return new event number.
450  label getEvent() const;
451 
452 
453  // Edit
454 
455  //- Add a regIOobject to registry. A nullptr is ignored.
456  bool checkIn(regIOobject* io) const;
457 
458  //- Add a regIOobject to registry
459  bool checkIn(regIOobject& io) const;
460 
461  //- Remove a regIOobject from registry and free memory if the
462  //- object is ownedByRegistry. A nullptr is ignored.
463  bool checkOut(regIOobject* io) const;
464 
465  //- Remove a regIOobject from registry and free memory if the
466  //- object is ownedByRegistry.
467  bool checkOut(regIOobject& io) const;
468 
469  //- Remove a regIOobject by name from registry and free memory if the
470  //- object is ownedByRegistry
471  bool checkOut(const word& key) const;
472 
473  //- Clear all entries from the registry
474  // Performs a checkOut() for all objects that are ownedByRegistry
475  void clear();
476 
477  //- Clear all entries from the registry and the table itself.
478  void clearStorage();
479 
480  //- Erase an entry specified by the given iterator.
481  // Performs a checkOut() if the object was ownedByRegistry.
482  // \return True if the entry existed and was removed
483  bool erase(const iterator& iter);
484 
485  //- Erase an entry specified by the given key
486  // Performs a checkOut() if the object was ownedByRegistry.
487  // \return True if the entry existed and was removed
488  bool erase(const word& key);
489 
490  //- Remove entries given by the listed keys
491  // Performs a checkOut() for all objects that are ownedByRegistry.
492  // \return The number of items removed
493  label erase(std::initializer_list<word> keys);
494 
495  //- Remove entries given by the listed keys
496  // Performs a checkOut() for all objects that are ownedByRegistry.
497  // \return The number of items removed
498  label erase(const UList<word>& keys);
499 
500  //- Rename
501  virtual void rename(const word& newName);
502 
503 
504  // Reading
505 
506  //- Return true if any of the object's files have been modified
507  virtual bool modified() const;
508 
509  //- Read the objects that have been modified
510  void readModifiedObjects();
511 
512  //- Read object if modified
513  virtual bool readIfModified();
514 
515 
516  // Writing
517 
518  //- writeData function required by regIOobject but not used.
519  // For this class, write is used instead
520  virtual bool writeData(Ostream&) const
521  {
523  return false;
524  }
525 
526  //- Write the objects using stream options
527  virtual bool writeObject
528  (
529  IOstreamOption streamOpt,
530  const bool valid
531  ) const;
532 
533 
534  // Housekeeping
535 
536  //- Deprecated(2018-10) find object
537  // \deprecated(2018-10) - use findObject() method
538  template<class Type>
539  FOAM_DEPRECATED_FOR(2018-10, "findObject / cfindObject() methods")
540  const Type* lookupObjectPtr
541  (
542  const word& name,
543  bool recursive = false
544  ) const
545  {
546  return this->cfindObject<Type>(name, recursive);
547  }
548 
549  //- Deprecated(2018-10) get object pointer, ignoring constness
550  // \deprecated(2018-10) - use getObjectPtr() method
551  template<class Type>
552  FOAM_DEPRECATED_FOR(2018-10, "getObjectPtr() method")
554  (
555  const word& name,
556  bool recursive = false
557  ) const
558  {
559  return this->getObjectPtr<Type>(name, recursive);
560  }
561 };
562 
563 
564 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
565 
566 } // End namespace Foam
567 
568 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
569 
570 #ifdef NoRepository
571  #include "objectRegistryTemplates.C"
572 #endif
573 
574 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
575 
576 #endif
577 
578 // ************************************************************************* //
regIOobject.H
Foam::objectRegistry::getObjectPtr
Type * getObjectPtr(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:423
Foam::objectRegistry::readIfModified
virtual bool readIfModified()
Read object if modified.
Definition: objectRegistry.C:471
wordRes.H
Foam::objectRegistry::sortedNames
wordList sortedNames() const
The sorted names of all objects.
Definition: objectRegistry.C:153
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::objectRegistry::~objectRegistry
virtual ~objectRegistry()
Destructor, with checkOut() for all objects that are ownedByRegistry.
Definition: objectRegistry.C:120
Foam::objectRegistry::writeData
virtual bool writeData(Ostream &) const
writeData function required by regIOobject but not used.
Definition: objectRegistry.H:519
Foam::objectRegistry::getEvent
label getEvent() const
Return new event number.
Definition: objectRegistry.C:200
Foam::objectRegistry::lookupObjectRefPtr
Type * lookupObjectRefPtr(const word &name, bool recursive=false) const
Deprecated(2018-10) get object pointer, ignoring constness.
Definition: objectRegistry.H:553
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
HashTable.H
Foam::objectRegistry::isTimeDb
bool isTimeDb() const noexcept
True if the registry is Time.
Definition: objectRegistry.C:128
Foam::objectRegistry::TypeName
TypeName("objectRegistry")
Declare type name for this IOobject.
Foam::objectRegistry::clearStorage
void clearStorage()
Clear all entries from the registry and the table itself.
Definition: objectRegistry.C:344
Foam::objectRegistry::parent
const objectRegistry & parent() const noexcept
Return the parent objectRegistry.
Definition: objectRegistry.H:172
Foam::regIOobject::checkIn
bool checkIn()
Add object to registry, if not already registered.
Definition: regIOobject.C:188
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::objectRegistry::clear
void clear()
Clear all entries from the registry.
Definition: objectRegistry.C:318
Foam::objectRegistry::lookupObjectPtr
const Type * lookupObjectPtr(const word &name, bool recursive=false) const
Deprecated(2018-10) find object.
Definition: objectRegistry.H:540
Foam::objectRegistry::foundObject
bool foundObject(const word &name, const bool recursive=false) const
Is the named Type found?
Definition: objectRegistryTemplates.C:379
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::objectRegistry::lookupClass
HashTable< const Type * > lookupClass(const bool strict=false) const
Return all objects with a class satisfying isA<Type>
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
Foam::objectRegistry::dbDir
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
Definition: objectRegistry.H:187
Foam::objectRegistry::rename
virtual void rename(const word &newName)
Rename.
Definition: objectRegistry.C:392
Foam::objectRegistry::thisDb
const objectRegistry & thisDb() const noexcept
Return the object registry.
Definition: objectRegistry.H:166
Foam::objectRegistry::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: objectRegistry.C:134
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::objectRegistry::cfindIOobject
const regIOobject * cfindIOobject(const word &name, const bool recursive=false) const
Return const pointer to the regIOobject.
Definition: objectRegistry.C:411
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
HashSet.H
Foam::objectRegistry::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write the objects using stream options.
Definition: objectRegistry.C:479
Foam::objectRegistry::found
bool found(const word &name, const bool recursive=false) const
Can the regIOobject object be found (by name).
Definition: objectRegistry.C:432
objectRegistryTemplates.C
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::objectRegistry::lookupObjectRef
Type & lookupObjectRef(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:478
Foam::objectRegistry::modified
virtual bool modified() const
Return true if any of the object's files have been modified.
Definition: objectRegistry.C:441
Foam::objectRegistry::readModifiedObjects
void readModifiedObjects()
Read the objects that have been modified.
Definition: objectRegistry.C:455
Foam::HashTable< regIOobject * >::keys
const_iterator_pair< const_key_iterator, this_type > keys() const
A const iterator begin/end pair for iterating over keys.
Definition: HashTable.H:880
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::objectRegistry::erase
bool erase(const iterator &iter)
Erase an entry specified by the given iterator.
Definition: objectRegistry.C:351
Foam::objectRegistry::findObject
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:401
Foam::regIOobject::checkOut
bool checkOut()
Remove all file watches and remove object from registry.
Definition: regIOobject.C:224
Foam::objectRegistry::cfindObject
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:390
Foam::objectRegistry::names
wordList names() const
The names of all objects.
Definition: objectRegistry.C:147
Foam::objectRegistry::subRegistry
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false, const bool recursive=false) const
Lookup and return a const sub-objectRegistry.
Definition: objectRegistry.C:174
Foam::List< word >
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
stringListOps.H
Operations on lists of strings.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::objectRegistry::count
label count(const char *clsName) const
The number of objects of the given class name.
Definition: objectRegistry.C:140
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::IOobject::db
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:487