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-2020 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 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
36 (
37  const word& entryName,
38  const entry* eptr,
39  const dictionary& dict,
40  const word& redirectType,
41  const bool mandatory
42 )
43 {
44  word modelType(redirectType);
45 
46  const dictionary* coeffs = (eptr ? eptr->dictPtr() : nullptr);
47 
48  if (coeffs)
49  {
50  // Dictionary entry
51 
52  coeffs->readEntry
53  (
54  "type",
55  modelType,
56  keyType::LITERAL,
57  modelType.empty() // "type" entry is mandatory if no 'redirect'
58  );
59 
60  // Fallthrough
61  }
62  else if (eptr)
63  {
64  // Primitive entry
65  // - non-word : value for constant function
66  // - word : the modelType
67 
68  Istream& is = eptr->stream();
69 
70  token firstToken(is);
71 
72  if (!firstToken.isWord())
73  {
74  // A value
75  is.putBack(firstToken);
76 
77  const Type constValue = pTraits<Type>(is);
78 
79  return autoPtr<Function1<Type>>
80  (
81  new Function1Types::Constant<Type>(entryName, constValue)
82  );
83  }
84  else
85  {
86  modelType = firstToken.wordToken();
87  }
88 
89  // Fallthrough
90  }
91 
92 
93  if (modelType.empty())
94  {
95  // Entry missing
96 
97  if (mandatory)
98  {
100  << "Missing or invalid Function1 entry: "
101  << entryName << nl
102  << exit(FatalIOError);
103  }
104 
105  return nullptr;
106  }
107  else if (!coeffs)
108  {
109  // Primitive entry. Coeffs dictionary is optional.
110 
111  const word& kw =
112  (
113  eptr
114  ? eptr->keyword() // Could be a compatibility lookup
115  : entryName
116  );
117 
118  coeffs = &dict.optionalSubDict(kw + "Coeffs", keyType::LITERAL);
119  }
120 
121 
122  auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
123 
124  if (!cstrIter.found())
125  {
127  << "Unknown Function1 type "
128  << modelType << " for " << entryName
129  << "\n\nValid Function1 types :\n"
130  << dictionaryConstructorTablePtr_->sortedToc() << nl
131  << exit(FatalIOError);
132  }
133 
134  return cstrIter()(entryName, *coeffs);
135 }
136 
137 
138 template<class Type>
141 (
142  const word& entryName,
143  const dictionary& dict,
144  const word& redirectType,
145  const bool mandatory
146 )
147 {
148  return Function1<Type>::New
149  (
150  entryName,
151  dict.findEntry(entryName, keyType::LITERAL),
152  dict,
153  redirectType,
154  mandatory
155  );
156 }
157 
158 
159 template<class Type>
162 (
163  const word& entryName,
164  std::initializer_list<std::pair<const char*,int>> compat,
165  const dictionary& dict,
166  const word& redirectType,
167  const bool mandatory
168 )
169 {
170  return Function1<Type>::New
171  (
172  entryName,
173  dict.findCompat(entryName, compat, keyType::LITERAL),
174  dict,
175  redirectType,
176  mandatory
177  );
178 }
179 
180 
181 template<class Type>
184 (
185  const word& entryName,
186  const dictionary& dict,
187  const bool mandatory
188 )
189 {
190  return Function1<Type>::New(entryName, dict, word::null, mandatory);
191 }
192 
193 
194 template<class Type>
197 (
198  const word& entryName,
199  const dictionary& dict,
200  const word& redirectType
201 )
202 {
203  // mandatory = false
204  return Function1<Type>::New(entryName, dict, redirectType, false);
205 }
206 
207 
208 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::FatalIOError
IOerror FatalIOError
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:86
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::Function1::NewIfPresent
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType=word::null)
An optional selector.
Definition: Function1New.C:197
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::Function1::NewCompat
static autoPtr< Function1< Type > > NewCompat(const word &entryName, std::initializer_list< std::pair< const char *, int >> compat, const dictionary &dict, const word &redirectType=word::null, const bool mandatory=true)
Compatibility selector, with fallback redirection.
Definition: Function1New.C:162
Constant.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401