functionObjectList: Rationalized and simplified the handling of disabled functionObjects

Simplified and generalized the handling of functionObjects which fail to
construct by removing them from the list rather than maintaining an
"enabled" switch in each functionObject.
This commit is contained in:
Henry Weller
2016-05-11 09:03:52 +01:00
parent 8663237a0f
commit de7ac625e4
9 changed files with 222 additions and 271 deletions

View File

@ -45,7 +45,7 @@ Foam::functionObject* Foam::functionObjectList::remove
{
oldIndex = fnd();
// retrieve the pointer and remove it from the old list
// Retrieve the pointer and remove it from the old list
ptr = this->set(oldIndex, 0).ptr();
indices_.erase(fnd);
}
@ -168,7 +168,7 @@ void Foam::functionObjectList::on()
void Foam::functionObjectList::off()
{
// for safety, also force a read() when execution is turned back on
// For safety, also force a read() when execution is turned back on
updated_ = execution_ = false;
}
@ -274,10 +274,10 @@ bool Foam::functionObjectList::read()
bool ok = true;
updated_ = execution_;
// avoid reading/initializing if execution is off
// Avoid reading/initializing if execution is off
if (!execution_)
{
return ok;
return true;
}
// Update existing and add new functionObjects
@ -296,31 +296,42 @@ bool Foam::functionObjectList::read()
label nFunc = 0;
if (entryPtr->isDict())
if (!entryPtr->isDict())
{
// a dictionary of functionObjects
const dictionary& functionDicts = entryPtr->dict();
FatalIOErrorInFunction(parentDict_)
<< "'functions' entry is not a dictionary"
<< exit(FatalIOError);
}
newPtrs.setSize(functionDicts.size());
newDigs.setSize(functionDicts.size());
const dictionary& functionDicts = entryPtr->dict();
forAllConstIter(dictionary, functionDicts, iter)
newPtrs.setSize(functionDicts.size());
newDigs.setSize(functionDicts.size());
forAllConstIter(dictionary, functionDicts, iter)
{
const word& key = iter().keyword();
if (!iter().isDict())
{
// safety:
if (!iter().isDict())
{
continue;
}
const word& key = iter().keyword();
const dictionary& dict = iter().dict();
IOWarningInFunction(parentDict_)
<< "Entry " << key << " is not a dictionary" << endl;
continue;
}
newDigs[nFunc] = dict.digest();
const dictionary& dict = iter().dict();
bool enabled = dict.lookupOrDefault("enabled", true);
label oldIndex;
functionObject* objPtr = remove(key, oldIndex);
if (objPtr)
newDigs[nFunc] = dict.digest();
label oldIndex;
functionObject* objPtr = remove(key, oldIndex);
if (objPtr)
{
if (enabled)
{
// an existing functionObject, and dictionary changed
// Dictionary changed for an existing functionObject
if (newDigs[nFunc] != digests_[oldIndex])
{
ok = objPtr->read(dict) && ok;
@ -328,65 +339,48 @@ bool Foam::functionObjectList::read()
}
else
{
// new functionObject
objPtr = functionObject::New(key, time_, dict).ptr();
ok = objPtr->start() && ok;
}
newPtrs.set(nFunc, objPtr);
newIndices.insert(key, nFunc);
nFunc++;
}
}
else
{
// a list of functionObjects
PtrList<entry> functionDicts(entryPtr->stream());
newPtrs.setSize(functionDicts.size());
newDigs.setSize(functionDicts.size());
forAllIter(PtrList<entry>, functionDicts, iter)
{
// safety:
if (!iter().isDict())
{
// Delete the disabled functionObject
delete objPtr;
objPtr = NULL;
continue;
}
const word& key = iter().keyword();
const dictionary& dict = iter().dict();
}
else if (enabled)
{
autoPtr<functionObject> foPtr;
newDigs[nFunc] = dict.digest();
label oldIndex;
functionObject* objPtr = remove(key, oldIndex);
if (objPtr)
FatalError.throwExceptions();
FatalIOError.throwExceptions();
try
{
// an existing functionObject, and dictionary changed
if (newDigs[nFunc] != digests_[oldIndex])
{
ok = objPtr->read(dict) && ok;
}
foPtr = functionObject::New(key, time_, dict);
}
else
catch (...)
{}
FatalError.dontThrowExceptions();
FatalIOError.dontThrowExceptions();
if (foPtr.valid())
{
// new functionObject
objPtr = functionObject::New(key, time_, dict).ptr();
objPtr = foPtr.ptr();
ok = objPtr->start() && ok;
}
}
// Insert active functionObjects into the list
if (objPtr)
{
newPtrs.set(nFunc, objPtr);
newIndices.insert(key, nFunc);
nFunc++;
}
}
// safety:
newPtrs.setSize(nFunc);
newDigs.setSize(nFunc);
// updating the PtrList of functionObjects also deletes any existing,
// but unused functionObjects
// Updating the PtrList of functionObjects deletes any
// existing unused functionObjects
PtrList<functionObject>::transfer(newPtrs);
digests_.transfer(newDigs);
indices_.transfer(newIndices);