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-2018 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 // ************************************************************************* //
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.
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:814
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.
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)
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:941
#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