evalEntry.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) 2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "evalEntry.H"
29 #include "dictionary.H"
30 #include "OTstream.H"
31 #include "stringOps.H"
32 #include "fieldExprDriver.H"
34 
35 #undef DetailInfo
36 #define DetailInfo if (::Foam::infoDetailLevel > 0) InfoErr
37 
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43 namespace functionEntries
44 {
46  (
47  functionEntry,
48  evalEntry,
49  execute,
50  primitiveEntryIstream,
51  eval
52  );
53 
54 } // End namespace functionEntry
55 } // End namespace Foam
56 
57 
58 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
59 
60 Foam::tokenList Foam::functionEntries::evalEntry::evaluate
61 (
62  const dictionary& parentDict,
63  Istream& is
64 )
65 {
66  #ifdef FULLDEBUG
68  << "Using #eval - line "
69  << is.lineNumber() << " in file " << parentDict.name() << nl;
70  #endif
71 
72  // String to evaluate
73  string s;
74 
75  token tok(is);
76 
77  if (!tok.good())
78  {
80  << "Bad token - could not get string to evaluate"
81  << exit(FatalIOError);
82 
83  return tokenList();
84  }
85 
86  if (tok.isString())
87  {
88  s = tok.stringToken();
89  }
90  else if (tok == token::BEGIN_BLOCK)
91  {
92  dynamic_cast<ISstream&>(is).getLine(s, token::END_BLOCK);
93  }
94  else
95  {
96  is.putBack(tok);
97 
99  << "Invalid input for #eval" << nl
100  << exit(FatalIOError);
101  }
102 
103  #ifdef FULLDEBUG
104  DetailInfo
105  << "input: " << s << endl;
106  #endif
107 
108  // Expand with env=true, empty=true, subDict=false
109  // with comments stripped.
110  // Special handling of $[...] syntax enabled.
111  expressions::exprString::inplaceExpand(s, parentDict, true);
113 
114  // An extraneous trailing ';' is a common input error, catch it now.
115  // May need to relax in the future, trim or something else
116 
117  if (std::string::npos != s.find(';'))
118  {
120  << "Invalid input for #eval" << nl
121  << s << endl
122  << exit(FatalIOError);
123  }
124 
125  #ifdef FULLDEBUG
126  DetailInfo
127  << "expanded: " << s << endl;
128  #endif
129 
130  if (s.empty())
131  {
132  InfoErr
133  << "Empty #eval - line "
134  << is.lineNumber() << " in file " << parentDict.name() << nl;
135 
136  return tokenList();
137  }
138 
139  expressions::exprResult result;
140  {
142  driver.parse(s);
143  result = std::move(driver.result());
144  }
145 
146  if (!result.hasValue() || !result.size())
147  {
148  InfoErr
149  << "Failed #eval - line "
150  << is.lineNumber() << " in file " << parentDict.name() << nl;
151 
152  return tokenList();
153  }
154 
155  // Could average/reduce to a single value, but probably not needed
157 
158  OTstream toks;
159  result.writeValue(toks);
160 
161  return std::move(toks);
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
168 (
169  const dictionary& parentDict,
171  Istream& is
172 )
173 {
174  tokenList toks(evaluate(parentDict, is));
175 
176  entry.append(std::move(toks), true); // Lazy resizing
177 
178  return true;
179 }
180 
181 
182 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::primitiveEntry
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
Definition: primitiveEntry.H:63
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
Foam::stringOps::inplaceTrim
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1069
evalEntry.H
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
addToMemberFunctionSelectionTable.H
Macros for easy insertion into member function selection tables.
DetailInfo
#define DetailInfo
Definition: evalEntry.C:36
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:127
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::functionEntries::addNamedToMemberFunctionSelectionTable
addNamedToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream, calc)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:126
fieldExprDriver.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::tokenList
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
dictionary.H
Foam::functionEntries::evalEntry::execute
static bool execute(const dictionary &parentDict, primitiveEntry &thisEntry, Istream &is)
Execute in a primitiveEntry context.
Definition: evalEntry.C:168
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::expressions::exprString::inplaceExpand
static void inplaceExpand(std::string &str, const dictionary &dict, const bool stripComments=true)
Definition: exprString.C:35
OTstream.H
stringOps.H
Foam::expressions::fieldExprDriver
fieldExpr::parseDriver fieldExprDriver
Typedef for fieldExpr parseDriver.
Definition: fieldExprFwd.H:62
Foam::stringOps::evaluate
string evaluate(const std::string &s, size_t pos=0, size_t len=std::string::npos)
Definition: stringOpsEvaluate.C:37