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
This commit is contained in:
Mark Olesen
2011-05-26 08:46:05 +02:00
parent 020d5f9b2d
commit e087e004f0
2 changed files with 125 additions and 2 deletions

View File

@ -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

View File

@ -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)