mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: improve robustness and usability of Switch
- add operator=(const switchType) to avoid this type of problem:
Switch sw;
sw = Switch::asEnum("foo", true);
if (sw) ... // evaluated incorrectly
- add constructor Switch(const switchType) for convenience
- add valid() method to avoid using the switchType directly
This commit is contained in:
@ -141,6 +141,12 @@ public:
|
||||
switch_(Switch::FALSE)
|
||||
{}
|
||||
|
||||
//- Construct from enumerated value
|
||||
Switch(const switchType value)
|
||||
:
|
||||
switch_(value)
|
||||
{}
|
||||
|
||||
//- Construct from bool
|
||||
Switch(const bool value)
|
||||
:
|
||||
@ -154,15 +160,17 @@ public:
|
||||
{}
|
||||
|
||||
//- Construct from std::string, string, word
|
||||
Switch(const std::string& value)
|
||||
// Optionally allow bad words, and catch the error elsewhere
|
||||
Switch(const std::string& value, const bool allowInvalid=false)
|
||||
:
|
||||
switch_(asEnum(value))
|
||||
switch_(asEnum(value, allowInvalid))
|
||||
{}
|
||||
|
||||
//- Construct from character array
|
||||
Switch(const char* value)
|
||||
// Optionally allow bad words, and catch the error elsewhere
|
||||
Switch(const char* value, const bool allowInvalid=false)
|
||||
:
|
||||
switch_(asEnum(std::string(value)))
|
||||
switch_(asEnum(std::string(value, allowInvalid)))
|
||||
{}
|
||||
|
||||
//- Construct from Istream
|
||||
@ -178,6 +186,15 @@ public:
|
||||
);
|
||||
|
||||
|
||||
// Member Functions
|
||||
|
||||
//- Return true if the Switch has a valid value
|
||||
bool valid() const
|
||||
{
|
||||
return switch_ <= Switch::NONE;
|
||||
}
|
||||
|
||||
|
||||
// Member Operators
|
||||
|
||||
//- Conversion to bool
|
||||
@ -186,6 +203,13 @@ public:
|
||||
return (switch_ & 0x1);
|
||||
}
|
||||
|
||||
//- Assignment from enumerated value
|
||||
const Switch& operator=(const switchType sw)
|
||||
{
|
||||
switch_ = sw;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//- Assignment from bool
|
||||
const Switch& operator=(const bool b)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user