codeBlockStreamEntry.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 "codeBlockStreamEntry.H"
27 #include "codeBlockEntry.H"
28 #include "OSspecific.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 
56 Foam::OTstream Foam::functionEntries::codeBlockStreamEntry::resultStream
57 (
58  const dictionary& dict,
59  Istream& is
60 )
61 {
62  if (debug)
63  {
64  Info<< "Expanding " << typeName << " at line " << is.lineNumber()
65  << " in file " << dict.name() << endl;
66  }
67 
68  // Construct the name of the function
69  // corresponding to this codeBlockStreamEntry
70  const word functionName
71  (
72  codeBlockPtr_->codeBlockName_ + "_" + Foam::name(index_)
73  );
74 
75  // Find the function handle in the library
76  const codeStream::streamingFunctionType function =
77  reinterpret_cast<codeStream::streamingFunctionType>
78  (
79  dlSym(codeBlockPtr_->lib_, functionName)
80  );
81 
82  if (!function)
83  {
85  << "Failed looking up symbol " << functionName
86  << " in library " << codeBlockPtr_->lib_ << exit(FatalIOError);
87  }
88 
89  // Use function to write stream
90  OTstream ots(is.name(), is.format());
91  (*function)(ots, dict);
92 
93  // Return the OTstream containing the results of the calculation
94  return ots;
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
101 (
102  const label lineNumber,
103  const dictionary& parentDict,
104  Istream& is
105 )
106 :
107  functionEntry(typeName, lineNumber, parentDict),
108  codeBlockPtr_(nullptr),
109  index_(-1)
110 {
111  token t(is);
112 
113  if (t.isUnsignedInteger64())
114  {
115  codeBlockPtr_ =
116  reinterpret_cast<const codeBlockEntry*>(t.unsignedInteger64Token());
117 
118  is.read(t);
119 
120  if (t.isLabel())
121  {
122  index_ = t.labelToken();
123  }
124  else
125  {
127  << "Wrong argument type for " << typeName << nl
128  << " Expected a label but found token " << t
129  << exit(FatalIOError);
130  }
131  }
132  else
133  {
135  << "Wrong argument type for " << typeName << nl
136  << " Expected a unsignedInteger64 but found token " << t
137  << exit(FatalIOError);
138  }
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143 
145 (
146  dictionary& contextDict,
147  Istream& is
148 )
149 {
150  return insert(contextDict, resultStream(contextDict, is));
151 }
152 
153 
155 (
156  const dictionary& contextDict,
157  primitiveEntry& contextEntry,
158  Istream& is
159 )
160 {
161  return insert
162  (
163  contextDict,
164  contextEntry,
165  codeBlockStreamEntry(is.lineNumber(), contextDict, is)
166  .resultStream(contextDict, is)
167  );
168 }
169 
170 
172 {}
173 
174 
175 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
Output token stream.
Definition: OTstream.H:56
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
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
Part of the #codeBlock...#codeBlock clause.
codeBlockStreamEntry(const label lineNumber, const dictionary &parentDict, Istream &is)
Construct from line number, dictionary and Istream.
virtual void write(Ostream &) const
Dummy write.
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
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 token holds items read from Istream.
Definition: token.H:74
bool isLabel() const
Definition: tokenI.H:615
bool isUnsignedInteger64() const
Definition: tokenI.H:581
label labelToken() const
Definition: tokenI.H:634
uint64_t unsignedInteger64Token() const
Definition: tokenI.H:590
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.
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1276
static const char nl
Definition: Ostream.H:297
dictionary dict