Loading...
Searching...
No Matches
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 entry* eptr = csearch(keyword, matchOpt).ptr();
155
156 if (eptr)
157 {
158 T val;
159
160 ITstream& is = eptr->stream();
161 is >> val;
162
163 checkITstream(is, keyword);
164
165 return val;
166 }
167 else if (writeOptionalEntries)
168 {
169 reportDefault(keyword, deflt);
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 entry* eptr = csearch(keyword, matchOpt).ptr();
185
186 if (eptr)
187 {
188 T val;
189
190 ITstream& is = eptr->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 }
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 entry* eptr = csearch(keyword, matchOpt).ptr();
227
228 if (eptr)
229 {
230 T val;
231
232 ITstream& is = eptr->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);
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 entry* eptr = csearch(keyword, matchOpt).ptr();
273
274 if (eptr)
275 {
276 T val;
277
278 ITstream& is = eptr->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 }
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,
307) const
308{
309 if (readOpt == IOobjectOption::NO_READ)
310 {
311 return false;
312 }
313
314 const entry* eptr = csearch(keyword, matchOpt).ptr();
315
316 if (eptr)
317 {
318 ITstream& is = eptr->stream();
319 is >> val;
320
321 checkITstream(is, keyword);
322
323 return true;
324 }
325 else if (IOobjectOption::isReadRequired(readOpt))
326 {
328 << "Entry '" << keyword << "' not found in dictionary "
329 << name() << nl
330 << exit(FatalIOError);
332
333 return false;
334}
335
336
337template<class T, class Predicate>
339(
340 const word& keyword,
341 T& val,
342 const Predicate& pred,
343 enum keyType::option matchOpt,
345) const
346{
347 if (readOpt == IOobjectOption::NO_READ)
348 {
349 return false;
350 }
351
352 const entry* eptr = csearch(keyword, matchOpt).ptr();
353
354 if (eptr)
355 {
356 ITstream& is = eptr->stream();
357 is >> val;
358
359 checkITstream(is, keyword);
360
361 if (!pred(val))
362 {
363 raiseBadInput(is, keyword);
364 }
365
366 return true;
367 }
368 else if (IOobjectOption::isReadRequired(readOpt))
369 {
371 << "Entry '" << keyword << "' not found in dictionary "
372 << name() << nl
373 << exit(FatalIOError);
375
376 return false;
377}
378
379
380template<class T>
382(
383 const word& keyword,
384 std::initializer_list<std::pair<const char*,int>> compat,
385 T& val,
386 enum keyType::option matchOpt,
388) const
389{
390 if (readOpt == IOobjectOption::NO_READ)
391 {
392 return false;
393 }
394
395 const entry* eptr = csearchCompat(keyword, compat, matchOpt).ptr();
396
397 if (eptr)
398 {
399 ITstream& is = eptr->stream();
400 is >> val;
401
402 checkITstream(is, keyword);
403
404 return true;
405 }
406 else if (IOobjectOption::isReadRequired(readOpt))
407 {
409 << "Entry '" << keyword << "' not found in dictionary "
410 << name() << nl
411 << exit(FatalIOError);
413
414 return false;
415}
416
417
418template<class T>
420(
421 const word& keyword,
422 T& val,
423 enum keyType::option matchOpt
424) const
425{
426 // Reading is optional
427 return readEntry<T>
428 (
429 keyword,
430 val,
431 matchOpt,
433 );
434}
435
436
437template<class T, class Predicate>
439(
440 const word& keyword,
441 T& val,
442 const Predicate& pred,
443 enum keyType::option matchOpt
444) const
445{
446 // Reading is optional
447 return readCheck<T, Predicate>
448 (
449 keyword,
450 val,
451 pred,
452 matchOpt,
454 );
455}
456
457
458template<class T>
460(
461 const word& keyword,
462 std::initializer_list<std::pair<const char*,int>> compat,
463 const T& deflt,
464 enum keyType::option matchOpt
465) const
466{
467 const entry* eptr = csearchCompat(keyword, compat, matchOpt).ptr();
468
469 if (eptr)
470 {
471 T val;
472
473 ITstream& is = eptr->stream();
474 is >> val;
475
476 checkITstream(is, keyword);
477
478 return val;
479 }
480 else if (writeOptionalEntries)
481 {
482 reportDefault(keyword, deflt);
484
485 return deflt;
486}
487
488
489template<class T>
491(
492 const word& keyword,
493 std::initializer_list<std::pair<const char*,int>> compat,
494 T& val,
495 enum keyType::option matchOpt
496) const
497{
498 // Reading is optional
499 return readCompat<T>
500 (
501 keyword,
502 compat,
503 val,
504 matchOpt,
506 );
507}
508
509
510// ************************************************************************* //
label k
static bool isReadRequired(readOption opt) noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
readOption
Enumeration defining read preferences.
@ NO_READ
Nothing to be read.
@ READ_IF_PRESENT
Reading is optional [identical to LAZY_READ].
An input stream of tokens.
Definition ITstream.H:56
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition OSstream.C:101
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
bool isNullDict() const noexcept
The dictionary is actually dictionary::null (root dictionary).
Definition dictionaryI.H:71
bool readIfPresentCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val using any compatibility names if needed....
void checkITstream(const ITstream &is, const word &keyword) const
Check after reading if the input token stream has unconsumed tokens remaining or if there were no tok...
Definition dictionary.C:251
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, IOobjectOption::readOption readOpt=IOobjectOption::MUST_READ) const
Find entry and assign to T val. FatalIOError if it is found and the number of tokens is incorrect,...
bool readCheck(const word &keyword, T &val, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX, IOobjectOption::readOption readOpt=IOobjectOption::MUST_READ) const
Find entry and assign to T val. FatalIOError if it is found and the number of tokens is incorrect,...
static refPtr< OSstream > reportingOutput
Output location when reporting default values.
Definition dictionary.H:492
const_searcher csearchCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword and any compatibility names.
static int writeOptionalEntries
Report optional keywords and values if not present in dictionary.
Definition dictionary.H:482
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition dictionary.C:179
T getCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T using any compatibility names if needed. FatalIOError if not found,...
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
T getOrDefaultCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value using any compatibility names if needed.
T getCheck(const word &keyword, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T with additional checking FatalIOError if not found, or if the number of tokens is...
friend class entry
Declare friendship with the entry class for IO.
Definition dictionary.H:338
T getCheckOrDefault(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
bool readCompat(const word &keyword, std::initializer_list< std::pair< const char *, int > > compat, T &val, enum keyType::option matchOpt=keyType::REGEX, IOobjectOption::readOption readOpt=IOobjectOption::MUST_READ) const
Find entry and assign to T val using any compatibility names if needed. FatalIOError if there are exc...
wordList sortedToc() const
Return the sorted table of contents.
Definition dictionary.C:601
const_searcher csearch(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword.
T getOrAdd(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX)
Find and return a T, or return the given default value and add it to dictionary. FatalIOError if it i...
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition dictionary.C:625
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition dictionary.C:765
bool readCheckIfPresent(const word &keyword, T &val, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
T getCheckOrAdd(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX)
Find and return a T, or return the given default value and add it to dictionary. FatalIOError if it i...
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
A class for handling keywords in dictionaries.
Definition keyType.H:69
option
Enumeration for the data type and search/match modes (bitmask).
Definition keyType.H:80
OSstream & stream(OSstream *alternative=nullptr, int communicator=-1)
Return OSstream for output operations.
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
@ DQUOTE
Double quote.
Definition token.H:154
A class for handling words, derived from Foam::string.
Definition word.H:66
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
auto & name
List< word > wordList
List of word.
Definition fileName.H:60
void add(DimensionedField< scalar, GeoMesh > &result, const dimensioned< scalar > &dt1, const DimensionedField< scalar, GeoMesh > &f2)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
messageStream InfoErr
Information stream (stderr output on master, null elsewhere).
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50