functionEntry.C
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-2018 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 "functionEntry.H"
27 #include "IOstreams.H"
28 #include "ISstream.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
35  (
36  functionEntry,
37  execute,
38  dictionaryIstream
39  );
40 
42  (
43  functionEntry,
44  execute,
45  primitiveEntryIstream
46  );
47 }
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 Foam::token Foam::functionEntry::readLine(const word& key, Istream& is)
53 {
54  string s;
55  dynamic_cast<ISstream&>(is).getLine(s);
56 
57  return token(string(key+s), is.lineNumber());
58 }
59 
60 
61 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
62 
63 Foam::functionEntry::functionEntry
64 (
65  const word& key,
66  const dictionary& dict,
67  Istream& is
68 )
69 :
71  (
72  word(key+dict.name()+Foam::name(is.lineNumber())),
73  readLine(key, is)
74  )
75 {}
76 
77 
78 // * * * * * * * * * * * * Member Function Selectors * * * * * * * * * * * * //
79 
81 (
82  const word& functionName,
83  dictionary& parentDict,
84  Istream& is
85 )
86 {
87  is.fatalCheck
88  (
89  "functionEntry::execute"
90  "(const word& functionName, dictionary& parentDict, Istream&)"
91  );
92 
93  if (!executedictionaryIstreamMemberFunctionTablePtr_)
94  {
95  cerr<< "functionEntry::execute"
96  << "(const word&, dictionary&, Istream&)"
97  << " not yet initialized, function = "
98  << functionName.c_str() << std::endl;
99 
100  // Return true to keep reading
101  return true;
102  }
103 
104  executedictionaryIstreamMemberFunctionTable::iterator mfIter =
105  executedictionaryIstreamMemberFunctionTablePtr_->find(functionName);
106 
107  if (mfIter == executedictionaryIstreamMemberFunctionTablePtr_->end())
108  {
110  << "Unknown functionEntry '" << functionName
111  << "' in " << is.name() << " near line " << is.lineNumber()
112  << nl << nl
113  << "Valid functionEntries are :" << endl
114  << executedictionaryIstreamMemberFunctionTablePtr_->toc()
115  << exit(FatalError);
116  }
117 
118  return mfIter()(parentDict, is);
119 }
120 
121 
123 (
124  const word& functionName,
125  const dictionary& parentDict,
126  primitiveEntry& entry,
127  Istream& is
128 )
129 {
130  is.fatalCheck
131  (
132  "functionEntry::execute"
133  "(const word&, const dictionary&, primitiveEntry&, Istream&)"
134  );
135 
136  if (!executeprimitiveEntryIstreamMemberFunctionTablePtr_)
137  {
138  cerr<< "functionEntry::execute"
139  << "(const word&, const dictionary&, primitiveEntry&, Istream&)"
140  << " not yet initialized, function = "
141  << functionName.c_str() << std::endl;
142 
143  // return true to keep reading anyhow
144  return true;
145  }
146 
147  executeprimitiveEntryIstreamMemberFunctionTable::iterator mfIter =
148  executeprimitiveEntryIstreamMemberFunctionTablePtr_->find(functionName);
149 
150  if (mfIter == executeprimitiveEntryIstreamMemberFunctionTablePtr_->end())
151  {
153  << "Unknown functionEntry '" << functionName
154  << "' in " << is.name() << " near line " << is.lineNumber()
155  << nl << nl
156  << "Valid functionEntries are :" << endl
157  << executeprimitiveEntryIstreamMemberFunctionTablePtr_->toc()
158  << exit(FatalError);
159  }
160 
161  return mfIter()(parentDict, entry, is);
162 }
163 
164 
166 {
167  // Contents should be single string token
168  const token& t = operator[](0);
169  const string& s = t.stringToken();
170 
171  for (size_t i = 0; i < s.size(); i++)
172  {
173  os.write(s[i]);
174  }
175 
176  os << endl;
177 }
178 
179 
180 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:69
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
virtual void write(Ostream &) const
Write.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
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))
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
static bool execute(const word &functionName, dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: functionEntry.C:81
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
const string & stringToken() const
Definition: tokenI.H:258
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual Ostream & write(const token &)=0
Write next token to stream.
defineMemberFunctionSelectionTable(edgeMesh, write, fileExtension)
Namespace for OpenFOAM.