Commit c6520033 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: rationalize dictionary access methods

- use keyType::option enum to consolidate searching options.
  These enumeration names should be more intuitive to use
  and improve code readability.

    Eg,   lookupEntry(key, keyType::REGEX);
    vs    lookupEntry(key, false, true);

  or

    Eg,   lookupEntry(key, keyType::LITERAL_RECURSIVE);
    vs    lookupEntry(key, true, false);

- new findEntry(), findDict(), findScoped() methods with consolidated
  search options for shorter naming and access names more closely
  aligned with other components. Behave simliarly to the
  methods lookupEntryPtr(), subDictPtr(), lookupScopedEntryPtr(),
  respectively. Default search parameters consistent with lookupEntry().

    Eg, const entry* e = dict.findEntry(key);
    vs  const entry* e = dict.lookupEntryPtr(key, false, true);

- added '*' and '->' dereference operators to dictionary searchers.
parent 4f9e45fb
......@@ -82,8 +82,8 @@ int main(int argc, char *argv[])
Info<< "dict1.toc(): " << dict1.name() << " " << dict1.toc()
<< endl;
dictionary dict3(dict2.subDictPtr("boundaryField"));
dictionary dict4(dict2.subDictPtr("NONEXISTENT"));
dictionary dict3(dict2.findDict("boundaryField"));
dictionary dict4(dict2.findDict("NONEXISTENT"));
Info<< "dictionary construct from pointer" << nl
<< "ok = " << dict3.name() << " " << dict3.toc() << nl
......@@ -105,23 +105,17 @@ int main(int argc, char *argv[])
Info<< "Pattern find \"abc\" in top directory : "
<< dict.lookup("abc") << endl;
Info<< "Pattern find \"abc\" in sub directory : "
<< dict.subDict("someDict").lookup("abc")
<< endl;
<< dict.subDict("someDict").lookup("abc") << nl;
Info<< "Recursive pattern find \"def\" in sub directory : "
<< dict.subDict("someDict").lookup("def", true)
<< endl;
<< dict.subDict("someDict").lookup("def", true) << nl;
Info<< "Recursive pattern find \"foo\" in sub directory : "
<< dict.subDict("someDict").lookup("foo", true)
<< endl;
<< dict.subDict("someDict").lookup("foo", true) << nl;
Info<< "Recursive pattern find \"fooz\" in sub directory : "
<< dict.subDict("someDict").lookup("fooz", true)
<< endl;
<< dict.subDict("someDict").lookup("fooz", true) << nl;
Info<< "Recursive pattern find \"bar\" in sub directory : "
<< dict.subDict("someDict").lookup("bar", true)
<< endl;
<< dict.subDict("someDict").lookup("bar", true) << nl;
Info<< "Recursive pattern find \"xxx\" in sub directory : "
<< dict.subDict("someDict").lookup("xxx", true)
<< endl;
<< dict.subDict("someDict").lookup("xxx", true) << nl;
}
}
else
......
......@@ -52,20 +52,16 @@ bool checkDictionaryContent(const dictionary& dict1, const dictionary& dict2)
forAllConstIter(dictionary, dict1, iter1)
{
const entry* entryPtr = dict2.lookupEntryPtr
(
iter1().keyword(),
false,
false
);
const entry* eptr =
dict2.findEntry(iter1().keyword(), keyType::LITERAL);
if (!entryPtr)
if (!eptr)
{
return false;
}
const entry& entry1 = iter1();
const entry& entry2 = *entryPtr;
const entry& entry2 = *eptr;
bool ok = false;
if (entry1.isDict())
......
......@@ -63,9 +63,13 @@ Foam::cellSizeFunction::cellSizeFunction
defaultCellSize_(defaultCellSize),
regionIndices_(regionIndices),
sideMode_(),
priority_(cellSizeFunctionDict.get<label>("priority", true))
priority_
(
cellSizeFunctionDict.get<label>("priority", keyType::REGEX_RECURSIVE)
)
{
const word mode = cellSizeFunctionDict.get<word>("mode", true);
const word mode =
cellSizeFunctionDict.get<word>("mode", keyType::REGEX_RECURSIVE);
if (surface_.hasVolumeType())
{
......
......@@ -837,7 +837,7 @@ void Foam::conformalVoronoiMesh::checkCellSizing()
= dict.subDict("meshQualityControls");
const scalar maxNonOrtho =
meshQualityDict.get<scalar>("maxNonOrtho", true);
meshQualityDict.get<scalar>("maxNonOrtho", keyType::REGEX_RECURSIVE);
label nWrongFaces = 0;
......
......@@ -339,7 +339,7 @@ Foam::conformationSurfaces::conformationSurfaces
{
const word& geomName = allGeometry_.names()[geomI];
const entry* ePtr = surfacesDict.lookupEntryPtr(geomName, false, true);
const entry* ePtr = surfacesDict.findEntry(geomName, keyType::REGEX);
if (ePtr)
{
......
......@@ -125,7 +125,7 @@ autoPtr<refinementSurfaces> createRefinementSurfaces
{
const word& geomName = allGeometry.names()[geomi];
const entry* ePtr = surfacesDict.lookupEntryPtr(geomName, false, true);
const entry* ePtr = surfacesDict.findEntry(geomName, keyType::REGEX);
if (ePtr)
{
......
......@@ -170,9 +170,10 @@ class dictAndKeyword
word key_;
public:
dictAndKeyword(const word& scopedName)
{
string::size_type i = scopedName.rfind('/');
auto i = scopedName.rfind('/');
if (i == string::npos)
{
i = scopedName.rfind('.');
......@@ -212,7 +213,7 @@ const dictionary& lookupScopedDict
return dict;
}
const entry* eptr = dict.lookupScopedEntryPtr(subDictName, false, false);
const entry* eptr = dict.findScoped(subDictName, keyType::LITERAL);
if (!eptr || !eptr->isDict())
{
......@@ -231,7 +232,7 @@ void removeDict(dictionary& dict, const dictionary& dictToRemove)
{
for (const entry& refEntry : dictToRemove)
{
auto finder = dict.search(refEntry.keyword(), false, false);
auto finder = dict.search(refEntry.keyword(), keyType::LITERAL);
bool purge = false;
......@@ -357,8 +358,7 @@ int main(int argc, char *argv[])
bool changed = false;
// Read but preserve headers
dictionary dict;
dict.read(dictFile(), true);
dictionary dict(dictFile(), true);
if (listIncludes)
{
......@@ -455,12 +455,7 @@ int main(int argc, char *argv[])
changed = true;
// Print the changed entry
const auto finder = dict.csearchScoped
(
scopedName,
false,
true // Support wildcards
);
const auto finder = dict.csearchScoped(scopedName, keyType::REGEX);
if (finder.found())
{
......@@ -489,8 +484,8 @@ int main(int argc, char *argv[])
const dictionary& d1(lookupScopedDict(dict, dAk.dict()));
const dictionary& d2(lookupScopedDict(diffDict, dAk.dict()));
const entry* e1Ptr = d1.lookupEntryPtr(dAk.key(), false, true);
const entry* e2Ptr = d2.lookupEntryPtr(dAk.key(), false, true);
const entry* e1Ptr = d1.findEntry(dAk.key(), keyType::REGEX);
const entry* e2Ptr = d2.findEntry(dAk.key(), keyType::REGEX);
if (e1Ptr && e2Ptr)
{
......@@ -509,12 +504,7 @@ int main(int argc, char *argv[])
}
}
const auto finder = dict.csearchScoped
(
scopedName,
false,
true // Support wildcards
);
const auto finder = dict.csearchScoped(scopedName, keyType::REGEX);
if (!finder.found())
{
......
......@@ -183,7 +183,7 @@ int main(int argc, char *argv[])
// Assumed to be good if it has 'profiling' sub-dict
const dictionary* ptr = dict.subDictPtr(blockNameProfiling);
const dictionary* ptr = dict.findDict(blockNameProfiling);
if (ptr)
{
++nDict;
......@@ -295,13 +295,12 @@ int main(int argc, char *argv[])
for (const dictionary& procDict : profiles)
{
const dictionary* inDictPtr =
procDict.subDictPtr(level1Name);
const dictionary* inDictPtr = procDict.findDict(level1Name);
if (inDictPtr && hasDictEntries)
{
// descend to the next level as required
inDictPtr = inDictPtr->subDictPtr(level2Name);
// Descend to the next level as required
inDictPtr = inDictPtr->findDict(level2Name);
}
if (!inDictPtr)
......@@ -313,16 +312,13 @@ int main(int argc, char *argv[])
for (const word& tag : tags)
{
const entry* eptr = inDictPtr->lookupEntryPtr
(
tag,
false,
false
);
scalar val;
if (eptr)
if
(
inDictPtr->readIfPresent(tag, val, keyType::LITERAL)
)
{
const scalar val = readScalar(eptr->stream());
stats(tag).append(val);
}
}
......@@ -339,7 +335,7 @@ int main(int argc, char *argv[])
if (hasDictEntries)
{
outputDict.add(level2Name, level1Dict.subDict(level2Name));
outDictPtr = outputDict.subDictPtr(level2Name);
outDictPtr = outputDict.findDict(level2Name);
}
else
{
......
......@@ -235,10 +235,9 @@ bool merge
// Save current (non-wildcard) keys before adding items.
wordHashSet thisKeysSet;
{
List<keyType> keys = thisDict.keys(false);
forAll(keys, i)
for (const word& k : thisDict.keys(false))
{
thisKeysSet.insert(keys[i]);
thisKeysSet.insert(k);
}
}
......@@ -261,25 +260,20 @@ bool merge
}
else if (literalRE || !(key.isPattern() || shortcuts.found(key)))
{
entry* entryPtr = thisDict.lookupEntryPtr
(
key,
false, // recursive
false // patternMatch
);
entry* eptr = thisDict.findEntry(key, keyType::LITERAL);
if (entryPtr)
if (eptr)
{
// Mark thisDict entry as having been match for wildcard
// handling later on.
thisKeysSet.erase(entryPtr->keyword());
thisKeysSet.erase(eptr->keyword());
if
(
addEntry
(
thisDict,
*entryPtr,
*eptr,
mergeIter(),
literalRE,
shortcuts
......@@ -310,7 +304,7 @@ bool merge
// Pass 2. Wildcard or shortcut matches (if any) on any non-match keys.
if (!literalRE && thisKeysSet.size() > 0)
if (!literalRE && thisKeysSet.size())
{
// Pick up remaining dictionary entries
wordList thisKeys(thisKeysSet.toc());
......@@ -336,10 +330,10 @@ bool merge
);
// Remove all matches
forAll(matches, i)
for (const label matchi : matches)
{
const word& thisKey = thisKeys[matches[i]];
thisKeysSet.erase(thisKey);
const word& k = thisKeys[matchi];
thisKeysSet.erase(k);
}
changed = true;
}
......@@ -358,21 +352,18 @@ bool merge
);
// Add all matches
forAll(matches, i)
for (const label matchi : matches)
{
const word& thisKey = thisKeys[matches[i]];
const word& k = thisKeys[matchi];
entry& thisEntry = const_cast<entry&>
(
thisDict.lookupEntry(thisKey, false, false)
);
entry* eptr = thisDict.findEntry(k, keyType::LITERAL);
if
(
addEntry
(
thisDict,
thisEntry,
*eptr,
mergeIter(),
literalRE,
HashTable<wordList>(0) // no shortcuts
......@@ -627,8 +618,7 @@ int main(int argc, char *argv[])
fieldDict.lookupEntry
(
doneKeys[i],
false,
true
keyType::REGEX
).clone()
);
fieldDict.remove(doneKeys[i]);
......
......@@ -342,11 +342,11 @@ void Foam::Time::setControls()
void Foam::Time::setMonitoring(const bool forceProfiling)
{
const dictionary* profilingDict = controlDict_.subDictPtr("profiling");
const dictionary* profilingDict = controlDict_.findDict("profiling");
if (!profilingDict)
{
// ... or from etc/controlDict
profilingDict = debug::controlDict().subDictPtr("profiling");
profilingDict = debug::controlDict().findDict("profiling");
}
// initialize profiling on request
......
......@@ -99,7 +99,7 @@ void Foam::Time::readDict()
// DebugSwitches
if
(
(localDict = controlDict_.subDictPtr("DebugSwitches")) != nullptr
(localDict = controlDict_.findDict("DebugSwitches")) != nullptr
&& localDict->size()
)
{
......@@ -146,7 +146,7 @@ void Foam::Time::readDict()
// InfoSwitches
if
(
(localDict = controlDict_.subDictPtr("InfoSwitches")) != nullptr
(localDict = controlDict_.findDict("InfoSwitches")) != nullptr
&& localDict->size()
)
{
......@@ -192,7 +192,7 @@ void Foam::Time::readDict()
// OptimisationSwitches
if
(
(localDict = controlDict_.subDictPtr("OptimisationSwitches")) != nullptr
(localDict = controlDict_.findDict("OptimisationSwitches")) != nullptr
&& localDict->size()
)
{
......@@ -273,7 +273,7 @@ void Foam::Time::readDict()
if
(
(localDict = controlDict_.subDictPtr("DimensionedConstants")) != nullptr
(localDict = controlDict_.findDict("DimensionedConstants")) != nullptr
&& localDict->size()
)
{
......@@ -310,7 +310,7 @@ void Foam::Time::readDict()
// DimensionSets
if
(
(localDict = controlDict_.subDictPtr("DimensionSets")) != nullptr
(localDict = controlDict_.findDict("DimensionSets")) != nullptr
&& localDict->size()
)
{
......
......@@ -134,13 +134,14 @@ void Foam::dictionary::checkITstream
Foam::dictionary::dictionary()
:
name_(),
parent_(dictionary::null)
{}
Foam::dictionary::dictionary(const fileName& name)
:
dictionaryName(name),
name_(name),
parent_(dictionary::null)
{}
......@@ -151,8 +152,8 @@ Foam::dictionary::dictionary
const dictionary& dict
)
:
dictionaryName(dict.name()),
parent_type(dict, *this),
name_(dict.name()),
parent_(parentDict)
{
forAllIter(parent_type, *this, iter)
......@@ -173,8 +174,8 @@ Foam::dictionary::dictionary
const dictionary& dict
)
:
dictionaryName(dict.name()),
parent_type(dict, *this),
name_(dict.name()),
parent_(dictionary::null)
{
forAllIter(parent_type, *this, iter)
......@@ -190,16 +191,14 @@ Foam::dictionary::dictionary
}
Foam::dictionary::dictionary
(
const dictionary* dictPtr
)
Foam::dictionary::dictionary(const dictionary* dict)
:
name_(),
parent_(dictionary::null)
{
if (dictPtr)
if (dict)
{
operator=(*dictPtr);
operator=(*dict);
}
}
......@@ -210,6 +209,7 @@ Foam::dictionary::dictionary
dictionary&& dict
)
:
name_(),
parent_(parentDict)
{
transfer(dict);
......@@ -222,6 +222,7 @@ Foam::dictionary::dictionary
dictionary&& dict
)
:
name_(),
parent_(dictionary::null)
{
transfer(dict);
......@@ -310,44 +311,50 @@ Foam::tokenList Foam::dictionary::tokens() const
bool Foam::dictionary::found
(
const word& keyword,
bool recursive,
bool patternMatch
enum keyType::option matchOpt
) const
{
return csearch(keyword, recursive, patternMatch).found();
return csearch(keyword, matchOpt).found();
}
Foam::entry* Foam::dictionary::findEntry
(
const word& keyword,
enum keyType::option matchOpt
)
{
return search(keyword, matchOpt).ptr();
}
const Foam::entry* Foam::dictionary::lookupEntryPtr
const Foam::entry* Foam::dictionary::findEntry
(
const word& keyword,
bool recursive,
bool patternMatch
enum keyType::option matchOpt
) const
{
return csearch(keyword, recursive, patternMatch).ptr();
return csearch(keyword, matchOpt).ptr();
}
Foam::entry* Foam::dictionary::lookupEntryPtr
const Foam::entry* Foam::dictionary::findScoped
(
const word& keyword,
bool recursive,
bool patternMatch
)
enum keyType::option matchOpt
) const
{
return search(keyword, recursive, patternMatch).ptr();
return csearchScoped(keyword, matchOpt).ptr();
}
const Foam::entry& Foam::dictionary::lookupEntry
(
const word& keyword,
bool recursive,
bool patternMatch
enum keyType::option matchOpt
) const
{
const const_searcher finder(csearch(keyword, recursive, patternMatch));
const const_searcher finder(csearch(keyword, matchOpt));
if (!finder.found())
{
......@@ -364,22 +371,10 @@ const Foam::entry& Foam::dictionary::lookupEntry
Foam::ITstream& Foam::dictionary::lookup
(
const word& keyword,
bool recursive,
bool patternMatch
enum keyType::option matchOpt
) const
{
return lookupEntry(keyword, recursive, patternMatch).stream();
}
const Foam::entry* Foam::dictionary::lookupScopedEntryPtr
(
const word& keyword,
bool recursive,
bool patternMatch
) const
{
return csearchScoped(keyword, recursive, patternMatch).ptr();
return lookupEntry(keyword, matchOpt).stream();
}
......@@ -394,7 +389,7 @@ bool Foam::dictionary::substituteKeyword(const word& keyword, bool mergeEntry)
const word varName(keyword.substr(1), false);
// Lookup the variable name in the given dictionary
const const_searcher finder(csearch(varName, true, true));
const const_searcher finder(csearch(varName, keyType::REGEX_RECURSIVE));
// If defined insert its entries into this dictionary
if (finder.found())
......@@ -428,7 +423,7 @@ bool Foam::dictionary::substituteScopedKeyword