From db9fc1481c2e04160ed5610872a4b9d451c92c38 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 1 Aug 2019 17:58:07 +0200 Subject: [PATCH] ENH: support search options on more dictionary methods - can now specify literal matches for sub-dictionary methods: isDict(key, keyType::REGEX) optionalSubDict(key, keyType::REGEX) subDict(key, keyType::REGEX) subOrEmptyDict(key, keyType::REGEX, mandatory) There is no change in behaviour of the methods, just the search option is now exposed as an optional parameter. NOTE: minor breaking change for subOrEmptyDict() old: subOrEmptyDict(key, bool=false) new: subOrEmptyDict(key, keyType::option=keyType::REGEX, bool=false) This affects code that previously explicitly set the bool parameter. Within OpenFOAM itself, this only affected a single file: KinematicCloud.C --- src/OpenFOAM/db/dictionary/dictionary.C | 43 ++++++++++++------- src/OpenFOAM/db/dictionary/dictionary.H | 38 ++++++++++++---- .../Templates/KinematicCloud/KinematicCloud.C | 8 +++- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index cfefae5c51..9ae65c9393 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -397,7 +397,11 @@ Foam::ITstream& Foam::dictionary::lookup } -bool Foam::dictionary::substituteKeyword(const word& keyword, bool mergeEntry) +bool Foam::dictionary::substituteKeyword +( + const word& keyword, + bool mergeEntry +) { if (keyword.size() < 2) { @@ -457,10 +461,13 @@ bool Foam::dictionary::substituteScopedKeyword } -bool Foam::dictionary::isDict(const word& keyword) const +bool Foam::dictionary::isDict +( + const word& keyword, + enum keyType::option matchOpt +) const { - // Allow patterns, non-recursive - return csearch(keyword, keyType::REGEX).isDict(); + return csearch(keyword, matchOpt).isDict(); } @@ -484,10 +491,13 @@ const Foam::dictionary* Foam::dictionary::findDict } -const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const +const Foam::dictionary& Foam::dictionary::subDict +( + const word& keyword, + enum keyType::option matchOpt +) const { - // Allow patterns, non-recursive - const const_searcher finder(csearch(keyword, keyType::REGEX)); + const const_searcher finder(csearch(keyword, matchOpt)); if (!finder.good()) { @@ -501,10 +511,13 @@ const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const } -Foam::dictionary& Foam::dictionary::subDict(const word& keyword) +Foam::dictionary& Foam::dictionary::subDict +( + const word& keyword, + enum keyType::option matchOpt +) { - // Allow patterns, non-recursive - searcher finder(search(keyword, keyType::REGEX)); + searcher finder(search(keyword, matchOpt)); if (!finder.good()) { @@ -521,11 +534,11 @@ Foam::dictionary& Foam::dictionary::subDict(const word& keyword) Foam::dictionary Foam::dictionary::subOrEmptyDict ( const word& keyword, + enum keyType::option matchOpt, const bool mandatory ) const { - // Allow patterns, non-recursive - const const_searcher finder(csearch(keyword, keyType::REGEX)); + const const_searcher finder(csearch(keyword, matchOpt)); if (finder.isDict()) { @@ -556,11 +569,11 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict const Foam::dictionary& Foam::dictionary::optionalSubDict ( - const word& keyword + const word& keyword, + enum keyType::option matchOpt ) const { - // Allow patterns, non-recursive - const const_searcher finder(csearch(keyword, keyType::REGEX)); + const const_searcher finder(csearch(keyword, matchOpt)); if (finder.isDict()) { diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index e18ad24ad6..59599fb3e8 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -610,7 +610,7 @@ public: // // \param val the value to read into // \param matchOpt the default search is non-recursive with patterns - // \param mandatory the keyword is mandatory + // \param mandatory the keyword is mandatory (default: true) // // \return true if the entry was found. template @@ -720,29 +720,45 @@ public: //- Check if entry is found and and is a sub-dictionary. // - // Search type: non-recursive with patterns. - bool isDict(const word& keyword) const; + // \param matchOpt the default search is non-recursive with patterns + // + // \return true if the entry was found. + bool isDict + ( + const word& keyword, + enum keyType::option matchOpt = keyType::REGEX + ) const; //- Find and return a sub-dictionary. // Fatal if the entry does not exist or is not a sub-dictionary. // - // Search type: non-recursive with patterns. - const dictionary& subDict(const word& keyword) const; + // \param matchOpt the default search is non-recursive with patterns + const dictionary& subDict + ( + const word& keyword, + enum keyType::option matchOpt = keyType::REGEX + ) const; //- Find and return a sub-dictionary for manipulation. // Fatal if the entry does not exist or is not a sub-dictionary. // - // Search type: non-recursive with patterns. - dictionary& subDict(const word& keyword); + // \param matchOpt the default search is non-recursive with patterns + dictionary& subDict + ( + 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. // - // Search type: non-recursive with patterns. + // \param matchOpt the default search is non-recursive with patterns + // \param mandatory the keyword is mandatory (default: false) dictionary subOrEmptyDict ( const word& keyword, + enum keyType::option matchOpt = keyType::REGEX, const bool mandatory = false ) const; @@ -750,7 +766,11 @@ public: // Warn if the entry exists but is not a sub-dictionary. // // Search type: non-recursive with patterns. - const dictionary& optionalSubDict(const word& keyword) const; + const dictionary& optionalSubDict + ( + const word& keyword, + enum keyType::option matchOpt = keyType::REGEX + ) const; //- Return the table of contents wordList toc() const; diff --git a/src/lagrangian/intermediate/clouds/Templates/KinematicCloud/KinematicCloud.C b/src/lagrangian/intermediate/clouds/Templates/KinematicCloud/KinematicCloud.C index 684b803eb0..f926e24c9f 100644 --- a/src/lagrangian/intermediate/clouds/Templates/KinematicCloud/KinematicCloud.C +++ b/src/lagrangian/intermediate/clouds/Templates/KinematicCloud/KinematicCloud.C @@ -325,7 +325,12 @@ Foam::KinematicCloud::KinematicCloud constProps_(particleProperties_), subModelProperties_ ( - particleProperties_.subOrEmptyDict("subModels", solution_.active()) + particleProperties_.subOrEmptyDict + ( + "subModels", + keyType::REGEX, + solution_.active() + ) ), rndGen_(Pstream::myProcNo()), cellOccupancyPtr_(), @@ -342,6 +347,7 @@ Foam::KinematicCloud::KinematicCloud subModelProperties_.subOrEmptyDict ( "particleForces", + keyType::REGEX, solution_.active() ), solution_.active()