functionObjectStateTemplates.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) 2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
27 
28 template<class Type>
30 {
31  active_ = true;
32 
33  if (!isA<Type>(obr_))
34  {
36  << "No " << Type::typeName << " available, deactivating " << name_
37  << endl;
38 
39  active_ = false;
40  }
41 
42  return active_;
43 }
44 
45 
46 template<class Type>
48 (
49  const word& entryName,
50  const Type& defaultValue
51 ) const
52 {
53  Type result = defaultValue;
54  getProperty(entryName, result);
55  return result;
56 }
57 
58 
59 template<class Type>
61 (
62  const word& entryName,
63  Type& value
64 ) const
65 {
66  getObjectProperty(name_, entryName, value);
67 }
68 
69 
70 template<class Type>
72 (
73  const word& entryName,
74  const Type& value
75 )
76 {
77  setObjectProperty(name_, entryName, value);
78 }
79 
80 
81 template<class Type>
83 (
84  const word& objectName,
85  const word& entryName,
86  const Type& defaultValue
87 ) const
88 {
89  Type result = defaultValue;
90  getObjectProperty(objectName, entryName, result);
91  return result;
92 }
93 
94 
95 template<class Type>
97 (
98  const word& objectName,
99  const word& entryName,
100  Type& value
101 ) const
102 {
103  if (stateDict_.found(objectName))
104  {
105  const dictionary& baseDict = stateDict_.subDict(objectName);
106  if (baseDict.found(entryName))
107  {
108  if (baseDict.isDict(entryName))
109  {
110  value = baseDict.subDict(entryName);
111  }
112  else
113  {
114  baseDict.lookup(entryName) >> value;
115  }
116  }
117  }
118 }
119 
120 
121 template<class Type>
123 (
124  const word& objectName,
125  const word& entryName,
126  const Type& value
127 )
128 {
129  if (!stateDict_.found(objectName))
130  {
131  stateDict_.add(objectName, dictionary());
132  }
133 
134  dictionary& baseDict = stateDict_.subDict(objectName);
135  baseDict.add(entryName, value, true);
136 }
137 
138 
139 template<class Type>
141 (
142  const word& entryName,
143  const Type& value
144 )
145 {
146  setObjectResult(name_, entryName, value);
147 }
148 
149 
150 template<class Type>
152 (
153  const word& objectName,
154  const word& entryName,
155  const Type& value
156 )
157 {
158  if (!stateDict_.found(resultsName_))
159  {
160  stateDict_.add(resultsName_, dictionary());
161  }
162 
163  dictionary& resultsDict = stateDict_.subDict(resultsName_);
164 
165  if (!resultsDict.found(objectName))
166  {
167  resultsDict.add(name_, dictionary());
168  }
169 
170  dictionary& objectDict = resultsDict.subDict(objectName);
171 
172  const word& dictTypeName = pTraits<Type>::typeName;
173 
174  if (!objectDict.found(dictTypeName))
175  {
176  objectDict.add(dictTypeName, dictionary());
177  }
178 
179  dictionary& resultTypeDict = objectDict.subDict(dictTypeName);
180 
181  resultTypeDict.add(entryName, value, true);
182 }
183 
184 
185 template<class Type>
187 (
188  const word& entryName,
189  const Type& defaultValue
190 ) const
191 {
192  return getObjectResult(name_, entryName, defaultValue);
193 }
194 
195 
196 template<class Type>
198 (
199  const word& objectName,
200  const word& entryName,
201  const Type& defaultValue
202 ) const
203 {
204  Type result = defaultValue;
205  getObjectResult(objectName, entryName, result);
206  return result;
207 }
208 
209 
210 template<class Type>
212 (
213  const word& objectName,
214  const word& entryName,
215  Type& value
216 ) const
217 {
218  if (stateDict_.found(resultsName_))
219  {
220  const dictionary& resultsDict = stateDict_.subDict(resultsName_);
221 
222  if (resultsDict.found(objectName))
223  {
224  const dictionary& objectDict = resultsDict.subDict(objectName);
225 
226  const word& dictTypeName = pTraits<Type>::typeName;
227 
228  if (objectDict.found(dictTypeName))
229  {
230  const dictionary& resultTypeDict =
231  objectDict.subDict(dictTypeName);
232 
233  resultTypeDict.readIfPresent<Type>(entryName, value);
234  }
235  }
236  }
237 }
238 
239 
240 // ************************************************************************* //
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::functionObjectState::getProperty
Type getProperty(const word &entryName, const Type &defaultValue=pTraits< Type >::zero) const
Retrieve generic property.
Definition: functionObjectStateTemplates.C:48
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::functionObjectState::setActive
bool setActive()
Set the active status by querying objectRegistry type.
Definition: functionObjectStateTemplates.C:29
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::dictionary::isDict
bool isDict(const word &) const
Check if entry is a sub-dictionary.
Definition: dictionary.C:600
Foam::functionObjectState::setProperty
void setProperty(const word &entryName, const Type &value)
Add generic property.
Definition: functionObjectStateTemplates.C:72
Foam::functionObjectState::setObjectResult
void setObjectResult(const word &objectName, const word &entryName, const Type &value)
Add result from named object.
Definition: functionObjectStateTemplates.C:152
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::functionObjectState::getResult
Type getResult(const word &entryName, const Type &defaultValue=pTraits< Type >::zero) const
Retrieve result.
Definition: functionObjectStateTemplates.C:187
Foam::functionObjectState::obr_
const objectRegistry & obr_
Reference to the database.
Definition: functionObjectState.H:64
Foam::functionObjectState::setResult
void setResult(const word &entryName, const Type &value)
Add result.
Definition: functionObjectStateTemplates.C:141
Foam::functionObjectState::setObjectProperty
void setObjectProperty(const word &objectName, const word &entryName, const Type &value)
Add generic property from named object.
Definition: functionObjectStateTemplates.C:123
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::functionObjectState::getObjectResult
Type getObjectResult(const word &objectName, const word &entryName, const Type &defaultValue=pTraits< Type >::zero) const
Retrieve result from named object.
Definition: functionObjectStateTemplates.C:198
Foam::functionObjectState::active_
bool active_
Flag to indicate whether the object is active.
Definition: functionObjectState.H:75
Foam::functionObjectState::name_
const word name_
Name of model.
Definition: functionObjectState.H:72
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
Foam::functionObjectState::getObjectProperty
Type getObjectProperty(const word &objectName, const word &entryName, const Type &defaultValue=pTraits< Type >::zero) const
Retrieve generic property from named object.
Definition: functionObjectStateTemplates.C:83
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729