Loading...
Searching...
No Matches
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-2023 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
33template<class Type>
35Foam::Function1<Type>::New
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 // The "type" entry - mandatory if no redirectType provided
58 coeffs->readEntry
59 (
60 "type",
61 modelType,
62 keyType::LITERAL,
63 (
64 modelType.empty()
65 ? IOobjectOption::MUST_READ : IOobjectOption::READ_IF_PRESENT
66 )
67 );
68
69 // Fallthrough
70 }
71 else if (eptr)
72 {
73 // Primitive entry
74 // - word : the modelType
75 // - non-word : value for constant function
76
78 << "For " << entryName << " with primitive entry" << nl;
79
80 ITstream& is = eptr->stream();
81
82 if (is.peek().isWord())
83 {
84 modelType = is.peek().wordToken();
85 }
86 else
87 {
88 // A value - compatibility for reading constant
89
90 const Type constValue = pTraits<Type>(is);
91
92 return autoPtr<Function1<Type>>
93 (
94 new Function1Types::Constant<Type>
95 (
96 entryName,
97 constValue,
98 obrPtr
99 )
100 );
101 }
102
103 // Fallthrough
104 }
105
106
107 if (modelType.empty())
108 {
109 // Entry missing
110
111 if (mandatory)
112 {
114 << "Missing or invalid Function1 entry: "
115 << entryName << nl
116 << exit(FatalIOError);
117 }
118
119 return nullptr;
120 }
121 else if (!coeffs)
122 {
123 // Primitive entry. Coeffs dictionary is optional.
124
125 const word& kw =
126 (
127 eptr
128 ? eptr->keyword() // Could be a compatibility lookup
129 : entryName
130 );
131
132 coeffs = &dict.optionalSubDict(kw + "Coeffs", keyType::LITERAL);
133 }
134
135
136 auto* ctorPtr = dictionaryConstructorTable(modelType);
137
138 if (!ctorPtr)
139 {
141 << "Unknown Function1 type "
142 << modelType << " for " << entryName
143 << "\n\nValid Function1 types :\n"
144 << dictionaryConstructorTablePtr_->sortedToc() << nl
145 << exit(FatalIOError);
146 }
148 return ctorPtr(entryName, *coeffs, obrPtr);
149}
150
151
152template<class Type>
154Foam::Function1<Type>::New
155(
156 const word& entryName,
157 const dictionary& dict,
158 const word& redirectType,
159 const objectRegistry* obrPtr,
160 const bool mandatory
161)
162{
163 return Function1<Type>::New
164 (
165 entryName,
166 dict.findEntry(entryName, keyType::LITERAL),
167 dict,
168 redirectType,
169 obrPtr,
170 mandatory
171 );
172}
173
174
175template<class Type>
178(
179 const word& entryName,
180 std::initializer_list<std::pair<const char*,int>> compat,
181 const dictionary& dict,
182 const word& redirectType,
183 const objectRegistry* obrPtr,
184 const bool mandatory
185)
186{
187 return Function1<Type>::New
188 (
189 entryName,
190 dict.findCompat(entryName, compat, keyType::LITERAL),
191 dict,
192 redirectType,
193 obrPtr,
194 mandatory
195 );
196}
197
198
199template<class Type>
201Foam::Function1<Type>::New
202(
203 const word& entryName,
204 const dictionary& dict,
205 const objectRegistry* obrPtr,
206 const bool mandatory
207)
209 return Function1<Type>::New(entryName, dict, word::null, obrPtr, mandatory);
211
212
213template<class Type>
216(
217 const word& entryName,
218 const dictionary& dict,
219 const word& redirectType,
220 const objectRegistry* obrPtr
221)
222{
223 // mandatory = false
224 return Function1<Type>::New(entryName, dict, redirectType, obrPtr, false);
225}
226
227
228template<class Type>
231(
232 const word& entryName,
233 const dictionary& dict,
234 const objectRegistry* obrPtr
235)
236{
237 // mandatory = false
238 return Function1<Type>::New(entryName, dict, word::null, obrPtr, false);
239}
240
241
242template<class Type>
244Foam::Function1<Type>::New
245(
247
248 const word& entryName,
249 const dictionary& dict,
250 enum keyType::option matchOpt,
251 const objectRegistry* obrPtr,
252 const bool mandatory
253)
254{
255 // Use the dictionary to find the keyword (allowing wildcards).
256 // Alternative would be to have
257 // a HashTable where the key type uses a wildcard match
258
259
260 refPtr<Function1<Type>> fref; // return value
261
262 // Try for direct cache hit
263 fref.cref(cache.get(entryName));
264
265 if (fref)
266 {
267 return fref;
268 }
269
270
271 // Lookup from dictionary
272 const entry* eptr = dict.findEntry(entryName, matchOpt);
273
274 if (eptr)
275 {
276 // Use keyword (potentially a wildcard) instead of entry name
277 const auto& kw = eptr->keyword();
278
279 // Try for a cache hit
280 fref.cref(cache.get(kw));
281
282 if (!fref)
283 {
284 // Create new entry
285 auto fauto
286 (
287 Function1<Type>::New
288 (
289 kw,
290 eptr, // Already resolved
291 dict,
292 word::null,
293 obrPtr,
294 mandatory
295 )
296 );
297
298 if (fauto)
299 {
300 // Cache the newly created function
301 fref.cref(fauto.get());
302 cache.set(kw, fauto);
303 }
304 }
305 }
306
307 if (mandatory && !fref)
308 {
310 << "No match for " << entryName << nl
311 << exit(FatalIOError);
312 }
313
314 return fref;
315}
316
317
343
344// ************************************************************************* //
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition Function1.H:92
static autoPtr< Function1< Type > > NewIfPresent(const word &entryName, const dictionary &dict, const word &redirectType, const objectRegistry *obrPtr=nullptr)
An optional selector, with fallback redirection.
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.
Function1(const word &entryName, const objectRegistry *obrPtr=nullptr)
Construct from entry name.
Definition Function1.C:31
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
const keyType & keyword() const noexcept
Return keyword.
Definition entry.H:231
option
Enumeration for the data type and search/match modes (bitmask).
Definition keyType.H:80
@ LITERAL
String literal.
Definition keyType.H:82
Registry of regIOobjects.
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition refPtrI.H:216
A class for handling words, derived from Foam::string.
Definition word.H:66
static const word null
An empty word.
Definition word.H:84
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define DebugInFunction
Report an information message using Foam::Info.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict