stateFunctionObjectTemplates.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IOdictionary.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const word& entryName,
37  const Type& defaultValue
38 ) const
39 {
40  Type result = defaultValue;
41  getProperty(entryName, result);
42  return result;
43 }
44 
45 
46 template<class Type>
48 (
49  const word& entryName,
50  Type& value
51 ) const
52 {
53  return getObjectProperty(name(), entryName, value);
54 }
55 
56 
57 template<class Type>
59 (
60  const word& entryName,
61  const Type& value
62 )
63 {
64  setObjectProperty(name(), entryName, value);
65 }
66 
67 
68 template<class Type>
70 (
71  const word& objectName,
72  const word& entryName,
73  const Type& defaultValue
74 ) const
75 {
76  Type result = defaultValue;
77  getObjectProperty(objectName, entryName, result);
78  return result;
79 }
80 
81 
82 template<class Type>
84 (
85  const word& objectName,
86  const word& entryName,
87  Type& value
88 ) const
89 {
90  const IOdictionary& stateDict = this->stateDict();
91 
92  if (stateDict.found(objectName))
93  {
94  const dictionary& baseDict = stateDict.subDict(objectName);
95  return baseDict.readIfPresent(entryName, value);
96  }
97 
98  return false;
99 }
100 
101 
102 template<class Type>
104 (
105  const word& objectName,
106  const word& entryName,
107  const Type& value
108 )
109 {
110  IOdictionary& stateDict = this->stateDict();
111 
112  if (!stateDict.found(objectName))
113  {
114  stateDict.add(objectName, dictionary());
115  }
116 
117  dictionary& baseDict = stateDict.subDict(objectName);
118  baseDict.add(entryName, value, true);
119 }
120 
121 
122 template<class Type>
124 (
125  const word& entryName,
126  const Type& value
127 )
128 {
129  setObjectResult(name(), entryName, value);
130 }
131 
132 
133 template<class Type>
135 (
136  const word& objectName,
137  const word& entryName,
138  const Type& value
139 )
140 {
141  IOdictionary& stateDict = this->stateDict();
142 
143  if (!stateDict.found(resultsName_))
144  {
145  stateDict.add(resultsName_, dictionary());
146  }
147 
148  dictionary& resultsDict = stateDict.subDict(resultsName_);
149 
150  if (!resultsDict.found(objectName))
151  {
152  resultsDict.add(name(), dictionary());
153  }
154 
155  dictionary& objectDict = resultsDict.subDict(objectName);
156 
157  const word& dictTypeName = pTraits<Type>::typeName;
158 
159  if (!objectDict.found(dictTypeName))
160  {
161  objectDict.add(dictTypeName, dictionary());
162  }
163 
164  dictionary& resultTypeDict = objectDict.subDict(dictTypeName);
165 
166  resultTypeDict.add(entryName, value, true);
167 }
168 
169 
170 template<class Type>
172 (
173  const word& entryName,
174  const Type& defaultValue
175 ) const
176 {
177  return getObjectResult(name(), entryName, defaultValue);
178 }
179 
180 
181 template<class Type>
183 (
184  const word& objectName,
185  const word& entryName,
186  const Type& defaultValue
187 ) const
188 {
189  Type result = defaultValue;
190  (void)getObjectResult(objectName, entryName, result);
191  return result;
192 }
193 
194 
195 template<class Type>
197 (
198  const word& objectName,
199  const word& entryName,
200  Type& value
201 ) const
202 {
203  const IOdictionary& stateDict = this->stateDict();
204 
205  if (stateDict.found(resultsName_))
206  {
207  const dictionary& resultsDict = stateDict.subDict(resultsName_);
208 
209  if (resultsDict.found(objectName))
210  {
211  const dictionary& objectDict = resultsDict.subDict(objectName);
212 
213  const word& dictTypeName = pTraits<Type>::typeName;
214 
215  if (objectDict.found(dictTypeName))
216  {
217  const dictionary& resultTypeDict =
218  objectDict.subDict(dictTypeName);
219 
220  return resultTypeDict.readIfPresent<Type>(entryName, value);
221  }
222  }
223  }
224 
225  return false;
226 }
227 
228 
229 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionary.C:364
Foam::functionObjects::stateFunctionObject::getProperty
Type getProperty(const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve generic property.
Definition: stateFunctionObjectTemplates.C:35
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:528
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
IOdictionary.H
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
Foam::functionObjects::stateFunctionObject::getObjectResult
Type getObjectResult(const word &objectName, const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve result from named object.
Definition: stateFunctionObjectTemplates.C:183
Foam::functionObjects::stateFunctionObject::setObjectResult
void setObjectResult(const word &objectName, const word &entryName, const Type &value)
Add result from named object.
Definition: stateFunctionObjectTemplates.C:135
Foam::functionObjects::stateFunctionObject::setObjectProperty
void setObjectProperty(const word &objectName, const word &entryName, const Type &value)
Add generic property from named object.
Definition: stateFunctionObjectTemplates.C:104
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:708
Foam::functionObjects::stateFunctionObject::setResult
void setResult(const word &entryName, const Type &value)
Add result.
Definition: stateFunctionObjectTemplates.C:124
Foam::functionObjects::stateFunctionObject::setProperty
void setProperty(const word &entryName, const Type &value)
Add generic property.
Definition: stateFunctionObjectTemplates.C:59
Foam::functionObjects::stateFunctionObject::getResult
Type getResult(const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve result.
Definition: stateFunctionObjectTemplates.C:172
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417
Foam::functionObjects::stateFunctionObject::getObjectProperty
Type getObjectProperty(const word &objectName, const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve generic property from named object.
Definition: stateFunctionObjectTemplates.C:70