Commit 3b16c365 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: make Switch construct from string explicit (issue #568)

- actually prevent this type of thing:

      Switch sw;
      sw = "none";

  without relinquishing automatic conversion to/from bool.
  Nonetheless, make construct from string explicit.

- Added some minor optimization for the lookup of the switch names.
parent b3128e6c
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -32,10 +32,8 @@ License ...@@ -32,10 +32,8 @@ License
const char* Foam::Switch::names[Foam::Switch::INVALID+1] = const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
{ {
"false", "true", "false", "true",
"off", "on",
"no", "yes", "no", "yes",
"n", "y", "off", "on",
"f", "t",
"none", "true", // Is there a reasonable counterpart to "none"? "none", "true", // Is there a reasonable counterpart to "none"?
"invalid" "invalid"
}; };
...@@ -49,55 +47,53 @@ Foam::Switch::switchType Foam::Switch::asEnum ...@@ -49,55 +47,53 @@ Foam::Switch::switchType Foam::Switch::asEnum
const bool allowInvalid const bool allowInvalid
) )
{ {
for (int sw = 0; sw < Switch::INVALID; ++sw) const std::string::size_type len = str.size();
switch (len)
{ {
if (str == names[sw]) case 1: // (f|n|t|y) - single-character forms
{ {
// handle aliases switch (str[0])
switch (sw)
{ {
case Switch::NO_1: case 'f': return switchType::FALSE;
case Switch::NONE: case 'n': return switchType::NO;
{ case 't': return switchType::TRUE;
return Switch::NO; case 'y': return switchType::YES;
break;
}
case Switch::YES_1:
{
return Switch::YES;
break;
}
case Switch::FALSE_1:
{
return Switch::FALSE;
break;
}
case Switch::TRUE_1:
{
return Switch::TRUE;
break;
}
default:
{
return switchType(sw);
break;
}
} }
break;
}
case 2: // (no|on)
{
if (str == names[switchType::NO]) return switchType::NO;
if (str == names[switchType::ON]) return switchType::ON;
break;
}
case 3: // (off|yes)
{
if (str == names[switchType::OFF]) return switchType::OFF;
if (str == names[switchType::YES]) return switchType::YES;
break;
}
case 4: // (none|true)
{
if (str == names[switchType::NONE]) return switchType::NONE;
if (str == names[switchType::TRUE]) return switchType::TRUE;
break;
}
case 5: // (false)
{
if (str == names[switchType::FALSE]) return switchType::FALSE;
break;
} }
} }
if (!allowInvalid) if (!allowInvalid)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "unknown switch word " << str << nl << "Unknown switch word " << str << nl
<< abort(FatalError); << abort(FatalError);
} }
return Switch::INVALID; return switchType::INVALID;
} }
...@@ -116,7 +112,7 @@ Foam::Switch Foam::Switch::lookupOrAddToDict ...@@ -116,7 +112,7 @@ Foam::Switch Foam::Switch::lookupOrAddToDict
bool Foam::Switch::valid() const bool Foam::Switch::valid() const
{ {
return switch_ <= Switch::NONE; return switch_ <= switchType::NONE;
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -69,17 +69,13 @@ public: ...@@ -69,17 +69,13 @@ public:
// Public data types // Public data types
// avoid issues with pre-processor defines // Avoid issues with possible pre-processor defines
#undef FALSE #undef FALSE
#undef TRUE #undef TRUE
#undef OFF
#undef ON
#undef NO #undef NO
#undef YES #undef YES
#undef NO_1 #undef OFF
#undef YES_1 #undef ON
#undef FALSE_1
#undef TRUE_1
#undef NONE #undef NONE
#undef PLACEHOLDER #undef PLACEHOLDER
#undef INVALID #undef INVALID
...@@ -89,11 +85,9 @@ public: ...@@ -89,11 +85,9 @@ public:
enum switchType enum switchType
{ {
FALSE = 0, TRUE = 1, FALSE = 0, TRUE = 1,
OFF = 2, ON = 3, NO = 2, YES = 3,
NO = 4, YES = 5, OFF = 4, ON = 5,
NO_1 = 6, YES_1 = 7, NONE = 6, PLACEHOLDER = 7,
FALSE_1 = 8, TRUE_1 = 9,
NONE = 10, PLACEHOLDER = 11,
INVALID INVALID
}; };
...@@ -123,7 +117,7 @@ public: ...@@ -123,7 +117,7 @@ public:
//- Construct null as false //- Construct null as false
Switch() Switch()
: :
switch_(Switch::FALSE) switch_(switchType::FALSE)
{} {}
//- Construct from enumerated value //- Construct from enumerated value
...@@ -135,25 +129,37 @@ public: ...@@ -135,25 +129,37 @@ public:
//- Construct from bool //- Construct from bool
Switch(const bool b) Switch(const bool b)
: :
switch_(b ? Switch::TRUE : Switch::FALSE) switch_(b ? switchType::TRUE : switchType::FALSE)
{} {}
//- Construct from integer values (treats integer as bool value) //- Construct from integer values (treats integer as bool value)
Switch(const int i) Switch(const int i)
: :
switch_(i ? Switch::TRUE : Switch::FALSE) switch_(i ? switchType::TRUE : switchType::FALSE)
{}
//- Construct from string - catches bad input.
explicit Switch(const std::string& str)
:
switch_(asEnum(str, false))
{}
//- Construct from character array - catches bad input.
explicit Switch(const char* str)
:
switch_(asEnum(std::string(str), false))
{} {}
//- Construct from std::string, string, word //- Construct from string.
// Optionally allow bad words, and catch the error elsewhere // Optionally allow bad words, and catch the error elsewhere
Switch(const std::string& str, const bool allowInvalid=false) Switch(const std::string& str, const bool allowInvalid)
: :
switch_(asEnum(str, allowInvalid)) switch_(asEnum(str, allowInvalid))
{} {}
//- Construct from character array //- Construct from character array.
// Optionally allow bad words, and catch the error elsewhere // Optionally allow bad words, and catch the error elsewhere
Switch(const char* str, const bool allowInvalid=false) Switch(const char* str, const bool allowInvalid)
: :
switch_(asEnum(std::string(str), allowInvalid)) switch_(asEnum(std::string(str), allowInvalid))
{} {}
...@@ -167,7 +173,7 @@ public: ...@@ -167,7 +173,7 @@ public:
( (
const word& name, const word& name,
dictionary& dict, dictionary& dict,
const Switch& defaultValue = false const Switch& defaultValue = switchType::FALSE
); );
......
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