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-------------------------------------------------------------------------------
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>
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
148template<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{
160 (
161 entryName,
162 dict.findEntry(entryName, keyType::LITERAL),
163 dict,
164 redirectType,
165 obrPtr,
166 mandatory
167 );
168}
170
171template<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{
184 (
185 entryName,
186 dict.findCompat(entryName, compat, keyType::LITERAL),
187 dict,
188 redirectType,
189 obrPtr,
190 mandatory
191 );
192}
193
194
195template<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
209template<class Type>
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
224template<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,
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// ************************************************************************* //
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:96
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
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
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:79
Registry of regIOobjects.
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
const T & cref() const
Definition: refPtrI.H:189
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#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:215
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict