IOobjectListTemplates.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) 2018-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "IOobjectList.H"
29#include "predicates.H"
30
31// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32
33// Templated implementation for classes()
34template<class MatchPredicate>
35Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classesImpl
36(
37 const IOobjectList& list,
38 const MatchPredicate& matchName
39)
40{
41 HashTable<wordHashSet> summary(2*list.size());
42
43 // Summary (key,val) = (class-name, object-names)
44 forAllConstIters(list, iter)
45 {
46 const word& key = iter.key();
47 const IOobject* io = iter.val();
48
49 if (matchName(key))
50 {
51 // Create entry (if needed) and insert
52 summary(io->headerClassName()).insert(key);
53 }
54 }
55
56 return summary;
57}
58
59
60// Templated implementation for count()
61template<class MatchPredicate1, class MatchPredicate2>
62Foam::label Foam::IOobjectList::countImpl
63(
64 const IOobjectList& list,
65 const MatchPredicate1& matchClass,
66 const MatchPredicate2& matchName
67)
68{
69 label count = 0;
70
71 forAllConstIters(list, iter)
72 {
73 const IOobject* io = iter.val();
74
75 if (matchClass(io->headerClassName()) && matchName(io->name()))
76 {
77 ++count;
78 }
79 }
80
81 return count;
82}
83
84
85// Templated implementation for count()
86template<class Type, class MatchPredicate>
87Foam::label Foam::IOobjectList::countTypeImpl
88(
89 const IOobjectList& list,
90 const MatchPredicate& matchName
91)
92{
93 label count = 0;
94
95 forAllConstIters(list, iter)
96 {
97 const IOobject* io = iter.val();
98
99 if (io->isHeaderClass<Type>() && matchName(io->name()))
100 {
101 ++count;
102 }
103 }
104
105 return count;
106}
107
108
109// Templated implementation for names(), sortedNames()
110template<class MatchPredicate1, class MatchPredicate2>
111Foam::wordList Foam::IOobjectList::namesImpl
112(
113 const IOobjectList& list,
114 const MatchPredicate1& matchClass,
115 const MatchPredicate2& matchName,
116 const bool doSort
117)
118{
119 wordList objNames(list.size());
120
121 label count = 0;
122 forAllConstIters(list, iter)
123 {
124 const word& key = iter.key();
125 const IOobject* io = iter.val();
126
127 if (matchClass(io->headerClassName()) && matchName(key))
128 {
129 objNames[count] = key;
130 ++count;
131 }
132 }
133
134 objNames.resize(count);
135
136 if (doSort)
137 {
138 Foam::sort(objNames);
139 }
140
141 return objNames;
142}
143
144
145// Templated implementation for names(), sortedNames()
146template<class Type, class MatchPredicate>
147Foam::wordList Foam::IOobjectList::namesTypeImpl
148(
149 const IOobjectList& list,
150 const MatchPredicate& matchName,
151 const bool doSort
152)
153{
154 wordList objNames(list.size());
155
156 label count = 0;
157 forAllConstIters(list, iter)
158 {
159 const word& key = iter.key();
160 const IOobject* io = iter.val();
161
162 if (io->isHeaderClass<Type>() && matchName(key))
163 {
164 objNames[count] = key;
165 ++count;
166 }
167 }
168
169 objNames.resize(count);
170
171 if (doSort)
172 {
173 Foam::sort(objNames);
174 }
175
176 return objNames;
177}
178
179
180// Templated implementation for sorted()
181template<class Type, class MatchPredicate>
183Foam::IOobjectList::objectsTypeImpl
184(
185 const IOobjectList& list,
186 const MatchPredicate& matchName
187)
188{
189 UPtrList<const IOobject> result(list.size());
190
191 label count = 0;
192 forAllConstIters(list, iter)
193 {
194 const word& key = iter.key();
195 const IOobject* io = iter.val();
196
197 if (io->isHeaderClass<Type>() && matchName(key))
198 {
199 result.set(count, io);
200 ++count;
201 }
202 }
203
204 result.resize(count);
205
206 Foam::sort(result, nameOp<IOobject>()); // Sort by object name()
207
208 return result;
209}
210
211
212// Templated implementation for lookup()
213template<class MatchPredicate>
214Foam::IOobjectList Foam::IOobjectList::lookupImpl
215(
216 const IOobjectList& list,
217 const MatchPredicate& matchName
218)
219{
220 IOobjectList results(list.size());
221
222 forAllConstIters(list, iter)
223 {
224 const word& key = iter.key();
225 const IOobject* io = iter.val();
226
227 if (matchName(key))
228 {
229 if (IOobject::debug)
230 {
231 InfoInFunction << "Found " << key << endl;
232 }
233
234 results.set(key, new IOobject(*io));
235 }
236 }
237
238 return results;
239}
240
241
242// Templated implementation for lookupClass()
243template<class MatchPredicate1, class MatchPredicate2>
244Foam::IOobjectList Foam::IOobjectList::lookupClassImpl
245(
246 const IOobjectList& list,
247 const MatchPredicate1& matchClass,
248 const MatchPredicate2& matchName
249)
250{
251 IOobjectList results(list.size());
252
253 forAllConstIters(list, iter)
254 {
255 const word& key = iter.key();
256 const IOobject* io = iter.val();
257
258 if (matchClass(io->headerClassName()) && matchName(key))
259 {
260 if (IOobject::debug)
261 {
262 InfoInFunction << "Found " << key << endl;
263 }
264
265 results.set(key, new IOobject(*io));
266 }
267 }
268
269 return results;
270}
271
272
273// Templated implementation for lookupClass()
274template<class Type, class MatchPredicate>
275Foam::IOobjectList Foam::IOobjectList::lookupClassTypeImpl
276(
277 const IOobjectList& list,
278 const MatchPredicate& matchName
279)
280{
281 IOobjectList results(list.size());
282
283 forAllConstIters(list, iter)
284 {
285 const word& key = iter.key();
286 const IOobject* io = iter.val();
287
288 if (io->isHeaderClass<Type>() && matchName(key))
289 {
290 if (IOobject::debug)
291 {
292 InfoInFunction << "Found " << key << endl;
293 }
294
295 results.set(key, new IOobject(*io));
296 }
297 }
298
299 return results;
300}
301
302
303// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
304
305template<class Type>
307(
308 const word& objName
309) const
310{
311 const_iterator iter = cfind(objName);
312
313 if (iter.found())
314 {
315 const IOobject* io = iter.val();
316
317 if (io->isHeaderClass<Type>())
318 {
319 if (IOobject::debug)
320 {
321 InfoInFunction << "Found " << objName << endl;
322 }
323
324 return io;
325 }
326 else if (IOobject::debug)
327 {
329 << "Found " << objName << " of different type" << endl;
330 }
331 }
332 else if (IOobject::debug)
333 {
334 InfoInFunction << "Could not find " << objName << endl;
335 }
336
337 return nullptr;
338}
339
340
341template<class Type>
343(
344 const word& objName
345) const
346{
347 return cfindObject<Type>(objName);
348}
349
350
351template<class Type>
353{
354 return const_cast<IOobject*>(cfindObject<Type>(objName));
355}
356
357
358template<class Type>
360{
361 return const_cast<IOobject*>(cfindObject<Type>(objName));
362}
363
364
365template<class MatchPredicate>
367(
368 const MatchPredicate& matchName
369) const
370{
371 return lookupImpl(*this, matchName);
372}
373
374
375template<class MatchPredicate>
377(
378 const MatchPredicate& matchClass
379) const
380{
381 return lookupClassImpl(*this, matchClass, predicates::always());
382}
383
384
385template<class MatchPredicate1, class MatchPredicate2>
387(
388 const MatchPredicate1& matchClass,
389 const MatchPredicate2& matchName
390) const
391{
392 return lookupClassImpl(*this, matchClass, matchName);
393}
394
395
396template<class Type>
398{
399 return lookupClassTypeImpl<Type>(*this, predicates::always());
400}
401
402
403template<class Type, class MatchPredicate>
405(
406 const MatchPredicate& matchName
407) const
408{
409 return lookupClassImpl<Type>(*this, matchName);
410}
411
412
413template<class MatchPredicate>
416(
417 const MatchPredicate& matchName
418) const
419{
420 return classesImpl(*this, matchName);
421}
422
423
424template<class MatchPredicate>
426(
427 const MatchPredicate& matchClass
428) const
429{
430 return countImpl(*this, matchClass, predicates::always());
431}
432
433
434template<class MatchPredicate1, class MatchPredicate2>
436(
437 const MatchPredicate1& matchClass,
438 const MatchPredicate2& matchName
439) const
440{
441 return countImpl(*this, matchClass, matchName);
442}
443
444
445template<class Type>
446Foam::label Foam::IOobjectList::count() const
447{
448 return countTypeImpl<Type>(*this, predicates::always());
449}
450
451
452template<class Type, class MatchPredicate>
454(
455 const MatchPredicate& matchName
456) const
457{
458 return countTypeImpl<Type>(*this, matchName);
459}
460
461
462
463// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464
465template<class Type>
468{
469 return objectsTypeImpl<Type>(*this, predicates::always());
470}
471
472
473template<class Type>
475Foam::IOobjectList::sorted(const bool syncPar) const
476{
478 (
479 objectsTypeImpl<Type>(*this, predicates::always())
480 );
481
482 checkObjectOrder(list, syncPar);
483
484 return list;
485}
486
487
488template<class Type, class MatchPredicate>
491(
492 const MatchPredicate& matchName
493) const
494{
495 return objectsTypeImpl<Type>(*this, matchName);
496}
497
498
499template<class Type, class MatchPredicate>
502(
503 const MatchPredicate& matchName,
504 const bool syncPar
505) const
506{
508 (
509 objectsTypeImpl<Type>(*this, matchName)
510 );
511
512 checkObjectOrder(list, syncPar);
513
514 return list;
515}
516
517
518// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
519
520template<class MatchPredicate>
522(
523 const MatchPredicate& matchClass
524) const
525{
526 return namesImpl(*this, matchClass, predicates::always(), false);
527}
528
529
530template<class MatchPredicate>
532(
533 const MatchPredicate& matchClass,
534 const bool syncPar
535) const
536{
537 return sortedNames(matchClass, syncPar);
538}
539
540
541template<class MatchPredicate1, class MatchPredicate2>
543(
544 const MatchPredicate1& matchClass,
545 const MatchPredicate2& matchName
546) const
547{
548 return namesImpl(*this, matchClass, matchName, false);
549}
550
551
552template<class MatchPredicate1, class MatchPredicate2>
554(
555 const MatchPredicate1& matchClass,
556 const MatchPredicate2& matchName,
557 const bool syncPar
558) const
559{
560 return sortedNames(matchClass, matchName, syncPar);
561}
562
563
564template<class Type>
566{
567 return namesTypeImpl<Type>(*this, predicates::always(), false);
568}
569
570
571template<class Type>
573{
574 return sortedNames<Type>(syncPar);
575}
576
577
578template<class Type, class MatchPredicate>
580(
581 const MatchPredicate& matchName
582) const
583{
584 return namesTypeImpl<Type>(*this, matchName, false);
585}
586
587
588template<class Type, class MatchPredicate>
590(
591 const MatchPredicate& matchName,
592 const bool syncPar
593) const
594{
595 return sortedNames<Type>(matchName, syncPar);
596}
597
598
599// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
600
601template<class MatchPredicate>
603(
604 const MatchPredicate& matchClass
605) const
606{
607 return namesImpl(*this, matchClass, predicates::always(), true);
608}
609
610
611template<class MatchPredicate>
613(
614 const MatchPredicate& matchClass,
615 const bool syncPar
616) const
617{
618 wordList objNames
619 (
620 namesImpl(*this, matchClass, predicates::always(), true)
621 );
622
623 checkNameOrder(objNames, syncPar);
624 return objNames;
625}
626
627
628template<class MatchPredicate1, class MatchPredicate2>
630(
631 const MatchPredicate1& matchClass,
632 const MatchPredicate2& matchName
633) const
634{
635 return namesImpl(*this, matchClass, matchName, true);
636}
637
638
639template<class MatchPredicate1, class MatchPredicate2>
641(
642 const MatchPredicate1& matchClass,
643 const MatchPredicate2& matchName,
644 const bool syncPar
645) const
646{
647 wordList objNames(namesImpl(*this, matchClass, matchName, true));
648
649 checkNameOrder(objNames, syncPar);
650 return objNames;
651}
652
653
654template<class Type>
656{
657 return namesTypeImpl<Type>(*this, predicates::always(), true);
658}
659
660
661template<class Type>
663{
664 wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
665
666 checkNameOrder(objNames, syncPar);
667 return objNames;
668}
669
670
671template<class Type, class MatchPredicate>
673(
674 const MatchPredicate& matchName
675) const
676{
677 return namesTypeImpl<Type>(*this, matchName, true);
678}
679
680
681template<class Type, class MatchPredicate>
683(
684 const MatchPredicate& matchName,
685 const bool syncPar
686) const
687{
688 wordList objNames(namesTypeImpl<Type>(*this, matchName, true));
689
690 checkNameOrder(objNames, syncPar);
691 return objNames;
692}
693
694
695// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
696
697template<class UnaryPredicate>
699(
700 const UnaryPredicate& pred,
701 const bool pruning
702)
703{
704// This is like
705// return HashPtrTable<IOobject>::filterValues
706// (
707// [&](const IOobject* io){ return pred(io->headerClassName()); },
708// pruning
709// );
710// which is really
711// return HashTable<IOobject*>::filterValues
712//
713// except that it does not leak
714
715 label changed = 0;
716
717 for (iterator iter = begin(); iter != end(); ++iter)
718 {
719 // Matches? either prune (pruning) or keep (!pruning)
720 if
721 (
722 (pred(iter.val()->headerClassName()) ? pruning : !pruning)
723 && erase(iter)
724 )
725 {
726 ++changed;
727 }
728 }
729
730 return changed;
731}
732
733
734template<class UnaryPredicate>
736(
737 const UnaryPredicate& pred,
738 const bool pruning
739)
740{
741// This is like
742// return HashPtrTable<IOobject>::filterKeys(pred, pruning);
743// which is really
744// return HashTable<IOobject*>::filterKeys(pred, pruning);
745//
746// except that it does not leak
747
748 label changed = 0;
749
750 for (iterator iter = begin(); iter != end(); ++iter)
751 {
752 // Matches? either prune (pruning) or keep (!pruning)
753 if
754 (
755 (pred(iter.key()) ? pruning : !pruning)
756 && erase(iter)
757 )
758 {
759 ++changed;
760 }
761 }
762
763 return changed;
764}
765
766
767template<class Type>
769{
770 wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
771
772 syncNames(objNames);
773 return objNames;
774}
775
776
777// ************************************************************************* //
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
bool found() const noexcept
True if iterator points to an entry - same as good()
Forward iterator with const access.
Definition: HashTable.H:794
reference val() const
Const access to referenced object (value)
Definition: HashTable.H:846
Forward iterator with non-const access.
Definition: HashTable.H:720
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:59
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:293
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:383
label filterClasses(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given classes.
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:268
IOobject * getObject(const word &objName) const
Definition: IOobjectList.C:307
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:320
label filterObjects(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given object names.
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:429
label count() const
The number of objects with headerClassName == Type::typeName.
UPtrList< const IOobject > sorted() const
The sorted list of IOobjects.
Definition: IOobjectList.C:336
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
wordList names() const
The unsorted names of the IOobjects.
Definition: IOobjectList.C:351
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
bool isHeaderClass() const
Check if headerClassName() equals Type::typeName.
Definition: IOobjectI.H:156
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
const T * set(const label i) const
Definition: UPtrList.H:248
void resize(const label newLen)
Change the size of the list.
Definition: UPtrListI.H:190
Lookup type of boundary radiation properties.
Definition: lookup.H:66
A class for handling words, derived from Foam::string.
Definition: word.H:68
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
#define InfoInFunction
Report an information message using Foam::Info.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
srcOptions erase("case")
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:238
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:56