dictionaryTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 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  const T& deflt,
36  bool recursive,
37  bool patternMatch
38 ) const
39 {
40  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
41 
42  if (entryPtr)
43  {
44  return pTraits<T>(entryPtr->stream());
45  }
46  else
47  {
48  if (writeOptionalEntries)
49  {
50  IOInfoInFunction(*this)
51  << "Optional entry '" << keyword << "' is not present,"
52  << " returning the default value '" << deflt << "'"
53  << endl;
54  }
55 
56  return deflt;
57  }
58 }
59 
60 
61 template<class T>
63 (
64  const word& keyword,
65  const T& deflt,
66  bool recursive,
67  bool patternMatch
68 )
69 {
70  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
71 
72  if (entryPtr)
73  {
74  return pTraits<T>(entryPtr->stream());
75  }
76  else
77  {
78  if (writeOptionalEntries)
79  {
80  IOInfoInFunction(*this)
81  << "Optional entry '" << keyword << "' is not present,"
82  << " adding and returning the default value '" << deflt << "'"
83  << endl;
84  }
85 
86  add(new primitiveEntry(keyword, deflt));
87  return deflt;
88  }
89 }
90 
91 
92 template<class T>
94 (
95  const word& keyword,
96  T& val,
97  bool recursive,
98  bool patternMatch
99 ) const
100 {
101  const entry* entryPtr = lookupEntryPtr(keyword, recursive, patternMatch);
102 
103  if (entryPtr)
104  {
105  entryPtr->stream() >> val;
106  return true;
107  }
108  else
109  {
110  if (writeOptionalEntries)
111  {
112  IOInfoInFunction(*this)
113  << "Optional entry '" << keyword << "' is not present,"
114  << " the default value '" << val << "' will be used."
115  << endl;
116  }
117 
118  return false;
119  }
120 }
121 
122 
123 template<class T>
124 void Foam::dictionary::add(const keyType& k, const T& t, bool overwrite)
125 {
126  add(new primitiveEntry(k, t), overwrite);
127 }
128 
129 
130 template<class T>
131 void Foam::dictionary::set(const keyType& k, const T& t)
132 {
133  set(new primitiveEntry(k, t));
134 }
135 
136 
137 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
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:253
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:737
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.
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
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:864
#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.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65