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-2025 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 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
30 
31 template<class ... Entries>
32 std::tuple<const Entries& ...> Foam::dictionary::entries
33 (
34  const Entries& ... entries
35 )
36 {
37  return std::tuple<const Entries& ...>(entries ...);
38 }
39 
40 
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 
43 template<class T>
44 T Foam::dictionary::readType
45 (
46  const word& keyword,
47  const unitConversion& defaultUnits,
48  ITstream& is
49 ) const
50 {
51  assertNoConvertUnits(pTraits<T>::typeName, keyword, defaultUnits, is);
52 
53  return pTraits<T>(is);
54 }
55 
56 
57 template<class T>
58 T Foam::dictionary::readType(const word& keyword, ITstream& is) const
59 {
60  return pTraits<T>(is);
61 }
62 
63 
64 template<class ... Entries, size_t ... Indices>
65 void Foam::dictionary::set
66 (
67  const std::tuple<const Entries& ...>& entries,
68  const std::integer_sequence<size_t, Indices ...>&
69 )
70 {
71  set(std::get<Indices>(entries) ...);
72 }
73 
74 
75 template<class ... Entries>
76 void Foam::dictionary::set
77 (
78  const std::tuple<const Entries& ...>& entries
79 )
80 {
81  set(entries, std::make_integer_sequence<size_t, sizeof ... (Entries)>());
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
87 template<class ... Entries>
88 Foam::dictionary::dictionary(const std::tuple<const Entries& ...>& entries)
89 :
90  dictionary()
91 {
92  set(entries);
93 }
94 
95 
96 template<class ... Entries>
98 (
99  const fileName& name,
100  const std::tuple<const Entries& ...>& entries
101 )
102 :
104 {
105  set(entries);
106 }
107 
108 
109 template<class ... Entries>
111 (
112  const fileName& name,
113  const dictionary& parentDict,
114  const std::tuple<const Entries& ...>& entries
115 )
116 :
117  dictionary(name, parentDict)
118 {
119  set(entries);
120 }
121 
122 
123 template<class ... Entries>
125 (
126  const dictionary& dict,
127  const std::tuple<const Entries& ...>& entries
128 )
129 :
131 {
132  set(entries);
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
138 template<class T>
140 (
141  const word& keyword,
142  bool recursive,
143  bool patternMatch
144 ) const
145 {
146  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
147 
148  if (entryPtr == nullptr)
149  {
151  << "keyword " << keyword << " is undefined in dictionary "
152  << name() << exit(FatalIOError);
153  }
154 
155  return readType<T>(keyword, entryPtr->stream());
156 }
157 
158 
159 template<class T>
161 (
162  const word& keyword,
163  const unitConversion& defaultUnits,
164  bool recursive,
165  bool patternMatch
166 ) const
167 {
168  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
169 
170  if (entryPtr == nullptr)
171  {
173  << "keyword " << keyword << " is undefined in dictionary "
174  << name() << exit(FatalIOError);
175  }
176 
177  return readType<T>(keyword, defaultUnits, entryPtr->stream());
178 }
179 
180 
181 template<class T>
183 (
184  const wordList& keywords,
185  bool recursive,
186  bool patternMatch
187 ) const
188 {
189  const entry* entryPtr =
190  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
191 
192  if (entryPtr)
193  {
194  return readType<T>(entryPtr->keyword(), entryPtr->stream());
195  }
196  else
197  {
198  // Generate error message using the first keyword
199  return lookup<T>(keywords[0], recursive, patternMatch);
200  }
201 }
202 
203 
204 template<class T>
206 (
207  const wordList& keywords,
208  const unitConversion& defaultUnits,
209  bool recursive,
210  bool patternMatch
211 ) const
212 {
213  const entry* entryPtr =
214  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
215 
216  if (entryPtr)
217  {
218  return
219  readType<T>(entryPtr->keyword(), defaultUnits, entryPtr->stream());
220  }
221  else
222  {
223  // Generate error message using the first keyword
224  return lookup<T>(keywords[0], recursive, patternMatch);
225  }
226 }
227 
228 
229 template<class T>
231 (
232  const word& keyword,
233  const T& defaultValue,
234  const bool writeDefault
235 ) const
236 {
237  const entry* entryPtr = lookupEntryPtr(keyword, false, false);
238 
239  if (entryPtr)
240  {
241  return readType<T>(keyword, entryPtr->stream());
242  }
243  else
244  {
245  if (writeDefault)
246  {
247  Info<< indent << "Default: " << keyword
248  << " " << defaultValue
249  << " in " << name().relativePath() << endl;
250  }
251 
252  return defaultValue;
253  }
254 }
255 
256 
257 template<class T>
259 (
260  const word& keyword,
261  const unitConversion& defaultUnits,
262  const T& defaultValue,
263  const bool writeDefault
264 ) const
265 {
266  const entry* entryPtr = lookupEntryPtr(keyword, false, false);
267 
268  if (entryPtr)
269  {
270  return readType<T>(keyword, defaultUnits, entryPtr->stream());
271  }
272  else
273  {
274  if (writeDefault)
275  {
276  Info<< indent << "Default: " << keyword
277  << " " << defaultValue
278  << " in " << name().relativePath() << endl;
279  }
280 
281  return defaultValue;
282  }
283 }
284 
285 
286 template<class T>
288 (
289  const wordList& keywords,
290  const T& defaultValue
291 ) const
292 {
293  const entry* entryPtr =
294  lookupEntryPtrBackwardsCompatible(keywords, false, false);
295 
296  if (entryPtr)
297  {
298  return readType<T>(entryPtr->keyword(), entryPtr->stream());
299  }
300  else
301  {
302  // Generate debugging messages using the first keyword
303  return lookupOrDefault<T>(keywords[0], defaultValue);
304  }
305 }
306 
307 
308 template<class T>
310 (
311  const wordList& keywords,
312  const unitConversion& defaultUnits,
313  const T& defaultValue
314 ) const
315 {
316  const entry* entryPtr =
317  lookupEntryPtrBackwardsCompatible(keywords, false, false);
318 
319  if (entryPtr)
320  {
321  return
322  readType<T>(entryPtr->keyword(), defaultUnits, entryPtr->stream());
323  }
324  else
325  {
326  // Generate debugging messages using the first keyword
327  return lookupOrDefault<T>(keywords[0], defaultValue);
328  }
329 }
330 
331 
332 template<class T>
334 (
335  const word& keyword,
336  const T& defaultValue
337 )
338 {
339  const entry* entryPtr = lookupEntryPtr(keyword, false, false);
340 
341  if (entryPtr)
342  {
343  return readType<T>(keyword, entryPtr->stream());
344  }
345  else
346  {
347  if (writeOptionalEntries > 1)
348  {
349  Info<< indent << "Added default: " << keyword
350  << " " << defaultValue
351  << " to " << name().relativePath() << endl;
352  }
353 
354  add(new primitiveEntry(keyword, defaultValue));
355  return defaultValue;
356  }
357 }
358 
359 
360 template<class T>
362 (
363  const word& keyword,
364  T& val,
365  bool recursive,
366  bool patternMatch
367 ) const
368 {
369  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
370 
371  if (entryPtr)
372  {
373  val = readType<T>(keyword, entryPtr->stream());
374  return true;
375  }
376  else
377  {
378  if (writeOptionalEntries > 1)
379  {
380  Info<< indent << "Default: " << keyword
381  << " " << val
382  << " in " << name().relativePath() << endl;
383  }
384 
385  return false;
386  }
387 }
388 
389 
390 template<class T>
392 (
393  const word& keyword,
394  const unitConversion& defaultUnits,
395  T& val,
396  bool recursive,
397  bool patternMatch
398 ) const
399 {
400  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
401 
402  if (entryPtr)
403  {
404  val = readType<T>(keyword, defaultUnits, entryPtr->stream());
405  return true;
406  }
407  else
408  {
409  if (writeOptionalEntries > 1)
410  {
411  Info<< indent << "Default: " << keyword
412  << " " << val
413  << " in " << name().relativePath() << endl;
414  }
415 
416  return false;
417  }
418 }
419 
420 
421 template<class T>
423 (
424  const word& keyword,
425  bool recursive,
426  bool patternMatch
427 ) const
428 {
429  const entry* entryPtr =
430  lookupScopedEntryPtr(keyword, recursive, patternMatch);
431 
432  if (entryPtr == nullptr)
433  {
435  << "keyword " << keyword << " is undefined in dictionary "
436  << name() << exit(FatalIOError);
437  }
438 
439  return pTraits<T>(entryPtr->stream());
440 }
441 
442 
443 template<class T>
445 (
446  const word& keyword,
447  bool recursive,
448  bool patternMatch
449 ) const
450 {
451  const entry* entryPtr =
452  lookupScopedEntryPtr(keyword, recursive, patternMatch);
453 
454  if (entryPtr == nullptr)
455  {
457  (
458  *this
459  ) << "keyword " << keyword << " is undefined in dictionary "
460  << name()
461  << exit(FatalIOError);
462  }
463 
464  token firstToken(entryPtr->stream());
465 
466  return dynamicCast<const token::Compound<T>>(firstToken.compoundToken());
467 }
468 
469 
470 template<class T>
471 void Foam::dictionary::add(const keyType& k, const T& t, bool overwrite)
472 {
473  add(new primitiveEntry(k, t), overwrite);
474 }
475 
476 
477 template<class T>
478 void Foam::dictionary::set(const keyType& k, const T& t)
479 {
480  set(new primitiveEntry(k, t));
481 }
482 
483 
484 template<class ... Entries>
485 void Foam::dictionary::set(const entry& e, const Entries& ... entries)
486 {
487  set(e);
488  set(entries ...);
489 }
490 
491 
492 template<class T, class ... Entries>
493 void Foam::dictionary::set
494 (
495  const keyType& k,
496  const T& t,
497  const Entries& ... entries
498 )
499 {
500  set(k, t);
501  set(entries ...);
502 }
503 
504 
505 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
506 
507 template<class EntryType>
509 (
510  Ostream& os,
511  const word& entryName,
512  const EntryType& value
513 )
514 {
515  writeKeyword(os, entryName);
516  writeEntry(os, value);
517  os << token::END_STATEMENT << endl;
518 }
519 
520 
521 template<class EntryType>
523 (
524  Ostream& os,
525  const word& entryName,
526  const unitConversion& defaultUnits,
527  const EntryType& value
528 )
529 {
530  writeKeyword(os, entryName);
531  writeEntry(os, defaultUnits, value);
532  os << token::END_STATEMENT << endl;
533 }
534 
535 
536 template<class EntryType>
538 (
539  Ostream& os,
540  const word& entryName,
541  const EntryType& value1,
542  const EntryType& value2
543 )
544 {
545  if (value1 != value2)
546  {
547  writeEntry(os, entryName, value2);
548  }
549 }
550 
551 
552 template<class EntryType>
554 (
555  Ostream& os,
556  const word& entryName,
557  const unitConversion& defaultUnits,
558  const EntryType& value1,
559  const EntryType& value2
560 )
561 {
562  if (value1 != value2)
563  {
564  writeEntry(os, entryName, defaultUnits, value2);
565  }
566 }
567 
568 
569 // ************************************************************************* //
label k
Input token stream.
Definition: ITstream.H:53
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:327
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:740
T lookupOrAddDefault(const word &, const T &)
Find and return a T, if not found return the given.
T lookupOrDefaultBackwardsCompatible(const wordList &, const T &) const
Find and return a T, trying a list of keywords in sequence,.
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1020
T lookupOrDefault(const word &, const T &, const bool writeDefault=writeOptionalEntries > 0) const
Find and return a T, if not found return the given default.
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:751
const T & lookupCompoundScoped(const word &keyword, bool recursive, bool patternMatch) const
Find return the reference to the compound T,.
static std::tuple< const Entries &... > entries(const Entries &...)
Construct an entries tuple from which to make a dictionary.
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:136
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const doubleScalar e
Definition: doubleScalar.H:106
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:258
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
messageStream Info
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
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 add(LagrangianPatchField< typename typeOfSum< Type1, Type2 >::type > &f, const LagrangianPatchField< Type1 > &f1, const LagrangianPatchField< Type2 > &f2)
void assertNoConvertUnits(const word &typeName, const Function1s::unitConversions &units, const dictionary &dict)
Generate an error in an context where unit conversions are not supported.
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:228
dictionary dict
const bool overwrite
Definition: setNoOverwrite.H:1