mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: minor adjustments to Switch
- assignment operators return a value, for consistency with bool. - partial revert of DEFAULT_TRUE, DEFAULT_FALSE, to reduce complexity.
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)];
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user