streamEntry.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) 2025-2026 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 "streamEntry.H"
27 #include "codeIncludeEntry.H"
28 #include "dictionary.H"
29 #include "codeStream.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace functionEntries
38 {
40 
42 
44  (
47  execute,
48  primitiveEntryIstream
49  );
50 }
51 }
52 
53 
54 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
55 
57 (
58  const label index,
59  const dictionary& codeDict,
60  Istream& is,
61  const string& startString,
62  const string& endString
63 )
64 {
65  // Read the code expression string delimited by either '"..."' or '#{...#}'
66  token t(is);
67 
68  if (t.isString() || t.isVerbatimString())
69  {
70  return
71  (
72  "CODE_BLOCK_STREAM_FUNCTION(" + Foam::name(index) + ")\n"
73  "{\n"
74  " #line " + Foam::name(t.lineNumber())
75  + " \"" + codeDict.name() + "\"\n"
76  + startString + t.anyStringToken() + endString +
77  "\n}\n\n"
78  );
79  }
80  else
81  {
83  << "Wrong string type for " << typeName << nl
84  << " Expected either a string delimited by '\"...\"' "
85  "or a verbatim string delimited by '#{...#}' " << nl
86  << " found token " << t
87  << exit(FatalIOError);
88  return string::null;
89  }
90 }
91 
92 
94 (
95  const dictionary& dict,
96  Istream& is,
97  const string& startString,
98  const string& endString
99 )
100 {
101  if (debug)
102  {
103  Info<< "Expanding " << typeName << " at line " << is.lineNumber()
104  << " in file " << dict.name() << endl;
105  }
106 
107  // Construct codeDict for codeStream with the parent dictionary provided for
108  // string expansion and variable substitution and the same name as the
109  // parent for consistent error messaging
111 
112  // Read the code expression string delimited by either '"..."' or '#{...#}'
113  token t(is);
114 
115  if (t.isString() || t.isVerbatimString())
116  {
117  codeDict.add
118  (
120  (
121  "code",
122  startString + t.anyStringToken() + endString,
123  t.lineNumber()
124  )
125  );
126  }
127  else
128  {
130  << "Wrong string type for " << typeName << nl
131  << " Expected either a string delimited by '\"...\"' "
132  "or a verbatim string delimited by '#{...#}' " << nl
133  << " found token " << t
134  << exit(FatalIOError);
135  }
136 
137  codeStream::streamingFunctionType function = codeStream::getFunction
138  (
139  dict,
140  codeDict
141  );
142 
143  // Use function to write stream
144  OTstream ots(is.name(), is.format());
145  ots.lineNumber() = is.lineNumber();
146  (*function)(ots, dict);
147 
148  // Return the OTstream containing the results of the calculation
149  return ots;
150 }
151 
152 
153 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
154 
156 (
157  const label lineNumber,
158  const dictionary& parentDict,
159  Istream& is
160 )
161 :
162  functionEntry(typeName, lineNumber, parentDict)
163 {}
164 
165 
166 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
167 
169 (
170  dictionary& contextDict,
171  Istream& is
172 )
173 {
174  return insert(contextDict, resultStream(contextDict, is));
175 }
176 
177 
179 (
180  const dictionary& contextDict,
181  primitiveEntry& contextEntry,
182  Istream& is
183 )
184 {
185  return insert(contextDict, contextEntry, resultStream(contextDict, is));
186 }
187 
188 
189 // ************************************************************************* //
Macros for easy insertion into member function selection tables.
Macros for easy insertion into run-time selection tables.
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Output token stream.
Definition: OTstream.H:56
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
static const fileName null
An empty fileName.
Definition: fileName.H:97
Compiles and executes C++ OpenFOAM code string expressions.
Definition: codeDict.H:131
Compiles and executes C++ OpenFOAM code string expressions.
Definition: streamEntry.H:112
static OTstream resultStream(const dictionary &dict, Istream &is, const string &startString=string::null, const string &endString=string::null)
Perform the calculation and return the result in an OTstream.
Definition: streamEntry.C:94
streamEntry(const label lineNumber, const dictionary &parentDict, Istream &is)
Construct from line number, dictionary and Istream.
Definition: streamEntry.C:156
static string codeString(const label index, const dictionary &codeDict, Istream &, const string &startString=string::null, const string &endString=string::null)
Return the code string.
Definition: streamEntry.C:57
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
Definition: streamEntry.C:169
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 fileName & name() const
Return the dictionary name.
A class for handling character strings derived from std::string.
Definition: string.H:79
static const string null
An empty string.
Definition: string.H:88
A token holds items read from Istream.
Definition: token.H:74
bool isVerbatimString() const
Definition: tokenI.H:414
const string & anyStringToken() const
Definition: tokenI.H:444
bool isString() const
Definition: tokenI.H:396
label lineNumber() const
Definition: tokenI.H:847
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
defineFunctionTypeNameAndDebug(includeFvConstraintEntry, 0)
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, primitiveEntryIstream)
addToRunTimeSelectionTable(functionEntry, includeFvConstraintEntry, dictionary)
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
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
messageStream Info
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:297
dictionary dict