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  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 Foam_exprTools_expressionEntry_H
69 #define Foam_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  //- Default construct
119  expressionEntry() noexcept = 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 // ************************************************************************* //
dictionary dict
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
expressionEntry() noexcept=default
Default construct.
static void inplaceExpand(std::string &s, const dictionary &dict)
Inplace expand expression with dictionary variables/entries.
TypeNameNoDebug("expressionEntry")
Runtime type information.
static string evaluate(const entry &e)
Generic concatenate tokens to space-separated string.
virtual string toExpr(const entry &e) const
To string. Normally with comma separators.
Convert dictionary entry to a stringified expression.
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
static autoPtr< expressionEntry > New(const word &name)
Return an entry to expression converter.
static expressions::exprString expand(const std::string &str, const dictionary &dict)
Expand expression with dictionary entries.
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual ~expressionEntry()=default
Destructor.
static string toExprStr(const Type &data)
Stringified version of data with comma-separated components. Uses prefix corresponding to the pTraits...
A variant of Foam::string with expansion of dictionary variables into a comma-separated form...
Definition: exprString.H:55
const direction noexcept
Definition: Scalar.H:258
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Macros to ease declaration of run-time selection tables.
declareRunTimeSelectionTable(autoPtr, expressionEntry, empty,(),())
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))
An input stream of tokens.
Definition: ITstream.H:52
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63