Commit 2599c9e1 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: make Switch implementation more opaque to protect against user mistakes

parent ad1f99ff
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -44,23 +44,17 @@ const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::Switch::switchType Foam::Switch::asEnum(const bool b)
{
return b ? Switch::TRUE : Switch::FALSE;
}
Foam::Switch::switchType Foam::Switch::asEnum
(
const std::string& str,
const bool allowInvalid
)
{
for (int sw = 0; sw < Switch::INVALID; sw++)
for (int sw = 0; sw < Switch::INVALID; ++sw)
{
if (str == names[sw])
{
// convert n/y to no/yes (perhaps should deprecate y/n)
// convert n/y to no/yes - perhaps should deprecate y/n
if (sw == Switch::NO_1 || sw == Switch::NONE)
{
return Switch::NO;
......@@ -78,7 +72,7 @@ Foam::Switch::switchType Foam::Switch::asEnum
if (!allowInvalid)
{
FatalErrorIn("Switch::asEnum(const std::string&)")
FatalErrorIn("Switch::asEnum(const std::string&, const bool)")
<< "unknown switch word " << str << nl
<< abort(FatalError);
}
......@@ -87,63 +81,31 @@ Foam::Switch::switchType Foam::Switch::asEnum
}
bool Foam::Switch::asBool(const switchType sw)
{
// relies on (INVALID & 0x1) evaluating to false
return (sw & 0x1);
}
bool Foam::Switch::asBool
Foam::Switch Foam::Switch::lookupOrAddToDict
(
const std::string& str,
const bool allowInvalid
const word& name,
dictionary& dict,
const Switch& defaultValue
)
{
// allow invalid values, but catch after for correct error message
switchType sw = asEnum(str, true);
if (sw == Switch::INVALID)
{
if (!allowInvalid)
{
FatalErrorIn("Switch::asBool(const std::string&)")
<< "unknown switch word " << str << nl
<< abort(FatalError);
}
return false;
}
return (sw & 0x1);
return dict.lookupOrAddDefault<Switch>(name, defaultValue);
}
const char* Foam::Switch::asText(const bool b)
{
return b ? names[Switch::TRUE] : names[Switch::FALSE];
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
const char* Foam::Switch::asText(const switchType sw)
bool Foam::Switch::valid() const
{
return names[sw];
return switch_ <= Switch::NONE;
}
Foam::Switch Foam::Switch::lookupOrAddToDict
(
const word& name,
dictionary& dict,
const Switch& defaultValue
)
const char* Foam::Switch::asText() const
{
return dict.lookupOrAddDefault<Switch>(name, defaultValue);
return names[switch_];
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
{
return dict.readIfPresent<Switch>(name, *this);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -48,11 +48,11 @@ namespace Foam
// Forward declaration of friend functions and operators
class Switch;
class dictionary;
Istream& operator>>(Istream&, Switch&);
Ostream& operator<<(Ostream&, const Switch&);
class dictionary;
/*---------------------------------------------------------------------------*\
Class Switch Declaration
......@@ -60,8 +60,6 @@ class dictionary;
class Switch
{
private:
// Private data
//- The logic and enumerated text representation stored as a single byte
......@@ -71,19 +69,21 @@ public:
// Public data types
// avoid issues with pre-processor defines
#undef FALSE
#undef TRUE
#undef OFF
#undef ON
#undef NO
#undef YES
#undef NO_1
#undef YES_1
#undef NONE
#undef PLACEHOLDER
#undef INVALID
//- The various text representations for a switch value.
// These also correspond to the entries in names.
# undef FALSE
# undef TRUE
# undef OFF
# undef ON
# undef NO
# undef YES
# undef NO_1
# undef YES_1
# undef NONE
# undef PLACEHOLDER
# undef INVALID
enum switchType
{
FALSE = 0, TRUE = 1,
......@@ -94,7 +94,6 @@ public:
INVALID
};
// Static data members
//- The set of names corresponding to the switchType enumeration
......@@ -102,36 +101,15 @@ public:
static const char* names[INVALID+1];
// Static Member Functions
private:
//- Return a switchType representation of a bool
static switchType asEnum(const bool);
// Static Member Functions
//- Return a switchType representation of a word
// Optionally allow bad words, and catch the error elsewhere
static switchType asEnum
(
const std::string&,
const bool allowInvalid=false
);
//- Return a bool representation of a switchType
static bool asBool(const switchType);
//- Return a bool representation of a word
// Optionally allow bad words, and catch the error elsewhere
static bool asBool
(
const std::string&,
const bool allowInvalid=false
);
static switchType asEnum(const std::string&, const bool allowInvalid);
//- Return a text representation of a bool value
static const char* asText(const bool);
//- Return a text representation of a switchType
static const char* asText(const switchType);
public:
// Constructors
......@@ -142,35 +120,35 @@ public:
{}
//- Construct from enumerated value
Switch(const switchType value)
Switch(const switchType sw)
:
switch_(value)
switch_(sw)
{}
//- Construct from bool
Switch(const bool value)
Switch(const bool b)
:
switch_(asEnum(value))
switch_(b ? Switch::TRUE : Switch::FALSE)
{}
//- Construct from integer values (treats integer as bool value)
Switch(const int value)
Switch(const int i)
:
switch_(asEnum(bool(value)))
switch_(i ? Switch::TRUE : Switch::FALSE)
{}
//- Construct from std::string, string, word
// Optionally allow bad words, and catch the error elsewhere
Switch(const std::string& value, const bool allowInvalid=false)
Switch(const std::string& str, const bool allowInvalid=false)
:
switch_(asEnum(value, allowInvalid))
switch_(asEnum(str, allowInvalid))
{}
//- Construct from character array
// Optionally allow bad words, and catch the error elsewhere
Switch(const char* value, const bool allowInvalid=false)
Switch(const char* str, const bool allowInvalid=false)
:
switch_(asEnum(std::string(value, allowInvalid)))
switch_(asEnum(std::string(str), allowInvalid))
{}
//- Construct from Istream
......@@ -189,10 +167,13 @@ public:
// Member Functions
//- Return true if the Switch has a valid value
bool valid() const
{
return switch_ <= Switch::NONE;
}
bool valid() const;
//- Return a text representation of the Switch
const char* asText() const;
//- Update the value of the Switch if it is found in the dictionary
bool readIfPresent(const word&, const dictionary&);
// Member Operators
......@@ -218,12 +199,6 @@ public:
}
// Member fuctions
//- Update the value of the Switch if it is found in the dictionary
bool readIfPresent(const word&, const dictionary&);
// IOstream Operators
friend Istream& operator>>(Istream&, Switch&);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -48,7 +48,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
if (t.isLabel())
{
s.switch_ = Switch::asEnum(bool(t.labelToken()));
s = bool(t.labelToken());
}
else if (t.isWord())
{
......@@ -57,12 +57,12 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
if (sw.valid())
{
s.switch_ = sw.switch_;
s = sw;
}
else
{
is.setBad();
FatalIOErrorIn("operator>>(Istream&, Switch&)", is)
FatalIOErrorIn("operator>>(Istream&, bool/Switch&)", is)
<< "expected 'true/false', 'on/off' ... found " << t.wordToken()
<< exit(FatalIOError);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -50,7 +50,7 @@ Foam::Istream& Foam::operator>>(Istream& is, bool& b)
Foam::Ostream& Foam::operator<<(Ostream& os, const bool b)
{
// we could also write as text string without any difficulty
// os << Switch::asText(b);
// os << (b ? "true" : "false");
os.write(label(b));
os.check("Ostream& operator<<(Ostream&, const bool)");
return os;
......@@ -59,10 +59,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const bool b)
bool Foam::readBool(Istream& is)
{
bool val;
is >> val;
bool b;
is >> b;
return val;
return 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