mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-12-28 03:37:59 +00:00
ENH: add 'default' as possible Switch state, but not as input/output
- in some circumstances we need to pass a bool value upwards to the
caller and know if the true/false value was set based on real input
or is a default value.
Eg, in the object::read() we might normally have
enabled_(dict.readIfPresent(key, true));
but would lose information about why the value is true/false.
We can change that by using
enabled_(dict.readIfPresent<Switch>(key, Switch::DEFAULT_ON));
After which we can use this information is testing.
if
(
child.enabled().nonDefault()
? child.enabled()
: parent.enabled()
)
{ ... }
And thus enable output if the parent requested it explicitly or by
default and it has not been explicitly disabled in the child.
No difference when testing as a bool and the text representation
of DEFAULT_ON / DEFAULT_OFF will simply be "true" / "false".
ENH: add construction of Switch from dictionary (similar to Enum)
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
@ -35,6 +35,8 @@ Description
|
||||
#include "NASCore.H"
|
||||
#include "parsing.H"
|
||||
#include "Tuple2.H"
|
||||
#include "Switch.H"
|
||||
#include "dictionary.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
@ -45,6 +47,20 @@ inline scalar readNasScalar(const std::string& str)
|
||||
}
|
||||
|
||||
|
||||
// As a function
|
||||
inline Switch readSwitch(const std::string& str)
|
||||
{
|
||||
Switch sw(str);
|
||||
|
||||
if (sw.type() == Switch::ON)
|
||||
{
|
||||
Info<< "Was 'on'" << nl;
|
||||
}
|
||||
|
||||
return sw;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
@ -96,7 +112,8 @@ unsigned testParsing
|
||||
string errMsg;
|
||||
|
||||
// Expect some failures
|
||||
const bool prev = FatalIOError.throwExceptions();
|
||||
const bool prev1 = FatalError.throwExceptions();
|
||||
const bool prev2 = FatalIOError.throwExceptions();
|
||||
|
||||
for (const std::pair<bool, std::string>& test : tests)
|
||||
{
|
||||
@ -120,7 +137,8 @@ unsigned testParsing
|
||||
hadParsingError(test, result, errMsg);
|
||||
}
|
||||
|
||||
FatalIOError.throwExceptions(prev);
|
||||
FatalError.throwExceptions(prev1);
|
||||
FatalIOError.throwExceptions(prev2);
|
||||
|
||||
return nFail;
|
||||
}
|
||||
@ -130,6 +148,36 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned nFail = 0;
|
||||
|
||||
{
|
||||
Info<< nl << "Test Switch parsing:" << nl;
|
||||
nFail += testParsing
|
||||
(
|
||||
&readSwitch,
|
||||
{
|
||||
{ false, "True" },
|
||||
{ true, "false" },
|
||||
{ true, "on" },
|
||||
{ false, "None" },
|
||||
{ false, "default" },
|
||||
}
|
||||
);
|
||||
|
||||
dictionary dict;
|
||||
dict.add("key1" , "true");
|
||||
|
||||
{
|
||||
Switch sw("key", dict, Switch::DEFAULT_ON);
|
||||
Info<<"got: " << sw << " type is DEFAULT_ON? "
|
||||
<< (sw.type() == Switch::DEFAULT_ON) << nl;
|
||||
}
|
||||
|
||||
{
|
||||
Switch sw("key1", dict, Switch::DEFAULT_ON);
|
||||
Info<<"got: " << sw << " type is DEFAULT_ON? "
|
||||
<< (sw.type() == Switch::DEFAULT_ON) << nl;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Info<< nl << "Test readDouble: (small=" << doubleScalarVSMALL
|
||||
<< " great=" << doubleScalarVSMALL << "):" << nl;
|
||||
|
||||
Reference in New Issue
Block a user