objectRegistryCache.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) 2019 OpenFOAM Foundation
9  Copyright (C) 2023 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 "Time.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::objectRegistry::readCacheTemporaryObjects() const
35 {
36  if (cacheTemporaryObjectsActive_) return;
37 
38  const auto* eptr = time_.controlDict().findEntry
39  (
40  "cacheTemporaryObjects",
42  );
43 
44  if (eptr)
45  {
46  cacheTemporaryObjectsActive_ = true;
47 
48  // Clear old cache?
49  // cacheTemporaryObjects_.clear();
50 
51  wordList objectNames;
52 
53  if (eptr->isDict())
54  {
55  // Per region (sub-dictionary syntax)
56  eptr->dict().readIfPresent(name(), objectNames);
57  }
58  else
59  {
60  // All regions
61  eptr->readEntry(objectNames);
62  }
63 
64  for (const word& objName : objectNames)
65  {
66  cacheTemporaryObjects_.emplace(objName, false, false);
67  }
68  }
69 }
70 
71 
72 void Foam::objectRegistry::deleteCachedObject(regIOobject* io) const
73 {
74  if (io)
75  {
76  io->release(); // Relinquish any ownership by registry
77  io->checkOut();
78  delete io;
79  }
80 }
81 
82 
83 // FUTURE: (currently not needed)
84 // void Foam::objectRegistry::addTemporaryObject
85 // (
86 // const word& name
87 // ) const
88 // {
89 // cacheTemporaryObjects_.emplace(name, false, false);
90 // }
91 
92 
94 (
95  const word& name
96 ) const
97 {
98  return cacheTemporaryObjects_.found(name);
99 }
100 
101 
103 (
104  const regIOobject* io
105 ) const
106 {
107  if (io && !cacheTemporaryObjects_.empty())
108  {
109  auto iter = cacheTemporaryObjects_.find(io->name());
110 
111  // Reset the cached flag
112  if (iter.good())
113  {
114  iter.val().first() = false;
115  }
116  }
117 }
118 
119 
121 (
123 ) const
124 {
125  resetCacheTemporaryObject(&io);
126 }
127 
128 
130 {
131  bool enabled = cacheTemporaryObjects_.size();
132 
133  forAllConstIters(*this, iter)
134  {
135  const auto* subObr = dynamic_cast<const objectRegistry*>(iter.val());
136 
137  // Protect against re-searching the top-level registry
138  if (subObr && subObr != this)
139  {
140  enabled = subObr->checkCacheTemporaryObjects() || enabled;
141  }
142  }
143 
144  if (enabled)
145  {
146  OSstream* emitWarning = nullptr;
147 
148  forAllIters(cacheTemporaryObjects_, iter)
149  {
150  if (!iter.val().second())
151  {
152  if (!emitWarning)
153  {
154  emitWarning = &(Foam::Warning.stream());
155 
156  *emitWarning
157  << "objectRegistry '"
158  << name() << "' has missing temporary objects:" << nl;
159  }
160 
161  *emitWarning<< " " << iter.key() << nl;
162  }
163  else
164  {
165  iter.val().second() = false;
166  }
167  }
168 
169  if (emitWarning)
170  {
171  *emitWarning
172  << "Available temporary objects: "
173  << temporaryObjects_.sortedToc() << endl;
174  }
175 
176  temporaryObjects_.clear();
177  }
178 
179  return enabled;
180 }
181 
182 
183 // ************************************************************************* //
bool cacheTemporaryObject(const word &name) const
True if given name is in the cacheTemporaryObjects set.
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:162
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:329
String literal.
Definition: keyType.H:82
bool checkCacheTemporaryObjects() const
Check that all objects specified in the cacheTemporaryObjects were also cached.
OSstream & stream(OSstream *alternative=nullptr)
Return OSstream for output operations. Use the alternative stream for serial-only output if it is a v...
Definition: messageStream.C:68
void resetCacheTemporaryObject(const regIOobject *io) const
Reset the cache state of the given object (nullptr is ignored)
messageStream Warning
Warning stream (stdout output on master, null elsewhere), with additional &#39;FOAM Warning&#39; header text...
List< word > wordList
List of word.
Definition: fileName.H:58
const dictionary & controlDict() const
Return read access to the controlDict dictionary.
Definition: Time.H:462
const entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Definition: dictionaryI.H:80
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Registry of regIOobjects.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28