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 -------------------------------------------------------------------------------
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  defineTypeName(exprResultGlobals);
40 
41 } // End namespace expressions
42 } // End namespace Foam
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
47 Foam::expressions::exprResultGlobals::exprResultGlobals
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 
103 void 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 {
123  typedef expressions::exprResultGlobals Type;
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 {
148  typedef expressions::exprResultGlobals Type;
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 // ************************************************************************* //
exprResultGlobals.H
Foam::objectRegistry::getObjectPtr
Type * getObjectPtr(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:423
Foam::expressions::exprResultGlobals::writeData
virtual bool writeData(Ostream &os) const
Write variables.
Definition: exprResultGlobals.C:163
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::expressions::defineTypeName
defineTypeName(fvExprDriverWriter)
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
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:198
Foam::expressions::exprResultGlobals::getNamespace
Table & getNamespace(const word &name)
Get an existing table for the namespace.
Definition: exprResultGlobals.C:190
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::expressions::exprResult::null
static const exprResult null
An empty result.
Definition: exprResult.H:318
Foam::TimeState::timeIndex
label timeIndex() const noexcept
Return current time index.
Definition: TimeStateI.H:37
Foam::HashPtrTable< exprResult >::set
bool set(const word &key, exprResult *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:135
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:124
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:70
Foam::expressions::exprResultGlobals
A globally available registry of expression results. These are currently registered on Time (may chan...
Definition: exprResultGlobals.H:61
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:223
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::expressions::exprResultGlobals::readData
virtual bool readData(Istream &os)
Read variables.
Definition: exprResultGlobals.C:176
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:123
Foam::expressions::exprResultGlobals::removeValue
bool removeValue(const word &name, const word &scope)
Remove named result from specified scope.
Definition: exprResultGlobals.C:318
os
OBJstream os(runTime.globalPath()/outputName)
Foam::expressions::exprResultGlobals::Table::Table
Table()=default
Default construct.
Foam::IOstream::bad
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::expressions::exprResultGlobals::Delete
static bool Delete(const objectRegistry &obr)
Static destructor for singleton.
Definition: exprResultGlobals.C:146
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
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:232
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
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::objectRegistry::checkOut
bool checkOut(regIOobject *io) const
Definition: objectRegistry.C:254
timeIndex
label timeIndex
Definition: getTimeIndex.H:30
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:129
Foam::expressions::exprResultGlobals::New
static exprResultGlobals & New(const objectRegistry &obr)
Static constructor for singleton.
Definition: exprResultGlobals.C:119
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:436