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 -------------------------------------------------------------------------------
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 sorted()
181 template<class Type, class MatchPredicate>
183 Foam::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()
213 template<class MatchPredicate>
214 Foam::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()
243 template<class MatchPredicate1, class MatchPredicate2>
244 Foam::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()
274 template<class Type, class MatchPredicate>
275 Foam::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 
305 template<class Type>
307 (
308  const word& objName
309 ) const
310 {
311  // Like HashPtrTable::get(), or lookup() with a nullptr
312  const IOobject* io = nullptr;
313 
314  const const_iterator iter(cfind(objName));
315  if (iter.good())
316  {
317  io = iter.val();
318  }
319 
320  if (IOobject::debug)
321  {
322  if (io)
323  {
324  if (io->isHeaderClass<Type>())
325  {
326  InfoInFunction << "Found " << objName << endl;
327  }
328  else
329  {
330  InfoInFunction << "Found " << objName
331  << " with different type" << endl;
332  }
333  }
334  else
335  {
336  InfoInFunction << "Could not find " << objName << endl;
337  }
338  }
339 
340  if (io && io->isHeaderClass<Type>())
341  {
342  return io;
343  }
345  return nullptr;
346 }
347 
348 
349 template<class Type>
351 (
352  const word& objName
353 ) const
354 {
355  return cfindObject<Type>(objName);
356 }
357 
358 
359 template<class Type>
361 {
362  return const_cast<IOobject*>(cfindObject<Type>(objName));
363 }
364 
365 
366 template<class Type>
368 {
369  return const_cast<IOobject*>(cfindObject<Type>(objName));
370 }
371 
372 
373 template<class MatchPredicate>
375 (
376  const MatchPredicate& matchName
377 ) const
378 {
379  return lookupImpl(*this, matchName);
380 }
381 
382 
383 template<class MatchPredicate>
385 (
386  const MatchPredicate& matchClass
387 ) const
388 {
389  return lookupClassImpl(*this, matchClass, predicates::always());
390 }
391 
392 
393 template<class MatchPredicate1, class MatchPredicate2>
395 (
396  const MatchPredicate1& matchClass,
397  const MatchPredicate2& matchName
398 ) const
399 {
400  return lookupClassImpl(*this, matchClass, matchName);
401 }
402 
403 
404 template<class Type>
406 {
407  return lookupClassTypeImpl<Type>(*this, predicates::always());
408 }
409 
410 
411 template<class Type, class MatchPredicate>
413 (
414  const MatchPredicate& matchName
415 ) const
416 {
417  return lookupClassImpl<Type>(*this, matchName);
418 }
419 
420 
421 template<class MatchPredicate>
424 (
425  const MatchPredicate& matchName
426 ) const
427 {
428  return classesImpl(*this, matchName);
429 }
430 
431 
432 template<class MatchPredicate>
433 Foam::label Foam::IOobjectList::count
434 (
435  const MatchPredicate& matchClass
436 ) const
437 {
438  return countImpl(*this, matchClass, predicates::always());
439 }
440 
441 
442 template<class MatchPredicate1, class MatchPredicate2>
443 Foam::label Foam::IOobjectList::count
444 (
445  const MatchPredicate1& matchClass,
446  const MatchPredicate2& matchName
447 ) const
448 {
449  return countImpl(*this, matchClass, matchName);
450 }
451 
452 
453 template<class Type>
454 Foam::label Foam::IOobjectList::count() const
455 {
456  return countTypeImpl<Type>(*this, predicates::always());
457 }
458 
459 
460 template<class Type, class MatchPredicate>
461 Foam::label Foam::IOobjectList::count
462 (
463  const MatchPredicate& matchName
464 ) const
465 {
466  return countTypeImpl<Type>(*this, matchName);
467 }
469 
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 template<class Type>
476 {
477  return objectsTypeImpl<Type>(*this, predicates::always());
478 }
479 
480 
481 template<class Type>
483 Foam::IOobjectList::sorted(const bool syncPar) const
484 {
486  (
487  objectsTypeImpl<Type>(*this, predicates::always())
488  );
489 
490  checkObjectOrder(list, syncPar);
491 
492  return list;
493 }
494 
495 
496 template<class Type, class MatchPredicate>
499 (
500  const MatchPredicate& matchName
501 ) const
502 {
503  return objectsTypeImpl<Type>(*this, matchName);
504 }
505 
506 
507 template<class Type, class MatchPredicate>
510 (
511  const MatchPredicate& matchName,
512  const bool syncPar
513 ) const
514 {
516  (
517  objectsTypeImpl<Type>(*this, matchName)
518  );
519 
520  checkObjectOrder(list, syncPar);
521 
522  return list;
523 }
524 
525 
526 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
527 
528 template<class MatchPredicate>
530 (
531  const MatchPredicate& matchClass
532 ) const
533 {
534  return namesImpl(*this, matchClass, predicates::always(), false);
535 }
536 
537 
538 template<class MatchPredicate>
540 (
541  const MatchPredicate& matchClass,
542  const bool syncPar
543 ) const
544 {
545  return sortedNames(matchClass, syncPar);
546 }
547 
548 
549 template<class MatchPredicate1, class MatchPredicate2>
551 (
552  const MatchPredicate1& matchClass,
553  const MatchPredicate2& matchName
554 ) const
555 {
556  return namesImpl(*this, matchClass, matchName, false);
557 }
558 
559 
560 template<class MatchPredicate1, class MatchPredicate2>
562 (
563  const MatchPredicate1& matchClass,
564  const MatchPredicate2& matchName,
565  const bool syncPar
566 ) const
567 {
568  return sortedNames(matchClass, matchName, syncPar);
569 }
570 
571 
572 template<class Type>
574 {
575  return namesTypeImpl<Type>(*this, predicates::always(), false);
576 }
577 
578 
579 template<class Type>
580 Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
581 {
582  return sortedNames<Type>(syncPar);
583 }
584 
585 
586 template<class Type, class MatchPredicate>
588 (
589  const MatchPredicate& matchName
590 ) const
591 {
592  return namesTypeImpl<Type>(*this, matchName, false);
593 }
594 
595 
596 template<class Type, class MatchPredicate>
598 (
599  const MatchPredicate& matchName,
600  const bool syncPar
601 ) const
602 {
603  return sortedNames<Type>(matchName, syncPar);
604 }
605 
606 
607 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
608 
609 template<class MatchPredicate>
611 (
612  const MatchPredicate& matchClass
613 ) const
614 {
615  return namesImpl(*this, matchClass, predicates::always(), true);
616 }
617 
618 
619 template<class MatchPredicate>
621 (
622  const MatchPredicate& matchClass,
623  const bool syncPar
624 ) const
625 {
626  wordList objNames
627  (
628  namesImpl(*this, matchClass, predicates::always(), true)
629  );
630 
631  checkNameOrder(objNames, syncPar);
632  return objNames;
633 }
634 
635 
636 template<class MatchPredicate1, class MatchPredicate2>
638 (
639  const MatchPredicate1& matchClass,
640  const MatchPredicate2& matchName
641 ) const
642 {
643  return namesImpl(*this, matchClass, matchName, true);
644 }
645 
646 
647 template<class MatchPredicate1, class MatchPredicate2>
649 (
650  const MatchPredicate1& matchClass,
651  const MatchPredicate2& matchName,
652  const bool syncPar
653 ) const
654 {
655  wordList objNames(namesImpl(*this, matchClass, matchName, true));
657  checkNameOrder(objNames, syncPar);
658  return objNames;
659 }
660 
661 
662 template<class Type>
664 {
665  return namesTypeImpl<Type>(*this, predicates::always(), true);
666 }
667 
668 
669 template<class Type>
670 Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
671 {
672  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
673 
674  checkNameOrder(objNames, syncPar);
675  return objNames;
676 }
677 
678 
679 template<class Type, class MatchPredicate>
681 (
682  const MatchPredicate& matchName
683 ) const
684 {
685  return namesTypeImpl<Type>(*this, matchName, true);
686 }
687 
688 
689 template<class Type, class MatchPredicate>
691 (
692  const MatchPredicate& matchName,
693  const bool syncPar
694 ) const
695 {
696  wordList objNames(namesTypeImpl<Type>(*this, matchName, true));
697 
698  checkNameOrder(objNames, syncPar);
699  return objNames;
700 }
701 
702 
703 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
704 
705 template<class UnaryPredicate>
707 (
708  const UnaryPredicate& pred,
709  const bool pruning
710 )
711 {
712 // This is like
713 // return HashPtrTable<IOobject>::filterValues
714 // (
715 // [&](const IOobject* io){ return pred(io->headerClassName()); },
716 // pruning
717 // );
718 // which is really
719 // return HashTable<IOobject*>::filterValues
720 //
721 // except that it does not leak
722 
723  label changed = 0;
724 
725  for (iterator iter = begin(); iter != end(); ++iter)
726  {
727  // Matches? either prune (pruning) or keep (!pruning)
728  if
729  (
730  (pred(iter.val()->headerClassName()) ? pruning : !pruning)
731  && erase(iter)
732  )
733  {
734  ++changed;
735  }
736  }
738  return changed;
739 }
740 
741 
742 template<class UnaryPredicate>
744 (
745  const UnaryPredicate& pred,
746  const bool pruning
747 )
748 {
749 // This is like
750 // return HashPtrTable<IOobject>::filterKeys(pred, pruning);
751 // which is really
752 // return HashTable<IOobject*>::filterKeys(pred, pruning);
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.key()) ? pruning : !pruning)
764  && erase(iter)
765  )
766  {
767  ++changed;
768  }
769  }
770 
771  return changed;
772 }
773 
774 
775 template<class Type>
777 {
778  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
779 
780  syncNames(objNames);
781  return objNames;
782 }
783 
784 
785 // ************************************************************************* //
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:162
UPtrList< const IOobject > sorted() const
The sorted list of IOobjects.
Definition: IOobjectList.C:223
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:270
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
wordList names() const
The unsorted names of the IOobjects.
Definition: IOobjectList.C:238
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:331
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:348
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:180
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:316
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
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:194
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:194
List< word > wordList
List of word.
Definition: fileName.H:58
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:180
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:161
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
bool isHeaderClass() const
Check if headerClassName() equals Type::typeName.
Definition: IOobjectI.H:225
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:207
#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:150