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-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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()
34 template<class MatchPredicate>
35 Foam::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()
61 template<class MatchPredicate1, class MatchPredicate2>
62 Foam::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()
86 template<class Type, class MatchPredicate>
87 Foam::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()
110 template<class MatchPredicate1, class MatchPredicate2>
111 Foam::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()
146 template<class Type, class MatchPredicate>
147 Foam::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 csorted(), csorted()
181 template<class Type, class MatchPredicate>
183 Foam::IOobjectList::objectsTypeImpl
184 (
185  const IOobjectList& list,
186  const MatchPredicate& matchName,
187  const bool doSort
188 )
189 {
190  UPtrList<const IOobject> result(list.size());
191 
192  label count = 0;
193  forAllConstIters(list, iter)
194  {
195  const word& key = iter.key();
196  const IOobject* io = iter.val();
197 
198  if (io->isHeaderClass<Type>() && matchName(key))
199  {
200  result.set(count, io);
201  ++count;
202  }
203  }
204 
205  result.resize(count);
206 
207  if (doSort)
208  {
209  Foam::sort(result, nameOp<IOobject>()); // Sort by object name()
210  }
211 
212  return result;
213 }
214 
215 
216 // Templated implementation for lookup()
217 template<class MatchPredicate>
218 Foam::IOobjectList Foam::IOobjectList::lookupImpl
219 (
220  const IOobjectList& list,
221  const MatchPredicate& matchName
222 )
223 {
224  IOobjectList results(list.size());
225 
226  forAllConstIters(list, iter)
227  {
228  const word& key = iter.key();
229  const IOobject* io = iter.val();
230 
231  if (matchName(key))
232  {
233  if (IOobject::debug)
234  {
235  InfoInFunction << "Found " << key << endl;
236  }
237 
238  results.set(key, new IOobject(*io));
239  }
240  }
241 
242  return results;
243 }
244 
245 
246 // Templated implementation for lookupClass()
247 template<class MatchPredicate1, class MatchPredicate2>
248 Foam::IOobjectList Foam::IOobjectList::lookupClassImpl
249 (
250  const IOobjectList& list,
251  const MatchPredicate1& matchClass,
252  const MatchPredicate2& matchName
253 )
254 {
255  IOobjectList results(list.size());
256 
257  forAllConstIters(list, iter)
258  {
259  const word& key = iter.key();
260  const IOobject* io = iter.val();
261 
262  if (matchClass(io->headerClassName()) && matchName(key))
263  {
264  if (IOobject::debug)
265  {
266  InfoInFunction << "Found " << key << endl;
267  }
268 
269  results.set(key, new IOobject(*io));
270  }
271  }
272 
273  return results;
274 }
275 
276 
277 // Templated implementation for lookupClass()
278 template<class Type, class MatchPredicate>
279 Foam::IOobjectList Foam::IOobjectList::lookupClassTypeImpl
280 (
281  const IOobjectList& list,
282  const MatchPredicate& matchName
283 )
284 {
285  IOobjectList results(list.size());
286 
287  forAllConstIters(list, iter)
288  {
289  const word& key = iter.key();
290  const IOobject* io = iter.val();
291 
292  if (io->isHeaderClass<Type>() && matchName(key))
293  {
294  if (IOobject::debug)
295  {
296  InfoInFunction << "Found " << key << endl;
297  }
298 
299  results.set(key, new IOobject(*io));
300  }
301  }
302 
303  return results;
304 }
305 
306 
307 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
308 
309 template<class Type>
311 (
312  const word& objName
313 ) const
314 {
315  // Like HashPtrTable::get(), or lookup() with a nullptr
316  const IOobject* io = nullptr;
317 
318  const const_iterator iter(cfind(objName));
319  if (iter.good())
320  {
321  io = iter.val();
322  }
323 
324  if (IOobject::debug)
325  {
326  if (io)
327  {
328  if (io->isHeaderClass<Type>())
329  {
330  InfoInFunction << "Found " << objName << endl;
331  }
332  else
333  {
334  InfoInFunction << "Found " << objName
335  << " with different type" << endl;
336  }
337  }
338  else
339  {
340  InfoInFunction << "Could not find " << objName << endl;
341  }
342  }
343 
344  if (io && io->isHeaderClass<Type>())
345  {
346  return io;
347  }
349  return nullptr;
350 }
351 
352 
353 template<class Type>
355 (
356  const word& objName
357 ) const
358 {
359  return cfindObject<Type>(objName);
360 }
361 
362 
363 template<class Type>
365 {
366  return const_cast<IOobject*>(cfindObject<Type>(objName));
367 }
368 
369 
370 template<class Type>
372 {
373  return const_cast<IOobject*>(cfindObject<Type>(objName));
374 }
375 
376 
377 template<class MatchPredicate>
379 (
380  const MatchPredicate& matchName
381 ) const
382 {
383  return lookupImpl(*this, matchName);
384 }
385 
386 
387 template<class MatchPredicate>
389 (
390  const MatchPredicate& matchClass
391 ) const
392 {
393  return lookupClassImpl(*this, matchClass, predicates::always());
394 }
395 
396 
397 template<class MatchPredicate1, class MatchPredicate2>
399 (
400  const MatchPredicate1& matchClass,
401  const MatchPredicate2& matchName
402 ) const
403 {
404  return lookupClassImpl(*this, matchClass, matchName);
405 }
406 
407 
408 template<class Type>
410 {
411  return lookupClassTypeImpl<Type>(*this, predicates::always());
412 }
413 
414 
415 template<class Type, class MatchPredicate>
417 (
418  const MatchPredicate& matchName
419 ) const
420 {
421  return lookupClassImpl<Type>(*this, matchName);
422 }
423 
424 
425 template<class MatchPredicate>
428 (
429  const MatchPredicate& matchName
430 ) const
431 {
432  return classesImpl(*this, matchName);
433 }
434 
435 
436 template<class MatchPredicate>
437 Foam::label Foam::IOobjectList::count
438 (
439  const MatchPredicate& matchClass
440 ) const
441 {
442  return countImpl(*this, matchClass, predicates::always());
443 }
444 
445 
446 template<class MatchPredicate1, class MatchPredicate2>
447 Foam::label Foam::IOobjectList::count
448 (
449  const MatchPredicate1& matchClass,
450  const MatchPredicate2& matchName
451 ) const
452 {
453  return countImpl(*this, matchClass, matchName);
454 }
455 
456 
457 template<class Type>
458 Foam::label Foam::IOobjectList::count() const
459 {
460  return countTypeImpl<Type>(*this, predicates::always());
461 }
462 
463 
464 template<class Type, class MatchPredicate>
465 Foam::label Foam::IOobjectList::count
466 (
467  const MatchPredicate& matchName
468 ) const
469 {
470  return countTypeImpl<Type>(*this, matchName);
471 }
473 
474 
475 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
476 
477 template<class Type>
480 {
481  // doSort = false
482  return objectsTypeImpl<Type>(*this, predicates::always(), false);
483 }
484 
485 
486 template<class Type>
489 {
490  // doSort = true
491  return objectsTypeImpl<Type>(*this, predicates::always(), true);
492 }
493 
494 
495 template<class Type>
497 Foam::IOobjectList::csorted(const bool syncPar) const
498 {
500  (
501  // doSort = true
502  objectsTypeImpl<Type>(*this, predicates::always(), true)
503  );
504 
505  checkObjectOrder(list, syncPar);
506 
507  return list;
508 }
509 
510 
511 template<class Type, class MatchPredicate>
514 (
515  const MatchPredicate& matchName
516 ) const
517 {
518  // doSort = false
519  return objectsTypeImpl<Type>(*this, matchName, false);
520 }
521 
522 
523 template<class Type, class MatchPredicate>
526 (
527  const MatchPredicate& matchName
528 ) const
529 {
530  // doSort = true
531  return objectsTypeImpl<Type>(*this, matchName, true);
532 }
533 
534 
535 template<class Type, class MatchPredicate>
538 (
539  const MatchPredicate& matchName,
540  const bool syncPar
541 ) const
542 {
544  (
545  // doSort = true
546  objectsTypeImpl<Type>(*this, matchName, true)
547  );
548 
549  checkObjectOrder(list, syncPar);
550 
551  return list;
552 }
553 
554 
555 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
556 
557 template<class MatchPredicate>
559 (
560  const MatchPredicate& matchClass
561 ) const
562 {
563  // doSort = false
564  return namesImpl(*this, matchClass, predicates::always(), false);
565 }
566 
567 
568 template<class MatchPredicate>
570 (
571  const MatchPredicate& matchClass,
572  const bool syncPar
573 ) const
574 {
575  return sortedNames(matchClass, syncPar);
576 }
577 
578 
579 template<class MatchPredicate1, class MatchPredicate2>
581 (
582  const MatchPredicate1& matchClass,
583  const MatchPredicate2& matchName
584 ) const
585 {
586  // doSort = false
587  return namesImpl(*this, matchClass, matchName, false);
588 }
589 
590 
591 template<class MatchPredicate1, class MatchPredicate2>
593 (
594  const MatchPredicate1& matchClass,
595  const MatchPredicate2& matchName,
596  const bool syncPar
597 ) const
598 {
599  return sortedNames(matchClass, matchName, syncPar);
600 }
601 
602 
603 template<class Type>
605 {
606  // doSort = false
607  return namesTypeImpl<Type>(*this, predicates::always(), false);
608 }
609 
610 
611 template<class Type>
612 Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
613 {
614  return sortedNames<Type>(syncPar);
615 }
616 
617 
618 template<class Type, class MatchPredicate>
620 (
621  const MatchPredicate& matchName
622 ) const
623 {
624  // doSort = false
625  return namesTypeImpl<Type>(*this, matchName, false);
626 }
627 
628 
629 template<class Type, class MatchPredicate>
631 (
632  const MatchPredicate& matchName,
633  const bool syncPar
634 ) const
635 {
636  return sortedNames<Type>(matchName, syncPar);
637 }
638 
639 
640 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
641 
642 template<class MatchPredicate>
644 (
645  const MatchPredicate& matchClass
646 ) const
647 {
648  return namesImpl(*this, matchClass, predicates::always(), true);
649 }
650 
651 
652 template<class MatchPredicate>
654 (
655  const MatchPredicate& matchClass,
656  const bool syncPar
657 ) const
658 {
659  wordList objNames
660  (
661  namesImpl(*this, matchClass, predicates::always(), true)
662  );
663 
664  checkNameOrder(objNames, syncPar);
665  return objNames;
666 }
667 
668 
669 template<class MatchPredicate1, class MatchPredicate2>
671 (
672  const MatchPredicate1& matchClass,
673  const MatchPredicate2& matchName
674 ) const
675 {
676  return namesImpl(*this, matchClass, matchName, true);
677 }
678 
679 
680 template<class MatchPredicate1, class MatchPredicate2>
682 (
683  const MatchPredicate1& matchClass,
684  const MatchPredicate2& matchName,
685  const bool syncPar
686 ) const
687 {
688  wordList objNames(namesImpl(*this, matchClass, matchName, true));
690  checkNameOrder(objNames, syncPar);
691  return objNames;
692 }
693 
694 
695 template<class Type>
697 {
698  return namesTypeImpl<Type>(*this, predicates::always(), true);
699 }
700 
701 
702 template<class Type>
703 Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
704 {
705  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
706 
707  checkNameOrder(objNames, syncPar);
708  return objNames;
709 }
710 
711 
712 template<class Type, class MatchPredicate>
714 (
715  const MatchPredicate& matchName
716 ) const
717 {
718  return namesTypeImpl<Type>(*this, matchName, true);
719 }
720 
721 
722 template<class Type, class MatchPredicate>
724 (
725  const MatchPredicate& matchName,
726  const bool syncPar
727 ) const
728 {
729  wordList objNames(namesTypeImpl<Type>(*this, matchName, true));
730 
731  checkNameOrder(objNames, syncPar);
732  return objNames;
733 }
734 
735 
736 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
737 
738 template<class UnaryPredicate>
740 (
741  const UnaryPredicate& pred,
742  const bool pruning
743 )
744 {
745 // This is like
746 // return HashPtrTable<IOobject>::filterValues
747 // (
748 // [&](const IOobject* io){ return pred(io->headerClassName()); },
749 // pruning
750 // );
751 // which is really
752 // return HashTable<IOobject*>::filterValues
753 //
754 // except that it does not leak
755 
756  label changed = 0;
757 
758  for (iterator iter = begin(); iter != end(); ++iter)
759  {
760  // Matches? either prune (pruning) or keep (!pruning)
761  if
762  (
763  (pred(iter.val()->headerClassName()) ? pruning : !pruning)
764  && erase(iter)
765  )
766  {
767  ++changed;
768  }
769  }
771  return changed;
772 }
773 
774 
775 template<class UnaryPredicate>
777 (
778  const UnaryPredicate& pred,
779  const bool pruning
780 )
781 {
782 // This is like
783 // return HashPtrTable<IOobject>::filterKeys(pred, pruning);
784 // which is really
785 // return HashTable<IOobject*>::filterKeys(pred, pruning);
786 //
787 // except that it does not leak
788 
789  label changed = 0;
790 
791  for (iterator iter = begin(); iter != end(); ++iter)
792  {
793  // Matches? either prune (pruning) or keep (!pruning)
794  if
795  (
796  (pred(iter.key()) ? pruning : !pruning)
797  && erase(iter)
798  )
799  {
800  ++changed;
801  }
802  }
803 
804  return changed;
805 }
806 
807 
808 template<class Type>
810 {
811  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
812 
813  syncNames(objNames);
814  return objNames;
815 }
816 
817 
818 // ************************************************************************* //
label filterClasses(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given classes.
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable, so the various sorted methods should be used if traversing in parallel.
Definition: IOobjectList.H:55
srcOptions erase("case")
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:250
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
wordList names() const
The unsorted names of the IOobjects.
Definition: IOobjectList.C:218
IOobjectList lookup(const MatchPredicate &matchName) const
The list of IOobjects that have a matching object name.
label size() const noexcept
The number of elements in table.
Definition: HashTable.H:342
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
A class for handling words, derived from Foam::string.
Definition: word.H:63
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:176
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:296
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
UPtrList< const IOobject > cobjects() const
The unsorted list of IOobjects with headerClassName == Type::typeName.
int debug
Static debugging option.
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:201
IOobject * getObject(const word &objName) const
Return non-const pointer to the object found by name, using a const-cast to have it behave like a mut...
Definition: IOobjectList.C:190
List< word > wordList
List of word.
Definition: fileName.H:59
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:213
bool insert(const word &, IOobject *)=delete
No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set() ...
label count() const
The number of objects with headerClassName == Type::typeName.
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
label filterObjects(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given object names.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
UPtrList< const IOobject > csorted() const
The sorted list of IOobjects with headerClassName == Type::typeName.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
bool isHeaderClass() const
Check if headerClassName() equals Type::typeName.
Definition: IOobjectI.H:258
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:203
#define InfoInFunction
Report an information message using Foam::Info.
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:146