From 5415991e7974ebedfa96213246b4e909ef684f7b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 10 Dec 2018 14:09:53 +0100 Subject: [PATCH] ENH: argList::getList with optional parameter - this allows it to work like readListIfPresent() but with a list as the return value, which can be useful for a const context. --- .../temporalInterpolate/temporalInterpolate.C | 4 ++-- .../preProcessing/mapFieldsPar/mapFieldsPar.C | 4 ++-- src/OpenFOAM/global/argList/argList.H | 9 ++++++--- src/OpenFOAM/global/argList/argListI.H | 19 ++++++++++++++----- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/applications/utilities/postProcessing/miscellaneous/temporalInterpolate/temporalInterpolate.C b/applications/utilities/postProcessing/miscellaneous/temporalInterpolate/temporalInterpolate.C index 1018f0ea80..5f934dc020 100644 --- a/applications/utilities/postProcessing/miscellaneous/temporalInterpolate/temporalInterpolate.C +++ b/applications/utilities/postProcessing/miscellaneous/temporalInterpolate/temporalInterpolate.C @@ -215,8 +215,8 @@ int main(int argc, char *argv[]) #include "setRootCase.H" #include "createTime.H" - wordRes selectedFields; - args.readListIfPresent("fields", selectedFields); + // Non-mandatory + const wordRes selectedFields(args.getList("fields", false)); if (selectedFields.empty()) { diff --git a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C index 29d582178c..c459d15e2a 100644 --- a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C +++ b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C @@ -288,8 +288,8 @@ int main(int argc, char *argv[]) Info<< "Subtracting mapped source field from target" << endl; } - wordRes selectedFields; - args.readListIfPresent("fields", selectedFields); + // Non-mandatory + const wordRes selectedFields(args.getList("fields", false)); const bool noLagrangian = args.found("noLagrangian"); diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H index af2dd2779d..18cc39417a 100644 --- a/src/OpenFOAM/global/argList/argList.H +++ b/src/OpenFOAM/global/argList/argList.H @@ -371,12 +371,15 @@ public: const T& deflt ) const; - //- Read a List of values from the named option, + //- Get a List of values from the named option, //- treating a single entry like a list of size 1. + // \param optName the option name to read from + // \param mandatory if the option is non-mandatory, the behaviour + // is similar to readListIfPresent(). template - inline List getList(const word& optName) const; + inline List getList(const word& optName, bool mandatory=true) const; - //- If named option is present, read a List of values + //- If named option is present, get a List of values //- treating a single entry like a list of size 1. // \return true if the named option was found. template diff --git a/src/OpenFOAM/global/argList/argListI.H b/src/OpenFOAM/global/argList/argListI.H index 81e8a6443c..9d7dca7639 100644 --- a/src/OpenFOAM/global/argList/argListI.H +++ b/src/OpenFOAM/global/argList/argListI.H @@ -322,14 +322,22 @@ inline Foam::List Foam::argList::getList(const label index) const template -inline Foam::List Foam::argList::getList(const word& optName) const +inline Foam::List Foam::argList::getList +( + const word& optName, + bool mandatory +) const { - ITstream is(optName, options_[optName]); - List list; - readList(is, list); - checkITstream(is, optName); + if (mandatory || found(optName)) + { + ITstream is(optName, options_[optName]); + + readList(is, list); + + checkITstream(is, optName); + } return list; } @@ -345,6 +353,7 @@ inline bool Foam::argList::readListIfPresent if (found(optName)) { ITstream is(optName, options_[optName]); + readList(is, list); checkITstream(is, optName);