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-2024 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  // Parent dictionary provided for string expansion and variable substitution
79  dictionary codeDict(dict, dictionary());
80 
81  // Read the code expression string delimited by either '"..."' or '#{...#}'
82  token t(is);
83 
84  if (t.isVerbatimString())
85  {
86  const verbatimString& s = t.verbatimStringToken();
87 
89  codeDict.add("code", s);
90  }
91  else if (t.isString())
92  {
93  const string& s = t.stringToken();
94 
96  codeDict.add("code", "os << (" + s + ");");
97  }
98  else
99  {
101  << "Wrong string type for #calc" << nl
102  << " Expected either a string delimited by '\"...\"' "
103  "or a verbatim string delimited by '#{...#}' " << nl
104  << " found token " << t
105  << exit(FatalIOError);
106  }
107 
108  codeStream::streamingFunctionType function = codeStream::getFunction
109  (
110  dict,
111  codeDict
112  );
113 
114  // Use function to write stream
115  OStringStream os(is.format());
116  (*function)(os, dict);
117 
118  // Return the string containing the results of the calculation
119  return os.str();
120 }
121 
122 
123 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
124 
126 (
127  dictionary& dict,
128  Istream& is
129 )
130 {
131  return insert(dict, calc(dict, is));
132 }
133 
134 
136 (
137  const dictionary& dict,
138  primitiveEntry& thisEntry,
139  Istream& is
140 )
141 {
142  return insert(dict, thisEntry, calc(dict, is));
143 }
144 
145 
146 // ************************************************************************* //
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
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:126
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(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::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:258
messageStream Info
IOerror FatalIOError
static const char nl
Definition: Ostream.H:267
dictionary dict