codeDict.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) 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 "codeDict.H"
27 #include "OSspecific.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 namespace functionEntries
35 {
37 
39 }
40 }
41 
43 (
44  "codeDictOptions"
45 );
46 
48 {
49  "codeDictTemplate.C"
50 };
51 
52 
53 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
54 
55 Foam::string Foam::functionEntries::codeDict::codeString
56 (
57  const label index,
58  const dictionary& contextDict,
59  Istream& is
60 )
61 {
62  return codeStream::codeString
63  (
64  typeName,
65  "CODE_BLOCK_DICT_FUNCTION",
66  index,
67  contextDict,
68  is
69  );
70 }
71 
72 
73 Foam::functionEntries::codeDict::codeDictFunctionType
74 Foam::functionEntries::codeDict::getFunction
75 (
76  const dictionary& contextDict,
77  const dictionary& codeDict
78 )
79 {
80  word codeName;
81  void* lib = compile
82  (
83  typeName,
84  contextDict,
85  codeDict,
86  codeOptions,
87  compileFiles,
88  codeName
89  );
90 
91  // Find the function handle in the library
92  const codeDictFunctionType function =
93  reinterpret_cast<codeDictFunctionType>
94  (
95  dlSym(lib, codeName)
96  );
97 
98  if (!function)
99  {
101  (
102  contextDict
103  ) << "Failed looking up symbol " << codeName
104  << " in library " << lib << exit(FatalIOError);
105  }
106 
107  return function;
108 }
109 
110 
111 bool Foam::functionEntries::codeDict::resultStream
112 (
113  dictionary& contextDict,
114  Istream& is
115 )
116 {
117  if (debug)
118  {
119  Info<< "Using " << typeName << " at line " << is.lineNumber()
120  << " in file " << contextDict.name() << endl;
121  }
122 
123  // Construct codeDict for codeDict using the context dictionary
124  // for string expansion and variable substitution
125  const dictionary codeDict(typeName, contextDict, is);
126 
127  // Compile and link the code library and get the function pointer
128  const codeDictFunctionType function = getFunction(contextDict, codeDict);
129 
130  // Use function to append to contextDict
131  (*function)(contextDict, is);
132 
133  return true;
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
138 
140 (
141  const label lineNumber,
142  const dictionary& parentDict,
143  Istream& is
144 )
145 :
146  codeStream(typeName, lineNumber, parentDict)
147 {}
148 
149 
150 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
151 
153 (
154  dictionary& contextDict,
155  Istream& is
156 )
157 {
158  return resultStream(contextDict, is);
159 }
160 
161 
162 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Macros for easy insertion into run-time selection tables.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Compiles and executes C++ OpenFOAM code string expressions.
Definition: codeDict.H:131
static const word codeOptions
Name of the code options file to be used.
Definition: codeDict.H:162
static const wordList compileFiles
Name of the C code template to be used.
Definition: codeDict.H:165
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
Definition: codeDict.C:153
Compiles and executes C++ OpenFOAM code string expressions.
Definition: codeStream.H:214
friend class codeDict
Declare friendship with the calcEntry class.
Definition: codeStream.H:295
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A class for handling character strings derived from std::string.
Definition: string.H:79
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
defineFunctionTypeNameAndDebug(includeFvConstraintEntry, 0)
addToRunTimeSelectionTable(functionEntry, includeFvConstraintEntry, dictionary)
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
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1276