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.
This commit is contained in:
Mark Olesen
2018-12-10 14:09:53 +01:00
parent a38b459ab0
commit 5415991e79
4 changed files with 24 additions and 12 deletions

View File

@ -215,8 +215,8 @@ int main(int argc, char *argv[])
#include "setRootCase.H" #include "setRootCase.H"
#include "createTime.H" #include "createTime.H"
wordRes selectedFields; // Non-mandatory
args.readListIfPresent<wordRe>("fields", selectedFields); const wordRes selectedFields(args.getList<wordRe>("fields", false));
if (selectedFields.empty()) if (selectedFields.empty())
{ {

View File

@ -288,8 +288,8 @@ int main(int argc, char *argv[])
Info<< "Subtracting mapped source field from target" << endl; Info<< "Subtracting mapped source field from target" << endl;
} }
wordRes selectedFields; // Non-mandatory
args.readListIfPresent<wordRe>("fields", selectedFields); const wordRes selectedFields(args.getList<wordRe>("fields", false));
const bool noLagrangian = args.found("noLagrangian"); const bool noLagrangian = args.found("noLagrangian");

View File

@ -371,12 +371,15 @@ public:
const T& deflt const T& deflt
) const; ) 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. //- 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<class T> template<class T>
inline List<T> getList(const word& optName) const; inline List<T> 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. //- treating a single entry like a list of size 1.
// \return true if the named option was found. // \return true if the named option was found.
template<class T> template<class T>

View File

@ -322,14 +322,22 @@ inline Foam::List<T> Foam::argList::getList(const label index) const
template<class T> template<class T>
inline Foam::List<T> Foam::argList::getList(const word& optName) const inline Foam::List<T> Foam::argList::getList
(
const word& optName,
bool mandatory
) const
{
List<T> list;
if (mandatory || found(optName))
{ {
ITstream is(optName, options_[optName]); ITstream is(optName, options_[optName]);
List<T> list;
readList(is, list); readList(is, list);
checkITstream(is, optName); checkITstream(is, optName);
}
return list; return list;
} }
@ -345,6 +353,7 @@ inline bool Foam::argList::readListIfPresent
if (found(optName)) if (found(optName))
{ {
ITstream is(optName, options_[optName]); ITstream is(optName, options_[optName]);
readList(is, list); readList(is, list);
checkITstream(is, optName); checkITstream(is, optName);