added lookupOrAddDefault and readIfPresent functions

This commit is contained in:
andy 2008-06-16 15:54:11 +01:00
parent 4fc2fe6596
commit 349ddc5f59
2 changed files with 61 additions and 0 deletions

View File

@ -197,6 +197,25 @@ public:
template<class T>
T lookupOrDefault(const word&, const T&, bool recusive=false) const;
//- Find and return a T, if not found return the given default value,
// and add to dictionary. If recusive search parent dictionaries
template<class T>
T lookupOrAddDefault
(
const word&,
const T&,
bool recusive=false
);
//- Find an entry if present, and assign to T
template<class T>
void readIfPresent
(
const word&,
T&,
bool recusive=false
) const;
//- Check if entry is a sub-dictionary
bool isDict(const word&) const;

View File

@ -50,6 +50,48 @@ T Foam::dictionary::lookupOrDefault
}
template<class T>
T Foam::dictionary::lookupOrAddDefault
(
const word& keyword,
const T& deft,
bool recusive
)
{
const entry* ePtr = lookupEntryPtr(keyword, recusive);
if (ePtr == NULL)
{
entry* defPtr = new primitiveEntry(keyword, deft);
append(defPtr);
hashedEntries_.insert(defPtr->keyword(), defPtr);
return deft;
}
else
{
return pTraits<T>(ePtr->stream());
}
}
template<class T>
void Foam::dictionary::readIfPresent
(
const word& keyword,
T& deft,
bool recusive
) const
{
const entry* ePtr = lookupEntryPtr(keyword, recusive);
if (ePtr != NULL)
{
ePtr->stream() >> deft;
}
}
template<class T>
void Foam::dictionary::add(const word& keyword, const T& t)
{