codeStream.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::functionEntries::codeStream
29
30Description
31 Dictionary entry that contains C++ OpenFOAM code that is compiled to
32 generate the entry itself. So
33 - codeStream reads three entries: 'code', 'codeInclude' (optional),
34 'codeOptions' (optional)
35 and uses those to generate library sources inside \c codeStream/
36 - these get compiled using 'wmake libso'
37 - the resulting library is loaded in executed with as arguments
38 \code
39 (const dictionary& dict, Ostream& os)
40 \endcode
41 where the dictionary is the current dictionary.
42 - the code has to write into Ostream which is then used to construct
43 the actual dictionary entry.
44
45
46 E.g. to set the internal field of a field:
47
48 \verbatim
49 internalField #codeStream
50 {
51 code
52 #{
53 const IOdictionary& d = static_cast<const IOdictionary&>(dict);
54 const fvMesh& mesh = refCast<const fvMesh>(d.db());
55 scalarField fld(mesh.nCells(), 12.34);
56 fld.writeEntry("", os);
57 #};
58
59 //- Optional:
60 codeInclude
61 #{
62 #include "fvCFD.H"
63 #};
64
65 //- Optional:
66 codeOptions
67 #{
68 -I$(LIB_SRC)/finiteVolume/lnInclude
69 #};
70 };
71 \endverbatim
72
73
74 Note the \c \#{ ... \c \#} syntax is a 'verbatim' input mode that allows
75 inputting strings with embedded newlines.
76
77 Limitations:
78 - '~' symbol not allowed inside the code sections.
79 - probably some other limitations (uses string::expand which expands
80 \c \$ and \c ~ sequences)
81
82Note
83 The code to be compiled is stored under the local \c codeStream directory
84 with a subdirectory name corresponding to the SHA1 of the contents.
85
86 The corresponding library code is located under the local
87 \c codeStream/platforms/$WM_OPTIONS/lib directory in a library
88 \c libcodeStream_SHA1.so
89
90SourceFiles
91 codeStream.C
92
93\*---------------------------------------------------------------------------*/
94
95#ifndef functionEntries_codeStream_H
96#define functionEntries_codeStream_H
97
98#include "functionEntry.H"
99
100// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
101
102namespace Foam
103{
104// Forward Declarations
105class dlLibraryTable;
106
107namespace functionEntries
108{
109
110// Forward Declarations
111class calcEntry;
112
113/*---------------------------------------------------------------------------*\
114 Class codeStream Declaration
115\*---------------------------------------------------------------------------*/
117class codeStream
118:
119 public functionEntry
120{
121protected:
122
123 //- Interpreter function type
124 typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
125
126
127 // Protected Member Functions
128
129 //- Helper: access IOobject for master-only-reading functionality
130 static bool doingMasterOnlyReading(const dictionary& dict);
131
132 //- Helper function: access to dlLibraryTable of Time
133 static dlLibraryTable& libs(const dictionary& dict);
134
135 //- Construct, compile, load and return streaming function
137 (
138 const dictionary& parentDict,
139 const dictionary& codeDict
140 );
141
142
143 //- Evaluate dynamically compiled code, returning result as string
144 static string evaluate(const dictionary& parentDict, Istream& is);
145
146
147public:
148
149 //- Name of the C code template to be used
150 static constexpr const char* const codeTemplateC = "codeStreamTemplate.C";
151
152 //- Runtime type information
153 ClassName("codeStream");
154
155
156 // Member Functions
157
158 //- Execute in a primitiveEntry context
159 static bool execute
160 (
161 const dictionary& parentDict,
163 Istream& is
164 );
165
166 //- Execute in a sub-dict context
167 static bool execute(dictionary& parentDict, Istream& is);
168};
169
170
171// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
172
173} // End namespace functionEntries
174} // End namespace Foam
175
176// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177
178#endif
179
180// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A table of dynamically loaded libraries.
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself....
Definition: codeStream.H:119
ClassName("codeStream")
Runtime type information.
static bool doingMasterOnlyReading(const dictionary &dict)
Helper: access IOobject for master-only-reading functionality.
Definition: codeStream.C:77
static streamingFunctionType getFunction(const dictionary &parentDict, const dictionary &codeDict)
Construct, compile, load and return streaming function.
Definition: codeStream.C:107
static bool execute(const dictionary &parentDict, primitiveEntry &entry, Istream &is)
Execute in a primitiveEntry context.
Definition: codeStream.C:384
void(* streamingFunctionType)(Ostream &, const dictionary &)
Interpreter function type.
Definition: codeStream.H:123
static string evaluate(const dictionary &parentDict, Istream &is)
Evaluate dynamically compiled code, returning result as string.
Definition: codeStream.C:352
static dlLibraryTable & libs(const dictionary &dict)
Helper function: access to dlLibraryTable of Time.
Definition: codeStream.C:68
static constexpr const char *const 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:69
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
virtual const dictionary & dict() const
This entry is not a dictionary,.
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:67
Namespace for OpenFOAM.