diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 93fc495d59..4bb9bc1849 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -197,6 +197,25 @@ public: template 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 + T lookupOrAddDefault + ( + const word&, + const T&, + bool recusive=false + ); + + //- Find an entry if present, and assign to T + template + void readIfPresent + ( + const word&, + T&, + bool recusive=false + ) const; + //- Check if entry is a sub-dictionary bool isDict(const word&) const; diff --git a/src/OpenFOAM/db/dictionary/dictionaryTemplates.C b/src/OpenFOAM/db/dictionary/dictionaryTemplates.C index 73b97fa664..6c1361ff07 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryTemplates.C +++ b/src/OpenFOAM/db/dictionary/dictionaryTemplates.C @@ -50,6 +50,48 @@ T Foam::dictionary::lookupOrDefault } +template +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(ePtr->stream()); + } +} + + +template +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 void Foam::dictionary::add(const word& keyword, const T& t) {