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-2021 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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
31 template<class T>
33 (
34  const word& keyword,
35  bool recursive,
36  bool patternMatch
37 ) const
38 {
39  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
40 
41  if (entryPtr == nullptr)
42  {
44  (
45  *this
46  ) << "keyword " << keyword << " is undefined in dictionary "
47  << name()
48  << exit(FatalIOError);
49  }
50 
51  return pTraits<T>(entryPtr->stream());
52 }
53 
54 
55 template<class T>
57 (
58  const wordList& keywords,
59  bool recursive,
60  bool patternMatch
61 ) const
62 {
63  const entry* entryPtr =
64  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
65 
66  if (entryPtr)
67  {
68  return pTraits<T>(entryPtr->stream());
69  }
70  else
71  {
72  // Generate error message using the first keyword
73  return lookup<T>(keywords[0], recursive, patternMatch);
74  }
75 }
76 
77 
78 template<class T>
80 (
81  const word& keyword,
82  const T& deflt,
83  bool recursive,
84  bool patternMatch
85 ) const
86 {
87  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
88 
89  if (entryPtr)
90  {
91  return pTraits<T>(entryPtr->stream());
92  }
93  else
94  {
95  if (writeOptionalEntries)
96  {
97  IOInfoInFunction(*this)
98  << "Optional entry '" << keyword << "' is not present,"
99  << " returning the default value '" << deflt << "'"
100  << endl;
101  }
102 
103  return deflt;
104  }
105 }
106 
107 
108 template<class T>
110 (
111  const wordList& keywords,
112  const T& deflt,
113  bool recursive,
114  bool patternMatch
115 ) const
116 {
117  const entry* entryPtr =
118  lookupEntryPtrBackwardsCompatible(keywords, recursive, patternMatch);
119 
120  if (entryPtr)
121  {
122  return pTraits<T>(entryPtr->stream());
123  }
124  else
125  {
126  // Generate debugging messages using the first keyword
127  return lookupOrDefault<T>(keywords[0], deflt, recursive, patternMatch);
128  }
129 }
130 
131 
132 template<class T>
134 (
135  const word& keyword,
136  const T& deflt,
137  bool recursive,
138  bool patternMatch
139 )
140 {
141  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
142 
143  if (entryPtr)
144  {
145  return pTraits<T>(entryPtr->stream());
146  }
147  else
148  {
149  if (writeOptionalEntries)
150  {
151  IOInfoInFunction(*this)
152  << "Optional entry '" << keyword << "' is not present,"
153  << " adding and returning the default value '" << deflt << "'"
154  << endl;
155  }
156 
157  add(new primitiveEntry(keyword, deflt));
158  return deflt;
159  }
160 }
161 
162 
163 template<class T>
165 (
166  const word& keyword,
167  T& val,
168  bool recursive,
169  bool patternMatch
170 ) const
171 {
172  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
173 
174  if (entryPtr)
175  {
176  entryPtr->stream() >> val;
177  return true;
178  }
179  else
180  {
181  if (writeOptionalEntries)
182  {
183  IOInfoInFunction(*this)
184  << "Optional entry '" << keyword << "' is not present,"
185  << " the default value '" << val << "' will be used."
186  << endl;
187  }
188 
189  return false;
190  }
191 }
192 
193 
194 template<class T>
195 void Foam::dictionary::add(const keyType& k, const T& t, bool overwrite)
196 {
197  add(new primitiveEntry(k, t), overwrite);
198 }
199 
200 
201 template<class T>
202 void Foam::dictionary::set(const keyType& k, const T& t)
203 {
204  set(new primitiveEntry(k, t));
205 }
206 
207 
208 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
209 
210 template<class EntryType>
211 void Foam::writeEntry
212 (
213  Ostream& os,
214  const word& entryName,
215  const EntryType& value
216 )
217 {
218  writeKeyword(os, entryName);
219  writeEntry(os, value);
220  os << token::END_STATEMENT << endl;
221 }
222 
223 
224 template<class EntryType>
226 (
227  Ostream& os,
228  const word& entryName,
229  const EntryType& value1,
230  const EntryType& value2
231 )
232 {
233  if (value1 != value2)
234  {
235  writeEntry(os, entryName, value2);
236  }
237 }
238 
239 
240 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
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
Traits class for primitives.
Definition: pTraits.H:50
label k
Boltzmann constant.
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1133
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
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.
A class for handling words, derived from string.
Definition: word.H:59
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
const volScalarField & T
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:1271
#define IOInfoInFunction(ios)
Report an IO information message using Foam::Info.
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
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:855
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844
IOerror FatalIOError