Commit 0d5114c4 authored by Mark Olesen's avatar Mark Olesen
Browse files

move Switch -> primitives/ and make asBool(), asWord() static

- motivation: we could probably reuse the Switch::asBool() within boolIO.C
  ... and possibly make Switch is-a bool and has-a word (or has-a enum for
  the word) for reuse with nicer output values for bool
parent cdd22664
......@@ -3,8 +3,10 @@ global/dimensionedConstants/dimensionedConstants.C
global/argList/argList.C
global/clock/clock.C
primitives/bool/bool.C
primitives/bool/boolIO.C
primitives/bools/bool/bool.C
primitives/bools/bool/boolIO.C
primitives/bools/Switch/Switch.C
primitives/bools/Switch/SwitchIO.C
primitives/char/charIO.C
primitives/int/intIO.C
primitives/uint/uintIO.C
......@@ -142,10 +144,6 @@ db/functionObjectList/functionObjectList.C
db/CallbackRegistry/CallbackRegistryName.C
db/dlLibraryTable/dlLibraryTable.C
Switch = db/Switch
$(Switch)/Switch.C
$(Switch)/SwitchIO.C
Time = db/Time
$(Time)/TimePaths.C
$(Time)/TimeState.C
......
......@@ -40,16 +40,24 @@ Foam::Switch Foam::Switch::lookupOrAddToDict
return dict.lookupOrAddDefault<Switch>(name, defaultValue);
}
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
// * * * * * * * * * * * * * 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
// };
Foam::word Foam::Switch::wordValue(const bool val) const
{
return word(val ? "on" : "off");
}
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
bool Foam::Switch::boolValue(const word& val) const
bool Foam::Switch::asBool(const word& val)
{
if (val == "on" || val == "true" || val == "yes" || val == "y")
{
......@@ -61,7 +69,7 @@ bool Foam::Switch::boolValue(const word& val) const
}
else
{
FatalErrorIn("Switch::boolValue(const word&) const")
FatalErrorIn("Switch::asBool(const word&)")
<< "unknown switch word " << val
<< abort(FatalError);
}
......@@ -70,11 +78,17 @@ bool Foam::Switch::boolValue(const word& val) const
}
Foam::word Foam::Switch::asWord(const bool val)
{
return word((val ? "on" : "off"), false);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
{
return dict.readIfPresent(name, logicalSwitch_);
return dict.readIfPresent(name, bool_);
}
......
......@@ -63,32 +63,33 @@ class Switch
{
// Private data
word wordSwitch_;
bool logicalSwitch_;
bool bool_;
// Private member functions
word word_;
// could be static instead:
word wordValue(const bool) const;
bool boolValue(const word&) const;
public:
// Private member functions
//- Return a bool representation of a word
static bool asBool(const word&);
public:
//- Return a word representation of a bool value
static word asWord(const bool);
// Constructors
//- Construct from components
//- Construct from bool
Switch(const bool value)
:
wordSwitch_(wordValue(value)),
logicalSwitch_(value)
bool_(value),
word_(asWord(value))
{}
//- Construct from word
Switch(const word& w)
Switch(const word& value)
:
wordSwitch_(w),
logicalSwitch_(boolValue(w))
bool_(asBool(value)),
word_(value)
{}
//- Construct from Istream
......@@ -106,22 +107,22 @@ public:
// Member Operators
Switch& operator=(const Switch& s)
Switch& operator=(const Switch& rhs)
{
wordSwitch_ = s.wordSwitch_;
logicalSwitch_ = s.logicalSwitch_;
bool_ = rhs.bool_;
word_ = rhs.word_;
return *this;
}
operator bool() const
{
return logicalSwitch_;
return bool_;
}
operator const word&() const
{
return wordSwitch_;
return word_;
}
......
......@@ -30,18 +30,17 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::Switch::Switch(Istream& is)
:
wordSwitch_(is),
logicalSwitch_(boolValue(wordSwitch_))
{}
{
is >> *this;
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
{
is >> s.wordSwitch_;
s.logicalSwitch_ = s.boolValue(s.wordSwitch_);
is >> s.word_;
s.bool_ = Switch::asBool(s.word_);
is.check("Istream& operator>>(Istream&, Switch&)");
......@@ -51,7 +50,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s)
{
os << s.wordSwitch_;
os << s.word_;
os.check("Ostream& operator<<(Ostream&, const Switch&)");
......
......@@ -31,25 +31,15 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
const char* const pTraits<bool>::typeName = "bool";
const bool pTraits<bool>::zero = 0;
const bool pTraits<bool>::one = 1;
const char* const Foam::pTraits<bool>::typeName = "bool";
const bool Foam::pTraits<bool>::zero = 0;
const bool Foam::pTraits<bool>::one = 1;
const char* pTraits<bool>::componentNames[] = { "x" };
const char* Foam::pTraits<bool>::componentNames[] = { "x" };
pTraits<bool>::pTraits(Istream& is)
Foam::pTraits<bool>::pTraits(Istream& is)
{
is >> p_;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -50,7 +50,7 @@ class Ostream;
Istream& operator>>(Istream&, bool&);
Ostream& operator<<(Ostream&, const bool);
bool readBool(Istream& is);
bool readBool(Istream&);
} // End namespace Foam
......
......@@ -37,12 +37,7 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Istream& operator>>(Istream& is, bool& b)
Foam::Istream& Foam::operator>>(Istream& is, bool& b)
{
token t(is);
......@@ -58,6 +53,7 @@ Istream& operator>>(Istream& is, bool& b)
}
else if (t.isWord())
{
// use Switch asBool() here?
if (t.wordToken() == "true" || t.wordToken() == "on")
{
b = true;
......@@ -95,7 +91,7 @@ Istream& operator>>(Istream& is, bool& b)
}
Ostream& operator<<(Ostream& os, const bool b)
Foam::Ostream& Foam::operator<<(Ostream& os, const bool b)
{
os.write(label(b));
os.check("Ostream& operator<<(Ostream&, const bool&)");
......@@ -103,17 +99,12 @@ Ostream& operator<<(Ostream& os, const bool b)
}
bool readBool(Istream& is)
bool Foam::readBool(Istream& is)
{
bool rb;
is >> rb;
bool b;
is >> b;
return rb;
return b;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
Markdown is supported
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