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-2019 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 word& keyword,
59  const T& deflt,
60  bool recursive,
61  bool patternMatch
62 ) const
63 {
64  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
65 
66  if (entryPtr)
67  {
68  return pTraits<T>(entryPtr->stream());
69  }
70  else
71  {
72  if (writeOptionalEntries)
73  {
74  IOInfoInFunction(*this)
75  << "Optional entry '" << keyword << "' is not present,"
76  << " returning the default value '" << deflt << "'"
77  << endl;
78  }
79 
80  return deflt;
81  }
82 }
83 
84 
85 template<class T>
87 (
88  const word& keyword,
89  const T& deflt,
90  bool recursive,
91  bool patternMatch
92 )
93 {
94  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
95 
96  if (entryPtr)
97  {
98  return pTraits<T>(entryPtr->stream());
99  }
100  else
101  {
102  if (writeOptionalEntries)
103  {
104  IOInfoInFunction(*this)
105  << "Optional entry '" << keyword << "' is not present,"
106  << " adding and returning the default value '" << deflt << "'"
107  << endl;
108  }
109 
110  add(new primitiveEntry(keyword, deflt));
111  return deflt;
112  }
113 }
114 
115 
116 template<class T>
118 (
119  const word& keyword,
120  T& val,
121  bool recursive,
122  bool patternMatch
123 ) const
124 {
125  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
126 
127  if (entryPtr)
128  {
129  entryPtr->stream() >> val;
130  return true;
131  }
132  else
133  {
134  if (writeOptionalEntries)
135  {
136  IOInfoInFunction(*this)
137  << "Optional entry '" << keyword << "' is not present,"
138  << " the default value '" << val << "' will be used."
139  << endl;
140  }
141 
142  return false;
143  }
144 }
145 
146 
147 template<class T>
148 void Foam::dictionary::add(const keyType& k, const T& t, bool overwrite)
149 {
150  add(new primitiveEntry(k, t), overwrite);
151 }
152 
153 
154 template<class T>
155 void Foam::dictionary::set(const keyType& k, const T& t)
156 {
157  set(new primitiveEntry(k, t));
158 }
159 
160 
161 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
162 
163 template<class EntryType>
164 void Foam::writeEntry
165 (
166  Ostream& os,
167  const word& entryName,
168  const EntryType& value
169 )
170 {
171  os.writeKeyword(entryName);
172  writeEntry(os, value);
173  os << token::END_STATEMENT << endl;
174 }
175 
176 
177 template<class EntryType>
179 (
180  Ostream& os,
181  const word& entryName,
182  const EntryType& value1,
183  const EntryType& value2
184 )
185 {
186  if (value1 != value2)
187  {
188  writeEntry(os, entryName, value2);
189  }
190 }
191 
192 
193 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
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:256
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:821
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 lookupType(const word &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
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:53
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
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:331
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:948
#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.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
IOerror FatalIOError