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")
);
// 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
// constructor
NURBS3DVolume::New
(void) NURBS3DVolume::New
(
NURBSdict.subDict(boxName),
dEntry.dict(),
mesh,
false // do not compute parametric coordinates
);

View File

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

View File

@ -1278,6 +1278,30 @@ public:
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
//- Find and return a T, or return the given default value.
@ -1316,29 +1340,7 @@ public:
return getOrAdd<T>(keyword, deflt, matchOpt);
}
//- Find and return a T, or return the given default value
//- 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")
ITstream& operator[](const word& keyword) const
@ -1360,7 +1362,7 @@ public:
}
//- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version
// \deprecated(2018-10) - use findEntry() method
FOAM_DEPRECATED_FOR(2018-10, "findEntry(keyType::option)")
entry* lookupEntryPtr
(
@ -1373,7 +1375,7 @@ public:
}
//- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version
// \deprecated(2018-10) - use findEntry() method
FOAM_DEPRECATED_FOR(2018-10, "findEntry(keyType::option)")
const entry* lookupEntryPtr
(
@ -1386,7 +1388,7 @@ public:
}
//- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version
// \deprecated(2018-10) - use findScoped() method
FOAM_DEPRECATED_FOR(2018-10, "findScoped(keyType::option)")
const entry* lookupScopedEntryPtr
(
@ -1527,30 +1529,6 @@ public:
return get<T>(keyword, matchOpt(recursive, patternMatch));
}
#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))
{
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;
}
}

View File

@ -625,18 +625,19 @@ bool Foam::ccm::reader::remapMeshInfo
return false;
}
const dictionary* subDictPtr = nullptr;
// 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;
}
// Rename boundaries
if (remapDict.isDict("boundaryRegion"))
if ((subDictPtr = remapDict.findDict("boundaryRegion")) != nullptr)
{
boundaryRegion_.rename(remapDict.subDict("boundaryRegion"));
boundaryRegion_.rename(*subDictPtr);
ok = true;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -76,12 +76,14 @@ bool Foam::ccm::writer::newFieldNode
ccmID& fieldNode
) const
{
if (!nameMapping.found(fieldName) || !nameMapping.isDict(fieldName))
const dictionary* subDictPtr = nameMapping.findDict(fieldName);
if (!subDictPtr)
{
return false;
}
const dictionary& dict = nameMapping.subDict(fieldName);
const dictionary& dict = *subDictPtr;
word shortName;
if (!dict.readIfPresent("name", shortName))
@ -165,9 +167,11 @@ void Foam::ccm::writer::writeSolution
dictionary nameMapping(defaultNameMapping);
// 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
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -252,7 +252,9 @@ Foam::functionObjects::energyTransport::energyTransport
{
const word& key = iter().keyword();
if (!multiphaseThermo_.isDict(key))
const dictionary* subDictPtr = multiphaseThermo_.findDict(key);
if (!subDictPtr)
{
FatalErrorInFunction
<< "Found non-dictionary entry " << iter()
@ -260,7 +262,7 @@ Foam::functionObjects::energyTransport::energyTransport
<< exit(FatalError);
}
const dictionary& dict = multiphaseThermo_.subDict(key);
const dictionary& dict = *subDictPtr;
phaseNames_[phasei] = key;

View File

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

View File

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

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2020 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -81,9 +81,11 @@ Foam::radiation::boundaryRadiationProperties::boundaryRadiationProperties
{
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
(

View File

@ -50,12 +50,16 @@ Foam::liquidMixtureProperties::liquidMixtureProperties
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
(
i,
liquidProperties::New(dict.subDict(components_[i]))
liquidProperties::New(*subDictPtr)
);
}
else

View File

@ -40,12 +40,16 @@ Foam::solidMixtureProperties::solidMixtureProperties(const dictionary& dict)
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
(
i,
solidProperties::New(dict.subDict(components_[i]))
solidProperties::New(*subDictPtr)
);
}
else

View File

@ -49,9 +49,13 @@ thermophysicalPropertiesSelector
{
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
{

View File

@ -85,9 +85,12 @@ Foam::surfaceTensionModels::constant::sigma() const
bool Foam::surfaceTensionModels::constant::readDict(const dictionary& dict)
{
// 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
{