Switch : drop word data member in favour of enum

This commit is contained in:
Mark Olesen
2009-01-16 14:06:36 +01:00
parent 3adf0f5c8e
commit 7aee58e5fa
3 changed files with 147 additions and 61 deletions

View File

@ -30,6 +30,101 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// NB: values chosen such that bitwise '&' 0x1 yields the bool value
const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
{
"false", "true",
"off", "on",
"no", "yes",
"n", "y",
"invalid"
};
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::Switch::switchType Foam::Switch::asEnum
(
const word& val,
const bool ignoreError
)
{
for (int sw = 0; sw < INVALID; sw++)
{
if (val == names[sw])
{
if (sw == NO_1)
{
return NO;
}
else if (sw == YES_1)
{
return YES;
}
else
{
return switchType(sw);
}
}
}
if (!ignoreError)
{
FatalErrorIn("Switch::asEnum(const word&)")
<< "unknown switch word " << val
<< abort(FatalError);
}
return INVALID;
}
Foam::Switch::switchType Foam::Switch::asEnum(const bool val)
{
return val ? ON : OFF;
}
bool Foam::Switch::asBool
(
const word& val,
const bool ignoreError
)
{
switchType sw = asEnum(val, true);
// catch error here
if (sw == INVALID && !ignoreError)
{
FatalErrorIn("Switch::asBool(const word&)")
<< "unknown switch word " << val
<< abort(FatalError);
}
return asBool(sw);
}
bool Foam::Switch::asBool(const switchType& val)
{
return (val & 0x1);
}
Foam::word Foam::Switch::asWord(const bool val)
{
return word((val ? names[ON] : names[OFF]), false);
}
Foam::word Foam::Switch::asWord(const switchType& val)
{
return word(names[val], false);
}
Foam::Switch Foam::Switch::lookupOrAddToDict
(
const word& name,
@ -40,49 +135,6 @@ Foam::Switch Foam::Switch::lookupOrAddToDict
return dict.lookupOrAddDefault<Switch>(name, defaultValue);
}
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
// NOTE: possible alternative implementation
// make a direct bool, handle assignments and use switchTypes instead of word
// for the word representation ...
//
// //- Possible word representions
// enum switchTypes
// {
// OFF = 0, ON = 1,
// FALSE = 2, TRUE = 3,
// NO = 4, YES = 5
// };
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
bool Foam::Switch::asBool(const word& val)
{
if (val == "on" || val == "true" || val == "yes" || val == "y")
{
return true;
}
else if (val == "off" || val == "false" || val == "no" || val == "n")
{
return false;
}
else
{
FatalErrorIn("Switch::asBool(const word&)")
<< "unknown switch word " << val
<< abort(FatalError);
}
return false;
}
Foam::word Foam::Switch::asWord(const bool val)
{
return word((val ? "on" : "off"), false);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //

View File

@ -61,35 +61,69 @@ class dictionary;
class Switch
{
public:
//- The various text representations for a switch value.
enum switchType
{
FALSE = 0, TRUE = 1,
OFF = 2, ON = 3,
NO = 4, YES = 5,
NO_1 = 6, YES_1 = 7,
INVALID
};
private:
// Static data members
//- The set of names corresponding to the switchType enumeration
// Includes an extra entry for "invalid".
static const char* names[INVALID+1];
// Private data
bool bool_;
//- Enumerated string representation
switchType switch_;
word word_;
//- The boolean value
bool bool_;
public:
// Private member functions
//- Return a switchType representation of a bool
static switchType asEnum(const bool);
//- Return a switchType representation of a word
static switchType asEnum(const word&, const bool ignoreError=false);
//- Return a bool representation of a word
static bool asBool(const word&);
static bool asBool(const word&, const bool ignoreError=false);
//- Return a bool representation of a switchType
static bool asBool(const switchType&);
//- Return a word representation of a bool value
static word asWord(const bool);
//- Return a word representation of a switchType
static word asWord(const switchType&);
// Constructors
//- Construct from bool
Switch(const bool value)
:
bool_(value),
word_(asWord(value))
switch_(asEnum(value)),
bool_(value)
{}
//- Construct from word
Switch(const word& value)
:
bool_(asBool(value)),
word_(value)
switch_(asEnum(value)),
bool_(asBool(switch_))
{}
//- Construct from Istream
@ -109,8 +143,8 @@ public:
Switch& operator=(const Switch& rhs)
{
bool_ = rhs.bool_;
word_ = rhs.word_;
switch_ = rhs.switch_;
bool_ = rhs.bool_;
return *this;
}
@ -120,10 +154,10 @@ public:
return bool_;
}
operator const word&() const
{
return word_;
}
// operator const word&() const
// {
// return asWord(switch_);
// }
// Member fuctions

View File

@ -39,8 +39,10 @@ Foam::Switch::Switch(Istream& is)
Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
{
is >> s.word_;
s.bool_ = Switch::asBool(s.word_);
word w(is);
s.switch_ = Switch::asEnum(w);
s.bool_ = Switch::asBool(s.switch_);
is.check("Istream& operator>>(Istream&, Switch&)");
@ -50,10 +52,8 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s)
{
os << s.word_;
os << Switch::names[s.switch_];
os.check("Ostream& operator<<(Ostream&, const Switch&)");
return os;
}