Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
34void 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 if (objectRegistry::debug)
70 {
71 Info<< "objectRegistry::cacheTemporaryObjects : "
72 << flatOutput(objectNames) << endl;
73 }
74 }
75}
76
77
78void Foam::objectRegistry::deleteCachedObject(regIOobject* io) const
79{
80 if (io)
81 {
82 io->release(); // Relinquish any ownership by registry
83 io->checkOut();
84 // Additional safety - not certain this is actually needed...
85 io->rename(io->name() + "-Cache");
86 delete io;
87 }
88}
89
90
91// FUTURE: (currently not needed)
92// void Foam::objectRegistry::addTemporaryObject
93// (
94// const word& name
95// ) const
96// {
97// cacheTemporaryObjects_.emplace(name, false, false);
98// }
99
100
102(
103 const word& name
104) const
105{
106 return cacheTemporaryObjects_.contains(name);
107}
108
111{
112 return io && cacheTemporaryObjects_.contains(io->name());
113}
114
117{
118 return cacheTemporaryObjects_.contains(io.name());
119}
120
121
123(
124 const regIOobject* io
125) const
126{
127 if (io && !cacheTemporaryObjects_.empty())
128 {
129 auto iter = cacheTemporaryObjects_.find(io->name());
130
131 // Reset the cached flag
132 if (iter.good())
134 iter.val().first() = false;
135 }
136 }
137}
138
139
141(
143) const
144{
146}
147
148
150{
151 bool enabled = cacheTemporaryObjects_.size();
152
153 forAllConstIters(*this, iter)
154 {
155 const auto* subObr = dynamic_cast<const objectRegistry*>(iter.val());
156
157 // Protect against re-searching the top-level registry
158 if (subObr && subObr != this)
159 {
160 enabled = subObr->checkCacheTemporaryObjects() || enabled;
161 }
162 }
163
164 if (enabled)
165 {
166 OSstream* emitWarning = nullptr;
167
168 forAllIters(cacheTemporaryObjects_, iter)
169 {
170 if (!iter.val().second())
171 {
172 if (!emitWarning)
173 {
174 emitWarning = &(Foam::Warning.stream());
175
176 *emitWarning
177 << "objectRegistry '"
178 << name() << "' has missing temporary objects:" << nl;
179 }
180
181 *emitWarning<< " " << iter.key() << nl;
182 }
183 else
184 {
185 iter.val().second() = false;
186 }
187 }
188
189 if (emitWarning)
190 {
191 *emitWarning
192 << "Available temporary objects: "
193 << temporaryObjects_.sortedToc() << endl;
194 }
195
196 temporaryObjects_.clear();
197 }
198
199 return enabled;
200}
201
202
203// ************************************************************************* //
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
@ LITERAL
String literal.
Definition keyType.H:82
OSstream & stream(OSstream *alternative=nullptr, int communicator=-1)
Return OSstream for output operations.
Registry of regIOobjects.
void resetCacheTemporaryObject(const regIOobject *io) const
Reset the cache state of the given object (nullptr is ignored).
bool checkCacheTemporaryObjects() const
Check that all objects specified in the cacheTemporaryObjects were also cached.
void clear()
Clear all entries from the registry.
bool is_cacheTemporaryObject(const word &name) const
True if given name is in the cacheTemporaryObjects set.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
A class for handling words, derived from Foam::string.
Definition word.H:66
const auto & io
auto & name
List< word > wordList
List of word.
Definition fileName.H:60
messageStream Info
Information stream (stdout output on master, null elsewhere).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
messageStream Warning
Warning stream (stdout output on master, null elsewhere), with additional 'FOAM Warning' header text.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition stdFoam.H:214
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235