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 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::functionEntries::codeStream
29 
30 Description
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 
82 Note
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 
90 SourceFiles
91  codeStream.C
92 
93 \*---------------------------------------------------------------------------*/
94 
95 #ifndef functionEntries_codeStream_H
96 #define functionEntries_codeStream_H
97 
98 #include "functionEntry.H"
99 
100 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
101 
102 namespace Foam
103 {
104 
105 // Forward Declarations
106 class dlLibraryTable;
107 
108 namespace functionEntries
109 {
110 
111 // Forward Declarations
112 class calcEntry;
113 
114 /*---------------------------------------------------------------------------*\
115  Class codeStream Declaration
116 \*---------------------------------------------------------------------------*/
117 
118 class codeStream
119 :
120  public functionEntry
121 {
122 protected:
123 
124  //- Interpreter function type
125  typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
126 
127 
128  // Protected Member Functions
129 
130  //- Helper: access IOobject for master-only-reading functionality
131  static bool doingMasterOnlyReading(const dictionary& dict);
132 
133  //- Helper function: access to dlLibraryTable of Time
134  static dlLibraryTable& libs(const dictionary& dict);
135 
136  //- Construct, compile, load and return streaming function
138  (
139  const dictionary& parentDict,
140  const dictionary& codeDict
141  );
142 
143 
144  //- Evaluate dynamically compiled code, returning result as string
145  static string evaluate(const dictionary& parentDict, Istream& is);
146 
147 
148 public:
149 
150  //- Name of the C code template to be used
151  static constexpr const char* const codeTemplateC = "codeStreamTemplate.C";
152 
153  //- Runtime type information
154  ClassName("codeStream");
155 
156 
157  // Member Functions
158 
159  //- Execute in a primitiveEntry context
160  static bool execute
161  (
162  const dictionary& parentDict,
164  Istream& is
165  );
166 
167  //- Execute in a sub-dict context
168  static bool execute(dictionary& parentDict, Istream& is);
169 };
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 } // End namespace functionEntries
175 } // End namespace Foam
176 
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
178 
179 #endif
180 
181 // ************************************************************************* //
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static bool doingMasterOnlyReading(const dictionary &dict)
Helper: access IOobject for master-only-reading functionality.
Definition: codeStream.C:70
static streamingFunctionType getFunction(const dictionary &parentDict, const dictionary &codeDict)
Construct, compile, load and return streaming function.
Definition: codeStream.C:95
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static string evaluate(const dictionary &parentDict, Istream &is)
Evaluate dynamically compiled code, returning result as string.
Definition: codeStream.C:255
void(* streamingFunctionType)(Ostream &, const dictionary &)
Interpreter function type.
Definition: codeStream.H:124
static constexpr const char *const codeTemplateC
Name of the C code template to be used.
Definition: codeStream.H:160
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read...
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself...
Definition: codeStream.H:115
static bool execute(const dictionary &parentDict, primitiveEntry &entry, Istream &is)
Execute in a primitiveEntry context.
Definition: codeStream.C:287
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:63
A table of dynamically loaded libraries.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual const dictionary & dict() const
This entry is not a dictionary, calling this function generates a FatalError.
static dlLibraryTable & libs(const dictionary &dict)
Helper function: access to dlLibraryTable of Time.
Definition: codeStream.C:61
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
ClassName("codeStream")
Runtime type information.