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 | Copyright (C) 2011-2013 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  fld.writeEntry("", os);
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 
118  //- Interpreter function type
119  typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
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  //- Helper function: access to dlLibraryTable of Time
127  static dlLibraryTable& libs(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 
137  //- Disallow default bitwise copy construct
138  codeStream(const codeStream&);
139 
140  //- Disallow default bitwise assignment
141  void operator=(const codeStream&);
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  // Member Functions
162 
163  //- Execute the functionEntry in a sub-dict context
164  static bool execute(dictionary& parentDict, Istream&);
165 
166  //- Execute the functionEntry in a primitiveEntry context
167  static bool execute
168  (
169  const dictionary& parentDict,
171  Istream&
172  );
173 
174 };
175 
176 
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
178 
179 } // End namespace functionEntries
180 } // End namespace Foam
181 
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
183 
184 #endif
185 
186 // ************************************************************************* //
dictionary dict
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
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:68
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
A table of dynamically loaded libraries.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: codeStream.C:359
Namespace for OpenFOAM.
ClassName("codeStream")
Runtime type information.