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