functionObjectList with SHA1Digest tracking

This commit is contained in:
Mark Olesen
2009-02-12 10:51:30 +01:00
parent dfe1df4c61
commit b5a1f09321
5 changed files with 74 additions and 40 deletions

View File

@ -74,7 +74,7 @@ namespace Foam
} }
else else
{ {
functionObjectList fol(runTime, runTime.controlDict()); functionObjectList fol(runTime);
fol.start(); fol.start();
fol.execute(); fol.execute();
} }

View File

@ -71,10 +71,10 @@ Foam::autoPtr<Foam::functionObject> Foam::functionObject::New
FatalErrorIn FatalErrorIn
( (
"functionObject::New" "functionObject::New"
"(const word& functionType, const Time&, const dictionary&)" "(const word& name, const Time&, const dictionary&)"
) << "Unknown function type " ) << "Unknown function type "
<< functionType << endl << endl << functionType << nl << nl
<< "Table of functionObjects is empty" << "Table of functionObjects is empty" << endl
<< exit(FatalError); << exit(FatalError);
} }
@ -86,11 +86,11 @@ Foam::autoPtr<Foam::functionObject> Foam::functionObject::New
FatalErrorIn FatalErrorIn
( (
"functionObject::New" "functionObject::New"
"(const word& functionType, const Time&, const dictionary&)" "(const word& name, const Time&, const dictionary&)"
) << "Unknown function type " ) << "Unknown function type "
<< functionType << endl << endl << functionType << nl << nl
<< "Valid functions are : " << endl << "Valid functions are : " << nl
<< dictionaryConstructorTablePtr_->toc() << dictionaryConstructorTablePtr_->toc() << endl
<< exit(FatalError); << exit(FatalError);
} }

View File

@ -121,7 +121,7 @@ public:
// Selectors // Selectors
//- Select from Time and Istream //- Select from dictionary, based on its "type" entry
static autoPtr<functionObject> New static autoPtr<functionObject> New
( (
const word& name, const word& name,
@ -143,7 +143,7 @@ public:
//- execute is called at each ++ or += of the time-loop //- execute is called at each ++ or += of the time-loop
virtual bool execute() = 0; virtual bool execute() = 0;
//- Read and set the function object if its data has changed //- Read and set the function object if its data have changed
virtual bool read(const dictionary& dict) = 0; virtual bool read(const dictionary& dict) = 0;
}; };

View File

@ -29,7 +29,8 @@ License
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
Foam::functionObject* Foam::functionObjectList::remove(const word& key) Foam::functionObject*
Foam::functionObjectList::remove(const word& key, label& oldIndex)
{ {
functionObject* ptr = 0; functionObject* ptr = 0;
@ -38,10 +39,16 @@ Foam::functionObject* Foam::functionObjectList::remove(const word& key)
if (fnd != indices_.end()) if (fnd != indices_.end())
{ {
// remove the pointer from the old list oldIndex = fnd();
ptr = functions_.set(fnd(), 0).ptr();
// retrieve the pointer and remove it from the old list
ptr = this->set(oldIndex, 0).ptr();
indices_.erase(fnd); indices_.erase(fnd);
} }
else
{
oldIndex = -1;
}
return ptr; return ptr;
} }
@ -55,7 +62,8 @@ Foam::functionObjectList::functionObjectList
const bool execution const bool execution
) )
: :
functions_(), PtrList<functionObject>(),
digests_(),
indices_(), indices_(),
time_(t), time_(t),
parentDict_(t.controlDict()), parentDict_(t.controlDict()),
@ -71,7 +79,8 @@ Foam::functionObjectList::functionObjectList
const bool execution const bool execution
) )
: :
functions_(), PtrList<functionObject>(),
digests_(),
indices_(), indices_(),
time_(t), time_(t),
parentDict_(parentDict), parentDict_(parentDict),
@ -105,7 +114,7 @@ bool Foam::functionObjectList::execute()
read(); read();
} }
forAllIter(PtrList<functionObject>, functions_, iter) forAllIter(PtrList<functionObject>, *this, iter)
{ {
ok = iter().execute() && ok; ok = iter().execute() && ok;
} }
@ -123,7 +132,8 @@ void Foam::functionObjectList::on()
void Foam::functionObjectList::off() void Foam::functionObjectList::off()
{ {
execution_ = false; // for safety, also force a read() when execution is turned back on
updated_ = execution_ = false;
} }
@ -143,15 +153,19 @@ bool Foam::functionObjectList::read()
if (entryPtr) if (entryPtr)
{ {
PtrList<functionObject> newPtrs; PtrList<functionObject> newPtrs;
List<SHA1Digest> newDigs;
HashTable<label> newIndices; HashTable<label> newIndices;
label nFunc = 0; label nFunc = 0;
label oldIndex = -1;
if (entryPtr->isDict()) if (entryPtr->isDict())
{ {
// a dictionary of functionObjects // a dictionary of functionObjects
const dictionary& functionDicts = entryPtr->dict(); const dictionary& functionDicts = entryPtr->dict();
newPtrs.setSize(functionDicts.size()); newPtrs.setSize(functionDicts.size());
newDigs.setSize(functionDicts.size());
forAllConstIter(dictionary, functionDicts, iter) forAllConstIter(dictionary, functionDicts, iter)
{ {
@ -163,12 +177,17 @@ bool Foam::functionObjectList::read()
const word& key = iter().keyword(); const word& key = iter().keyword();
const dictionary& dict = iter().dict(); const dictionary& dict = iter().dict();
functionObject* objPtr = remove(key); newDigs[nFunc] = dict.digest();
functionObject* objPtr = remove(key, oldIndex);
if (objPtr) if (objPtr)
{ {
// existing functionObject // an existing functionObject, and dictionary changed
if (newDigs[nFunc] != digests_[oldIndex])
{
ok = objPtr->read(dict) && ok; ok = objPtr->read(dict) && ok;
} }
}
else else
{ {
// new functionObject // new functionObject
@ -185,7 +204,9 @@ bool Foam::functionObjectList::read()
{ {
// a list of functionObjects // a list of functionObjects
PtrList<entry> functionDicts(entryPtr->stream()); PtrList<entry> functionDicts(entryPtr->stream());
newPtrs.setSize(functionDicts.size()); newPtrs.setSize(functionDicts.size());
newDigs.setSize(functionDicts.size());
forAllIter(PtrList<entry>, functionDicts, iter) forAllIter(PtrList<entry>, functionDicts, iter)
{ {
@ -197,12 +218,17 @@ bool Foam::functionObjectList::read()
const word& key = iter().keyword(); const word& key = iter().keyword();
const dictionary& dict = iter().dict(); const dictionary& dict = iter().dict();
functionObject* objPtr = remove(key); newDigs[nFunc] = dict.digest();
functionObject* objPtr = remove(key, oldIndex);
if (objPtr) if (objPtr)
{ {
// existing functionObject // an existing functionObject, and dictionary changed
if (newDigs[nFunc] != digests_[oldIndex])
{
ok = objPtr->read(dict) && ok; ok = objPtr->read(dict) && ok;
} }
}
else else
{ {
// new functionObject // new functionObject
@ -218,15 +244,18 @@ bool Foam::functionObjectList::read()
// safety: // safety:
newPtrs.setSize(nFunc); newPtrs.setSize(nFunc);
newDigs.setSize(nFunc);
// update PtrList of functionObjects // updating the PtrList of functionObjects also deletes any existing,
// also deletes existing, unused functionObjects // but unused functionObjects
functions_.transfer(newPtrs); this->transfer(newPtrs);
digests_.transfer(newDigs);
indices_.transfer(newIndices); indices_.transfer(newIndices);
} }
else else
{ {
functions_.clear(); this->clear();
digests_.clear();
indices_.clear(); indices_.clear();
} }

View File

@ -43,6 +43,7 @@ SourceFiles
#include "functionObject.H" #include "functionObject.H"
#include "HashTable.H" #include "HashTable.H"
#include "PtrList.H" #include "PtrList.H"
#include "SHA1Digest.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -54,20 +55,20 @@ namespace Foam
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
class functionObjectList class functionObjectList
:
private PtrList<functionObject>
{ {
// Private data // Private data
//- A list of function objects //- A list of SHA1 digests for the function object dictionaries
// Avoid 'is-a' relationship for protection List<SHA1Digest> digests_;
PtrList<functionObject> functions_;
//- Quick lookup of the index into the PtrList<functionObject> //- Quick lookup of the index into functions/digests
// Currently only used to manage rereading/deletion
HashTable<label> indices_; HashTable<label> indices_;
const Time& time_; const Time& time_;
//- Dictionary containing the "functions" entry //- The parent dictionary containing a "functions" entry
// This entry can either be a list or a dictionary of // This entry can either be a list or a dictionary of
// functionObject specifications. // functionObject specifications.
const dictionary& parentDict_; const dictionary& parentDict_;
@ -75,15 +76,16 @@ class functionObjectList
//- Switch for the execution of the functionObjects //- Switch for the execution of the functionObjects
bool execution_; bool execution_;
//- Tracks if read() was called while execution was turned off //- Tracks if read() was called while execution is on
bool updated_; bool updated_;
// Private Member Functions // Private Member Functions
//- Remove and return the function object pointer by name. //- Remove and return the function object pointer by name,
// Return NULL if it didn't exist. // and returns the old index via the parameter.
functionObject* remove(const word&); // Returns a NULL pointer (and index -1) if it didn't exist.
functionObject* remove(const word&, label& oldIndex);
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
functionObjectList(const functionObjectList&); functionObjectList(const functionObjectList&);
@ -105,8 +107,11 @@ public:
); );
//- Construct from Time, dictionary with "functions" entry //- Construct from Time, a dictionary with "functions" entry
// and the execution setting // and the execution setting.
// @param[in] parentDict - the parent dictionary containing
// a "functions" entry, which can either be a list or a dictionary
// of functionObject specifications.
functionObjectList functionObjectList
( (
const Time&, const Time&,