Function1New.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "Constant.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const word& entryName,
37  const dictionary& dict,
38  const word& redirectType
39 )
40 {
41  word modelType(redirectType);
42 
43  const entry* eptr = dict.findEntry(entryName, keyType::LITERAL);
44 
45  if (!eptr)
46  {
47  if (modelType.empty())
48  {
50  << "No Function1 dictionary entry: "
51  << entryName << nl << nl
52  << exit(FatalIOError);
53  }
54  }
55  else if (eptr->isDict())
56  {
57  const dictionary& coeffsDict = eptr->dict();
58 
59  coeffsDict.readEntry
60  (
61  "type",
62  modelType,
63  keyType::LITERAL,
64  redirectType.empty() // mandatory when redirectType is empty
65  );
66 
67  auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
68 
69  if (!cstrIter.found())
70  {
72  << "Unknown Function1 type "
73  << modelType << " for " << entryName
74  << "\n\nValid Function1 types :\n"
75  << dictionaryConstructorTablePtr_->sortedToc() << nl
76  << exit(FatalIOError);
77  }
78 
79  return cstrIter()(entryName, coeffsDict);
80  }
81  else
82  {
83  Istream& is = eptr->stream();
84 
85  token firstToken(is);
86 
87  if (!firstToken.isWord())
88  {
89  is.putBack(firstToken);
91  (
92  new Function1Types::Constant<Type>(entryName, is)
93  );
94  }
95 
96  modelType = firstToken.wordToken();
97  }
98 
99 
100  auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
101 
102  if (!cstrIter.found())
103  {
105  << "Unknown Function1 type "
106  << modelType << " for " << entryName
107  << "\n\nValid Function1 types :\n"
108  << dictionaryConstructorTablePtr_->sortedToc() << nl
109  << exit(FatalIOError);
110  }
111 
112  return cstrIter()
113  (
114  entryName,
115  dict.optionalSubDict(entryName + "Coeffs")
116  );
117 }
118 
119 
120 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::entry::isDict
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:222
Foam::Function1Types::Constant
Templated function that returns a constant value.
Definition: Constant.H:59
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::dictionary::readEntry
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:314
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::token::isWord
bool isWord() const
Token is WORD or DIRECTIVE word.
Definition: tokenI.H:573
Foam::entry::dict
virtual const dictionary & dict() const =0
Return dictionary, if entry is a dictionary.
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:589
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
Constant.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:392
Foam::Function1::New
static autoPtr< Function1< Type > > New(const word &entryName, const dictionary &dict, const word &redirectType=word::null)
Selector.
Definition: Function1New.C:35