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-2019 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->isHeaderClassName<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->isHeaderClassName<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 lookup()
181 template<class MatchPredicate>
182 Foam::IOobjectList Foam::IOobjectList::lookupImpl
183 (
184  const IOobjectList& list,
185  const MatchPredicate& matchName
186 )
187 {
188  IOobjectList results(list.size());
189 
190  forAllConstIters(list, iter)
191  {
192  const word& key = iter.key();
193  const IOobject* io = iter.val();
194 
195  if (matchName(key))
196  {
197  if (IOobject::debug)
198  {
199  InfoInFunction << "Found " << key << endl;
200  }
201 
202  results.set(key, new IOobject(*io));
203  }
204  }
205 
206  return results;
207 }
208 
209 
210 // Templated implementation for lookupClass()
211 template<class MatchPredicate1, class MatchPredicate2>
212 Foam::IOobjectList Foam::IOobjectList::lookupClassImpl
213 (
214  const IOobjectList& list,
215  const MatchPredicate1& matchClass,
216  const MatchPredicate2& matchName
217 )
218 {
219  IOobjectList results(list.size());
220 
221  forAllConstIters(list, iter)
222  {
223  const word& key = iter.key();
224  const IOobject* io = iter.val();
225 
226  if (matchClass(io->headerClassName()) && matchName(key))
227  {
228  if (IOobject::debug)
229  {
230  InfoInFunction << "Found " << key << endl;
231  }
232 
233  results.set(key, new IOobject(*io));
234  }
235  }
236 
237  return results;
238 }
239 
240 
241 // Templated implementation for lookupClass()
242 template<class Type, class MatchPredicate>
243 Foam::IOobjectList Foam::IOobjectList::lookupClassTypeImpl
244 (
245  const IOobjectList& list,
246  const MatchPredicate& matchName
247 )
248 {
249  IOobjectList results(list.size());
250 
251  forAllConstIters(list, iter)
252  {
253  const word& key = iter.key();
254  const IOobject* io = iter.val();
255 
256  if (io->isHeaderClassName<Type>() && matchName(key))
257  {
258  if (IOobject::debug)
259  {
260  InfoInFunction << "Found " << key << endl;
261  }
262 
263  results.set(key, new IOobject(*io));
264  }
265  }
266 
267  return results;
268 }
269 
270 
271 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
272 
273 template<class Type>
275 (
276  const word& objName
277 ) const
278 {
279  const_iterator iter = cfind(objName);
280 
281  if (iter.found())
282  {
283  const IOobject* io = iter.val();
284 
285  if (io->isHeaderClassName<Type>())
286  {
287  if (IOobject::debug)
288  {
289  InfoInFunction << "Found " << objName << endl;
290  }
291 
292  return io;
293  }
294  else if (IOobject::debug)
295  {
297  << "Found " << objName << " of different type" << endl;
298  }
299  }
300  else if (IOobject::debug)
301  {
302  InfoInFunction << "Could not find " << objName << endl;
303  }
304 
305  return nullptr;
306 }
307 
308 
309 template<class Type>
311 (
312  const word& objName
313 ) const
314 {
315  return cfindObject<Type>(objName);
316 }
317 
318 
319 template<class Type>
321 {
322  return const_cast<IOobject*>(cfindObject<Type>(objName));
323 }
324 
325 
326 template<class Type>
328 {
329  return const_cast<IOobject*>(cfindObject<Type>(objName));
330 }
331 
332 
333 template<class MatchPredicate>
335 (
336  const MatchPredicate& matchName
337 ) const
338 {
339  return lookupImpl(*this, matchName);
340 }
341 
342 
343 template<class MatchPredicate>
345 (
346  const MatchPredicate& matchClass
347 ) const
348 {
349  return lookupClassImpl(*this, matchClass, predicates::always());
350 }
351 
352 
353 template<class MatchPredicate1, class MatchPredicate2>
355 (
356  const MatchPredicate1& matchClass,
357  const MatchPredicate2& matchName
358 ) const
359 {
360  return lookupClassImpl(*this, matchClass, matchName);
361 }
362 
363 
364 template<class Type>
366 {
367  return lookupClassTypeImpl<Type>(*this, predicates::always());
368 }
369 
370 
371 template<class Type, class MatchPredicate>
373 (
374  const MatchPredicate& matchName
375 ) const
376 {
377  return lookupClassImpl<Type>(*this, matchName);
378 }
379 
380 
381 template<class MatchPredicate>
384 (
385  const MatchPredicate& matchName
386 ) const
387 {
388  return classesImpl(*this, matchName);
389 }
390 
391 
392 template<class MatchPredicate>
393 Foam::label Foam::IOobjectList::count
394 (
395  const MatchPredicate& matchClass
396 ) const
397 {
398  return countImpl(*this, matchClass, predicates::always());
399 }
400 
401 
402 template<class MatchPredicate1, class MatchPredicate2>
403 Foam::label Foam::IOobjectList::count
404 (
405  const MatchPredicate1& matchClass,
406  const MatchPredicate2& matchName
407 ) const
408 {
409  return countImpl(*this, matchClass, matchName);
410 }
411 
412 
413 template<class Type>
414 Foam::label Foam::IOobjectList::count() const
415 {
416  return countTypeImpl<Type>(*this, predicates::always());
417 }
418 
419 
420 template<class Type, class MatchPredicate>
421 Foam::label Foam::IOobjectList::count
422 (
423  const MatchPredicate& matchName
424 ) const
425 {
426  return countTypeImpl<Type>(*this, matchName);
427 }
428 
429 
430 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
431 
432 template<class MatchPredicate>
434 (
435  const MatchPredicate& matchClass
436 ) const
437 {
438  return namesImpl(*this, matchClass, predicates::always(), false);
439 }
440 
441 
442 template<class MatchPredicate>
444 (
445  const MatchPredicate& matchClass,
446  const bool syncPar
447 ) const
448 {
449  wordList objNames
450  (
451  namesImpl(*this, matchClass, predicates::always(), false)
452  );
453 
454  checkNames(objNames, syncPar);
455  return objNames;
456 }
457 
458 
459 template<class MatchPredicate1, class MatchPredicate2>
461 (
462  const MatchPredicate1& matchClass,
463  const MatchPredicate2& matchName
464 ) const
465 {
466  return namesImpl(*this, matchClass, matchName, false);
467 }
468 
469 
470 template<class MatchPredicate1, class MatchPredicate2>
472 (
473  const MatchPredicate1& matchClass,
474  const MatchPredicate2& matchName,
475  const bool syncPar
476 ) const
477 {
478  wordList objNames(namesImpl(*this, matchClass, matchName, false));
479 
480  checkNames(objNames, syncPar);
481  return objNames;
482 }
483 
484 
485 template<class Type>
487 {
488  return namesTypeImpl<Type>(*this, predicates::always(), false);
489 }
490 
491 
492 template<class Type>
493 Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
494 {
495  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
496 
497  checkNames(objNames, syncPar);
498  return objNames;
499 }
500 
501 
502 template<class Type, class MatchPredicate>
504 (
505  const MatchPredicate& matchName
506 ) const
507 {
508  return namesTypeImpl<Type>(*this, matchName, false);
509 }
510 
511 
512 template<class Type, class MatchPredicate>
514 (
515  const MatchPredicate& matchName,
516  const bool syncPar
517 ) const
518 {
519  wordList objNames(namesTypeImpl<Type>(*this, matchName, false));
520 
521  checkNames(objNames, syncPar);
522  return objNames;
523 }
524 
525 
526 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
527 
528 template<class MatchPredicate>
530 (
531  const MatchPredicate& matchClass
532 ) const
533 {
534  return namesImpl(*this, matchClass, predicates::always(), true);
535 }
536 
537 
538 template<class MatchPredicate>
540 (
541  const MatchPredicate& matchClass,
542  const bool syncPar
543 ) const
544 {
545  wordList objNames
546  (
547  namesImpl(*this, matchClass, predicates::always(), true)
548  );
549 
550  checkNames(objNames, syncPar);
551  return objNames;
552 }
553 
554 
555 template<class MatchPredicate1, class MatchPredicate2>
557 (
558  const MatchPredicate1& matchClass,
559  const MatchPredicate2& matchName
560 ) const
561 {
562  return namesImpl(*this, matchClass, matchName, true);
563 }
564 
565 template<class MatchPredicate1, class MatchPredicate2>
567 (
568  const MatchPredicate1& matchClass,
569  const MatchPredicate2& matchName,
570  const bool syncPar
571 ) const
572 {
573  wordList objNames(namesImpl(*this, matchClass, matchName, true));
574 
575  checkNames(objNames, syncPar);
576  return objNames;
577 }
578 
579 
580 template<class Type>
582 {
583  return namesTypeImpl<Type>(*this, predicates::always(), true);
584 }
585 
586 
587 template<class Type>
589 {
590  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
591 
592  checkNames(objNames, syncPar);
593  return objNames;
594 }
595 
596 
597 template<class Type, class MatchPredicate>
599 (
600  const MatchPredicate& matchName
601 ) const
602 {
603  return namesTypeImpl<Type>(*this, matchName, true);
604 }
605 
606 
607 template<class Type, class MatchPredicate>
609 (
610  const MatchPredicate& matchName,
611  const bool syncPar
612 ) const
613 {
614  return namesTypeImpl<Type>(*this, matchName, true, syncPar);
615 }
616 
617 
618 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
619 
620 template<class UnaryPredicate>
622 (
623  const UnaryPredicate& pred,
624  const bool pruning
625 )
626 {
627 // This is like
628 // return HashPtrTable<IOobject>::filterValues
629 // (
630 // [&](const IOobject* io){ return pred(io->headerClassName()); },
631 // pruning
632 // );
633 // which is really
634 // return HashTable<IOobject*>::filterValues
635 //
636 // except that it does not leak
637 
638  label changed = 0;
639 
640  for (iterator iter = begin(); iter != end(); ++iter)
641  {
642  // Matches? either prune (pruning) or keep (!pruning)
643  if
644  (
645  (pred(iter.val()->headerClassName()) ? pruning : !pruning)
646  && erase(iter)
647  )
648  {
649  ++changed;
650  }
651  }
652 
653  return changed;
654 }
655 
656 
657 template<class UnaryPredicate>
659 (
660  const UnaryPredicate& pred,
661  const bool pruning
662 )
663 {
664 // This is like
665 // return HashPtrTable<IOobject>::filterKeys(pred, pruning);
666 // which is really
667 // return HashTable<IOobject*>::filterKeys(pred, pruning);
668 //
669 // except that it does not leak
670 
671  label changed = 0;
672 
673  for (iterator iter = begin(); iter != end(); ++iter)
674  {
675  // Matches? either prune (pruning) or keep (!pruning)
676  if
677  (
678  (pred(iter.key()) ? pruning : !pruning)
679  && erase(iter)
680  )
681  {
682  ++changed;
683  }
684  }
685 
686  return changed;
687 }
688 
689 
690 template<class Type>
692 {
693  wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
694 
695  syncNames(objNames);
696  return objNames;
697 }
698 
699 
700 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::IOobjectList::findObject
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:270
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::IOobjectList::sortedNames
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:345
IOobjectList.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
Foam::IOobjectList::count
label count() const
The number of objects with headerClassName == Type::typeName.
Foam::IOobjectList::names
wordList names() const
The names of the IOobjects.
Definition: IOobjectList.C:310
erase
srcOptions erase("case")
Foam::IOobjectList::filterObjects
label filterObjects(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given object names.
Foam::predicates::always
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:56
Foam::IOobjectList::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:297
Foam::IOobjectList::getObject
IOobject * getObject(const word &objName) const
Definition: IOobjectList.C:284
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::IOobjectList::cfindObject
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:245
Foam::IOobjectList::filterClasses
label filterClasses(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given classes.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::IOobjectList::allNames
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:391
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:55
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::HashPtrTable< IOobject >::const_iterator
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::IOobject::isHeaderClassName
bool isHeaderClassName(const word &clsName) const
Test if headerClassName() equals the given class name.
Definition: IOobjectI.H:149
Foam::IOobjectList::lookup
IOobjectList lookup(const MatchPredicate &matchName) const
The list of IOobjects that have a matching object name.
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::IOobjectList::lookupClass
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
Foam::List< word >
predicates.H
Foam::HashPtrTable< IOobject >::iterator
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88