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-2020 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;
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  // Protected Member Functions
142 
143  //- Return const pointer to the regIOobject.
144  //
145  // \param recursive search parent registries
146  //
147  // \return nullptr if the object was not found.
148  const regIOobject* cfindIOobject
149  (
150  const word& name,
151  const bool recursive = false
152  ) const;
153 
154 
155 public:
156 
157  //- Declare type name for this IOobject
158  TypeName("objectRegistry");
159 
160 
161  // Constructors
162 
163  //- Construct the time objectRegistry,
164  //- with estimated table capacity (default: 128)
165  explicit objectRegistry(const Time& db, const label nObjects=128);
166 
167  //- Construct sub-registry given an IObject to describe the registry,
168  //- with estimated table capacity (default: 128)
169  explicit objectRegistry(const IOobject& io, const label nObjects=128);
170 
171 
172  //- Destructor, with checkOut() for all objects that are ownedByRegistry
173  virtual ~objectRegistry();
174 
175 
176  // Member Functions
177 
178  // Access
179 
180  //- Return the object registry
181  const objectRegistry& thisDb() const
182  {
183  return *this;
184  }
185 
186  //- Return time
187  const Time& time() const
188  {
189  return time_;
190  }
191 
192  //- Return the parent objectRegistry
193  const objectRegistry& parent() const
194  {
195  return parent_;
196  }
197 
198  //- Local directory path of this objectRegistry relative to the time
199  virtual const fileName& dbDir() const
200  {
201  return dbDir_;
202  }
203 
204 
205  // Summary of classes
206 
207  //- A summary hash of classes used and their associated object names.
208  // Behaviour and usage as per IOobjectList::classes
210 
211  //- A summary hash of classes used and their associated object names,
212  //- restricted to objects that have a matching object name.
213  template<class MatchPredicate>
214  HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
215 
216 
217  // Number of items
218 
219  //- The number of objects of the given class name
220  // \note uses the class type() method
221  label count(const char* clsName) const;
222 
223  //- The number of objects of the given class name
224  // \note uses the class type() method
225  template<class MatchPredicate>
226  label count(const MatchPredicate& matchClass) const;
227 
228  //- The number of objects of the given class name
229  // \note uses the class type() method
230  template<class MatchPredicate1, class MatchPredicate2>
231  label count
232  (
233  const MatchPredicate1& matchClass,
234  const MatchPredicate2& matchName
235  ) const;
236 
237  //- The names of objects with a class satisfying \c isA<Type>
238  //
239  // \param strict use \c isType<Type> instead of \c isA<Type>
240  //
241  // \note The values of \c count<Type>() and \c count(Type::typeName)
242  // may be inconsistent, since they use different mechanisms for
243  // testing the class type.
244  // \note If \a Type is \c void, no isA check is used (always true).
245  template<class Type>
246  label count(const bool strict = false) const;
247 
248  //- The names of objects with a class satisfying \c isA<Type>
249  //- that also have a matching object name.
250  //
251  // \note If \a Type is \c void, no isA check is used (always true).
252  template<class Type, class MatchPredicate>
253  label count(const MatchPredicate& matchName) const;
254 
255 
256  // Summary of names
257 
258  //- The names of all objects
259  wordList names() const;
260 
261  //- The names of objects with the given class name.
262  // \note uses the class type() method
263  wordList names(const char* clsName) const;
264 
265  //- The names of objects with a matching class name
266  // \note uses the class type() method
267  template<class MatchPredicate>
268  wordList names(const MatchPredicate& matchClass) const;
269 
270  //- The names of objects with a matching class name
271  //- that also have a matching object name.
272  // \note uses the class type() method
273  template<class MatchPredicate1, class MatchPredicate2>
275  (
276  const MatchPredicate1& matchClass,
277  const MatchPredicate2& matchName
278  ) const;
279 
280  //- The names of objects with a class satisfying \c isA<Type>.
281  //
282  // \note If \a Type is \c void, no isA check is used (always true).
283  template<class Type>
284  wordList names() const;
285 
286  //- The names of objects with a class satisfying \c isA<Type>
287  //- that also have a matching object name.
288  //
289  // \note If \a Type is \c void, no isA check is used (always true).
290  template<class Type, class MatchPredicate>
291  wordList names(const MatchPredicate& matchName) const;
292 
293 
294  // Summary of names (sorted)
295 
296  //- The sorted names of all objects
297  wordList sortedNames() const;
298 
299  //- The sorted names of objects with the given class name.
300  // \note uses the class type() method
301  wordList sortedNames(const char* clsName) const;
302 
303  //- The sorted names objects with a matching class name
304  // \note uses the class type() method
305  template<class MatchPredicate>
306  wordList sortedNames(const MatchPredicate& matchClass) const;
307 
308  //- The sorted names of objects with a matching class name
309  //- that also have a matching object name.
310  // \note uses the class type() method
311  template<class MatchPredicate1, class MatchPredicate2>
313  (
314  const MatchPredicate1& matchClass,
315  const MatchPredicate2& matchName
316  ) const;
317 
318  //- The sorted names of objects with a class satisfying \c isA<Type>
319  //
320  // \note If \a Type is \c void, no isA check is used (always true).
321  template<class Type>
322  wordList sortedNames() const;
323 
324  //- The sorted names of objects with a class satisfying \c isA<Type>
325  //- that also have a matching object name.
326  //
327  // \note If \a Type is \c void, no isA check is used (always true).
328  template<class Type, class MatchPredicate>
329  wordList sortedNames(const MatchPredicate& matchName) const;
330 
331 
332  // Lookup
333 
334  //- Lookup and return a const sub-objectRegistry.
335  //
336  // \param forceCreate create it if it does not exist.
337  // \param recursive search parent registries.
339  (
340  const word& name,
341  const bool forceCreate = false,
342  const bool recursive = false
343  ) const;
344 
345 
346  //- Return all objects with a class satisfying \c isA<Type>
347  //
348  // \param strict use \c isType<Type> instead of \c isA<Type>
349  template<class Type>
350  HashTable<const Type*> lookupClass(const bool strict = false) const;
351 
352  //- Return all objects with a class satisfying \c isA<Type>
353  //
354  // \param strict use \c isType<Type> instead of \c isA<Type>
355  template<class Type>
356  HashTable<Type*> lookupClass(const bool strict = false);
357 
358 
359  //- Can the regIOobject object be found (by name).
360  //
361  // \param recursive search parent registries
362  bool found(const word& name, const bool recursive = false) const;
363 
364 
365  //- Is the named Type found?
366  //
367  // \param recursive search parent registries
368  template<class Type>
369  bool foundObject
370  (
371  const word& name,
372  const bool recursive = false
373  ) const;
374 
375  //- Return const pointer to the object of the given Type.
376  //
377  // \param recursive search parent registries
378  //
379  // \return nullptr if the object was not found or had incorrect type.
380  template<class Type>
381  const Type* cfindObject
382  (
383  const word& name,
384  const bool recursive = false
385  ) const;
386 
387  //- Return const pointer to the object of the given Type.
388  //
389  // \param recursive search parent registries
390  //
391  // \return nullptr if the object was not found or had incorrect type.
392  template<class Type>
393  const Type* findObject
394  (
395  const word& name,
396  const bool recursive = false
397  ) const;
398 
399  //- Return non-const pointer to the object of the given Type.
400  //
401  // \param recursive search parent registries
402  //
403  // \return nullptr if the object was not found or had incorrect type.
404  template<class Type>
405  Type* findObject
406  (
407  const word& name,
408  const bool recursive = false
409  );
410 
411  //- Return non-const pointer to the object of the given Type,
412  //- using a const-cast to have it behave like a mutable.
413  // Exercise caution when using.
414  //
415  // \param recursive search parent registries.
416  //
417  // \return nullptr if the object was not found or had incorrect type.
418  template<class Type>
419  Type* getObjectPtr
420  (
421  const word& name,
422  const bool recursive = false
423  ) const;
424 
425  //- Lookup and return const reference to the object
426  //- of the given Type. Fatal if not found or the wrong type.
427  //
428  // \param recursive search parent registries.
429  template<class Type>
430  const Type& lookupObject
431  (
432  const word& name,
433  const bool recursive = false
434  ) const;
435 
436  //- Lookup and return non-const reference to the object
437  //- of the given Type. Fatal if not found or the wrong type.
438  //
439  // \param recursive search parent registries.
440  template<class Type>
441  Type& lookupObjectRef
442  (
443  const word& name,
444  const bool recursive = false
445  ) const;
446 
447 
448  // Events
449 
450  //- Return new event number.
451  label getEvent() const;
452 
453 
454  // Edit
455 
456  //- Add a regIOobject to registry. A nullptr is ignored.
457  bool checkIn(regIOobject* io) const;
458 
459  //- Add a regIOobject to registry
460  bool checkIn(regIOobject& io) const;
461 
462  //- Remove a regIOobject from registry and free memory if the
463  //- object is ownedByRegistry. A nullptr is ignored.
464  bool checkOut(regIOobject* io) const;
465 
466  //- Remove a regIOobject from registry and free memory if the
467  //- object is ownedByRegistry.
468  bool checkOut(regIOobject& io) const;
469 
470  //- Remove a regIOobject by name from registry and free memory if the
471  //- object is ownedByRegistry
472  bool checkOut(const word& key) const;
473 
474  //- Clear all entries from the registry
475  // Performs a checkOut() for all objects that are ownedByRegistry
476  void clear();
477 
478  //- Clear all entries from the registry and the table itself.
479  void clearStorage();
480 
481  //- Erase an entry specified by the given iterator.
482  // Performs a checkOut() if the object was ownedByRegistry.
483  // \return True if the entry existed and was removed
484  bool erase(const iterator& iter);
485 
486  //- Erase an entry specified by the given key
487  // Performs a checkOut() if the object was ownedByRegistry.
488  // \return True if the entry existed and was removed
489  bool erase(const word& key);
490 
491  //- Remove entries given by the listed keys
492  // Performs a checkOut() for all objects that are ownedByRegistry.
493  // \return The number of items removed
494  label erase(std::initializer_list<word> keys);
495 
496  //- Remove entries given by the listed keys
497  // Performs a checkOut() for all objects that are ownedByRegistry.
498  // \return The number of items removed
499  label erase(const UList<word>& keys);
500 
501  //- Rename
502  virtual void rename(const word& newName);
503 
504 
505  // Reading
506 
507  //- Return true if any of the object's files have been modified
508  virtual bool modified() const;
509 
510  //- Read the objects that have been modified
511  void readModifiedObjects();
512 
513  //- Read object if modified
514  virtual bool readIfModified();
515 
516 
517  // Writing
518 
519  //- writeData function required by regIOobject but not used.
520  // For this class, write is used instead
521  virtual bool writeData(Ostream&) const
522  {
524  return false;
525  }
526 
527  //- Write the objects using stream options
528  virtual bool writeObject
529  (
530  IOstreamOption streamOpt,
531  const bool valid
532  ) const;
533 
534 
535  // Housekeeping
536 
537  //- Deprecated(2018-10) find object
538  // \deprecated(2018-10) - use findObject() method
539  template<class Type>
540  FOAM_DEPRECATED_FOR(2018-10, "findObject / cfindObject() methods")
541  const Type* lookupObjectPtr
542  (
543  const word& name,
544  bool recursive = false
545  ) const
546  {
547  return this->cfindObject<Type>(name, recursive);
548  }
549 
550  //- Deprecated(2018-10) get object pointer, ignoring constness
551  // \deprecated(2018-10) - use getObjectPtr() method
552  template<class Type>
553  FOAM_DEPRECATED_FOR(2018-10, "getObjectPtr() method")
555  (
556  const word& name,
557  bool recursive = false
558  ) const
559  {
560  return this->getObjectPtr<Type>(name, recursive);
561  }
562 };
563 
564 
565 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
566 
567 } // End namespace Foam
568 
569 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
570 
571 #ifdef NoRepository
572  #include "objectRegistryTemplates.C"
573 #endif
574 
575 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
576 
577 #endif
578 
579 // ************************************************************************* //
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:467
wordRes.H
Foam::objectRegistry::sortedNames
wordList sortedNames() const
The sorted names of all objects.
Definition: objectRegistry.C:170
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::objectRegistry::~objectRegistry
virtual ~objectRegistry()
Destructor, with checkOut() for all objects that are ownedByRegistry.
Definition: objectRegistry.C:143
Foam::objectRegistry::writeData
virtual bool writeData(Ostream &) const
writeData function required by regIOobject but not used.
Definition: objectRegistry.H:520
Foam::objectRegistry::getEvent
label getEvent() const
Return new event number.
Definition: objectRegistry.C:217
Foam::objectRegistry::lookupObjectRefPtr
Type * lookupObjectRefPtr(const word &name, bool recursive=false) const
Deprecated(2018-10) get object pointer, ignoring constness.
Definition: objectRegistry.H:554
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
HashTable.H
Foam::objectRegistry::TypeName
TypeName("objectRegistry")
Declare type name for this IOobject.
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
Foam::objectRegistry::clearStorage
void clearStorage()
Clear all entries from the registry and the table itself.
Definition: objectRegistry.C:361
Foam::regIOobject::checkIn
bool checkIn()
Add object to registry, if not already registered.
Definition: regIOobject.C:205
Foam::objectRegistry::clear
void clear()
Clear all entries from the registry.
Definition: objectRegistry.C:335
Foam::objectRegistry::lookupObjectPtr
const Type * lookupObjectPtr(const word &name, bool recursive=false) const
Deprecated(2018-10) find object.
Definition: objectRegistry.H:541
Foam::objectRegistry::foundObject
bool foundObject(const word &name, const bool recursive=false) const
Is the named Type found?
Definition: objectRegistryTemplates.C:379
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:457
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:445
Foam::objectRegistry::dbDir
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
Definition: objectRegistry.H:198
Foam::objectRegistry::rename
virtual void rename(const word &newName)
Rename.
Definition: objectRegistry.C:409
Foam::objectRegistry::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: objectRegistry.C:151
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
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:475
Foam::objectRegistry::found
bool found(const word &name, const bool recursive=false) const
Can the regIOobject object be found (by name).
Definition: objectRegistry.C:428
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:437
Foam::objectRegistry::readModifiedObjects
void readModifiedObjects()
Read the objects that have been modified.
Definition: objectRegistry.C:451
Foam::objectRegistry::thisDb
const objectRegistry & thisDb() const
Return the object registry.
Definition: objectRegistry.H:180
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:868
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:71
Foam::objectRegistry::erase
bool erase(const iterator &iter)
Erase an entry specified by the given iterator.
Definition: objectRegistry.C:368
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:241
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::parent
const objectRegistry & parent() const
Return the parent objectRegistry.
Definition: objectRegistry.H:192
Foam::objectRegistry::names
wordList names() const
The names of all objects.
Definition: objectRegistry.C:164
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:191
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:157