functionObjectPropertiesTemplates.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) 2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "IOdictionary.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const word& objectName,
36  const word& entryName,
37  const Type& defaultValue
38 ) const
39 {
40  Type result = defaultValue;
41  getObjectProperty(objectName, entryName, result);
42  return result;
43 }
44 
45 
46 template<class Type>
48 (
49  const word& objectName,
50  const word& entryName,
51  Type& value
52 ) const
53 {
54  if (this->found(objectName))
55  {
56  const dictionary& baseDict = this->subDict(objectName);
57  return baseDict.readIfPresent(entryName, value);
58  }
59 
60  return false;
61 }
62 
63 
64 template<class Type>
66 (
67  const word& objectName,
68  const word& entryName,
69  const Type& value
70 )
71 {
72  if (!this->found(objectName))
73  {
74  this->add(objectName, dictionary());
75  }
76 
77  dictionary& baseDict = this->subDict(objectName);
78  baseDict.add(entryName, value, true);
79 }
80 
81 
82 template<class Type>
84 (
85  const word& objectName,
86  const word& entryName,
87  const Type& value
88 )
89 {
90  if (!this->found(resultsName_))
91  {
92  this->add(resultsName_, dictionary());
93  }
94 
95  dictionary& resultsDict = this->subDict(resultsName_);
96 
97  if (!resultsDict.found(objectName))
98  {
99  resultsDict.add(objectName, dictionary());
100  }
101 
102  dictionary& objectDict = resultsDict.subDict(objectName);
103 
104  const word& dictTypeName = pTraits<Type>::typeName;
105 
106  if (!objectDict.found(dictTypeName))
107  {
108  objectDict.add(dictTypeName, dictionary());
109  }
110 
111  dictionary& resultTypeDict = objectDict.subDict(dictTypeName);
112 
113  resultTypeDict.add(entryName, value, true);
114 }
115 
116 
117 template<class Type>
119 (
120  const word& objectName,
121  const word& entryName,
122  const Type& defaultValue
123 ) const
124 {
125  Type result = defaultValue;
126  (void)getObjectResult(objectName, entryName, result);
127  return result;
128 }
129 
130 
131 template<class Type>
133 (
134  const word& objectName,
135  const word& entryName,
136  Type& value
137 ) const
138 {
139  if (this->found(resultsName_))
140  {
141  const dictionary& resultsDict = this->subDict(resultsName_);
142 
143  if (resultsDict.found(objectName))
144  {
145  const dictionary& objectDict = resultsDict.subDict(objectName);
146 
147  const word& dictTypeName = pTraits<Type>::typeName;
148 
149  if (objectDict.found(dictTypeName))
150  {
151  const dictionary& resultTypeDict =
152  objectDict.subDict(dictTypeName);
153 
154  return resultTypeDict.readIfPresent<Type>(entryName, value);
155  }
156  }
157  }
158 
159  return false;
160 }
161 
162 
163 // ************************************************************************* //
Foam::functionObjects::properties::getObjectProperty
Type getObjectProperty(const word &objectName, const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve generic property from named object.
Definition: functionObjectPropertiesTemplates.C:34
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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: dictionaryI.H:87
Foam::functionObjects::properties::getObjectResult
Type getObjectResult(const word &objectName, const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve result from named object.
Definition: functionObjectPropertiesTemplates.C:119
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:460
Foam::functionObjects::properties::setObjectProperty
void setObjectProperty(const word &objectName, const word &entryName, const Type &value)
Add generic property from named object.
Definition: functionObjectPropertiesTemplates.C:66
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
found
bool found
Definition: TABSMDCalcMethod2.H:32
IOdictionary.H
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::functionObjects::properties::setObjectResult
void setObjectResult(const word &objectName, const word &entryName, const Type &value)
Add result from named object.
Definition: functionObjectPropertiesTemplates.C:84
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:405