calcEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "calcEntry.H"
27 #include "calcIncludeEntry.H"
28 #include "dictionary.H"
29 #include "dynamicCode.H"
30 #include "codeStream.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace functionEntries
38 {
40 
42  (
44  calcEntry,
45  execute,
46  dictionaryIstream
47  );
48 
50  (
52  calcEntry,
53  execute,
54  primitiveEntryIstream
55  );
56 }
57 }
58 
59 
60 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
61 
62 Foam::string Foam::functionEntries::calcEntry::calc
63 (
64  const dictionary& dict,
65  Istream& is
66 )
67 {
68  Info<< "Expanding #calc at line " << is.lineNumber()
69  << " in file " << dict.name() << endl;
70 
72  (
73  "functionEntries::calcEntry::execute(..)",
74  dict
75  );
76 
77  // Construct codeDict for codeStream
78  // with dict as parent dictionary for string expansion
79  // and variable substitution
80  dictionary codeDict(dict, dictionary());
81 
82  // Read the code expression string delimited by either '"..."' or '#{...#}'
83  token t(is);
84 
85  if (t.isVerbatimString())
86  {
87  const verbatimString& s = t.verbatimStringToken();
88 
90  codeDict.add("code", s);
91  }
92  else if (t.isString())
93  {
94  const string& s = t.stringToken();
95 
97  codeDict.add("code", "os << (" + s + ");");
98  }
99  else
100  {
102  << "Wrong string type for #calc" << nl
103  << " Expected either a string delimited by '\"...\"' "
104  "or a verbatim string delimited by '#{...#}' " << nl
105  << " found token " << t
106  << exit(FatalIOError);
107  }
108 
109  codeStream::streamingFunctionType function = codeStream::getFunction
110  (
111  dict,
112  codeDict
113  );
114 
115  // Use function to write stream
116  OStringStream os(is.format());
117  (*function)(os, dict);
118 
119  // Return the string containing the results of the calculation
120  return os.str();
121 }
122 
123 
124 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
125 
127 (
128  dictionary& dict,
129  Istream& is
130 )
131 {
132  return insert(dict, calc(dict, is));
133 }
134 
135 
137 (
138  const dictionary& dict,
139  primitiveEntry& thisEntry,
140  Istream& is
141 )
142 {
143  return insert(dict, thisEntry, calc(dict, is));
144 }
145 
146 
147 // ************************************************************************* //
Macros for easy insertion into member function selection tables.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:58
Compiles and executes code string expressions, returning the result to the dictionary entry.
Definition: calcEntry.H:191
static bool execute(dictionary &dict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: calcEntry.C:127
static void codeInclude(dictionary &codeDict)
Add the cached include file names to the codeInclude entry.
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
const dictionary & dict() const
This entry is not a dictionary,.
A class for handling character strings derived from std::string.
Definition: string.H:79
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
defineTypeNameAndDebug(includeFvConstraintEntry, 0)
addToMemberFunctionSelectionTable(functionEntry, includeFvConstraintEntry, execute, dictionaryIstream)
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
messageStream Info
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266
dictionary dict