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-2023 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class T, class ... KeysAndTs>
33 (
34  const keyType& k,
35  const T& t,
36  const KeysAndTs& ... keysAndTs
37 )
38 :
39  parent_(dictionary::null)
40 {
41  set(k, t, keysAndTs ...);
42 }
43 
44 
45 template<class T, class ... KeysAndTs>
47 (
48  const fileName& name,
49  const keyType& k,
50  const T& t,
51  const KeysAndTs& ... keysAndTs
52 )
53 :
55  parent_(dictionary::null)
56 {
57  set(k, t, keysAndTs ...);
58 }
59 
60 
61 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62 
63 template<class T>
65 (
66  const word& keyword,
67  bool recursive,
68  bool patternMatch
69 ) const
70 {
71  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
72 
73  if (entryPtr == nullptr)
74  {
76  (
77  *this
78  ) << "keyword " << keyword << " is undefined in dictionary "
79  << name()
80  << exit(FatalIOError);
81  }
82 
83  return pTraits<T>(entryPtr->stream());
84 }
85 
86 
87 template<class T>
89 (
90  const wordList& keywords,
91  bool recursive,
92  bool patternMatch
93 ) const
94 {
95  const entry* entryPtr =
96  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
97 
98  if (entryPtr)
99  {
100  return pTraits<T>(entryPtr->stream());
101  }
102  else
103  {
104  // Generate error message using the first keyword
105  return lookup<T>(keywords[0], recursive, patternMatch);
106  }
107 }
108 
109 
110 template<class T>
112 (
113  const word& keyword,
114  const T& deflt,
115  bool recursive,
116  bool patternMatch
117 ) const
118 {
119  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
120 
121  if (entryPtr)
122  {
123  return pTraits<T>(entryPtr->stream());
124  }
125  else
126  {
127  if (writeOptionalEntries)
128  {
129  IOInfoInFunction(*this)
130  << "Optional entry '" << keyword << "' is not present,"
131  << " returning the default value '" << deflt << "'"
132  << endl;
133  }
134 
135  return deflt;
136  }
137 }
138 
139 
140 template<class T>
142 (
143  const wordList& keywords,
144  const T& deflt,
145  bool recursive,
146  bool patternMatch
147 ) const
148 {
149  const entry* entryPtr =
150  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
151 
152  if (entryPtr)
153  {
154  return pTraits<T>(entryPtr->stream());
155  }
156  else
157  {
158  // Generate debugging messages using the first keyword
159  return lookupOrDefault<T>(keywords[0], deflt, recursive, patternMatch);
160  }
161 }
162 
163 
164 template<class T>
166 (
167  const word& keyword,
168  const T& deflt,
169  bool recursive,
170  bool patternMatch
171 )
172 {
173  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
174 
175  if (entryPtr)
176  {
177  return pTraits<T>(entryPtr->stream());
178  }
179  else
180  {
181  if (writeOptionalEntries)
182  {
183  IOInfoInFunction(*this)
184  << "Optional entry '" << keyword << "' is not present,"
185  << " adding and returning the default value '" << deflt << "'"
186  << endl;
187  }
188 
189  add(new primitiveEntry(keyword, deflt));
190  return deflt;
191  }
192 }
193 
194 
195 template<class T>
197 (
198  const word& keyword,
199  T& val,
200  bool recursive,
201  bool patternMatch
202 ) const
203 {
204  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
205 
206  if (entryPtr)
207  {
208  entryPtr->stream() >> val;
209  return true;
210  }
211  else
212  {
213  if (writeOptionalEntries)
214  {
215  IOInfoInFunction(*this)
216  << "Optional entry '" << keyword << "' is not present,"
217  << " the default value '" << val << "' will be used."
218  << endl;
219  }
220 
221  return false;
222  }
223 }
224 
225 
226 template<class T>
227 void Foam::dictionary::add(const keyType& k, const T& t, bool overwrite)
228 {
229  add(new primitiveEntry(k, t), overwrite);
230 }
231 
232 
233 template<class T>
234 void Foam::dictionary::set(const keyType& k, const T& t)
235 {
236  set(new primitiveEntry(k, t));
237 }
238 
239 
240 template<class T, class ... KeysAndTs>
242 (
243  const keyType& k,
244  const T& t,
245  const KeysAndTs& ... keysAndTs
246 )
247 {
248  set(new primitiveEntry(k, t));
249  set(keysAndTs ...);
250 }
251 
252 
253 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
254 
255 template<class EntryType>
257 (
258  Ostream& os,
259  const word& entryName,
260  const EntryType& value
261 )
262 {
263  writeKeyword(os, entryName);
264  writeEntry(os, value);
265  os << token::END_STATEMENT << endl;
266 }
267 
268 
269 template<class EntryType>
271 (
272  Ostream& os,
273  const word& entryName,
274  const EntryType& value1,
275  const EntryType& value2
276 )
277 {
278  if (value1 != value2)
279  {
280  writeEntry(os, entryName, value2);
281  }
282 }
283 
284 
285 // ************************************************************************* //
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:160
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:436
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:860
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:1307
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:1169
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:871
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,...
@ END_STATEMENT
Definition: token.H:105
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:318
#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:251
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
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)