Commit a17daf7f authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent af12964b
......@@ -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)
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment