ENH: use dictionary findDict() instead of isDict() + subDict()

- avoids redundant dictionary searching

STYLE: remove dictionary lookupOrDefaultCompat wrapper

- deprecated and replaced by getOrDefaultCompat (2019-05).
  The function is usually specific to internal keyword upgrading
  (version compatibility) and unlikely to exist in any user code.
This commit is contained in:
Mark Olesen
2022-09-28 09:24:46 +02:00
parent 9d5a3a5c54
commit 55f5f8774b
14 changed files with 110 additions and 116 deletions

View File

@ -60,18 +60,16 @@ int main(int argc, char *argv[])
) )
).subDict("volumetricBSplinesMotionSolverCoeffs") ).subDict("volumetricBSplinesMotionSolverCoeffs")
); );
// Read box names and allocate size
wordList controlBoxes(NURBSdict.toc());
for (const word& boxName : controlBoxes) for (const entry& dEntry : NURBSdict)
{ {
if (NURBSdict.isDict(boxName)) if (dEntry.isDict())
{ {
// Creating an object writes the control points in the // Creating an object writes the control points in the
// constructor // constructor
NURBS3DVolume::New (void) NURBS3DVolume::New
( (
NURBSdict.subDict(boxName), dEntry.dict(),
mesh, mesh,
false // do not compute parametric coordinates false // do not compute parametric coordinates
); );

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2021 OpenCFD Ltd. Copyright (C) 2015-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -373,6 +373,9 @@ int main(int argc, char *argv[])
} }
} }
const dictionary* subDictPtr = nullptr;
// //
// Extract features using the preferred extraction method // Extract features using the preferred extraction method
// //
@ -382,9 +385,9 @@ int main(int argc, char *argv[])
// ~~~~~~~~ // ~~~~~~~~
// Option: "trimFeatures" (dictionary) // Option: "trimFeatures" (dictionary)
if (surfaceDict.isDict("trimFeatures")) if ((subDictPtr = surfaceDict.findDict("trimFeatures")) != nullptr)
{ {
const dictionary& trimDict = surfaceDict.subDict("trimFeatures"); const dictionary& trimDict = *subDictPtr;
const scalar minLen = const scalar minLen =
trimDict.getOrDefault<scalar>("minLen", 0); trimDict.getOrDefault<scalar>("minLen", 0);
@ -419,18 +422,15 @@ int main(int argc, char *argv[])
List<surfaceFeatures::edgeStatus> edgeStat(features().toStatus()); List<surfaceFeatures::edgeStatus> edgeStat(features().toStatus());
// Option: "subsetFeatures" (dictionary) // Option: "subsetFeatures" (dictionary)
if (surfaceDict.isDict("subsetFeatures")) if ((subDictPtr = surfaceDict.findDict("subsetFeatures")) != nullptr)
{ {
const dictionary& subsetDict = surfaceDict.subDict const dictionary& subsetDict = *subDictPtr;
(
"subsetFeatures" treeBoundBox bb;
);
// Suboption: "insideBox" // Suboption: "insideBox"
if (subsetDict.found("insideBox")) if (subsetDict.readIfPresent("insideBox", bb))
{ {
treeBoundBox bb(subsetDict.lookup("insideBox"));
Info<< "Subset edges inside box " << bb << endl; Info<< "Subset edges inside box " << bb << endl;
features().subsetBox(edgeStat, bb); features().subsetBox(edgeStat, bb);
@ -445,10 +445,8 @@ int main(int argc, char *argv[])
} }
} }
// Suboption: "outsideBox" // Suboption: "outsideBox"
else if (subsetDict.found("outsideBox")) else if (subsetDict.readIfPresent("outsideBox", bb))
{ {
treeBoundBox bb(subsetDict.lookup("outsideBox"));
Info<< "Exclude edges outside box " << bb << endl; Info<< "Exclude edges outside box " << bb << endl;
features().excludeBox(edgeStat, bb); features().excludeBox(edgeStat, bb);
@ -549,12 +547,11 @@ int main(int argc, char *argv[])
); );
if (surfaceDict.isDict("addFeatures")) if ((subDictPtr = surfaceDict.findDict("addFeatures")) != nullptr)
{ {
const word addFeName const dictionary& addFeaturesDict = *subDictPtr;
(
surfaceDict.subDict("addFeatures").get<word>("name") const word addFeName = addFeaturesDict.get<word>("name");
);
Info<< "Adding (without merging) features from " << addFeName Info<< "Adding (without merging) features from " << addFeName
<< nl << endl; << nl << endl;

View File

@ -1278,6 +1278,30 @@ public:
friend Ostream& operator<<(Ostream& os, const dictionary& dict); friend Ostream& operator<<(Ostream& os, const dictionary& dict);
// Shortcuts - when a templated classes also inherits from a dictionary
#undef defineDictionaryGetter
#define defineDictionaryGetter(Func, Type) \
/*! \brief Same as get\<Type\>(const word&, keyType::option) */ \
Type Func \
( \
const word& keyword, \
enum keyType::option matchOpt = keyType::REGEX \
) const \
{ \
return get<Type>(keyword, matchOpt); \
}
defineDictionaryGetter(getBool, bool);
defineDictionaryGetter(getLabel, label);
defineDictionaryGetter(getScalar, scalar);
defineDictionaryGetter(getString, string);
defineDictionaryGetter(getWord, word);
defineDictionaryGetter(getFileName, fileName);
#undef defineDictionaryGetter
// Housekeeping // Housekeeping
//- Find and return a T, or return the given default value. //- Find and return a T, or return the given default value.
@ -1316,29 +1340,7 @@ public:
return getOrAdd<T>(keyword, deflt, matchOpt); return getOrAdd<T>(keyword, deflt, matchOpt);
} }
//- Find and return a T, or return the given default value //- Deprecated(2018-07) - use lookup() method
//- using any compatibility names if needed.
//
// \param keyword the keyword to search for
// \param compat list of old compatibility keywords and the last
// OpenFOAM version for which they were used.
// \param deflt the default value to use
// \param matchOpt search mode (default: non-recursive with patterns)
template<class T>
FOAM_DEPRECATED_FOR(2019-05, "getOrDefaultCompat() method")
T lookupOrDefaultCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
const T& deflt,
enum keyType::option matchOpt = keyType::REGEX
) const
{
return getOrDefaultCompat<T>(keyword, compat, deflt, matchOpt);
}
//- Deprecated(2018-07) find and return an entry data stream
//
// \deprecated(2018-07) - use lookup() method // \deprecated(2018-07) - use lookup() method
FOAM_DEPRECATED_FOR(2018-07, "lookup() method") FOAM_DEPRECATED_FOR(2018-07, "lookup() method")
ITstream& operator[](const word& keyword) const ITstream& operator[](const word& keyword) const
@ -1360,7 +1362,7 @@ public:
} }
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use findEntry() method
FOAM_DEPRECATED_FOR(2018-10, "findEntry(keyType::option)") FOAM_DEPRECATED_FOR(2018-10, "findEntry(keyType::option)")
entry* lookupEntryPtr entry* lookupEntryPtr
( (
@ -1373,7 +1375,7 @@ public:
} }
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use findEntry() method
FOAM_DEPRECATED_FOR(2018-10, "findEntry(keyType::option)") FOAM_DEPRECATED_FOR(2018-10, "findEntry(keyType::option)")
const entry* lookupEntryPtr const entry* lookupEntryPtr
( (
@ -1386,7 +1388,7 @@ public:
} }
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use findScoped() method
FOAM_DEPRECATED_FOR(2018-10, "findScoped(keyType::option)") FOAM_DEPRECATED_FOR(2018-10, "findScoped(keyType::option)")
const entry* lookupScopedEntryPtr const entry* lookupScopedEntryPtr
( (
@ -1527,30 +1529,6 @@ public:
return get<T>(keyword, matchOpt(recursive, patternMatch)); return get<T>(keyword, matchOpt(recursive, patternMatch));
} }
#endif #endif
// Shortcuts - when a templated classes also inherits from a dictionary
#undef defineDictionaryGetter
#define defineDictionaryGetter(Func, Type) \
/*! \brief Same as get\<Type\>(const word&, keyType::option) */ \
Type Func \
( \
const word& keyword, \
enum keyType::option matchOpt = keyType::REGEX \
) const \
{ \
return get<Type>(keyword, matchOpt); \
}
defineDictionaryGetter(getBool, bool);
defineDictionaryGetter(getLabel, label);
defineDictionaryGetter(getScalar, scalar);
defineDictionaryGetter(getString, string);
defineDictionaryGetter(getWord, word);
defineDictionaryGetter(getFileName, fileName);
#undef defineDictionaryGetter
}; };

View File

@ -137,10 +137,11 @@ bool Foam::functionObjects::properties::getObjectDict
if (found(objectName)) if (found(objectName))
{ {
const dictionary& baseDict = subDict(objectName); const dictionary& baseDict = subDict(objectName);
const dictionary* subDictPtr = baseDict.findDict(objectName);
if (baseDict.found(entryName) && baseDict.isDict(entryName)) if (subDictPtr)
{ {
dict = baseDict.subDict(entryName); dict = *subDictPtr;
return true; return true;
} }
} }

View File

@ -625,18 +625,19 @@ bool Foam::ccm::reader::remapMeshInfo
return false; return false;
} }
const dictionary* subDictPtr = nullptr;
// Merge specified cellTable entries together // Merge specified cellTable entries together
if (remapDict.isDict("cellTable")) if ((subDictPtr = remapDict.findDict("cellTable")) != nullptr)
{ {
cellTable_.combine(remapDict.subDict("cellTable"), cellTableId_); cellTable_.combine(*subDictPtr, cellTableId_);
ok = true; ok = true;
} }
// Rename boundaries // Rename boundaries
if (remapDict.isDict("boundaryRegion")) if ((subDictPtr = remapDict.findDict("boundaryRegion")) != nullptr)
{ {
boundaryRegion_.rename(remapDict.subDict("boundaryRegion")); boundaryRegion_.rename(*subDictPtr);
ok = true; ok = true;
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -76,12 +76,14 @@ bool Foam::ccm::writer::newFieldNode
ccmID& fieldNode ccmID& fieldNode
) const ) const
{ {
if (!nameMapping.found(fieldName) || !nameMapping.isDict(fieldName)) const dictionary* subDictPtr = nameMapping.findDict(fieldName);
if (!subDictPtr)
{ {
return false; return false;
} }
const dictionary& dict = nameMapping.subDict(fieldName); const dictionary& dict = *subDictPtr;
word shortName; word shortName;
if (!dict.readIfPresent("name", shortName)) if (!dict.readIfPresent("name", shortName))
@ -165,9 +167,11 @@ void Foam::ccm::writer::writeSolution
dictionary nameMapping(defaultNameMapping); dictionary nameMapping(defaultNameMapping);
// Merge without overwrite // Merge without overwrite
if (remapDict.isDict("fields")) const dictionary* subDictPtr = remapDict.findDict("fields");
if (subDictPtr)
{ {
nameMapping |= remapDict.subDict("fields"); nameMapping |= *subDictPtr;
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd. Copyright (C) 2017-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -252,7 +252,9 @@ Foam::functionObjects::energyTransport::energyTransport
{ {
const word& key = iter().keyword(); const word& key = iter().keyword();
if (!multiphaseThermo_.isDict(key)) const dictionary* subDictPtr = multiphaseThermo_.findDict(key);
if (!subDictPtr)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Found non-dictionary entry " << iter() << "Found non-dictionary entry " << iter()
@ -260,7 +262,7 @@ Foam::functionObjects::energyTransport::energyTransport
<< exit(FatalError); << exit(FatalError);
} }
const dictionary& dict = multiphaseThermo_.subDict(key); const dictionary& dict = *subDictPtr;
phaseNames_[phasei] = key; phaseNames_[phasei] = key;

View File

@ -64,31 +64,26 @@ volBSplinesBase::volBSplinesBase
) )
).subDict("volumetricBSplinesMotionSolverCoeffs") ).subDict("volumetricBSplinesMotionSolverCoeffs")
); );
// Read box names and allocate size
wordList controlBoxes(NURBSdict.toc());
volume_.setSize(controlBoxes.size());
// Populate NURBS volumes // Populate NURBS volumes
volume_.resize(NURBSdict.size());
label iBox(0); label iBox(0);
for (const word& boxName : controlBoxes)
for (const entry& dEntry : NURBSdict)
{ {
if (NURBSdict.isDict(boxName)) if (dEntry.isDict())
{ {
volume_.set volume_.set
( (
iBox, iBox,
NURBS3DVolume::New NURBS3DVolume::New(dEntry.dict(), mesh, true)
(
NURBSdict.subDict(boxName),
mesh,
true
)
); );
volume_[iBox].writeParamCoordinates(); volume_[iBox].writeParamCoordinates();
iBox++; iBox++;
} }
} }
volume_.setSize(iBox); volume_.resize(iBox);
// Determine active design variables // Determine active design variables
activeDesignVariables_.setSize(3*getTotalControlPointsNumber(), -1); activeDesignVariables_.setSize(3*getTotalControlPointsNumber(), -1);

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012-2017 OpenFOAM Foundation Copyright (C) 2012-2017 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd. Copyright (C) 2019-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -50,7 +50,9 @@ Foam::autoPtr<ChemistryModel> Foam::basicChemistryModel::New
) )
); );
if (!chemistryDict.isDict("chemistryType")) const dictionary* subDictPtr = chemistryDict.findDict("chemistryType");
if (!subDictPtr)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Template parameter based chemistry solver selection is no " << "Template parameter based chemistry solver selection is no "
@ -64,8 +66,7 @@ Foam::autoPtr<ChemistryModel> Foam::basicChemistryModel::New
<< endl << " }" << exit(FatalError); << endl << " }" << exit(FatalError);
} }
const dictionary& chemistryTypeDict = const dictionary& chemistryTypeDict = *subDictPtr;
chemistryDict.subDict("chemistryType");
const word solverName const word solverName
( (

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2015-2020 OpenCFD Ltd. Copyright (C) 2015-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -81,9 +81,11 @@ Foam::radiation::boundaryRadiationProperties::boundaryRadiationProperties
{ {
const polyPatch& pp = mesh.boundaryMesh()[patchi]; const polyPatch& pp = mesh.boundaryMesh()[patchi];
if (radiationDict.isDict(pp.name())) const dictionary* subDictPtr = radiationDict.findDict(pp.name());
if (subDictPtr)
{ {
const dictionary& dict = radiationDict.subDict(pp.name()); const dictionary& dict = *subDictPtr;
radBoundaryPropertiesPtrList_[patchi].reset radBoundaryPropertiesPtrList_[patchi].reset
( (

View File

@ -50,12 +50,16 @@ Foam::liquidMixtureProperties::liquidMixtureProperties
forAll(components_, i) forAll(components_, i)
{ {
if (dict.isDict(components_[i])) // Handle sub-dictionary or primitive entry
const dictionary* subDictPtr = dict.findDict(components_[i]);
if (subDictPtr)
{ {
properties_.set properties_.set
( (
i, i,
liquidProperties::New(dict.subDict(components_[i])) liquidProperties::New(*subDictPtr)
); );
} }
else else

View File

@ -40,12 +40,16 @@ Foam::solidMixtureProperties::solidMixtureProperties(const dictionary& dict)
forAll(components_, i) forAll(components_, i)
{ {
if (dict.isDict(components_[i])) // Handle sub-dictionary or primitive entry
const dictionary* subDictPtr = dict.findDict(components_[i]);
if (subDictPtr)
{ {
properties_.set properties_.set
( (
i, i,
solidProperties::New(dict.subDict(components_[i])) solidProperties::New(*subDictPtr)
); );
} }
else else

View File

@ -49,9 +49,13 @@ thermophysicalPropertiesSelector
{ {
const word name(dict.first()->keyword()); const word name(dict.first()->keyword());
if (dict.isDict(name)) // Handle sub-dictionary or primitive entry
const dictionary* subDictPtr = dict.findDict(name);
if (subDictPtr)
{ {
propertiesPtr_ = ThermophysicalProperties::New(dict.subDict(name)); propertiesPtr_ = ThermophysicalProperties::New(*subDictPtr);
} }
else else
{ {

View File

@ -85,9 +85,12 @@ Foam::surfaceTensionModels::constant::sigma() const
bool Foam::surfaceTensionModels::constant::readDict(const dictionary& dict) bool Foam::surfaceTensionModels::constant::readDict(const dictionary& dict)
{ {
// Handle sub-dictionary format as a special case // Handle sub-dictionary format as a special case
if (dict.isDict("sigma"))
const dictionary* subDictPtr = dict.findDict("sigma");
if (subDictPtr)
{ {
dict.subDict("sigma").readEntry("sigma", sigma_); subDictPtr->readEntry("sigma", sigma_);
} }
else else
{ {