From cb47decbf18080c7180ceb709bb69e4a15716da3 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Wed, 14 Oct 2020 13:30:32 +0200 Subject: [PATCH] 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 --- .../test/primitives/Test-primitives.C | 25 +++++++++++++------ src/OpenFOAM/primitives/bools/Switch/Switch.C | 10 ++++++++ src/OpenFOAM/primitives/bools/Switch/Switch.H | 6 ++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/applications/test/primitives/Test-primitives.C b/applications/test/primitives/Test-primitives.C index 245d3291ad3..13a1d8f1285 100644 --- a/applications/test/primitives/Test-primitives.C +++ b/applications/test/primitives/Test-primitives.C @@ -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; } } diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index 36f9d8145a6..6ef876d200a 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -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)]; diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index dc937778e6b..4c099f51f79 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -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; -- GitLab