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