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 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:49
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
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 ))
Uses dynamic compilation to provide calculating functionality for entering dictionary entries...
Definition: calcEntry.H:68
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
messageStream Info
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Namespace for OpenFOAM.
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:57
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: calcEntry.C:108
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
defineTypeNameAndDebug(calcEntry, 0)
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:63
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
Output to memory buffer stream.
Definition: OStringStream.H:49
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:739
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
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.
Macros for easy insertion into member function selection tables.