exprResultGlobals.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) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
9 Copyright (C) 2019-2020 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 "exprResultGlobals.H"
30#include "Time.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
36namespace expressions
37{
38
40
41} // End namespace expressions
42} // End namespace Foam
43
44
45// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46
48(
49 const objectRegistry& obr
50)
51:
52 regIOobject
53 (
54 IOobject
55 (
56 exprResultGlobals::typeName,
57 obr.time().timeName(), // instance
58 "expressions", // local
59 obr.time(),
60 IOobject::READ_IF_PRESENT,
61 IOobject::AUTO_WRITE,
62 true // register
63 )
64 ),
65 variables_(),
66 timeIndex_(obr.time().timeIndex())
67{
68 if (headerOk())
69 {
70 readData
71 (
72 readStream(exprResultGlobals::typeName, true)
73 );
74 }
75}
76
77
79:
80 HashPtrTable<exprResult>(tbl.capacity())
81{
82 for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
83 {
84 this->set(iter.key(), (*iter)->clone());
85 }
86}
87
88
90:
91 HashPtrTable<exprResult>(std::move(tbl))
92{}
93
94
96:
98{}
99
100
101// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
102
103void Foam::expressions::exprResultGlobals::reset()
104{
105 forAllIters(variables_, tablesIter)
106 {
107 forAllIters((*tablesIter), iter)
108 {
109 (*iter)->reset();
110 }
111 }
112}
113
114
115// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
116
119(
120 const objectRegistry& obr
121)
122{
124
125 auto* ptr = obr.time().getObjectPtr<Type>(Type::typeName);
126
127 if (!ptr)
128 {
129 ptr = new Type(obr);
130 ptr->store();
131 }
132 else if (ptr->timeIndex_ != obr.time().timeIndex())
133 {
134 // If time changes, reset variables
135
136 ptr->timeIndex_ = obr.time().timeIndex();
137 ptr->reset();
138 }
139
140 return *ptr;
141}
142
143
144// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
145
147{
149
150 auto* ptr = obr.time().getObjectPtr<Type>(Type::typeName);
151
152 if (ptr)
153 {
154 return obr.time().checkOut(ptr);
155 }
156
157 return false;
158}
159
160
161// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
162
164{
165 // Enforce ASCII to avoid any potential binary issues
166 const auto oldFmt = os.format(IOstream::ASCII);
167
168 os << variables_;
169
170 os.format(oldFmt);
171
172 return os.good();
173}
174
175
177{
178 // Enforce ASCII to avoid any potential binary issues
179 const auto oldFmt = is.format(IOstream::ASCII);
180
181 is >> variables_;
182
183 is.format(oldFmt);
184
185 return !is.bad();
186}
187
188
191{
192 return variables_[name];
193}
194
195
198(
199 const word& name,
200 const wordUList& scopes
201) const
202{
203 for (const word& scopeName : scopes)
204 {
205 const auto tableIter = variables_.cfind(scopeName);
206
207 if (tableIter.found())
208 {
209 const auto resultIter = (*tableIter).cfind(name);
210
211 if (resultIter.found())
212 {
213 return *(*resultIter);
214 }
215 }
216 #ifdef FULLDEBUG
217 else
218 {
220 << "No scope " << scopeName << " for " << name << nl
221 << "Known global scopes: " << variables_.sortedToc() << nl;
222 }
223 #endif
224 }
225
226 return exprResult::null;
227}
228
229
232(
233 const word& name,
234 const word& scope,
235 const exprResult& value,
236 const bool overwrite
237)
238{
239 Table& tbl = getOrCreateScope(scope);
240
241 auto iter = tbl.find(name);
242
243 if (!iter.found())
244 {
245 tbl.set(name, new exprResult(value));
246 iter = tbl.find(name);
247 }
248 else if (overwrite)
249 {
250 *(*iter) = value;
251 }
252
253 return *(*iter);
254}
255
256
259(
260 const word& name,
261 const word& scope,
262 autoPtr<exprResult>&& value,
263 const bool overwrite
264)
265{
266 Table& tbl = getOrCreateScope(scope);
267
268 if (overwrite || !tbl.found(name))
269 {
270 tbl.set(name, std::move(value));
271 }
272
273 return *tbl[name];
274}
275
276
279(
280 const dictionary& dict,
281 const word& scope,
282 const bool overwrite
283)
284{
285 word scopeName(scope);
286
287 const word name(dict.get<word>("globalName"));
288
289 if (scopeName.empty())
290 {
291 scopeName = dict.get<word>("globalScope");
292 }
293
294 if (dict.found("resultType"))
295 {
296 return addValue
297 (
298 name,
299 scopeName,
301 overwrite
302 );
303 }
304 else
305 {
306 return addValue
307 (
308 name,
309 scopeName,
310 exprResult(dict, true),
311 overwrite
312 );
313 }
314}
315
316
318(
319 const word& name,
320 const word& scope
321)
322{
323 auto iter = variables_.find(scope);
324
325 return (iter.found() && (*iter).erase(name));
326}
327
328
329// ************************************************************************* //
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
bool set(const word &key, exprResult *ptr)
Assign a new entry, overwriting existing entries.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition: HashTableI.H:114
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
streamFormat format() const noexcept
Get the current stream format.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
label timeIndex() const noexcept
Return current time index.
Definition: TimeStateI.H:37
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionaryI.H:87
A globally available registry of expression results. These are currently registered on Time (may chan...
virtual bool writeData(Ostream &os) const
Write variables.
exprResult & addValue(const word &name, const word &scope, const exprResult &value, const bool overwrite=true)
Add named result to specified scope.
virtual bool readData(Istream &os)
Read variables.
Table & getNamespace(const word &name)
Get an existing table for the namespace.
static bool Delete(const objectRegistry &obr)
Static destructor for singleton.
bool removeValue(const word &name, const word &scope)
Remove named result from specified scope.
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:127
static const exprResult null
An empty result.
Definition: exprResult.H:318
Registry of regIOobjects.
const Time & time() const noexcept
Return time registry.
bool checkOut(regIOobject *io) const
Type * getObjectPtr(const word &name, const bool recursive=false) const
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeName(Type)
Define the typeName.
Definition: className.H:96
OBJstream os(runTime.globalPath()/outputName)
word timeName
Definition: getTimeIndex.H:3
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
label timeIndex
Definition: getTimeIndex.H:30
dictionary dict
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:260
static const char *const typeName
The type name used in ensight case files.