dictionaryTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dictionary.H"
27 #include "primitiveEntry.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class T>
32 T Foam::dictionary::readType
33 (
34  const word& keyword,
35  const unitConversion& defaultUnits,
36  ITstream& is
37 ) const
38 {
39  assertNoConvertUnits(pTraits<T>::typeName, keyword, defaultUnits, is);
40 
41  return pTraits<T>(is);
42 }
43 
44 
45 template<class T>
46 T Foam::dictionary::readType(const word& keyword, ITstream& is) const
47 {
48  return pTraits<T>(is);
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
54 template<class T, class ... KeysAndTs>
56 (
57  const keyType& k,
58  const T& t,
59  const KeysAndTs& ... keysAndTs
60 )
61 :
62  parent_(dictionary::null)
63 {
64  set(k, t, keysAndTs ...);
65 }
66 
67 
68 template<class T, class ... KeysAndTs>
70 (
71  const fileName& name,
72  const keyType& k,
73  const T& t,
74  const KeysAndTs& ... keysAndTs
75 )
76 :
78  parent_(dictionary::null)
79 {
80  set(k, t, keysAndTs ...);
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 template<class T>
88 (
89  const word& keyword,
90  bool recursive,
91  bool patternMatch
92 ) const
93 {
94  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
95 
96  if (entryPtr == nullptr)
97  {
99  << "keyword " << keyword << " is undefined in dictionary "
100  << name() << exit(FatalIOError);
101  }
102 
103  return readType<T>(keyword, entryPtr->stream());
104 }
105 
106 
107 template<class T>
109 (
110  const word& keyword,
111  const unitConversion& defaultUnits,
112  bool recursive,
113  bool patternMatch
114 ) const
115 {
116  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
117 
118  if (entryPtr == nullptr)
119  {
121  << "keyword " << keyword << " is undefined in dictionary "
122  << name() << exit(FatalIOError);
123  }
124 
125  return readType<T>(keyword, defaultUnits, entryPtr->stream());
126 }
127 
128 
129 template<class T>
131 (
132  const wordList& keywords,
133  bool recursive,
134  bool patternMatch
135 ) const
136 {
137  const entry* entryPtr =
138  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
139 
140  if (entryPtr)
141  {
142  return readType<T>(entryPtr->keyword(), entryPtr->stream());
143  }
144  else
145  {
146  // Generate error message using the first keyword
147  return lookup<T>(keywords[0], recursive, patternMatch);
148  }
149 }
150 
151 
152 template<class T>
154 (
155  const wordList& keywords,
156  const unitConversion& defaultUnits,
157  bool recursive,
158  bool patternMatch
159 ) const
160 {
161  const entry* entryPtr =
162  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
163 
164  if (entryPtr)
165  {
166  return
167  readType<T>(entryPtr->keyword(), defaultUnits, entryPtr->stream());
168  }
169  else
170  {
171  // Generate error message using the first keyword
172  return lookup<T>(keywords[0], recursive, patternMatch);
173  }
174 }
175 
176 
177 template<class T>
179 (
180  const word& keyword,
181  const T& deflt,
182  bool recursive,
183  bool patternMatch
184 ) const
185 {
186  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
187 
188  if (entryPtr)
189  {
190  return readType<T>(keyword, entryPtr->stream());
191  }
192  else
193  {
194  if (writeOptionalEntries)
195  {
196  IOInfoInFunction(*this)
197  << "Optional entry '" << keyword << "' is not present,"
198  << " returning the default value '" << deflt << "'"
199  << endl;
200  }
201 
202  return deflt;
203  }
204 }
205 
206 
207 template<class T>
209 (
210  const word& keyword,
211  const unitConversion& defaultUnits,
212  const T& deflt,
213  bool recursive,
214  bool patternMatch
215 ) const
216 {
217  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
218 
219  if (entryPtr)
220  {
221  return readType<T>(keyword, defaultUnits, entryPtr->stream());
222  }
223  else
224  {
225  if (writeOptionalEntries)
226  {
227  IOInfoInFunction(*this)
228  << "Optional entry '" << keyword << "' is not present,"
229  << " returning the default value '" << deflt << "'"
230  << endl;
231  }
232 
233  return deflt;
234  }
235 }
236 
237 
238 template<class T>
240 (
241  const wordList& keywords,
242  const T& deflt,
243  bool recursive,
244  bool patternMatch
245 ) const
246 {
247  const entry* entryPtr =
248  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
249 
250  if (entryPtr)
251  {
252  return readType<T>(entryPtr->keyword(), entryPtr->stream());
253  }
254  else
255  {
256  // Generate debugging messages using the first keyword
257  return lookupOrDefault<T>(keywords[0], deflt, recursive, patternMatch);
258  }
259 }
260 
261 
262 template<class T>
264 (
265  const wordList& keywords,
266  const unitConversion& defaultUnits,
267  const T& deflt,
268  bool recursive,
269  bool patternMatch
270 ) const
271 {
272  const entry* entryPtr =
273  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
274 
275  if (entryPtr)
276  {
277  return
278  readType<T>(entryPtr->keyword(), defaultUnits, entryPtr->stream());
279  }
280  else
281  {
282  // Generate debugging messages using the first keyword
283  return lookupOrDefault<T>(keywords[0], deflt, recursive, patternMatch);
284  }
285 }
286 
287 
288 template<class T>
290 (
291  const word& keyword,
292  const T& deflt,
293  bool recursive,
294  bool patternMatch
295 )
296 {
297  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
298 
299  if (entryPtr)
300  {
301  return readType<T>(keyword, entryPtr->stream());
302  }
303  else
304  {
305  if (writeOptionalEntries)
306  {
307  IOInfoInFunction(*this)
308  << "Optional entry '" << keyword << "' is not present,"
309  << " adding and returning the default value '" << deflt << "'"
310  << endl;
311  }
312 
313  add(new primitiveEntry(keyword, deflt));
314  return deflt;
315  }
316 }
317 
318 
319 template<class T>
321 (
322  const word& keyword,
323  T& val,
324  bool recursive,
325  bool patternMatch
326 ) const
327 {
328  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
329 
330  if (entryPtr)
331  {
332  val = readType<T>(keyword, entryPtr->stream());
333  return true;
334  }
335  else
336  {
337  if (writeOptionalEntries)
338  {
339  IOInfoInFunction(*this)
340  << "Optional entry '" << keyword << "' is not present,"
341  << " the default value '" << val << "' will be used."
342  << endl;
343  }
344 
345  return false;
346  }
347 }
348 
349 
350 template<class T>
352 (
353  const word& keyword,
354  const unitConversion& defaultUnits,
355  T& val,
356  bool recursive,
357  bool patternMatch
358 ) const
359 {
360  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
361 
362  if (entryPtr)
363  {
364  val = readType<T>(keyword, defaultUnits, entryPtr->stream());
365  return true;
366  }
367  else
368  {
369  if (writeOptionalEntries)
370  {
371  IOInfoInFunction(*this)
372  << "Optional entry '" << keyword << "' is not present,"
373  << " the default value '" << val << "' will be used."
374  << endl;
375  }
376 
377  return false;
378  }
379 }
380 
381 
382 template<class T>
384 (
385  const word& keyword,
386  bool recursive,
387  bool patternMatch
388 ) const
389 {
390  const entry* entryPtr =
391  lookupScopedEntryPtr(keyword, recursive, patternMatch);
392 
393  if (entryPtr == nullptr)
394  {
396  << "keyword " << keyword << " is undefined in dictionary "
397  << name() << exit(FatalIOError);
398  }
399 
400  return pTraits<T>(entryPtr->stream());
401 }
402 
403 
404 template<class T>
406 (
407  const word& keyword,
408  bool recursive,
409  bool patternMatch
410 ) const
411 {
412  const entry* entryPtr =
413  lookupScopedEntryPtr(keyword, recursive, patternMatch);
414 
415  if (entryPtr == nullptr)
416  {
418  (
419  *this
420  ) << "keyword " << keyword << " is undefined in dictionary "
421  << name()
422  << exit(FatalIOError);
423  }
424 
425  token firstToken(entryPtr->stream());
426 
427  return dynamicCast<const token::Compound<T>>(firstToken.compoundToken());
428 }
429 
430 
431 template<class T>
432 void Foam::dictionary::add(const keyType& k, const T& t, bool overwrite)
433 {
434  add(new primitiveEntry(k, t), overwrite);
435 }
436 
437 
438 template<class T>
439 void Foam::dictionary::set(const keyType& k, const T& t)
440 {
441  set(new primitiveEntry(k, t));
442 }
443 
444 
445 template<class T, class ... KeysAndTs>
447 (
448  const keyType& k,
449  const T& t,
450  const KeysAndTs& ... keysAndTs
451 )
452 {
453  set(new primitiveEntry(k, t));
454  set(keysAndTs ...);
455 }
456 
457 
458 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
459 
460 template<class EntryType>
462 (
463  Ostream& os,
464  const word& entryName,
465  const EntryType& value
466 )
467 {
468  writeKeyword(os, entryName);
469  writeEntry(os, value);
470  os << token::END_STATEMENT << endl;
471 }
472 
473 
474 template<class EntryType>
476 (
477  Ostream& os,
478  const word& entryName,
479  const unitConversion& defaultUnits,
480  const EntryType& value
481 )
482 {
483  writeKeyword(os, entryName);
484  writeEntry(os, defaultUnits, value);
485  os << token::END_STATEMENT << endl;
486 }
487 
488 
489 template<class EntryType>
491 (
492  Ostream& os,
493  const word& entryName,
494  const EntryType& value1,
495  const EntryType& value2
496 )
497 {
498  if (value1 != value2)
499  {
500  writeEntry(os, entryName, value2);
501  }
502 }
503 
504 
505 template<class EntryType>
507 (
508  Ostream& os,
509  const word& entryName,
510  const unitConversion& defaultUnits,
511  const EntryType& value1,
512  const EntryType& value2
513 )
514 {
515  if (value1 != value2)
516  {
517  writeEntry(os, entryName, defaultUnits, value2);
518  }
519 }
520 
521 
522 // ************************************************************************* //
label k
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
T lookupOrDefaultBackwardsCompatible(const wordList &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T, trying a list of keywords in sequence,.
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:320
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T, if not found return the given default.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:710
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:1152
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1014
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
ITstream & lookupBackwardsCompatible(const wordList &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream, trying a list of keywords.
Definition: dictionary.C:721
const T & lookupCompoundScoped(const word &keyword, bool recursive, bool patternMatch) const
Find return the reference to the compound T,.
T lookupScoped(const word &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
const keyType & keyword() const
Return keyword.
Definition: entry.H:123
A class for handling file names.
Definition: fileName.H:82
A class for handling keywords in dictionaries.
Definition: keyType.H:69
Traits class for primitives.
Definition: pTraits.H:53
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
A token holds items read from Istream.
Definition: token.H:73
@ END_STATEMENT
Definition: token.H:108
const compound & compoundToken() const
Definition: tokenI.H:789
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define IOInfoInFunction(ios)
Report an IO information message using Foam::Info.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void writeEntryIfDifferent(Ostream &os, const word &entryName, const EntryType &value1, const EntryType &value2)
Helper function to write the keyword and entry only if the.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
IOerror FatalIOError
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)