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-2020 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 directory
81  with a subdirectory name corresponding to the SHA1 of the contents.
82 
83  The corresponding library code is located under the local
84  \c codeStream/platforms/$WM_OPTIONS/lib directory in a library
85  \c libcodeStream_SHA1.so
86 
87 SourceFiles
88  codeStream.C
89 
90 \*---------------------------------------------------------------------------*/
91 
92 #ifndef codeStream_H
93 #define codeStream_H
94 
95 #include "functionEntry.H"
96 
97 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
98 
99 namespace Foam
100 {
101 class dlLibraryTable;
102 
103 namespace functionEntries
104 {
105 
106 // Forward declaration of friend classes
107 class calcEntry;
108 
109 /*---------------------------------------------------------------------------*\
110  Class codeStream Declaration
111 \*---------------------------------------------------------------------------*/
113 class codeStream
114 :
115  public functionEntry
116 {
117  //- Interpreter function type
118  typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
119 
120 
121  // Private Member Functions
122 
123  //- Helper function: parent (of parent etc.) of dictionary up to the top
124  static const dictionary& topDict(const dictionary&);
125 
126  //- Return true for master only reading of global dictionary
127  static bool masterOnlyRead(const dictionary& dict);
128 
129  //- Construct, compile, load and return streaming function
130  static streamingFunctionType getFunction
131  (
132  const dictionary& parentDict,
133  const dictionary& codeDict
134  );
135 
136  //- Compile and execute the code and return the resulting string
137  static string run
138  (
139  const dictionary& parentDict,
140  Istream& is
141  );
142 
143 
144 public:
145 
146  // Static Data Members
147 
148  //- Name of the C code template to be used
149  static const word codeTemplateC;
150 
151  // Related types
152 
153  //- Declare friendship with the calcEntry class
154  friend class calcEntry;
155 
156 
157  //- Runtime type information
158  ClassName("codeStream");
159 
160 
161  // Constructors
162 
163  //- Disallow default bitwise copy construction
164  codeStream(const codeStream&) = delete;
165 
166 
167  // Member Functions
168 
169  //- Execute the functionEntry in a sub-dict context
170  static bool execute(dictionary& parentDict, Istream&);
171 
172  //- Execute the functionEntry in a primitiveEntry context
173  static bool execute
174  (
175  const dictionary& parentDict,
177  Istream&
178  );
179 
180 
181  // Member Operators
182 
183  //- Disallow default bitwise assignment
184  void operator=(const codeStream&) = delete;
185 };
186 
187 
188 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
189 
190 } // End namespace functionEntries
191 } // End namespace Foam
192 
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
194 
195 #endif
196 
197 // ************************************************************************* //
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:112
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:148
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.