exprResultGlobals.H
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 Class
28  Foam::expressions::exprResultGlobals
29 
30 Description
31  A globally available registry of expression results.
32  These are currently registered on Time (may change in the future).
33 
34 Note
35  The storage mechanism is similar to a MeshObject, but always stores
36  on Time and flushes stale values according to the time index.
37 
38 SourceFiles
39  exprResultGlobals.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef expressions_exprResultGlobals_H
44 #define expressions_exprResultGlobals_H
45 
46 #include "exprResult.H"
47 #include "autoPtr.H"
48 #include "HashPtrTable.H"
49 #include "regIOobject.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 namespace expressions
56 {
57 
58 /*---------------------------------------------------------------------------*\
59  Class exprResultGlobals Declaration
60 \*---------------------------------------------------------------------------*/
61 
63 :
64  public regIOobject
65 {
66 public:
67 
68  // Public Classes
69 
70  //- The table of results
71  class Table
72  :
73  public HashPtrTable<exprResult>
74  {
75  public:
76 
77  //- Default construct
78  Table() = default;
79 
80  //- Copy (clone) construct
81  Table(const Table& tbl);
82 
83  //- Move construct
84  Table(Table&& tbl);
85 
86  //- Read construct from stream
87  explicit Table(Istream& is);
88  };
89 
90 
91 private:
92 
93  // Private Data
94 
95  //- The scoped table of results
96  HashTable<Table> variables_;
97 
98  //- The currently (or previously) used time-index
99  label timeIndex_;
100 
101 
102  // Private Member Functions
103 
104  //- Construct on Time for registry
105  explicit exprResultGlobals(const objectRegistry& obr);
106 
107  //- Reset all variables
108  void reset();
109 
110  //- Get or create a table for the scope
111  Table& getOrCreateScope(const word& scope)
112  {
113  return variables_(scope);
114  }
115 
116 
117 public:
118 
119  //- Runtime type information
120  TypeNameNoDebug("exprResultGlobals");
121 
122 
123  // Selectors
124 
125  //- Static constructor for singleton
126  static exprResultGlobals& New(const objectRegistry& obr);
127 
128  //- Static destructor for singleton
129  static bool Delete(const objectRegistry& obr);
130 
131 
132  //- Destructor
133  virtual ~exprResultGlobals() = default;
134 
135 
136  // Member Functions
137 
138  //- Get an existing table for the namespace
139  Table& getNamespace(const word& name);
140 
141 
142  //- Return a global variable, if it exists, or a exprResult::null
143  const exprResult& get
144  (
145  const word& name,
146  const wordUList& scopes
147  ) const;
148 
149  //- Add named result to specified scope
151  (
152  const word& name,
153  const word& scope,
154  const exprResult& value,
155  const bool overwrite = true
156  );
157 
158  //- Add named result to specified scope
160  (
161  const word& name,
162  const word& scope,
163  autoPtr<exprResult>&& value,
164  const bool overwrite = true
165  );
166 
167  //- Extract result from dictionary and add to the scope
168  //
169  // Controlled by dictionary entries:
170  // - globalName (mandatory)
171  // - globalScope (optional)
172  // - resultType (optional)
174  (
175  const dictionary& dict,
176  const word& scope = "",
177  const bool overwrite = true
178  );
179 
180  //- Remove named result from specified scope
181  // \return true if result was removed
182  bool removeValue
183  (
184  const word& name,
185  const word& scope
186  );
187 
188  //- Write variables
189  virtual bool writeData(Ostream& os) const;
190 
191  //- Read variables
192  virtual bool readData(Istream& os);
193 };
194 
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 } // End namespace expressions
199 } // End namespace Foam
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 #endif
204 
205 // ************************************************************************* //
regIOobject.H
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::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
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::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
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
Foam::expressions::exprResultGlobals::~exprResultGlobals
virtual ~exprResultGlobals()=default
Destructor.
os
OBJstream os(runTime.globalPath()/outputName)
Foam::expressions::exprResultGlobals::Table::Table
Table()=default
Default construct.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
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
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
Foam::expressions::exprResultGlobals::TypeNameNoDebug
TypeNameNoDebug("exprResultGlobals")
Runtime type information.
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
HashPtrTable.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::expressions::exprResultGlobals::New
static exprResultGlobals & New(const objectRegistry &obr)
Static constructor for singleton.
Definition: exprResultGlobals.C:119
exprResult.H
autoPtr.H