From 40122a8f642e8c5744093d8bbfcc6f8f449a3430 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 29 Jan 2019 09:38:09 +0100
Subject: [PATCH] ENH: minor adjustments to Switch

- assignment operators return a value, for consistency with bool.

- partial revert of DEFAULT_TRUE, DEFAULT_FALSE, to reduce complexity.
---
 .../test/primitives/Test-primitives.C         | 25 ++++++++-----
 src/OpenFOAM/primitives/bools/Switch/Switch.C | 12 -------
 src/OpenFOAM/primitives/bools/Switch/Switch.H | 35 +++++++++----------
 3 files changed, 33 insertions(+), 39 deletions(-)

diff --git a/applications/test/primitives/Test-primitives.C b/applications/test/primitives/Test-primitives.C
index ec8991c1d4..1b11d166eb 100644
--- a/applications/test/primitives/Test-primitives.C
+++ b/applications/test/primitives/Test-primitives.C
@@ -61,6 +61,12 @@ inline Switch readSwitch(const std::string& str)
 }
 
 
+void printInfo(const Switch& sw)
+{
+    Info<<"Switch " << sw.c_str() << " (enum=" << label(sw.type()) << ")\n";
+}
+
+
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 template<class T>
@@ -162,19 +168,22 @@ int main(int argc, char *argv[])
             }
         );
 
+        Info<< nl << "Test Switch defaults:" << nl;
+
         dictionary dict;
         dict.add("key1" , "true");
+        dict.add("key2" , "off");
 
+        for (const word& k : { "key", "key1", "key2" })
         {
-            Switch sw("key", dict, Switch::DEFAULT_ON);
-            Info<<"got: " << sw << " type is DEFAULT_ON? "
-                << (sw.type() == Switch::DEFAULT_ON) << nl;
-        }
+            Switch sw1(k, dict, Switch::YES);
+            Switch sw2(k, dict, Switch::NO);
 
-        {
-            Switch sw("key1", dict, Switch::DEFAULT_ON);
-            Info<<"got: " << sw << " type is DEFAULT_ON? "
-                << (sw.type() == Switch::DEFAULT_ON) << nl;
+            bool sw3(Switch(k, dict, Switch::YES));
+
+            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 ba349ccdc2..5a1c9a573a 100644
--- a/src/OpenFOAM/primitives/bools/Switch/Switch.C
+++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C
@@ -189,18 +189,6 @@ Foam::Switch::switchType Foam::Switch::type() const noexcept
 }
 
 
-bool Foam::Switch::isDefault() const noexcept
-{
-    return (switch_ & 0x10);
-}
-
-
-bool Foam::Switch::nonDefault() const noexcept
-{
-    return !isDefault();
-}
-
-
 const char* Foam::Switch::c_str() const noexcept
 {
     return names[(switch_ & 0x0F)];
diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H
index de4c6c77c3..0e644fec1e 100644
--- a/src/OpenFOAM/primitives/bools/Switch/Switch.H
+++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H
@@ -54,7 +54,6 @@ namespace Foam
 {
 
 // Forward declarations
-
 class Switch;
 class dictionary;
 
@@ -81,9 +80,7 @@ public:
         NO      = 2 /*!< "no" */,    YES  = 3 /*!< "yes" */,
         OFF     = 4 /*!< "off" */,   ON   = 5 /*!< "on" */,
         NONE    = 6 /*!< "none" */,
-        INVALID = 8 /*!< "invalid" */,
-        DEFAULT_OFF = 0x10 /*!< off/false (as default value) */,
-        DEFAULT_ON = 0x11  /*!< on/true (as default value) */
+        INVALID = 8 /*!< "invalid" */
     };
 
 
@@ -143,14 +140,14 @@ public:
 
         //- Construct from string.
         //  Optionally allow bad words, and catch the error elsewhere
-        Switch(const std::string& str, const bool allowBad)
+        Switch(const std::string& str, bool allowBad)
         :
             switch_(parse(str, allowBad))
         {}
 
         //- Construct from character array.
         //  Optionally allow bad words, and catch the error elsewhere
-        Switch(const char* str, const bool allowBad)
+        Switch(const char* str, bool allowBad)
         :
             switch_(parse(str, allowBad))
         {}
@@ -183,26 +180,20 @@ public:
         //- value is not found, it is added into the dictionary.
         static Switch lookupOrAddToDict
         (
-            const word& name,
-            dictionary& dict,
-            const Switch defaultValue = switchType::FALSE
+            const word& name,           //!< Lookup key. Uses REGEX!
+            dictionary& dict,           //!< dictionary
+            const Switch defaultValue = switchType::FALSE //!< default to add
         );
 
 
     // Member Functions
 
-        //- True if the Switch has a valid value
+        //- True if the Switch represents a valid enumeration
         bool valid() const noexcept;
 
         //- The underlying enumeration value
         switchType type() const noexcept;
 
-        //- Underlying enumeration is DEFAULT_ON or DEFAULT_OFF
-        bool isDefault() const noexcept;
-
-        //- Underlying enumeration is not DEFAULT_ON or DEFAULT_OFF
-        bool nonDefault() const noexcept;
-
         //- A string representation of the Switch value
         const char* c_str() const noexcept;
 
@@ -210,7 +201,11 @@ public:
         std::string str() const;
 
         //- Update the value of the Switch if it is found in the dictionary
-        bool readIfPresent(const word& name, const dictionary& dict);
+        bool readIfPresent
+        (
+            const word& name,           //!< Lookup key. Uses REGEX!
+            const dictionary& dict      //!< dictionary
+        );
 
 
     // Member Operators
@@ -222,15 +217,17 @@ public:
         }
 
         //- Assignment from enumerated value
-        void operator=(const switchType sw) noexcept
+        Switch& operator=(const switchType sw) noexcept
         {
             switch_ = sw;
+            return *this;
         }
 
         //- Assignment from bool
-        void operator=(const bool b) noexcept
+        Switch& operator=(const bool b) noexcept
         {
             switch_ = (b ? Switch::TRUE : Switch::FALSE);
+            return *this;
         }
 
 
-- 
GitLab