From e087e004f0fe6faa8b634de6fff61323aa847a31 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Thu, 26 May 2011 08:46:05 +0200 Subject: [PATCH] ENH: add support for set/unset options to argList - requested by Mattijs, this allows the developer to provide some default options, or otherwise adjust the logic. - the following set/unset operations are disallowed (FatalError) * changing -case, -roots, -parallel * changing type (bool <-> non-bool) * no mpi options --- src/OpenFOAM/global/argList/argList.C | 114 +++++++++++++++++++++++++- src/OpenFOAM/global/argList/argList.H | 13 +++ 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index a020f3e5bdc..fba0cefe17f 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -414,11 +414,11 @@ Foam::argList::argList ( ( validOptions.found(optionName) - && validOptions[optionName] != "" + && !validOptions[optionName].empty() ) || ( validParOptions.found(optionName) - && validParOptions[optionName] != "" + && !validParOptions[optionName].empty() ) ) { @@ -833,6 +833,116 @@ Foam::argList::~argList() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::argList::setOption(const word& opt, const string& param) +{ + bool changed = false; + + // only allow valid options + if (validOptions.found(opt)) + { + // some options are to be protected + if + ( + opt == "case" + || opt == "parallel" + || opt == "roots" + ) + { + FatalError + <<"used argList::setOption on a protected option: '" + << opt << "'" << endl; + FatalError.exit(); + } + + if (validOptions[opt].empty()) + { + // bool option + if (!param.empty()) + { + // disallow change of type + FatalError + <<"used argList::setOption to change bool to non-bool: '" + << opt << "'" << endl; + FatalError.exit(); + } + else + { + // did not previously exist + changed = !options_.found(opt); + } + } + else + { + // non-bool option + if (param.empty()) + { + // disallow change of type + FatalError + <<"used argList::setOption to change non-bool to bool: '" + << opt << "'" << endl; + FatalError.exit(); + } + else + { + // existing value needs changing, or did not previously exist + changed = options_.found(opt) ? options_[opt] != param : true; + } + } + } + else + { + FatalError + <<"used argList::setOption on an invalid option: '" + << opt << "'" << nl << "allowed are the following:" + << validOptions << endl; + FatalError.exit(); + } + + // set/change the option as required + if (changed) + { + options_.set(opt, param); + } + + return changed; +} + + +bool Foam::argList::unsetOption(const word& opt) +{ + // only allow valid options + if (validOptions.found(opt)) + { + // some options are to be protected + if + ( + opt == "case" + || opt == "parallel" + || opt == "roots" + ) + { + FatalError + <<"used argList::unsetOption on a protected option: '" + << opt << "'" << endl; + FatalError.exit(); + } + + // remove the option, return true if state changed + return options_.erase(opt); + } + else + { + FatalError + <<"used argList::unsetOption on an invalid option: '" + << opt << "'" << nl << "allowed are the following:" + << validOptions << endl; + FatalError.exit(); + } + + return false; +} + + void Foam::argList::printNotes() const { // output notes directly - no automatic text wrapping diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H index db5c6204518..4f2d236338f 100644 --- a/src/OpenFOAM/global/argList/argList.H +++ b/src/OpenFOAM/global/argList/argList.H @@ -334,6 +334,19 @@ public: static void noParallel(); + //- Set option directly (use with caution) + // An option with an empty param is a bool option. + // Not all valid options can also be set: eg, -case, -roots, ... + // Return true if the existing option value needed changing, + // or if the option did not previously exist. + bool setOption(const word& opt, const string& param = ""); + + //- Unset option directly (use with caution) + // Not all valid options can also be unset: eg, -case, -roots ... + // Return true if the option existed before being unset. + bool unsetOption(const word& opt); + + // Print //- Print notes (if any) -- GitLab