mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add Switch::negate() method (no-op for invalid state)
- flips state while preserving the textual representation. Eg, OFF <-> ON, YES <-> NO etc. - fix test case to avoid triggering abort(), which we cannot try/catch
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -53,11 +53,14 @@ inline scalar readNasScalar(const std::string& str)
|
||||
// As a function
|
||||
inline Switch readSwitch(const std::string& str)
|
||||
{
|
||||
Switch sw(str);
|
||||
Switch sw(Switch::find(str));
|
||||
|
||||
if (sw.type() == Switch::ON)
|
||||
// Trap bad input and raise as exit error, not abort
|
||||
if (sw.bad())
|
||||
{
|
||||
Info<< "Was 'on'" << nl;
|
||||
FatalErrorInFunction
|
||||
<< "Unknown switch " << str << nl
|
||||
<< exit(FatalError);
|
||||
}
|
||||
|
||||
return sw;
|
||||
@ -180,6 +183,8 @@ int main(int argc, char *argv[])
|
||||
{ true, "false" },
|
||||
{ true, "on" },
|
||||
{ false, "None" },
|
||||
{ true, "yes" },
|
||||
{ true, "none" },
|
||||
{ false, "default" },
|
||||
}
|
||||
);
|
||||
@ -189,17 +194,23 @@ int main(int argc, char *argv[])
|
||||
dictionary dict;
|
||||
dict.add("key1" , "true");
|
||||
dict.add("key2" , "off");
|
||||
dict.add("key3" , "any");
|
||||
|
||||
for (const word& k : { "key", "key1", "key2" })
|
||||
for (const word& k : { "key", "key1", "key2" , "key3" })
|
||||
{
|
||||
Switch sw1(k, dict, Switch::YES);
|
||||
Switch sw2(k, dict, Switch::NO);
|
||||
|
||||
bool sw3(Switch(k, dict, Switch::YES));
|
||||
|
||||
Info<< nl;
|
||||
printInfo(sw1);
|
||||
printInfo(sw2);
|
||||
Info<< "bool " << bool(sw1) << nl;
|
||||
|
||||
sw1.negate();
|
||||
sw2.negate();
|
||||
Info<< "negated" << nl;
|
||||
printInfo(sw1);
|
||||
printInfo(sw2);
|
||||
Info<<"bool " << sw3 << nl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -309,6 +309,16 @@ Foam::Switch::switchType Foam::Switch::type() const noexcept
|
||||
}
|
||||
|
||||
|
||||
void Foam::Switch::negate() noexcept
|
||||
{
|
||||
if (value_ < switchType::INVALID)
|
||||
{
|
||||
// Toggle final bit. So NO <-> YES, OFF <-> ON ...
|
||||
value_ ^= 0x1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char* Foam::Switch::c_str() const noexcept
|
||||
{
|
||||
return names[(value_ & 0x0F)];
|
||||
|
||||
@ -91,7 +91,7 @@ public:
|
||||
NO = 2 /*!< "no" */, YES = 3 /*!< "yes" */,
|
||||
OFF = 4 /*!< "off" */, ON = 5 /*!< "on" */,
|
||||
NONE = 6 /*!< "none" */, ANY = 7 /*!< "any" */,
|
||||
INVALID = 8 /*!< "invalid" */,
|
||||
INVALID = 8 /*!< "invalid" (output only) */,
|
||||
};
|
||||
|
||||
|
||||
@ -227,6 +227,10 @@ public:
|
||||
//- The underlying enumeration value
|
||||
switchType type() const noexcept;
|
||||
|
||||
//- Flip the type, so OFF becomes ON, etc.
|
||||
// Ignored if the Switch is INVALID
|
||||
void negate() noexcept;
|
||||
|
||||
//- A C-string representation of the Switch value
|
||||
const char* c_str() const noexcept;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user