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 // Forward Declarations
105 class dlLibraryTable;
106 
107 namespace functionEntries
108 {
109 
110 // Forward Declarations
111 class calcEntry;
112 
113 /*---------------------------------------------------------------------------*\
114  Class codeStream Declaration
115 \*---------------------------------------------------------------------------*/
116 
117 class codeStream
118 :
119  public functionEntry
120 {
121 protected:
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 
147 public:
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::primitiveEntry
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
Definition: primitiveEntry.H:63
Foam::functionEntries::codeStream::evaluate
static string evaluate(const dictionary &parentDict, Istream &is)
Evaluate dynamically compiled code, returning result as string.
Definition: codeStream.C:352
Foam::functionEntries::codeStream::ClassName
ClassName("codeStream")
Runtime type information.
Foam::functionEntries::codeStream::execute
static bool execute(const dictionary &parentDict, primitiveEntry &entry, Istream &is)
Execute in a primitiveEntry context.
Definition: codeStream.C:384
functionEntry.H
Foam::primitiveEntry::dict
virtual const dictionary & dict() const
This entry is not a dictionary,.
Definition: primitiveEntry.C:299
Foam::functionEntries::codeStream::getFunction
static streamingFunctionType getFunction(const dictionary &parentDict, const dictionary &codeDict)
Construct, compile, load and return streaming function.
Definition: codeStream.C:107
Foam::functionEntries::codeStream::streamingFunctionType
void(* streamingFunctionType)(Ostream &, const dictionary &)
Interpreter function type.
Definition: codeStream.H:123
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::functionEntries::codeStream::libs
static dlLibraryTable & libs(const dictionary &dict)
Helper function: access to dlLibraryTable of Time.
Definition: codeStream.C:68
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionEntry
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
Foam::functionEntries::codeStream::doingMasterOnlyReading
static bool doingMasterOnlyReading(const dictionary &dict)
Helper: access IOobject for master-only-reading functionality.
Definition: codeStream.C:77
Foam::functionEntries::codeStream::codeTemplateC
static constexpr const char *const codeTemplateC
Name of the C code template to be used.
Definition: codeStream.H:149
Foam::functionEntries::codeStream
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself....
Definition: codeStream.H:116
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56