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:
Mark Olesen
2020-10-14 13:30:32 +02:00
parent f7839acd47
commit cb47decbf1
3 changed files with 34 additions and 9 deletions

View File

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

View File

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