codeStream.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) 2011-2022 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::codeStream
26 
27 Description
28  Dictionary entry that contains C++ OpenFOAM code that is compiled to
29  generate the entry itself. So
30  - codeStream reads three entries: 'code', 'codeInclude' (optional),
31  'codeOptions' (optional)
32  and uses those to generate library sources inside \c codeStream/
33  - these get compiled using 'wmake libso'
34  - the resulting library is loaded in executed with as arguments
35  \code
36  (const dictionary& dict, Ostream& os)
37  \endcode
38  where the dictionary is the current dictionary.
39  - the code has to write into Ostream which is then used to construct
40  the actual dictionary entry.
41 
42 
43  E.g. to set the internal field of a field:
44 
45  \verbatim
46  internalField #codeStream
47  {
48  code
49  #{
50  const IOdictionary& d = static_cast<const IOdictionary&>(dict);
51  const fvMesh& mesh = refCast<const fvMesh>(d.db());
52  scalarField fld(mesh.nCells(), 12.34);
53  writeEntry(os, "", fld);
54  #};
55 
56  //- Optional:
57  codeInclude
58  #{
59  #include "fvCFD.H"
60  #};
61 
62  //- Optional:
63  codeOptions
64  #{
65  -I$(LIB_SRC)/finiteVolume/lnInclude
66  #};
67  };
68  \endverbatim
69 
70 
71  Note the \c \#{ ... \c \#} syntax is a 'verbatim' input mode that allows
72  inputting strings with embedded newlines.
73 
74  Limitations:
75  - '~' symbol not allowed inside the code sections.
76  - probably some other limitations (uses string::expand which expands
77  \c \$ and \c ~ sequences)
78 
79  Note:
80  The code to be compiled is stored under the local \c codeStream
81  directory with a subdirectory name corresponding to the SHA1 of the
82  contents.
83 
84  The corresponding library code is located under the local
85  \c codeStream/platforms/$WM_OPTIONS/lib directory in a library
86  \c libcodeStream_SHA1.so
87 
88 SourceFiles
89  codeStream.C
90 
91 \*---------------------------------------------------------------------------*/
92 
93 #ifndef codeStream_H
94 #define codeStream_H
95 
96 #include "functionEntry.H"
97 
98 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
99 
100 namespace Foam
101 {
102 class dlLibraryTable;
103 
104 namespace functionEntries
105 {
106 
107 // Forward declaration of friend classes
108 class calcEntry;
109 
110 /*---------------------------------------------------------------------------*\
111  Class codeStream Declaration
112 \*---------------------------------------------------------------------------*/
114 class codeStream
115 :
116  public functionEntry
117 {
118  //- Interpreter function type
119  typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
120 
121 
122  // Private Member Functions
123 
124  //- Helper function: parent (of parent etc.) of dictionary up to the top
125  static const dictionary& topDict(const dictionary&);
126 
127  //- Return true for master only reading of global dictionary
128  static bool masterOnlyRead(const dictionary& dict);
129 
130  //- Construct, compile, load and return streaming function
131  static streamingFunctionType getFunction
132  (
133  const dictionary& parentDict,
134  const dictionary& codeDict
135  );
136 
137  //- Compile and execute the code and return the resulting string
138  static string run
139  (
140  const dictionary& parentDict,
141  Istream& is
142  );
143 
144 
145 public:
146 
147  // Static Data Members
148 
149  //- Name of the C code template to be used
150  static const word codeTemplateC;
151 
152  // Related types
153 
154  //- Declare friendship with the calcEntry class
155  friend class calcEntry;
156 
157 
158  //- Runtime type information
159  ClassName("codeStream");
160 
161 
162  // Constructors
163 
164  //- Disallow default bitwise copy construction
165  codeStream(const codeStream&) = delete;
166 
167 
168  // Member Functions
169 
170  //- Execute the functionEntry in a sub-dict context
171  static bool execute(dictionary& parentDict, Istream&);
172 
173  //- Execute the functionEntry in a primitiveEntry context
174  static bool execute
175  (
176  const dictionary& parentDict,
178  Istream&
179  );
180 
181 
182  // Member Operators
183 
184  //- Disallow default bitwise assignment
185  void operator=(const codeStream&) = delete;
186 };
187 
188 
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
190 
191 } // End namespace functionEntries
192 } // End namespace Foam
193 
194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195 
196 #endif
197 
198 // ************************************************************************* //
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Uses dynamic compilation to provide calculating functionality for entering dictionary entries...
Definition: calcEntry.H:85
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself...
Definition: codeStream.H:113
A class for handling words, derived from string.
Definition: word.H:59
static const word codeTemplateC
Name of the C code template to be used.
Definition: codeStream.H:149
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:63
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
const dictionary & dict() const
This entry is not a dictionary,.
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: codeStream.C:351
void operator=(const codeStream &)=delete
Disallow default bitwise assignment.
codeStream(const codeStream &)=delete
Disallow default bitwise copy construction.
Namespace for OpenFOAM.
ClassName("codeStream")
Runtime type information.