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 -------------------------------------------------------------------------------
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 "dictionary.H"
30 #include "primitiveEntry.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T>
35 void Foam::dictionary::reportDefault
36 (
37  const word& keyword,
38  const T& deflt,
39  const bool added
40 ) const
41 {
42  if (writeOptionalEntries > 1)
43  {
44  FatalIOError(dictionary::executableName(), *this)
45  << "No optional entry: " << keyword
46  << " Default: " << deflt << nl
47  << exit(FatalIOError);
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 
84 template<class Compare>
85 Foam::wordList Foam::dictionary::sortedToc(const Compare& comp) const
86 {
87  return hashedEntries_.sortedToc(comp);
88 }
89 
90 
91 template<class T>
92 Foam::entry* Foam::dictionary::add(const keyType& k, const T& v, bool overwrite)
93 {
94  return add(new primitiveEntry(k, v), overwrite);
95 }
96 
97 
98 template<class T>
100 {
101  return set(new primitiveEntry(k, v));
102 }
103 
104 
105 template<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 
118 template<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 
132 template<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 
146 template<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 
176 template<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 
207 template<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 
253 template<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 
300 template<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 
332 template<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 
370 template<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 
403 template<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 
416 template<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 
430 template<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 
461 template<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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::dictionary::Searcher::ptr
pointer ptr() const noexcept
A pointer to the entry (nullptr if not found)
Definition: dictionary.H:219
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::primitiveEntry
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
Definition: primitiveEntry.H:63
primitiveEntry.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::dictionary::reportingOutput
static refPtr< OSstream > reportingOutput
Output location when reporting default values.
Definition: dictionary.H:395
Foam::dictionary::getOrAdd
T getOrAdd(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX)
Definition: dictionaryTemplates.C:178
Foam::dictionary::getCheckOrAdd
T getCheckOrAdd(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX)
Definition: dictionaryTemplates.C:255
Foam::dictionary::getOrDefaultCompat
T getOrDefaultCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, const T &deflt, enum keyType::option=keyType::REGEX) const
Definition: dictionaryTemplates.C:432
Foam::FatalIOError
IOerror FatalIOError
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::dictionary::getCompat
T getCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option=keyType::REGEX) const
Definition: dictionaryTemplates.C:134
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Foam::InfoErr
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
Foam::dictionary::Searcher
Generic const/non-const dictionary entry searcher.
Definition: dictionary.H:140
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::dictionary::readIfPresentCompat
bool readIfPresentCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, T &val, enum keyType::option=keyType::REGEX) const
Definition: dictionaryTemplates.C:463
Foam::token::DQUOTE
Double quote.
Definition: token.H:135
Foam::messageStream::stream
OSstream & stream(OSstream *alternative=nullptr)
Definition: messageStream.C:71
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
Foam::dictionary::relativeName
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::dictionary::readEntry
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:302
Foam::dictionary::getCheckOrDefault
T getCheckOrDefault(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:209
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
os
OBJstream os(runTime.globalPath()/outputName)
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::dictionary::writeOptionalEntries
static int writeOptionalEntries
Report optional keywords and values if not present in dictionary.
Definition: dictionary.H:389
Foam::dictionary::Searcher::good
bool good() const noexcept
True if entry was found.
Definition: dictionary.H:201
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::dictionary::readCheck
bool readCheck(const word &keyword, T &val, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:334
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< word >
Foam::dictionary::readCheckIfPresent
bool readCheckIfPresent(const word &keyword, T &val, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:418
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::dictionary::readCompat
bool readCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, T &val, enum keyType::option=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:372
dictionary.H
Foam::dictionary::getCheck
T getCheck(const word &keyword, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:120
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::dictionary::sortedToc
wordList sortedToc() const
Return the sorted table of contents.
Definition: dictionary.C:616
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:405
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:78
Foam::dictionary::isNullDict
bool isNullDict() const noexcept
The dictionary is actually dictionary::null (root dictionary)
Definition: dictionaryI.H:74