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-2021 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 objectRegistry* obrPtr,
42  const bool mandatory
43 )
44 {
45  word modelType(redirectType);
46 
47  const dictionary* coeffs = (eptr ? eptr->dictPtr() : nullptr);
48 
49  if (coeffs)
50  {
51  // Dictionary entry
52 
54  << "For " << entryName << " with dictionary entries: "
55  << flatOutput(coeffs->toc()) << nl;
56 
57  coeffs->readEntry
58  (
59  "type",
60  modelType,
61  keyType::LITERAL,
62  modelType.empty() // "type" entry is mandatory if no 'redirect'
63  );
64 
65  // Fallthrough
66  }
67  else if (eptr)
68  {
69  // Primitive entry
70  // - word : the modelType
71  // - non-word : value for constant function
72 
74  << "For " << entryName << " with primitive entry" << nl;
75 
76  ITstream& is = eptr->stream();
77 
78  if (is.peek().isWord())
79  {
80  modelType = is.peek().wordToken();
81  }
82  else
83  {
84  // A value - compatibility for reading constant
85 
86  const Type constValue = pTraits<Type>(is);
87 
88  return autoPtr<Function1<Type>>
89  (
90  new Function1Types::Constant<Type>
91  (
92  entryName,
93  constValue,
94  obrPtr
95  )
96  );
97  }
98 
99  // Fallthrough
100  }
101 
102 
103  if (modelType.empty())
104  {
105  // Entry missing
106 
107  if (mandatory)
108  {
110  << "Missing or invalid Function1 entry: "
111  << entryName << nl
112  << exit(FatalIOError);
113  }
114 
115  return nullptr;
116  }
117  else if (!coeffs)
118  {
119  // Primitive entry. Coeffs dictionary is optional.
120 
121  const word& kw =
122  (
123  eptr
124  ? eptr->keyword() // Could be a compatibility lookup
125  : entryName
126  );
127 
128  coeffs = &dict.optionalSubDict(kw + "Coeffs", keyType::LITERAL);
129  }
130 
131 
132  auto* ctorPtr = dictionaryConstructorTable(modelType);
133 
134  if (!ctorPtr)
135  {
137  << "Unknown Function1 type "
138  << modelType << " for " << entryName
139  << "\n\nValid Function1 types :\n"
140  << dictionaryConstructorTablePtr_->sortedToc() << nl
141  << exit(FatalIOError);
142  }
143 
144  return ctorPtr(entryName, *coeffs, obrPtr);
145 }
146 
147 
148 template<class Type>
151 (
152  const word& entryName,
153  const dictionary& dict,
154  const word& redirectType,
155  const objectRegistry* obrPtr,
156  const bool mandatory
157 )
158 {
159  return Function1<Type>::New
160  (
161  entryName,
162  dict.findEntry(entryName, keyType::LITERAL),
163  dict,
164  redirectType,
165  obrPtr,
166  mandatory
167  );
168 }
169 
170 
171 template<class Type>
174 (
175  const word& entryName,
176  std::initializer_list<std::pair<const char*,int>> compat,
177  const dictionary& dict,
178  const word& redirectType,
179  const objectRegistry* obrPtr,
180  const bool mandatory
181 )
182 {
183  return Function1<Type>::New
184  (
185  entryName,
186  dict.findCompat(entryName, compat, keyType::LITERAL),
187  dict,
188  redirectType,
189  obrPtr,
190  mandatory
191  );
192 }
193 
194 
195 template<class Type>
198 (
199  const word& entryName,
200  const dictionary& dict,
201  const objectRegistry* obrPtr,
202  const bool mandatory
203 )
204 {
205  return Function1<Type>::New(entryName, dict, word::null, obrPtr, mandatory);
206 }
207 
208 
209 template<class Type>
212 (
213  const word& entryName,
214  const dictionary& dict,
215  const word& redirectType,
216  const objectRegistry* obrPtr
217 )
218 {
219  // mandatory = false
220  return Function1<Type>::New(entryName, dict, redirectType, obrPtr, false);
221 }
222 
223 
224 template<class Type>
227 (
229 
230  const word& entryName,
231  const dictionary& dict,
232  enum keyType::option matchOpt,
233  const objectRegistry* obrPtr,
234  const bool mandatory
235 )
236 {
237  // Use the dictionary to find the keyword (allowing wildcards).
238  // Alternative would be to have
239  // a HashTable where the key type uses a wildcard match
240 
241 
242  refPtr<Function1<Type>> fref; // return value
243 
244  // Try for direct cache hit
245  fref.cref(cache.get(entryName));
246 
247  if (fref)
248  {
249  return fref;
250  }
251 
252 
253  // Lookup from dictionary
254  const entry* eptr = dict.findEntry(entryName, matchOpt);
255 
256  if (eptr)
257  {
258  // Use keyword (potentially a wildcard) instead of entry name
259  const auto& kw = eptr->keyword();
260 
261  // Try for a cache hit
262  fref.cref(cache.get(kw));
263 
264  if (!fref)
265  {
266  // Create new entry
267  auto fauto
268  (
270  (
271  kw,
272  eptr, // Already resolved
273  dict,
274  word::null,
275  obrPtr,
276  mandatory
277  )
278  );
279 
280  if (fauto)
281  {
282  // Cache the newly created function
283  fref.cref(fauto.get());
284  cache.set(kw, fauto);
285  }
286  }
287  }
288 
289  if (mandatory && !fref)
290  {
292  << "No match for " << entryName << nl
293  << exit(FatalIOError);
294  }
295 
296  return fref;
297 }
298 
299 
325 
326 // ************************************************************************* //
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:65
Foam::entry::keyword
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
Foam::FatalIOError
IOerror FatalIOError
Foam::Function1::NewIfPresent
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType=word::null, const objectRegistry *obrPtr=nullptr)
An optional selector.
Definition: Function1New.C:212
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::refPtr::cref
const T & cref() const
Definition: refPtrI.H:189
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:388
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:123
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
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::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:404
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
Constant.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
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 objectRegistry *obrPtr=nullptr, const bool mandatory=true)
Compatibility selector, with fallback redirection.
Definition: Function1New.C:174
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:78
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60