diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index f5be21f941..9ddb0d95c1 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,10 +32,8 @@ License const char* Foam::Switch::names[Foam::Switch::INVALID+1] = { "false", "true", - "off", "on", "no", "yes", - "n", "y", - "f", "t", + "off", "on", "none", "true", // Is there a reasonable counterpart to "none"? "invalid" }; @@ -49,55 +47,53 @@ Foam::Switch::switchType Foam::Switch::asEnum 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 (sw) + switch (str[0]) { - case Switch::NO_1: - case Switch::NONE: - { - return Switch::NO; - 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; - } + case 'f': return switchType::FALSE; + case 'n': return switchType::NO; + case 't': return switchType::TRUE; + case 'y': return switchType::YES; } + 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) { FatalErrorInFunction - << "unknown switch word " << str << nl + << "Unknown switch word " << str << nl << abort(FatalError); } - return Switch::INVALID; + return switchType::INVALID; } @@ -116,7 +112,7 @@ Foam::Switch Foam::Switch::lookupOrAddToDict bool Foam::Switch::valid() const { - return switch_ <= Switch::NONE; + return switch_ <= switchType::NONE; } diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index 63ef5ead52..256944b936 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -69,17 +69,13 @@ public: // Public data types - // avoid issues with pre-processor defines + // Avoid issues with possible pre-processor defines #undef FALSE #undef TRUE - #undef OFF - #undef ON #undef NO #undef YES - #undef NO_1 - #undef YES_1 - #undef FALSE_1 - #undef TRUE_1 + #undef OFF + #undef ON #undef NONE #undef PLACEHOLDER #undef INVALID @@ -89,11 +85,9 @@ public: enum switchType { FALSE = 0, TRUE = 1, - OFF = 2, ON = 3, - NO = 4, YES = 5, - NO_1 = 6, YES_1 = 7, - FALSE_1 = 8, TRUE_1 = 9, - NONE = 10, PLACEHOLDER = 11, + NO = 2, YES = 3, + OFF = 4, ON = 5, + NONE = 6, PLACEHOLDER = 7, INVALID }; @@ -123,7 +117,7 @@ public: //- Construct null as false Switch() : - switch_(Switch::FALSE) + switch_(switchType::FALSE) {} //- Construct from enumerated value @@ -135,25 +129,37 @@ public: //- Construct from bool 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) Switch(const int i) : - switch_(i ? Switch::TRUE : Switch::FALSE) + switch_(i ? switchType::TRUE : switchType::FALSE) {} - //- Construct from std::string, string, word + //- 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 string. // 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)) {} - //- Construct from character array + //- Construct from character array. // 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)) {} @@ -167,7 +173,7 @@ public: ( const word& name, dictionary& dict, - const Switch& defaultValue = false + const Switch& defaultValue = switchType::FALSE );