From 8a75bdb12fdbe4cfc8fc6eba1b60801e48ffcd47 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 25 Sep 2019 17:25:16 +0200 Subject: [PATCH] ENH: add dictionary method subDictOrAdd (related to #1442) --- src/OpenFOAM/db/dictionary/dictionary.C | 58 +++++++++++++++++++++---- src/OpenFOAM/db/dictionary/dictionary.H | 10 +++++ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index fbba0da0c6..9a85725e38 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -531,6 +531,46 @@ Foam::dictionary& Foam::dictionary::subDict } +Foam::dictionary& Foam::dictionary::subDictOrAdd +( + const word& keyword, + enum keyType::option matchOpt +) +{ + searcher finder(search(keyword, matchOpt)); + + dictionary* ptr = finder.dictPtr(); + + if (ptr) + { + // Found and a sub-dictionary + return *ptr; + } + + if (finder.good()) + { + FatalIOErrorInFunction(*this) + << "Entry '" << keyword + << "' is not a sub-dictionary in dictionary " + << name() << nl + << exit(FatalIOError); + } + + ptr = this->set(keyword, dictionary())->dictPtr(); + + if (!ptr) + { + FatalIOErrorInFunction(*this) + << "Failed to insert sub-dictionary '" << keyword + << "' in dictionary " + << name() << nl + << exit(FatalIOError); + } + + return *ptr; +} + + Foam::dictionary Foam::dictionary::subOrEmptyDict ( const word& keyword, @@ -551,7 +591,7 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict FatalIOErrorInFunction(*this) << "Entry '" << keyword << "' is not a sub-dictionary in dictionary " - << name() + << name() << nl << exit(FatalIOError); } @@ -809,7 +849,7 @@ bool Foam::dictionary::merge(const dictionary& dict) { FatalIOErrorInFunction(*this) << "Attempted merge to self for dictionary " - << name() + << name() << nl << abort(FatalIOError); } @@ -878,7 +918,7 @@ void Foam::dictionary::operator=(const dictionary& rhs) { FatalIOErrorInFunction(*this) << "Attempted assignment to self for dictionary " - << name() + << name() << nl << abort(FatalIOError); } @@ -900,8 +940,8 @@ void Foam::dictionary::operator+=(const dictionary& rhs) if (this == &rhs) { FatalIOErrorInFunction(*this) - << "attempted addition assignment to self for dictionary " - << name() + << "Attempted addition assignment to self for dictionary " + << name() << nl << abort(FatalIOError); } @@ -917,8 +957,8 @@ void Foam::dictionary::operator|=(const dictionary& rhs) if (this == &rhs) { FatalIOErrorInFunction(*this) - << "attempted assignment to self for dictionary " - << name() + << "Attempted assignment to self for dictionary " + << name() << nl << abort(FatalIOError); } @@ -937,8 +977,8 @@ void Foam::dictionary::operator<<=(const dictionary& rhs) if (this == &rhs) { FatalIOErrorInFunction(*this) - << "attempted assignment to self for dictionary " - << name() + << "Attempted assignment to self for dictionary " + << name() << nl << abort(FatalIOError); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 10e30f1d57..2dc09a2c14 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -749,6 +749,16 @@ public: enum keyType::option matchOpt = keyType::REGEX ); + //- Find and return a sub-dictionary for manipulation. + // Fatal if the entry exist and is not a sub-dictionary. + // + // \param matchOpt the default search is non-recursive with patterns + dictionary& subDictOrAdd + ( + const word& keyword, + enum keyType::option matchOpt = keyType::REGEX + ); + //- Find and return a sub-dictionary as a copy, otherwise return //- an empty dictionary. // Warn if the entry exists but is not a sub-dictionary.