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 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 "exprResultGlobals.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace expressions
37 {
38 
39  defineTypeNameAndDebug(exprResultGlobals, 0);
40 
41 } // End namespace expressions
42 } // End namespace Foam
43 
44 
46  Foam::expressions::exprResultGlobals::singleton_;
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 Foam::expressions::exprResultGlobals::exprResultGlobals
52 (
53  const objectRegistry& obr
54 )
55 :
56  regIOobject
57  (
58  IOobject
59  (
60  "exprResultGlobals",
61  obr.time().timeName(),
62  "expressions",
63  obr.time(),
64  IOobject::READ_IF_PRESENT,
65  IOobject::AUTO_WRITE
66  )
67  ),
68  timeIndex_(obr.time().timeIndex())
69 {
70  if (headerOk())
71  {
72  readData
73  (
74  readStream("exprResultGlobals", true)
75  );
76  }
77 }
78 
79 
81 :
83 {}
84 
85 
87 :
88  HashPtrTable<exprResult>(tbl.capacity())
89 {
90  for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
91  {
92  this->set(iter.key(), (*iter)->clone());
93  }
94 }
95 
96 
98 :
99  HashPtrTable<exprResult>(std::move(tbl))
100 {}
101 
102 
104 :
106 {}
107 
108 
109 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
110 
111 void Foam::expressions::exprResultGlobals::reset()
112 {
113  forAllIters(variables_, tablesIter)
114  {
115  forAllIters((*tablesIter), iter)
116  {
117  (*iter)->reset();
118  }
119  }
120 }
121 
122 
123 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
124 
126 {
127  // Enforce ASCII to avoid any potential binary issues
128  const auto oldFmt = os.format(IOstream::ASCII);
129 
130  os << variables_;
131 
132  os.format(oldFmt);
133 
134  return os.good();
135 }
136 
137 
139 {
140  // Enforce ASCII to avoid any potential binary issues
141  const auto oldFmt = is.format(IOstream::ASCII);
142 
143  is >> variables_;
144 
145  is.format(oldFmt);
146 
147  return !is.bad();
148 }
149 
150 
153 (
154  const objectRegistry& obr
155 )
156 {
157  if (!singleton_)
158  {
159  singleton_.reset(new exprResultGlobals(obr));
160  }
161 
162  if (singleton_->timeIndex_ != obr.time().timeIndex())
163  {
164  // Time changes, reset variables
165 
166  singleton_->timeIndex_ = obr.time().timeIndex();
167  singleton_->reset();
168  }
169 
170  return *singleton_;
171 }
172 
173 
176 {
177  return variables_[name];
178 }
179 
180 
183 (
184  const word& name,
185  const wordUList& scopes
186 ) const
187 {
188  for (const word& scopeName : scopes)
189  {
190  const auto tableIter = variables_.find(scopeName);
191 
192  if (tableIter.found())
193  {
194  const auto resultIter = (*tableIter).find(name);
195 
196  if (resultIter.found())
197  {
198  return *(*resultIter);
199  }
200  }
201  #ifdef FULLDEBUG
202  else
203  {
205  << "No scope " << scopeName << " for " << name << nl
206  << "Known global scopes: " << variables_.sortToc() << nl;
207  }
208  #endif
209  }
210 
211  return exprResult::null;
212 }
213 
214 
217 (
218  const word& name,
219  const word& scope,
220  const exprResult& value,
221  const bool overwrite
222 )
223 {
224  Table& tbl = getOrCreateScope(scope);
225 
226  auto iter = tbl.find(name);
227 
228  if (!iter.found())
229  {
230  tbl.set(name, new exprResult(value));
231  iter = tbl.find(name);
232  }
233  else if (overwrite)
234  {
235  *(*iter) = value;
236  }
237 
238  return *(*iter);
239 }
240 
241 
244 (
245  const word& name,
246  const word& scope,
247  autoPtr<exprResult>& value,
248  const bool overwrite
249 )
250 {
251  Table& tbl = getOrCreateScope(scope);
252 
253  if (overwrite || !tbl.found(name))
254  {
255  tbl.set(name, value);
256  }
257 
258  return *tbl[name];
259 }
260 
261 
264 (
265  const word& name,
266  const word& scope,
267  autoPtr<exprResult>&& value,
268  const bool overwrite
269 )
270 {
271  Table& tbl = getOrCreateScope(scope);
272 
273  if (overwrite || !tbl.found(name))
274  {
275  tbl.set(name, value);
276  }
277 
278  return *tbl[name];
279 }
280 
281 
284 (
285  const dictionary& dict,
286  const word& scope,
287  const bool overwrite
288 )
289 {
290  word scopeName(scope);
291 
292  const word name(dict.get<word>("globalName"));
293 
294  if (scopeName.empty())
295  {
296  scopeName = dict.get<word>("globalScope");
297  }
298 
299  if (dict.found("resultType"))
300  {
301  return addValue
302  (
303  name,
304  scopeName,
306  overwrite
307  );
308  }
309  else
310  {
311  return addValue
312  (
313  name,
314  scopeName,
315  exprResult(dict, true),
316  overwrite
317  );
318  }
319 }
320 
321 
323 (
324  const word& name,
325  const word& scope
326 )
327 {
328  auto iter = variables_.find(scope);
329 
330  return (iter.found() && (*iter).erase(name));
331 }
332 
333 
334 // ************************************************************************* //
exprResultGlobals.H
Foam::expressions::exprResultGlobals::writeData
virtual bool writeData(Ostream &os) const
Write variables.
Definition: exprResultGlobals.C:125
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:46
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::expressions::exprResultGlobals::Table::Table
Table()
Definition: exprResultGlobals.C:80
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::expressions::exprResultGlobals::get
const exprResult & get(const word &name, const wordUList &scopes) const
Return a global variable, if it exists, or a exprResult::null.
Definition: exprResultGlobals.C:183
Foam::expressions::exprResultGlobals::getNamespace
Table & getNamespace(const word &name)
Get an existing table for the namespace.
Definition: exprResultGlobals.C:175
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::expressions::exprResult::null
static const exprResult null
An empty result.
Definition: exprResult.H:330
Foam::HashPtrTable< exprResult >::set
bool set(const word &key, exprResult *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:84
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:128
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::expressions::exprResult::New
static autoPtr< exprResult > New(const dictionary &dict)
Return a reference to the selected value driver.
Definition: exprResult.C:304
Foam::expressions::exprResultGlobals::Table
The table of results.
Definition: exprResultGlobals.H:65
Foam::expressions::exprResultGlobals
A globally available registry of expression results.
Definition: exprResultGlobals.H:56
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:217
timeIndex
label timeIndex
Definition: getTimeIndex.H:4
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::expressions::exprResultGlobals::readData
virtual bool readData(Istream &os)
Read variables.
Definition: exprResultGlobals.C:138
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::expressions::exprResultGlobals::removeValue
bool removeValue(const word &name, const word &scope)
Remove named result from specified scope.
Definition: exprResultGlobals.C:323
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:234
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::expressions::exprResultGlobals::addValue
exprResult & addValue(const word &name, const word &scope, const exprResult &value, const bool overwrite=true)
Add named result to specified scope.
Definition: exprResultGlobals.C:217
Time.H
Foam::autoPtr< Foam::expressions::exprResultGlobals >
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>.
Definition: HashPtrTable.H:54
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::regIOobject::readStream
Istream & readStream(const word &, const bool valid=true)
Return Istream and check object type against that given.
Definition: regIOobjectRead.C:140
Foam::TimeState::timeIndex
label timeIndex() const
Return current time index.
Definition: TimeStateI.H:36
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::expressions::defineTypeNameAndDebug
defineTypeNameAndDebug(fvExprDriver, 0)
Foam::expressions::exprResultGlobals::New
static exprResultGlobals & New(const objectRegistry &obr)
Get the singleton.
Definition: exprResultGlobals.C:153
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:449