Loading...
Searching...
No Matches
Switch Class Reference

A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n. More...

#include <Switch.H>

Public Types

enum  switchType : unsigned char {
  FALSE = 0 , TRUE = 1 , NO = 2 , YES = 3 ,
  OFF = 4 , ON = 5 , NONE = 6 , ANY = 7 ,
  INVALID = 8
}
 Switch enumerations corresponding to common text representations. More...

Public Member Functions

 Switch (const Switch &) noexcept=default
 Copy construct.
Switchoperator= (const Switch &) noexcept=default
 Copy assignment.
constexpr Switch () noexcept
 Default construct as false.
constexpr Switch (switchType sw) noexcept
 Implicit construct from enumerated value.
constexpr Switch (bool b) noexcept
 Implicit construct from bool.
constexpr Switch (int i) noexcept
 Implicit construct from int (treat integer as bool value).
 Switch (const char *s)
 Construct from character array - catches bad input.
 Switch (const std::string &s)
 Construct from string - catches bad input.
 Switch (std::string_view s)
 Construct from string_view - catches bad input.
 Switch (const float val, const float tol=0.5)
 Construct from float with rounding to zero given by the tolerance (default: 0.5).
 Switch (const double val, const double tol=0.5)
 Construct from double with rounding to zero given by the tolerance (default: 0.5).
 Switch (const token &tok)
 Construct from token. Handles bool/label/word types.
 Switch (const word &key, const dictionary &dict)
 Construct from dictionary lookup.
 Switch (const word &key, const dictionary &dict, const Switch deflt, const bool warnOnly=false)
 Find the key in the dictionary and use the corresponding switch value or the default if not found in dictionary.
 Switch (Istream &is)
 Construct from Istream by reading a token.
bool good () const noexcept
 True if the Switch represents a valid enumeration.
bool bad () const noexcept
 True if the Switch does not represent a valid enumeration.
switchType type () const noexcept
 The underlying enumeration value.
void negate () noexcept
 Flip the type, so OFF becomes ON, etc.
const char * c_str () const noexcept
 A C-string representation of the Switch value.
std::string str () const
 A string representation of the Switch value.
bool readIfPresent (const word &key, const dictionary &dict)
 Update the value of the Switch if it is found in the dictionary.
bool is_any () const noexcept
 Test for 'any' type.
bool is_none () const noexcept
 Test for 'none' type.
 operator bool () const noexcept
 Conversion to bool.
Switchoperator= (switchType sw) noexcept
 Assignment from enumerated value.
Switchoperator= (bool b) noexcept
 Assignment from bool.
 Switch (const std::string &str, bool allowBad)
 Deprecated(2020-01) From string with/without bad input test.
 Switch (const char *str, bool allowBad)
 Deprecated(2020-01) From string with/without bad input test.
 FOAM_DEPRECATED_FOR (2019-02, "good() or static contains() method") bool valid() const noexcept
 Deprecated(2020-01) Use good() method, or static contains() method.

Static Public Member Functions

static Switch getOrDefault (const word &key, const dictionary &dict, const Switch deflt=switchType::FALSE)
 Construct Switch from dictionary, with default value.
static Switch getOrAddToDict (const word &key, dictionary &dict, const Switch deflt=switchType::FALSE)
 Construct from dictionary, supplying default value so that if the value is not found, it is added into the dictionary.
static const char * name (bool b) noexcept
 A string representation of bool as "false" / "true".
static bool contains (std::string_view s) noexcept
 True if there is a switch type corresponding to the given string.
static Switch find (const char *s)
 Find switchType for the given string, returning a Switch that can be tested for good() or bad().
static Switch find (const std::string &s) noexcept
 Find switchType for the given string, returning a Switch that can be tested for good() or bad().
static Switch find (std::string_view s) noexcept
 Find switchType for the given string, returning a Switch that can be tested for good() or bad().
static bool found (const std::string &str)
 Same as contains().
static Switch lookupOrAddToDict (const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
 Same as getOrAddToDict().

Detailed Description

A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n.

Source files

Definition at line 80 of file Switch.H.

Member Enumeration Documentation

◆ switchType

enum switchType : unsigned char

Switch enumerations corresponding to common text representations.

Note
The values here are critical for its proper behaviour. The values correspond to an index into the predefined output names for the c_str() method and the lower bit is tested to determine the true/false bool value.
Enumerator
FALSE 

"false"

TRUE 

"true"

NO 

"no"

YES 

"yes"

OFF 

"off"

ON 

"on"

NONE 

"none"

ANY 

"any"

INVALID 

"invalid" (output only)

Definition at line 94 of file Switch.H.

Constructor & Destructor Documentation

◆ Switch() [1/16]

Switch ( const Switch & )
defaultnoexcept

Copy construct.

References Switch().

Referenced by find(), find(), find(), getOrAddToDict(), getOrDefault(), lookupOrAddToDict(), operator=(), operator=(), operator=(), Switch(), Switch(), Switch(), and Switch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Switch() [2/16]

Switch ( )
inlineconstexprnoexcept

Default construct as false.

Definition at line 147 of file Switch.H.

References FALSE, and Foam::noexcept.

◆ Switch() [3/16]

Switch ( switchType sw)
inlineconstexprnoexcept

Implicit construct from enumerated value.

Definition at line 155 of file Switch.H.

References Foam::noexcept.

◆ Switch() [4/16]

Switch ( bool b)
inlineconstexprnoexcept

Implicit construct from bool.

Definition at line 163 of file Switch.H.

References b, FALSE, Foam::noexcept, and TRUE.

◆ Switch() [5/16]

Switch ( int i)
inlineconstexprnoexcept

Implicit construct from int (treat integer as bool value).

Definition at line 171 of file Switch.H.

References FALSE, Foam::noexcept, and TRUE.

◆ Switch() [6/16]

Switch ( const char * s)
explicit

Construct from character array - catches bad input.

Use static find() method for a failsafe alternative

References s().

Here is the call graph for this function:

◆ Switch() [7/16]

Switch ( const std::string & s)
explicit

Construct from string - catches bad input.

Use static find() method for a failsafe alternative

References s().

Here is the call graph for this function:

◆ Switch() [8/16]

Switch ( std::string_view s)
explicit

Construct from string_view - catches bad input.

Use static find() method for a failsafe alternative

References s().

Here is the call graph for this function:

◆ Switch() [9/16]

Switch ( const float val,
const float tol = 0.5 )
explicit

Construct from float with rounding to zero given by the tolerance (default: 0.5).

◆ Switch() [10/16]

Switch ( const double val,
const double tol = 0.5 )
explicit

Construct from double with rounding to zero given by the tolerance (default: 0.5).

◆ Switch() [11/16]

Switch ( const token & tok)
explicit

Construct from token. Handles bool/label/word types.

◆ Switch() [12/16]

Switch ( const word & key,
const dictionary & dict )

Construct from dictionary lookup.

FatalError if anything is incorrect.

Parameters
keyLookup key. Uses LITERAL (not REGEX)
dictdictionary

References dict.

◆ Switch() [13/16]

Switch ( const word & key,
const dictionary & dict,
const Switch deflt,
const bool warnOnly = false )

Find the key in the dictionary and use the corresponding switch value or the default if not found in dictionary.

FatalIOError if the switch name is incorrect. Specifying warnOnly downgrades the FatalIOError to an IOWarning.

Parameters
keyLookup key. Uses LITERAL (not REGEX)
dictdictionary
defltfallback if not found
warnOnlyWarn (not fail) on bad input

References dict, and Switch().

Here is the call graph for this function:

◆ Switch() [14/16]

Switch ( Istream & is)
explicit

Construct from Istream by reading a token.

◆ Switch() [15/16]

Switch ( const std::string & str,
bool allowBad )

Deprecated(2020-01) From string with/without bad input test.

Deprecated
(2020-01) - confusing syntax, use static find() method

References FOAM_DEPRECATED_FOR(), str(), and Switch().

Here is the call graph for this function:

◆ Switch() [16/16]

Switch ( const char * str,
bool allowBad )

Deprecated(2020-01) From string with/without bad input test.

Deprecated
(2020-01) - confusing syntax, use static find() method

References str(), and Switch().

Here is the call graph for this function:

Member Function Documentation

◆ operator=() [1/3]

Switch & operator= ( const Switch & )
defaultnoexcept

Copy assignment.

References Switch().

Here is the call graph for this function:

◆ getOrDefault()

Switch getOrDefault ( const word & key,
const dictionary & dict,
const Switch deflt = switchType::FALSE )
static

Construct Switch from dictionary, with default value.

Parameters
keyLookup key. Uses LITERAL (not REGEX)
dictdictionary
defltfallback if not found

References dict, FALSE, and Switch().

Here is the call graph for this function:

◆ getOrAddToDict()

Switch getOrAddToDict ( const word & key,
dictionary & dict,
const Switch deflt = switchType::FALSE )
static

Construct from dictionary, supplying default value so that if the value is not found, it is added into the dictionary.

Parameters
keyLookup key. Uses LITERAL (not REGEX)
dictdictionary
defltdefault value to add

References dict, FALSE, and Switch().

Referenced by lookupOrAddToDict().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ name()

const char * name ( bool b)
staticnoexcept

◆ contains()

bool contains ( std::string_view s)
staticnoexcept

True if there is a switch type corresponding to the given string.

References s().

Referenced by found().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find() [1/3]

Switch find ( const char * s)
static

Find switchType for the given string, returning a Switch that can be tested for good() or bad().

References s(), and Switch().

Referenced by IOstreamOption::compressionEnum(), isoSurfaceParams::getFilterType(), and error::useAbort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find() [2/3]

Switch find ( const std::string & s)
staticnoexcept

Find switchType for the given string, returning a Switch that can be tested for good() or bad().

References s(), and Switch().

Here is the call graph for this function:

◆ find() [3/3]

Switch find ( std::string_view s)
staticnoexcept

Find switchType for the given string, returning a Switch that can be tested for good() or bad().

References s(), and Switch().

Here is the call graph for this function:

◆ good()

bool good ( ) const
inlinenoexcept

True if the Switch represents a valid enumeration.

Definition at line 307 of file Switch.H.

References INVALID, and Foam::noexcept.

Referenced by bad(), FOAM_DEPRECATED_FOR(), isoSurfaceParams::getFilterType(), and functionObjectList::read().

Here is the caller graph for this function:

◆ bad()

bool bad ( ) const
inlinenoexcept

True if the Switch does not represent a valid enumeration.

Definition at line 312 of file Switch.H.

References good(), and Foam::noexcept.

Here is the call graph for this function:

◆ type()

switchType type ( ) const
inlinenoexcept

The underlying enumeration value.

Definition at line 317 of file Switch.H.

References Foam::noexcept.

◆ negate()

void negate ( )
noexcept

Flip the type, so OFF becomes ON, etc.

Ignored if the Switch is INVALID

References Foam::noexcept.

◆ c_str()

const char * c_str ( ) const
noexcept

A C-string representation of the Switch value.

References c_str(), and Foam::noexcept.

Referenced by c_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ str()

std::string str ( ) const

A string representation of the Switch value.

References str().

Referenced by found(), str(), Switch(), and Switch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readIfPresent()

bool readIfPresent ( const word & key,
const dictionary & dict )

Update the value of the Switch if it is found in the dictionary.

Parameters
keyLookup key. Uses LITERAL (not REGEX)
dictdictionary

References dict, and readIfPresent().

Referenced by readIfPresent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_any()

bool is_any ( ) const
inlinenoexcept

Test for 'any' type.

Definition at line 351 of file Switch.H.

References ANY, is_any(), and Foam::noexcept.

Referenced by is_any().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_none()

bool is_none ( ) const
inlinenoexcept

Test for 'none' type.

Definition at line 359 of file Switch.H.

References Foam::noexcept, and NONE.

◆ operator bool()

operator bool ( ) const
inlinenoexcept

Conversion to bool.

Definition at line 370 of file Switch.H.

References Foam::noexcept.

◆ operator=() [2/3]

Switch & operator= ( switchType sw)
inlinenoexcept

Assignment from enumerated value.

Definition at line 378 of file Switch.H.

References Switch().

Here is the call graph for this function:

◆ operator=() [3/3]

Switch & operator= ( bool b)
inlinenoexcept

Assignment from bool.

Definition at line 387 of file Switch.H.

References b, FALSE, Switch(), and TRUE.

Here is the call graph for this function:

◆ found()

bool found ( const std::string & str)
inlinestatic

Same as contains().

Definition at line 399 of file Switch.H.

References contains(), and str().

Here is the call graph for this function:

◆ FOAM_DEPRECATED_FOR()

FOAM_DEPRECATED_FOR ( 2019- 02,
"good() or static contains() method"  ) const
inlinenoexcept

Deprecated(2020-01) Use good() method, or static contains() method.

Deprecated
(2020-01) Use good() method, or static contains() method

Definition at line 422 of file Switch.H.

References FOAM_DEPRECATED_FOR(), good(), and Foam::noexcept.

Referenced by FOAM_DEPRECATED_FOR(), and Switch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookupOrAddToDict()

Switch lookupOrAddToDict ( const word & name,
dictionary & dict,
const Switch deflt = switchType::FALSE )
inlinestatic

Same as getOrAddToDict().

Definition at line 429 of file Switch.H.

References dict, FALSE, getOrAddToDict(), lookupOrAddToDict(), name(), and Switch().

Referenced by lookupOrAddToDict().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file:
  • src/OpenFOAM/primitives/bools/Switch/Switch.H