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-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 \*---------------------------------------------------------------------------*/
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 || 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 || 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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
188 
189 template<class MatchPredicate>
192 (
193  const MatchPredicate& matchName
194 ) const
195 {
196  return classesImpl(*this, matchName);
197 }
198 
199 
200 template<class MatchPredicate>
201 Foam::label Foam::objectRegistry::count
202 (
203  const MatchPredicate& matchClass
204 ) const
205 {
206  return countImpl(*this, matchClass, predicates::always());
207 }
208 
209 
210 template<class MatchPredicate1, class MatchPredicate2>
211 Foam::label Foam::objectRegistry::count
212 (
213  const MatchPredicate1& matchClass,
214  const MatchPredicate2& matchName
215 ) const
216 {
217  return countImpl(*this, matchClass, matchName);
218 }
219 
220 
221 template<class Type, class MatchPredicate>
222 Foam::label Foam::objectRegistry::count
223 (
224  const MatchPredicate& matchName
225 ) const
226 {
227  return countTypeImpl<Type>(*this, matchName);
228 }
229 
230 
231 template<class Type>
232 Foam::label Foam::objectRegistry::count
233 (
234  const bool strict
235 ) const
236 {
237  label nObjects = 0;
238 
239  forAllConstIters(*this, iter)
240  {
241  const regIOobject* obj = iter.val();
242 
243  if
244  (
245  std::is_void<Type>::value
246  || (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
247  )
248  {
249  ++nObjects;
250  }
251  }
252 
253  return nObjects;
254 }
255 
256 
257 template<class MatchPredicate>
259 (
260  const MatchPredicate& matchClass
261 ) const
262 {
263  return namesImpl(*this, matchClass, predicates::always(), false);
264 }
265 
266 
267 template<class MatchPredicate1, class MatchPredicate2>
269 (
270  const MatchPredicate1& matchClass,
271  const MatchPredicate2& matchName
272 ) const
273 {
274  return namesImpl(*this, matchClass, matchName, false);
275 }
276 
277 
278 template<class Type>
280 {
281  return namesTypeImpl<Type>(*this, predicates::always(), false);
282 }
283 
284 
285 template<class Type, class MatchPredicate>
287 (
288  const MatchPredicate& matchName
289 ) const
290 {
291  return namesTypeImpl<Type>(*this, matchName, false);
292 }
293 
294 
295 template<class MatchPredicate>
297 (
298  const MatchPredicate& matchClass
299 ) const
300 {
301  return namesImpl(*this, matchClass, predicates::always(), true);
302 }
303 
304 
305 template<class MatchPredicate1, class MatchPredicate2>
307 (
308  const MatchPredicate1& matchClass,
309  const MatchPredicate2& matchName
310 ) const
311 {
312  return namesImpl(*this, matchClass, matchName, true);
313 }
314 
315 
316 template<class Type>
318 {
319  return namesTypeImpl<Type>(*this, predicates::always(), true);
320 }
321 
322 
323 template<class Type, class MatchPredicate>
325 (
326  const MatchPredicate& matchName
327 ) const
328 {
329  return namesTypeImpl<Type>(*this, matchName, true);
330 }
331 
332 
333 template<class Type>
335 (
336  const bool strict
337 ) const
338 {
339  HashTable<const Type*> objectsOfClass(size());
340 
341  forAllConstIters(*this, iter)
342  {
343  const regIOobject* obj = iter.val();
344 
345  if (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
346  {
347  objectsOfClass.insert(obj->name(), dynamic_cast<const Type*>(obj));
348  }
349  }
350 
351  return objectsOfClass;
352 }
353 
354 
355 template<class Type>
357 (
358  const bool strict
359 )
360 {
361  HashTable<Type*> objectsOfClass(size());
362 
363  forAllIters(*this, iter)
364  {
365  regIOobject* obj = iter.val();
366 
367  if (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
368  {
369  objectsOfClass.insert(obj->name(), dynamic_cast<Type*>(obj));
370  }
371  }
372 
373  return objectsOfClass;
374 }
375 
376 
377 template<class Type>
379 (
380  const word& name,
381  const bool recursive
382 ) const
383 {
384  return this->cfindObject<Type>(name, recursive);
385 }
386 
387 
388 template<class Type>
390 (
391  const word& name,
392  const bool recursive
393 ) const
394 {
395  return dynamic_cast<const Type*>(this->cfindIOobject(name, recursive));
396 }
397 
398 
399 template<class Type>
401 (
402  const word& name,
403  const bool recursive
404 ) const
405 {
406  return this->cfindObject<Type>(name, recursive);
407 }
408 
409 
410 template<class Type>
412 (
413  const word& name,
414  const bool recursive
415 )
416 {
417  return const_cast<Type*>(this->cfindObject<Type>(name, recursive));
418 }
419 
420 
421 template<class Type>
423 (
424  const word& name,
425  const bool recursive
426 ) const
427 {
428  return const_cast<Type*>(this->cfindObject<Type>(name, recursive));
429 }
430 
431 
432 template<class Type>
434 (
435  const word& name,
436  const bool recursive
437 ) const
438 {
439  const_iterator iter = cfind(name);
440 
441  if (iter.found())
442  {
443  const Type* ptr = dynamic_cast<const Type*>(iter());
444 
445  if (ptr)
446  {
447  return *ptr;
448  }
449 
451  << nl
452  << " bad lookup of " << name << " (objectRegistry "
453  << this->name()
454  << ")\n expected a " << Type::typeName
455  << ", found a " << (*iter)->type() << nl
456  << exit(FatalError);
457  }
458  else if (recursive && this->parentNotTime())
459  {
460  return parent_.lookupObject<Type>(name, recursive);
461  }
462 
464  << nl
465  << " failed lookup of " << name << " (objectRegistry "
466  << this->name()
467  << ")\n available objects of type " << Type::typeName
468  << ':' << nl
469  << names<Type>() << nl
470  << exit(FatalError);
471 
472  return NullObjectRef<Type>();
473 }
474 
475 
476 template<class Type>
478 (
479  const word& name,
480  const bool recursive
481 ) const
482 {
483  const Type& ref = this->lookupObject<Type>(name, recursive);
484  // The above will already fail if things didn't work
485 
486  return const_cast<Type&>(ref);
487 }
488 
489 
490 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::objectRegistry::getObjectPtr
Type * getObjectPtr(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:423
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::objectRegistry::sortedNames
wordList sortedNames() const
The sorted names of all objects.
Definition: objectRegistry.C:153
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
objectRegistry.H
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
ref
rDeltaT ref()
Foam::objectRegistry::foundObject
bool foundObject(const word &name, const bool recursive=false) const
Is the named Type found?
Definition: objectRegistryTemplates.C:379
Foam::predicates::always
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:56
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>
Foam::objectRegistry::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: objectRegistry.C:134
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:223
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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::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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
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::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::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::objectRegistry::names
wordList names() const
The names of all objects.
Definition: objectRegistry.C:147
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< word >
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
predicates.H
Foam::objectRegistry::count
label count(const char *clsName) const
The number of objects of the given class name.
Definition: objectRegistry.C:140