calcEntry.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) 2011-2013 OpenFOAM Foundation
9  Copyright (C) 2018-2021 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 \*---------------------------------------------------------------------------*/
28 
29 #include "calcEntry.H"
30 #include "codeStream.H"
31 #include "dictionary.H"
32 #include "dynamicCode.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionEntries
40 {
42  (
44  calcEntry,
45  execute,
46  dictionaryIstream,
47  calc
48  );
49 
51  (
53  calcEntry,
54  execute,
55  primitiveEntryIstream,
56  calc
57  );
58 } // End namespace functionEntries
59 } // End namespace Foam
60 
61 
62 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
63 
64 Foam::string Foam::functionEntries::calcEntry::evaluate
65 (
66  const dictionary& parentDict,
67  Istream& is
68 )
69 {
71  << "Using #calc - line "
72  << is.lineNumber() << " in file "
73  << parentDict.relativeName() << nl;
74 
76  (
77  "functionEntries::calcEntry::evaluate(..)",
78  parentDict
79  );
80 
81  // Read string
82  string s(is);
83 
84  // Construct codeDict for codeStream
85  dictionary codeSubDict;
86  codeSubDict.add("code", "os << (" + s + ");");
87  dictionary codeDict(parentDict, codeSubDict);
88 
89  // Use function to write stream
90  OStringStream buf(is.format());
91  buf.precision(16); // Some reasonably high precision
92 
93  streamingFunctionType function = getFunction(parentDict, codeDict);
94  (*function)(buf, parentDict);
95 
96  // Return evaluated content as string
97  return buf.str();
98 }
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
104 (
105  const dictionary& parentDict,
107  Istream& is
108 )
109 {
110  IStringStream result(evaluate(parentDict, is));
111  entry.read(parentDict, result);
112 
113  return true;
114 }
115 
116 
118 (
119  dictionary& parentDict,
120  Istream& is
121 )
122 {
123  IStringStream result(evaluate(parentDict, is));
124  parentDict.read(result);
125 
126  return true;
127 }
128 
129 
130 // ************************************************************************* //
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
Definition: dictionaryIO.C:134
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static streamingFunctionType getFunction(const dictionary &parentDict, const dictionary &codeDict)
Construct, compile, load and return streaming function.
Definition: codeStream.C:95
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Uses dynamic compilation to provide calculating functionality for entering dictionary entries...
Definition: calcEntry.H:65
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:625
void(* streamingFunctionType)(Ostream &, const dictionary &)
Interpreter function type.
Definition: codeStream.H:124
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read...
virtual int precision() const override
Get precision of output field.
Definition: OSstream.C:334
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:63
string evaluate(label fieldWidth, const std::string &s, size_t pos=0, size_t len=std::string::npos)
String evaluation with specified (positive, non-zero) field width.
#define DetailInfo
Definition: evalEntry.C:30
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:179
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:390
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:120
Macros for easy insertion into member function selection tables.
addNamedToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream, calc)
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))
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:59
A class for handling character strings derived from std::string.
Definition: string.H:72
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
streamFormat format() const noexcept
Get the current stream format.
static bool execute(const dictionary &parentDict, primitiveEntry &entry, Istream &is)
Execute in a primitiveEntry context.
Definition: calcEntry.C:97
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63