codeDict.H
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 Class
25  Foam::functionEntries::codeDict
26 
27 Description
28  Compiles and executes C++ OpenFOAM code string expressions
29 
30  and provides non-const access to the dictionary into which generated
31  entries can be inserted or manipulated in any other way.
32 
33  \c \#codeDict reads three entries: \c code, \c codeInclude (optional),
34  \c codeOptions (optional) to generate the library source code stored in the
35  local \c dynamicCode directory with a subdirectory name corresponding to the
36  SHA1 of the code. The code is then compiled into a dynamically loaded
37  library libcodeDict_<SHA1>.so stored in the
38  \c dynamicCode/platforms/$WM_OPTIONS/lib directory using 'wmake libso'.
39  The resulting library is loaded in executed with arguments
40  \code
41  (dictionary& dict, Istream& is)
42  \endcode
43  where the dict is the current dictionary and is the stream from which the
44  dictionary is being read.
45 
46  The verbatim string format \c \#{ ... \c \#} is used to allow multi-line
47  input without the need to escape the newlines.
48 
49  Dictionary entries constructed with \c \#codeDict can conveniently access
50  and use typed variables. This means calculations involving vectors and
51  tensors and list etc. are possible. To access a variable and construct it
52  as a given type within a \c \#codeDict entry, put the type immediately
53  after the $ symbol inside angled brackets <>. So, $<vector>var or
54  $<vector>{var} substitutes a variable named var as a vector.
55 
56  The variable values are looked-up at run-time making the code corresponding
57  to each \c \#codeDict independent of the values in the dictionary and of
58  each other. Hence the \c \#codeDict code does not need to be recompiled
59  when the values in the dictionary are changed, only if the code is changed.
60 
61 Usage
62  Example to set a zoneGenerator in createZonesDict:
63  \verbatim
64  inner
65  {
66  type union;
67 
68  // Create inner baffles along the length of the pipe
69  #codeDict
70  {
71  code
72  #{
73  const label nBaffles = $<label>blockMeshDict!nBaffles;
74  for (label i = 1; i < nBaffles; i += 2)
75  {
76  const scalar y =
77  scalar(2*i + 1)/(2*nBaffles)
78  *$<scalar>blockMeshDict!height;
79 
80  dict.set
81  (
82  word("baffle" + name(i)),
83  dictionary::entries
84  (
85  "type", "plane",
86  "point", vector(0, y, 0),
87  "normal", vector(0, 1, 0)
88  )
89  );
90  }
91  #};
92  }
93  }
94  \endverbatim
95 
96 See also
97  Foam::functionEntries::codeStream
98 
99 SourceFiles
100  codeDict.C
101 
102 \*---------------------------------------------------------------------------*/
103 
104 #ifndef codeDict_H
105 #define codeDict_H
106 
107 #include "codeStream.H"
108 
109 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
110 
111 namespace Foam
112 {
113 
114 class dlLibraryTable;
115 
116 namespace functionEntries
117 {
118 
119 // Forward declaration of friend classes
120 class streamEntry;
121 class calcEntry;
122 class codeBlockEntry;
123 class codeBlockDictEntry;
124 
125 /*---------------------------------------------------------------------------*\
126  Class codeDict Declaration
127 \*---------------------------------------------------------------------------*/
128 
129 class codeDict
130 :
131  public codeStream
132 {
133  //- Interpreter function type
134  typedef void (*codeDictFunctionType)(dictionary&, Istream& is);
135 
136 
137  // Private Member Functions
138 
139  //- Return the code string
140  static string codeString
141  (
142  const label index,
143  const dictionary& codeDict,
144  Istream&
145  );
146 
147  //- Construct, compile, load and return streaming function
148  codeDictFunctionType getFunction
149  (
150  const dictionary& contextDict,
151  const dictionary& codeDict
152  );
153 
154  //- Compile and execute the code to manipulate the contextDict
155  bool resultStream(dictionary& contextDict, Istream& is);
156 
157 
158 public:
159 
160  // Static Data Members
161 
162  //- Name of the code options file to be used
163  static const word codeOptions;
164 
165  //- Name of the C code template to be used
166  static const wordList compileFiles;
167 
168 
169  // Related types
170 
171  //- Declare friendship with the codeBlockEntry class
172  friend class codeBlockEntry;
173  friend class codeBlockDictEntry;
174 
175 
176  //- Runtime type information
177  FunctionTypeName("#codeDict");
178 
179 
180  // Constructors
181 
182  //- Construct from line number, dictionary and Istream
183  codeDict
184  (
185  const label lineNumber,
186  const dictionary& parentDict,
187  Istream& is
188  );
189 
190  //- Copy construct
191  codeDict(const codeDict&) = default;
192 
193  //- Clone
194  virtual autoPtr<entry> clone(const dictionary&) const
195  {
196  return autoPtr<entry>(new codeDict(*this));
197  }
198 
199 
200  // Member Functions
201 
202  //- Expand the functionEntry into the contextDict
203  virtual bool execute(dictionary& contextDict, Istream&);
204 
205 
206  // Member Operators
207 
208  //- Disallow default bitwise assignment
209  void operator=(const codeDict&) = delete;
210 };
211 
212 
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 
215 } // End namespace functionEntries
216 } // End namespace Foam
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
220 #endif
221 
222 // ************************************************************************* //
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
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition: entry.C:56
Part of the #codeBlock...#codeBlock clause.
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
FunctionTypeName("#codeDict")
Runtime type information.
static const wordList compileFiles
Name of the C code template to be used.
Definition: codeDict.H:165
void operator=(const codeDict &)=delete
Disallow default bitwise assignment.
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 class for handling words, derived from string.
Definition: word.H:63
Namespace for OpenFOAM.
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