From 7aee58e5fa4916436bd97ebaeaf4ee9def262370 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Fri, 16 Jan 2009 14:06:36 +0100 Subject: [PATCH] Switch : drop word data member in favour of enum --- src/OpenFOAM/primitives/bools/Switch/Switch.C | 112 +++++++++++++----- src/OpenFOAM/primitives/bools/Switch/Switch.H | 60 ++++++++-- .../primitives/bools/Switch/SwitchIO.C | 10 +- 3 files changed, 134 insertions(+), 48 deletions(-) diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index 40900c3110f..3a5a3554e68 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -30,57 +30,109 @@ License // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -Foam::Switch Foam::Switch::lookupOrAddToDict -( - const word& name, - dictionary& dict, - const Switch& defaultValue -) -{ - return dict.lookupOrAddDefault<Switch>(name, defaultValue); -} +// NB: values chosen such that bitwise '&' 0x1 yields the bool value -// * * * * * * * * * * * * 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 -// }; +const char* Foam::Switch::names[Foam::Switch::INVALID+1] = +{ + "false", "true", + "off", "on", + "no", "yes", + "n", "y", + "invalid" +}; // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // -bool Foam::Switch::asBool(const word& val) + +Foam::Switch::switchType Foam::Switch::asEnum +( + const word& val, + const bool ignoreError +) { - if (val == "on" || val == "true" || val == "yes" || val == "y") + for (int sw = 0; sw < INVALID; sw++) { - return true; + if (val == names[sw]) + { + if (sw == NO_1) + { + return NO; + } + else if (sw == YES_1) + { + return YES; + } + else + { + return switchType(sw); + } + } } - else if (val == "off" || val == "false" || val == "no" || val == "n") + + if (!ignoreError) { - return false; + FatalErrorIn("Switch::asEnum(const word&)") + << "unknown switch word " << val + << abort(FatalError); } - else + + 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 false; + return asBool(sw); +} + + +bool Foam::Switch::asBool(const switchType& val) +{ + return (val & 0x1); } Foam::word Foam::Switch::asWord(const bool val) { - return word((val ? "on" : "off"), false); + 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, + dictionary& dict, + const Switch& defaultValue +) +{ + return dict.lookupOrAddDefault<Switch>(name, defaultValue); } diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index 1eca32502a0..ff46b8feeeb 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -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 diff --git a/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C b/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C index 3962935a3ad..5894c8c5c77 100644 --- a/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C +++ b/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C @@ -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; } -- GitLab