dictionaryTemplates.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) 2017-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 "dictionary.H"
30#include "primitiveEntry.H"
31
32// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33
34template<class T>
35void Foam::dictionary::reportDefault
36(
37 const word& keyword,
38 const T& deflt,
39 const bool added
40) const
41{
43 {
44 FatalIOError(dictionary::executableName(), *this)
45 << "No optional entry: " << keyword
46 << " Default: " << deflt << nl
48 }
49
50 OSstream& os = InfoErr.stream(reportingOutput.get());
51
52 // Tag with "-- " prefix to make the message stand out
53 os << "-- Executable: "
54 << dictionary::executableName()
55 << " Dictionary: ";
56
57 // Double-quote dictionary and entry for more reliably parsing,
58 // especially if the keyword contains regular expressions.
59
60 if (this->isNullDict())
61 {
62 // Output as "", but could have "(null)" etc
64 }
65 else
66 {
67 os.writeQuoted(this->relativeName(), true);
68 }
69
70 os << " Entry: ";
71 os.writeQuoted(keyword, true);
72 os << " Default: " << deflt;
73
74 if (added)
75 {
76 os << " Added: true";
77 }
78 os << nl;
79}
80
81
82// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
83
84template<class Compare>
86{
87 return hashedEntries_.sortedToc(comp);
88}
89
90
91template<class T>
92Foam::entry* Foam::dictionary::add(const keyType& k, const T& v, bool overwrite)
93{
94 return add(new primitiveEntry(k, v), overwrite);
95}
96
97
98template<class T>
100{
101 return set(new primitiveEntry(k, v));
102}
103
104
105template<class T>
107(
108 const word& keyword,
109 enum keyType::option matchOpt
110) const
111{
112 T val;
113 readEntry<T>(keyword, val, matchOpt);
114 return val;
115}
116
117
118template<class T, class Predicate>
120(
121 const word& keyword,
122 const Predicate& pred,
123 enum keyType::option matchOpt
124) const
125{
126 T val;
127 readCheck<T, Predicate>(keyword, val, pred, matchOpt);
128 return val;
129}
130
131
132template<class T>
134(
135 const word& keyword,
136 std::initializer_list<std::pair<const char*,int>> compat,
137 enum keyType::option matchOpt
138) const
139{
140 T val;
141 readCompat<T>(keyword, compat, val, matchOpt);
142 return val;
143}
144
145
146template<class T>
148(
149 const word& keyword,
150 const T& deflt,
151 enum keyType::option matchOpt
152) const
153{
154 const const_searcher finder(csearch(keyword, matchOpt));
155
156 if (finder.good())
157 {
158 T val;
159
160 ITstream& is = finder.ptr()->stream();
161 is >> val;
162
163 checkITstream(is, keyword);
164
165 return val;
166 }
167 else if (writeOptionalEntries)
168 {
169 reportDefault(keyword, deflt);
170 }
171
172 return deflt;
173}
174
175
176template<class T>
178(
179 const word& keyword,
180 const T& deflt,
181 enum keyType::option matchOpt
182)
183{
184 const const_searcher finder(csearch(keyword, matchOpt));
185
186 if (finder.good())
187 {
188 T val;
189
190 ITstream& is = finder.ptr()->stream();
191 is >> val;
192
193 checkITstream(is, keyword);
194
195 return val;
196 }
197 else if (writeOptionalEntries)
198 {
199 reportDefault(keyword, deflt, true); // Added
200 }
201
202 add(new primitiveEntry(keyword, deflt));
203 return deflt;
204}
205
206
207template<class T, class Predicate>
209(
210 const word& keyword,
211 const T& deflt,
212 const Predicate& pred,
213 enum keyType::option matchOpt
214) const
215{
216 #ifdef FULLDEBUG
217 if (!pred(deflt))
218 {
220 << "Entry '" << keyword << "' with invalid default in dictionary "
221 << name()
222 << exit(FatalIOError);
223 }
224 #endif
225
226 const const_searcher finder(csearch(keyword, matchOpt));
227
228 if (finder.good())
229 {
230 T val;
231
232 ITstream& is = finder.ptr()->stream();
233 is >> val;
234
235 checkITstream(is, keyword);
236
237 if (!pred(val))
238 {
239 raiseBadInput(is, keyword);
240 }
241
242 return val;
243 }
244 else if (writeOptionalEntries)
245 {
246 reportDefault(keyword, deflt);
247 }
248
249 return deflt;
250}
251
252
253template<class T, class Predicate>
255(
256 const word& keyword,
257 const T& deflt,
258 const Predicate& pred,
259 enum keyType::option matchOpt
260)
261{
262 #ifdef FULLDEBUG
263 if (!pred(deflt))
264 {
266 << "Entry '" << keyword << "' with invalid default in dictionary "
267 << name()
268 << exit(FatalIOError);
269 }
270 #endif
271
272 const const_searcher finder(csearch(keyword, matchOpt));
273
274 if (finder.good())
275 {
276 T val;
277
278 ITstream& is = finder.ptr()->stream();
279 is >> val;
280
281 checkITstream(is, keyword);
282
283 if (!pred(val))
284 {
285 raiseBadInput(is, keyword);
286 }
287
288 return val;
289 }
290 else if (writeOptionalEntries)
291 {
292 reportDefault(keyword, deflt, true); // Added
293 }
294
295 add(new primitiveEntry(keyword, deflt));
296 return deflt;
297}
298
299
300template<class T>
302(
303 const word& keyword,
304 T& val,
305 enum keyType::option matchOpt,
306 bool mandatory
307) const
308{
309 const const_searcher finder(csearch(keyword, matchOpt));
310
311 if (finder.good())
312 {
313 ITstream& is = finder.ptr()->stream();
314 is >> val;
315
316 checkITstream(is, keyword);
317
318 return true;
319 }
320 else if (mandatory)
321 {
323 << "Entry '" << keyword << "' not found in dictionary "
324 << name() << nl
325 << exit(FatalIOError);
326 }
327
328 return false;
329}
330
331
332template<class T, class Predicate>
334(
335 const word& keyword,
336 T& val,
337 const Predicate& pred,
338 enum keyType::option matchOpt,
339 bool mandatory
340) const
341{
342 const const_searcher finder(csearch(keyword, matchOpt));
343
344 if (finder.good())
345 {
346 ITstream& is = finder.ptr()->stream();
347 is >> val;
348
349 checkITstream(is, keyword);
350
351 if (!pred(val))
352 {
353 raiseBadInput(is, keyword);
354 }
355
356 return true;
357 }
358 else if (mandatory)
359 {
361 << "Entry '" << keyword << "' not found in dictionary "
362 << name() << nl
363 << exit(FatalIOError);
364 }
365
366 return false;
367}
368
369
370template<class T>
372(
373 const word& keyword,
374 std::initializer_list<std::pair<const char*,int>> compat,
375 T& val,
376 enum keyType::option matchOpt,
377 bool mandatory
378) const
379{
380 const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
381
382 if (finder.good())
383 {
384 ITstream& is = finder.ptr()->stream();
385 is >> val;
386
387 checkITstream(is, keyword);
388
389 return true;
390 }
391 else if (mandatory)
392 {
394 << "Entry '" << keyword << "' not found in dictionary "
395 << name() << nl
396 << exit(FatalIOError);
397 }
398
399 return false;
400}
401
402
403template<class T>
405(
406 const word& keyword,
407 T& val,
408 enum keyType::option matchOpt
409) const
410{
411 // Read is non-mandatory
412 return readEntry<T>(keyword, val, matchOpt, false);
413}
414
415
416template<class T, class Predicate>
418(
419 const word& keyword,
420 T& val,
421 const Predicate& pred,
422 enum keyType::option matchOpt
423) const
424{
425 // Read is non-mandatory
426 return readCheck<T, Predicate>(keyword, val, pred, matchOpt, false);
427}
428
429
430template<class T>
432(
433 const word& keyword,
434 std::initializer_list<std::pair<const char*,int>> compat,
435 const T& deflt,
436 enum keyType::option matchOpt
437) const
438{
439 const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
440
441 if (finder.good())
442 {
443 T val;
444
445 ITstream& is = finder.ptr()->stream();
446 is >> val;
447
448 checkITstream(is, keyword);
449
450 return val;
451 }
452 else if (writeOptionalEntries)
453 {
454 reportDefault(keyword, deflt);
455 }
456
457 return deflt;
458}
459
460
461template<class T>
463(
464 const word& keyword,
465 std::initializer_list<std::pair<const char*,int>> compat,
466 T& val,
467 enum keyType::option matchOpt
468) const
469{
470 // Read is non-mandatory
471 return readCompat<T>(keyword, compat, val, matchOpt, false);
472}
473
474
475// ************************************************************************* //
label k
An input stream of tokens.
Definition: ITstream.H:56
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OBJstream.C:108
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
Generic const/non-const dictionary entry searcher.
Definition: dictionary.H:141
bool good() const noexcept
True if entry was found.
Definition: dictionary.H:201
pointer ptr() const noexcept
A pointer to the entry (nullptr if not found)
Definition: dictionary.H:219
bool isNullDict() const noexcept
The dictionary is actually dictionary::null (root dictionary)
Definition: dictionaryI.H:74
bool readIfPresentCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, T &val, enum keyType::option matchOpt=keyType::REGEX) const
static refPtr< OSstream > reportingOutput
Output location when reporting default values.
Definition: dictionary.H:397
static int writeOptionalEntries
Report optional keywords and values if not present in dictionary.
Definition: dictionary.H:391
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
T getCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, enum keyType::option matchOpt=keyType::REGEX) const
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
bool readCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
T getOrDefaultCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
T getCheck(const word &keyword, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
T getCheckOrDefault(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
wordList sortedToc() const
Return the sorted table of contents.
Definition: dictionary.C:616
T getOrAdd(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX)
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
bool readCheckIfPresent(const word &keyword, T &val, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
T getCheckOrAdd(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX)
bool readCheck(const word &keyword, T &val, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
Sums a given list of (at least two or more) fields and outputs the result into a new field,...
Definition: add.H:161
A class for handling keywords in dictionaries.
Definition: keyType.H:71
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:79
OSstream & stream(OSstream *alternative=nullptr)
Definition: messageStream.C:71
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
@ DQUOTE
Double quote.
Definition: token.H:135
bool set() const
Are all the vector set.
Definition: triadI.H:76
A class for handling words, derived from Foam::string.
Definition: word.H:68
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
IOerror FatalIOError
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
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