objectRegistryTemplates.C
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-2015 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 \*---------------------------------------------------------------------------*/
28 
29 #include "objectRegistry.H"
30 #include "predicates.H"
31 #include <type_traits>
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 // Templated implementation for classes()
36 template<class MatchPredicate>
37 Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
38 (
39  const objectRegistry& list,
40  const MatchPredicate& matchName
41 )
42 {
43  HashTable<wordHashSet> summary(2*list.size());
44 
45  // Summary (key,val) = (class-name, object-names)
46  forAllConstIters(list, iter)
47  {
48  const regIOobject* obj = iter.val();
49 
50  if (matchName(obj->name()))
51  {
52  // Create entry (if needed) and insert
53  summary(obj->type()).insert(obj->name());
54  }
55  }
56 
57  return summary;
58 }
59 
60 
61 // Templated implementation for count()
62 template<class MatchPredicate1, class MatchPredicate2>
63 Foam::label Foam::objectRegistry::countImpl
64 (
65  const objectRegistry& list,
66  const MatchPredicate1& matchClass,
67  const MatchPredicate2& matchName
68 )
69 {
70  label count = 0;
71 
72  forAllConstIters(list, iter)
73  {
74  const regIOobject* obj = iter.val();
75 
76  if (matchClass(obj->type()) && matchName(obj->name()))
77  {
78  ++count;
79  }
80  }
81 
82  return count;
83 }
84 
85 
86 // Templated implementation for count()
87 template<class Type, class MatchPredicate>
88 Foam::label Foam::objectRegistry::countTypeImpl
89 (
90  const objectRegistry& list,
91  const MatchPredicate& matchName
92 )
93 {
94  label count = 0;
95 
96  forAllConstIters(list, iter)
97  {
98  const regIOobject* obj = iter.val();
99 
100  if
101  (
102  (std::is_void<Type>::value || Foam::isA<Type>(*obj))
103  && matchName(obj->name())
104  )
105  {
106  ++count;
107  }
108  }
109 
110  return count;
111 }
112 
113 
114 // Templated implementation for names(), sortedNames()
115 template<class MatchPredicate1, class MatchPredicate2>
116 Foam::wordList Foam::objectRegistry::namesImpl
117 (
118  const objectRegistry& list,
119  const MatchPredicate1& matchClass,
120  const MatchPredicate2& matchName,
121  const bool doSort
122 )
123 {
124  wordList objNames(list.size());
125 
126  label count=0;
127  forAllConstIters(list, iter)
128  {
129  const regIOobject* obj = iter.val();
130 
131  if (matchClass(obj->type()) && matchName(obj->name()))
132  {
133  objNames[count] = obj->name();
134  ++count;
135  }
136  }
137 
138  objNames.resize(count);
139 
140  if (doSort)
141  {
142  Foam::sort(objNames);
143  }
144 
145  return objNames;
146 }
147 
148 
149 // Templated implementation for names(), sortedNames()
150 template<class Type, class MatchPredicate>
151 Foam::wordList Foam::objectRegistry::namesTypeImpl
152 (
153  const objectRegistry& list,
154  const MatchPredicate& matchName,
155  const bool doSort
156 )
157 {
158  wordList objNames(list.size());
159 
160  label count = 0;
161  forAllConstIters(list, iter)
162  {
163  const regIOobject* obj = iter.val();
164 
165  if
166  (
167  (std::is_void<Type>::value || Foam::isA<Type>(*obj))
168  && matchName(obj->name())
169  )
170  {
171  objNames[count] = obj->name();
172  ++count;
173  }
174  }
175 
176  objNames.resize(count);
177 
178  if (doSort)
179  {
180  Foam::sort(objNames);
181  }
182 
183  return objNames;
184 }
185 
186 
187 // Templated implementation for sorted()
188 template<class Type, class MatchPredicate>
190 Foam::objectRegistry::objectsTypeImpl
191 (
192  const objectRegistry& list,
193  const MatchPredicate& matchName
194 )
195 {
196  typedef typename std::remove_cv<Type>::type BaseType;
197 
198  UPtrList<Type> result(list.size());
199 
200  label count = 0;
201  forAllConstIters(list, iter)
202  {
203  const BaseType* ptr = Foam::isA<BaseType>(*iter.val());
204 
205  if (ptr && matchName(ptr->name()))
206  {
207  result.set(count, const_cast<BaseType*>(ptr));
208  ++count;
209  }
210  }
211 
212  result.resize(count);
213 
214  Foam::sort(result, nameOp<Type>()); // Sort by object name()
215 
216  return result;
217 }
219 
220 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
221 
222 template<class MatchPredicate>
225 (
226  const MatchPredicate& matchName
227 ) const
228 {
229  return classesImpl(*this, matchName);
230 }
231 
232 
233 template<class MatchPredicate>
234 Foam::label Foam::objectRegistry::count
235 (
236  const MatchPredicate& matchClass
237 ) const
238 {
239  return countImpl(*this, matchClass, predicates::always());
240 }
241 
242 
243 template<class MatchPredicate1, class MatchPredicate2>
244 Foam::label Foam::objectRegistry::count
245 (
246  const MatchPredicate1& matchClass,
247  const MatchPredicate2& matchName
248 ) const
249 {
250  return countImpl(*this, matchClass, matchName);
251 }
252 
253 
254 template<class Type, class MatchPredicate>
255 Foam::label Foam::objectRegistry::count
256 (
257  const MatchPredicate& matchName
258 ) const
259 {
260  return countTypeImpl<Type>(*this, matchName);
261 }
262 
263 
264 template<class Type>
265 Foam::label Foam::objectRegistry::count
266 (
267  const bool strict
268 ) const
269 {
270  label nObjects = 0;
271 
272  forAllConstIters(*this, iter)
273  {
274  const regIOobject* obj = iter.val();
275 
276  if
277  (
278  std::is_void<Type>::value
279  ||
280  (
281  strict
282  ? bool(Foam::isType<Type>(*obj))
283  : bool(Foam::isA<Type>(*obj))
284  )
285  )
286  {
287  ++nObjects;
288  }
289  }
290 
291  return nObjects;
292 }
293 
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 template<class Type>
300 {
301  return objectsTypeImpl<const Type>(*this, predicates::always());
302 }
303 
304 
305 template<class Type>
308 {
309  return objectsTypeImpl<const Type>(*this, predicates::always());
310 }
311 
312 
313 template<class Type>
316 {
317  return objectsTypeImpl<Type>(*this, predicates::always());
318 }
319 
320 
321 template<class Type, class MatchPredicate>
324 (
325  const MatchPredicate& matchName
326 ) const
327 {
328  return objectsTypeImpl<const Type>(*this, matchName);
329 }
330 
331 
332 template<class Type, class MatchPredicate>
335 (
336  const MatchPredicate& matchName
337 ) const
338 {
339  return objectsTypeImpl<const Type>(*this, matchName);
340 }
341 
342 template<class Type, class MatchPredicate>
345 (
346  const MatchPredicate& matchName
347 )
348 {
349  return objectsTypeImpl<Type>(*this, matchName);
350 }
351 
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 template<class MatchPredicate>
357 (
358  const MatchPredicate& matchClass
359 ) const
360 {
361  return namesImpl(*this, matchClass, predicates::always(), false);
362 }
363 
364 
365 template<class MatchPredicate1, class MatchPredicate2>
367 (
368  const MatchPredicate1& matchClass,
369  const MatchPredicate2& matchName
370 ) const
371 {
372  return namesImpl(*this, matchClass, matchName, false);
373 }
374 
375 
376 template<class Type>
378 {
379  return namesTypeImpl<Type>(*this, predicates::always(), false);
380 }
381 
382 
383 template<class Type, class MatchPredicate>
385 (
386  const MatchPredicate& matchName
387 ) const
388 {
389  return namesTypeImpl<Type>(*this, matchName, false);
390 }
391 
392 
393 template<class MatchPredicate>
395 (
396  const MatchPredicate& matchClass
397 ) const
398 {
399  return namesImpl(*this, matchClass, predicates::always(), true);
400 }
401 
402 
403 template<class MatchPredicate1, class MatchPredicate2>
405 (
406  const MatchPredicate1& matchClass,
407  const MatchPredicate2& matchName
408 ) const
409 {
410  return namesImpl(*this, matchClass, matchName, true);
411 }
412 
413 
414 template<class Type>
416 {
417  return namesTypeImpl<Type>(*this, predicates::always(), true);
418 }
419 
420 
421 template<class Type, class MatchPredicate>
423 (
424  const MatchPredicate& matchName
425 ) const
426 {
427  return namesTypeImpl<Type>(*this, matchName, true);
428 }
429 
430 
431 template<class Type>
433 (
434  const bool strict
435 ) const
436 {
437  HashTable<const Type*> objectsOfClass(size());
438 
439  forAllConstIters(*this, iter)
440  {
441  const regIOobject* obj = iter.val();
442 
443  if
444  (
445  strict
446  ? bool(Foam::isType<Type>(*obj))
447  : bool(Foam::isA<Type>(*obj))
448  )
449  {
450  objectsOfClass.insert(obj->name(), dynamic_cast<const Type*>(obj));
451  }
452  }
454  return objectsOfClass;
455 }
456 
457 
458 template<class Type>
460 (
461  const bool strict
462 )
463 {
464  HashTable<Type*> objectsOfClass(size());
465 
466  forAllIters(*this, iter)
467  {
468  regIOobject* obj = iter.val();
469 
470  if
471  (
472  strict
473  ? bool(Foam::isType<Type>(*obj))
474  : bool(Foam::isA<Type>(*obj))
475  )
476  {
477  objectsOfClass.insert(obj->name(), dynamic_cast<Type*>(obj));
478  }
479  }
481  return objectsOfClass;
482 }
483 
484 
485 template<class Type>
487 (
488  const word& name,
489  const bool recursive
490 ) const
491 {
492  return this->cfindObject<Type>(name, recursive);
493 }
494 
495 
496 template<class Type>
498 (
499  const word& name,
500  const bool recursive
501 ) const
502 {
503  return dynamic_cast<const Type*>(this->cfindIOobject(name, recursive));
504 }
505 
506 
507 template<class Type>
509 (
510  const word& name,
511  const bool recursive
512 ) const
513 {
514  return this->cfindObject<Type>(name, recursive);
515 }
516 
517 
518 template<class Type>
520 (
521  const word& name,
522  const bool recursive
523 )
524 {
525  return const_cast<Type*>(this->cfindObject<Type>(name, recursive));
526 }
527 
528 
529 template<class Type>
531 (
532  const word& name,
533  const bool recursive
534 ) const
535 {
536  return const_cast<Type*>(this->cfindObject<Type>(name, recursive));
537 }
538 
539 
540 template<class Type>
542 (
543  const word& name,
544  const bool recursive
545 ) const
546 {
547  const_iterator iter = cfind(name);
548 
549  if (iter.found())
550  {
551  const Type* ptr = dynamic_cast<const Type*>(iter());
552 
553  if (ptr)
554  {
555  return *ptr;
556  }
557 
559  << nl
560  << " bad lookup of " << name << " (objectRegistry "
561  << this->name()
562  << ")\n expected a " << Type::typeName
563  << ", found a " << (*iter)->type() << nl
564  << exit(FatalError);
565  }
566  else if (recursive && this->parentNotTime())
567  {
568  return parent_.lookupObject<Type>(name, recursive);
569  }
570 
572  << nl
573  << " failed lookup of " << name << " (objectRegistry "
574  << this->name()
575  << ")\n available objects of type " << Type::typeName
576  << ':' << nl
577  << names<Type>() << nl
578  << exit(FatalError);
580  return NullObjectRef<Type>();
581 }
582 
583 
584 template<class Type>
586 (
587  const word& name,
588  const bool recursive
589 ) const
590 {
591  const Type& ref = this->lookupObject<Type>(name, recursive);
592  // The above will already fail if things didn't work
593 
594  return const_cast<Type&>(ref);
595 }
596 
597 
598 // ************************************************************************* //
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...
type
Types of root.
Definition: Roots.H:52
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
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...
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:150
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
wordList names() const
The unsorted names of all objects.
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:45
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
bool insert(const word &key, const regIOobject * &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:173
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:53
label count(const char *clsName) const
The number of objects of the given class name.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
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...
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:334
wordList sortedNames() const
The sorted names of all objects.
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:328
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
rDeltaT ref()
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:69
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.
HashTable< const Type * > lookupClass(const bool strict=false) const
Return all objects with a class satisfying isA<Type>
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.