calcEntry.C
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 \*---------------------------------------------------------------------------*/
25 
26 #include "calcEntry.H"
28 #include "dictionary.H"
29 #include "dynamicCode.H"
30 #include "codeStream.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionEntries
37 {
39 
41  (
43  calcEntry,
44  execute,
45  dictionaryIstream
46  );
47 
49  (
51  calcEntry,
52  execute,
53  primitiveEntryIstream
54  );
55 
56 }
57 }
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
63 (
64  const dictionary& parentDict,
65  primitiveEntry& thisEntry,
66  Istream& is
67 )
68 {
69  Info<< "Using #calcEntry at line " << is.lineNumber()
70  << " in file " << parentDict.name() << endl;
71 
73  (
74  "functionEntries::calcEntry::execute(..)",
75  parentDict
76  );
77 
78  // Read string
79  string s(is);
80  // Make sure we stop this entry
81  //is.putBack(token(token::END_STATEMENT, is.lineNumber()));
82 
83  // Construct codeDict for codeStream
84  // must reference parent for stringOps::expand to work nicely.
85  dictionary codeSubDict;
86  codeSubDict.add("code", "os << (" + s + ");");
87  dictionary codeDict(parentDict, codeSubDict);
88 
89  codeStream::streamingFunctionType function = codeStream::getFunction
90  (
91  parentDict,
92  codeDict
93  );
94 
95  // use function to write stream
96  OStringStream os(is.format());
97  (*function)(os, parentDict);
98 
99  // get the entry from this stream
100  IStringStream resultStream(os.str());
101  thisEntry.read(parentDict, resultStream);
102 
103  return true;
104 }
105 
106 
108 (
109  dictionary& parentDict,
110  Istream& is
111 )
112 {
113  Info<< "Using #calcEntry at line " << is.lineNumber()
114  << " in file " << parentDict.name() << endl;
115 
117  (
118  "functionEntries::calcEntry::execute(..)",
119  parentDict
120  );
121 
122  // Read string
123  string s(is);
124  // Make sure we stop this entry
125  //is.putBack(token(token::END_STATEMENT, is.lineNumber()));
126 
127  // Construct codeDict for codeStream
128  // must reference parent for stringOps::expand to work nicely.
129  dictionary codeSubDict;
130  codeSubDict.add("code", "os << (" + s + ");");
131  dictionary codeDict(parentDict, codeSubDict);
132 
133  codeStream::streamingFunctionType function = codeStream::getFunction
134  (
135  parentDict,
136  codeDict
137  );
138 
139  // use function to write stream
140  OStringStream os(is.format());
141  (*function)(os, parentDict);
142 
143  // get the entry from this stream
144  IStringStream resultStream(os.str());
145  parentDict.read(resultStream);
146 
147  return true;
148 }
149 
150 
151 // ************************************************************************* //
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Uses dynamic compilation to provide calculating functionality for entering dictionary entries...
Definition: calcEntry.H:68
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:737
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.
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:63
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: calcEntry.C:108
Input from memory buffer stream.
Definition: IStringStream.H:49
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
Macros for easy insertion into member function selection tables.
messageStream Info
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:58
Output to memory buffer stream.
Definition: OStringStream.H:49
defineTypeNameAndDebug(calcEntry, 0)
Namespace for OpenFOAM.