expressionEntry.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  Original code Copyright (C) 2014-2018 Bernhard Gschaider
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 Class
28  Foam::exprTools::expressionEntry
29 
30 Description
31  Convert dictionary entry to a stringified expression.
32 
33  The general OpenFOAM dictionary expansions will result in
34  space-separated values. For example,
35 
36  \verbatim
37  origin (0.21 0 0.01);
38 
39  condition "mag(pos() - $centre) < 0.5";
40  \endverbatim
41 
42  this will expand to the following:
43 
44  \verbatim
45  condition "mag(pos() - (0.21 0 0.01)) < 0.5";
46  \endverbatim
47 
48  For these type of expressions, we'd would like better control.
49  Using instead the special expansions, we can add an effective
50  type cast.
51 
52  \verbatim
53  condition "mag(pos() - $[(vector)centre]) < 0.5";
54  \endverbatim
55 
56  which will expand to the following:
57 
58  \verbatim
59  condition "mag(pos() - vector(0.21,0,0.01)) < 0.5";
60  \endverbatim
61 
62 SourceFiles
63  expressionEntry.C
64  expressionEntryI.H
65 
66 \*---------------------------------------------------------------------------*/
67 
68 #ifndef exprTools_expressionEntry_H
69 #define exprTools_expressionEntry_H
70 
71 #include "exprString.H"
72 #include "primitiveEntry.H"
73 #include "runTimeSelectionTables.H"
74 
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 
77 namespace Foam
78 {
79 namespace exprTools
80 {
81 
82 /*---------------------------------------------------------------------------*\
83  Class expressionEntry Declaration
84 \*---------------------------------------------------------------------------*/
85 
86 class expressionEntry
87 {
88 protected:
89 
90  //- Stringified version of data with comma-separated components.
91  //- Uses prefix corresponding to the pTraits of Type.
92  template<class Type>
93  static string toExprStr(const Type& data);
94 
95  //- Comma-separated stringified version of primitiveEntry of Type.
96  //- Prefix corresponding to the pTraits of Type
97  template<class Type>
98  static string toExprStr(ITstream& is);
99 
100 
101 public:
102 
103  //- Runtime type information
104  TypeNameNoDebug("expressionEntry");
105 
107  (
108  autoPtr,
110  empty,
111  (),
112  ()
113  );
114 
115 
116  // Constructors
117 
118  //- Construct null
119  expressionEntry() = default;
120 
121 
122  // Selectors
123 
124  //- Return an entry to expression converter
125  static autoPtr<expressionEntry> New(const word& name);
126 
127 
128  //- Destructor
129  virtual ~expressionEntry() = default;
130 
131 
132  // Static Member Functions
133 
134  //- Generic concatenate tokens to space-separated string.
135  inline static string evaluate(const entry& e);
136 
137  //- Inplace expand expression with dictionary variables/entries
138  //
139  // \par Expansion behaviour
140  // - alternatives = True
141  // - environment = True
142  // - allow empty = True
143  // - subDict = False
144  // .
145  static void inplaceExpand
146  (
147  std::string& s,
148  const dictionary& dict
149  );
150 
151  //- Expand expression with dictionary entries
153  (
154  const std::string& str,
155  const dictionary& dict
156  );
157 
158 
159  // Member Functions
160 
161  //- To string. Normally with comma separators.
162  virtual string toExpr(const entry& e) const
163  {
164  return evaluate(e);
165  }
166 };
167 
168 
169 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170 
171 } // End namespace exprTools
172 } // End namespace Foam
173 
174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
175 
176 #include "expressionEntryI.H"
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 #endif
181 
182 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::exprTools::expressionEntry::evaluate
static string evaluate(const entry &e)
Generic concatenate tokens to space-separated string.
Definition: expressionEntryI.H:34
primitiveEntry.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
exprString.H
Foam::exprTools::expressionEntry::toExpr
virtual string toExpr(const entry &e) const
To string. Normally with comma separators.
Definition: expressionEntry.H:161
Foam::exprTools::expressionEntry
Convert dictionary entry to a stringified expression.
Definition: expressionEntry.H:85
Foam::exprTools::expressionEntry::toExprStr
static string toExprStr(const Type &data)
expressionEntryI.H
Foam::exprTools::expressionEntry::expressionEntry
expressionEntry()=default
Construct null.
Foam::exprTools::expressionEntry::inplaceExpand
static void inplaceExpand(std::string &s, const dictionary &dict)
Inplace expand expression with dictionary variables/entries.
Definition: expressionEntry.C:141
Foam::exprTools::expressionEntry::TypeNameNoDebug
TypeNameNoDebug("expressionEntry")
Runtime type information.
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
Foam::exprTools::expressionEntry::New
static autoPtr< expressionEntry > New(const word &name)
Return an entry to expression converter.
Definition: expressionEntry.C:118
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
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exprTools::expressionEntry::expand
static expressions::exprString expand(const std::string &str, const dictionary &dict)
Expand expression with dictionary entries.
Definition: expressionEntry.C:281
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::exprTools::expressionEntry::~expressionEntry
virtual ~expressionEntry()=default
Destructor.
runTimeSelectionTables.H
Macros to ease declaration of run-time selection tables.
Foam::expressions::exprString
Definition: exprString.H:60
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::exprTools::expressionEntry::declareRunTimeSelectionTable
declareRunTimeSelectionTable(autoPtr, expressionEntry, empty,(),())
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55