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-2020 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  InfoErr
43  << "Dictionary: " << relativeName(true).c_str()
44  << " Entry: " << keyword;
45 
46  if (added)
47  {
48  InfoErr
49  << " Added";
50  }
51  InfoErr
52  << " Default: " << deflt << nl;
53 }
54 
55 
56 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
57 
58 template<class Compare>
59 Foam::wordList Foam::dictionary::sortedToc(const Compare& comp) const
60 {
61  return hashedEntries_.sortedToc(comp);
62 }
63 
64 
65 template<class T>
66 Foam::entry* Foam::dictionary::add(const keyType& k, const T& v, bool overwrite)
67 {
68  return add(new primitiveEntry(k, v), overwrite);
69 }
70 
71 
72 template<class T>
74 {
75  return set(new primitiveEntry(k, v));
76 }
77 
78 
79 template<class T>
81 (
82  const word& keyword,
83  enum keyType::option matchOpt
84 ) const
85 {
86  T val;
87  readEntry<T>(keyword, val, matchOpt);
88  return val;
89 }
90 
91 
92 template<class T, class Predicate>
94 (
95  const word& keyword,
96  const Predicate& pred,
97  enum keyType::option matchOpt
98 ) const
99 {
100  T val;
101  readCheck<T, Predicate>(keyword, val, pred, matchOpt);
102  return val;
103 }
104 
105 
106 template<class T>
108 (
109  const word& keyword,
110  std::initializer_list<std::pair<const char*,int>> compat,
111  enum keyType::option matchOpt
112 ) const
113 {
114  T val;
115  readCompat<T>(keyword, compat, val, matchOpt);
116  return val;
117 }
118 
119 
120 template<class T>
122 (
123  const word& keyword,
124  const T& deflt,
125  enum keyType::option matchOpt
126 ) const
127 {
128  const const_searcher finder(csearch(keyword, matchOpt));
129 
130  if (finder.good())
131  {
132  T val;
133 
134  ITstream& is = finder.ptr()->stream();
135  is >> val;
136 
137  checkITstream(is, keyword);
138 
139  return val;
140  }
141  else if (writeOptionalEntries)
142  {
143  if (writeOptionalEntries > 1)
144  {
146  << "No optional entry: " << keyword
147  << " Default: " << deflt << nl
148  << exit(FatalIOError);
149  }
150  else
151  {
152  reportDefault(keyword, deflt);
153  }
154  }
155 
156  return deflt;
157 }
158 
159 
160 template<class T>
162 (
163  const word& keyword,
164  const T& deflt,
165  enum keyType::option matchOpt
166 )
167 {
168  const const_searcher finder(csearch(keyword, matchOpt));
169 
170  if (finder.good())
171  {
172  T val;
173 
174  ITstream& is = finder.ptr()->stream();
175  is >> val;
176 
177  checkITstream(is, keyword);
178 
179  return val;
180  }
181  else if (writeOptionalEntries)
182  {
183  if (writeOptionalEntries > 1)
184  {
186  << "No optional entry: " << keyword
187  << " Default: " << deflt << nl
188  << exit(FatalIOError);
189  }
190  else
191  {
192  reportDefault(keyword, deflt, true);
193  }
194  }
195 
196  add(new primitiveEntry(keyword, deflt));
197  return deflt;
198 }
199 
200 
201 template<class T, class Predicate>
203 (
204  const word& keyword,
205  const T& deflt,
206  const Predicate& pred,
207  enum keyType::option matchOpt
208 ) const
209 {
210  if (!pred(deflt))
211  {
212  // Could be as FULLDEBUG instead?
214  << "Entry '" << keyword << "' with invalid default in dictionary "
215  << name()
216  << exit(FatalIOError);
217  }
218 
219  const const_searcher finder(csearch(keyword, matchOpt));
220 
221  if (finder.good())
222  {
223  T val;
224 
225  ITstream& is = finder.ptr()->stream();
226  is >> val;
227 
228  checkITstream(is, keyword);
229 
230  if (!pred(val))
231  {
232  raiseBadInput(is, keyword);
233  }
234 
235  return val;
236  }
237  else if (writeOptionalEntries)
238  {
239  if (writeOptionalEntries > 1)
240  {
242  << "No optional entry: " << keyword
243  << " Default: " << deflt << nl
244  << exit(FatalIOError);
245  }
246  else
247  {
248  reportDefault(keyword, deflt);
249  }
250  }
251 
252  return deflt;
253 }
254 
255 
256 template<class T, class Predicate>
258 (
259  const word& keyword,
260  const T& deflt,
261  const Predicate& pred,
262  enum keyType::option matchOpt
263 )
264 {
265  if (!pred(deflt))
266  {
267  // Could be as FULLDEBUG instead?
269  << "Entry '" << keyword << "' with invalid default in dictionary "
270  << name()
271  << exit(FatalIOError);
272  }
273 
274  const const_searcher finder(csearch(keyword, matchOpt));
275 
276  if (finder.good())
277  {
278  T val;
279 
280  ITstream& is = finder.ptr()->stream();
281  is >> val;
282 
283  checkITstream(is, keyword);
284 
285  if (!pred(val))
286  {
287  raiseBadInput(is, keyword);
288  }
289 
290  return val;
291  }
292  else if (writeOptionalEntries)
293  {
294  if (writeOptionalEntries > 1)
295  {
297  << "No optional entry: " << keyword
298  << " Default: " << deflt << nl
299  << exit(FatalIOError);
300  }
301  else
302  {
303  reportDefault(keyword, deflt, true);
304  }
305  }
306 
307  add(new primitiveEntry(keyword, deflt));
308  return deflt;
309 }
310 
311 
312 template<class T>
314 (
315  const word& keyword,
316  T& val,
317  enum keyType::option matchOpt,
318  bool mandatory
319 ) const
320 {
321  const const_searcher finder(csearch(keyword, matchOpt));
322 
323  if (finder.good())
324  {
325  ITstream& is = finder.ptr()->stream();
326  is >> val;
327 
328  checkITstream(is, keyword);
329 
330  return true;
331  }
332  else if (mandatory)
333  {
335  << "Entry '" << keyword << "' not found in dictionary "
336  << name() << nl
337  << exit(FatalIOError);
338  }
339 
340  return false;
341 }
342 
343 
344 template<class T, class Predicate>
346 (
347  const word& keyword,
348  T& val,
349  const Predicate& pred,
350  enum keyType::option matchOpt,
351  bool mandatory
352 ) const
353 {
354  const const_searcher finder(csearch(keyword, matchOpt));
355 
356  if (finder.good())
357  {
358  ITstream& is = finder.ptr()->stream();
359  is >> val;
360 
361  checkITstream(is, keyword);
362 
363  if (!pred(val))
364  {
365  raiseBadInput(is, keyword);
366  }
367 
368  return true;
369  }
370  else if (mandatory)
371  {
373  << "Entry '" << keyword << "' not found in dictionary "
374  << name() << nl
375  << exit(FatalIOError);
376  }
377 
378  return false;
379 }
380 
381 
382 template<class T>
384 (
385  const word& keyword,
386  std::initializer_list<std::pair<const char*,int>> compat,
387  T& val,
388  enum keyType::option matchOpt,
389  bool mandatory
390 ) const
391 {
392  const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
393 
394  if (finder.good())
395  {
396  ITstream& is = finder.ptr()->stream();
397  is >> val;
398 
399  checkITstream(is, keyword);
400 
401  return true;
402  }
403  else if (mandatory)
404  {
406  << "Entry '" << keyword << "' not found in dictionary "
407  << name() << nl
408  << exit(FatalIOError);
409  }
410 
411  return false;
412 }
413 
414 
415 template<class T>
417 (
418  const word& keyword,
419  T& val,
420  enum keyType::option matchOpt
421 ) const
422 {
423  // Read is non-mandatory
424  return readEntry<T>(keyword, val, matchOpt, false);
425 }
426 
427 
428 template<class T, class Predicate>
430 (
431  const word& keyword,
432  T& val,
433  const Predicate& pred,
434  enum keyType::option matchOpt
435 ) const
436 {
437  // Read is non-mandatory
438  return readCheck<T, Predicate>(keyword, val, pred, matchOpt, false);
439 }
440 
441 
442 template<class T>
444 (
445  const word& keyword,
446  std::initializer_list<std::pair<const char*,int>> compat,
447  const T& deflt,
448  enum keyType::option matchOpt
449 ) const
450 {
451  const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
452 
453  if (finder.good())
454  {
455  T val;
456 
457  ITstream& is = finder.ptr()->stream();
458  is >> val;
459 
460  checkITstream(is, keyword);
461 
462  return val;
463  }
464  else if (writeOptionalEntries)
465  {
466  if (writeOptionalEntries > 1)
467  {
469  << "No optional entry: " << keyword
470  << " Default: " << deflt << nl
471  << exit(FatalIOError);
472  }
473  else
474  {
475  reportDefault(keyword, deflt);
476  }
477  }
478 
479  return deflt;
480 }
481 
482 
483 template<class T>
485 (
486  const word& keyword,
487  std::initializer_list<std::pair<const char*,int>> compat,
488  T& val,
489  enum keyType::option matchOpt
490 ) const
491 {
492  // Read is non-mandatory
493  return readCompat<T>(keyword, compat, val, matchOpt, false);
494 }
495 
496 
497 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
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:62
Foam::dictionary::getOrAdd
T getOrAdd(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX)
Definition: dictionaryTemplates.C:162
Foam::dictionary::getCheckOrAdd
T getCheckOrAdd(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX)
Definition: dictionaryTemplates.C:258
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:444
Foam::FatalIOError
IOerror FatalIOError
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
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:108
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:847
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::dictionary::Searcher
Generic const/non-const dictionary entry searcher.
Definition: dictionary.H:138
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
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:485
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:314
Foam::dictionary::Searcher::good
bool good() const
True if entry was found.
Definition: dictionary.H:198
Foam::dictionary::getCheckOrDefault
T getCheckOrDefault(const word &keyword, const T &deflt, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:203
Foam::dictionary::Searcher::ptr
pointer ptr() const
A pointer to the entry (nullptr if not found)
Definition: dictionary.H:216
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
T
const volScalarField & T
Definition: createFieldRefs.H:2
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:346
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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:430
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:384
dictionary.H
Foam::dictionary::getCheck
T getCheck(const word &keyword, const Predicate &pred, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:94
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:708
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:122
Foam::dictionary::sortedToc
wordList sortedToc() const
Return the sorted table of contents.
Definition: dictionary.C:684
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:70