Switch Class Reference

A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none. More...

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

constexpr Switch () noexcept
 Null constructible as false. More...
 
constexpr Switch (const switchType sw) noexcept
 Construct from enumerated value. More...
 
constexpr Switch (const bool b) noexcept
 Construct from bool. More...
 
constexpr Switch (const int i) noexcept
 Construct from int (treat integer as bool value) More...
 
 Switch (const float val, const float tol=0.5)
 
 Switch (const double val, const double tol=0.5)
 
 Switch (const std::string &str)
 Construct from string - catches bad input. More...
 
 Switch (const char *str)
 Construct from character array - catches bad input. More...
 
 Switch (const std::string &str, bool allowBad)
 Construct from string. More...
 
 Switch (const char *str, bool allowBad)
 Construct from character array. More...
 
 Switch (const word &key, const dictionary &dict)
 Construct from dictionary lookup. More...
 
 Switch (const word &key, const dictionary &dict, const Switch deflt)
 
 Switch (Istream &is)
 Construct from Istream. More...
 
bool valid () const noexcept
 True if the Switch represents a valid enumeration. More...
 
switchType type () const noexcept
 The underlying enumeration value. More...
 
const char * c_str () const noexcept
 A string representation of the Switch value. More...
 
std::string str () const
 A string representation of the Switch value. More...
 
bool readIfPresent (const word &name, const dictionary &dict)
 Update the value of the Switch if it is found in the dictionary. More...
 
 operator bool () const noexcept
 Conversion to bool. More...
 
Switchoperator= (const switchType sw) noexcept
 Assignment from enumerated value. More...
 
Switchoperator= (const bool b) noexcept
 Assignment from bool. More...
 
const char * asText () const
 Deprecated(2018-03) text representation of the Switch value. More...
 

Static Public Member Functions

static Switch getOrAddToDict (const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
 
static const char * name (const bool b) noexcept
 A string representation of bool as "false" / "true". More...
 
static Switch lookupOrAddToDict (const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
 

Detailed Description

A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none.

Source files

Definition at line 70 of file Switch.H.

Member Enumeration Documentation

◆ switchType

enum switchType : unsigned char

Switch enumerations corresponding to common text representations.

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

"false"

TRUE 

"true"

NO 

"no"

YES 

"yes"

OFF 

"off"

ON 

"on"

NONE 

"none"

ANY 

"any"

INVALID 

"invalid"

Definition at line 79 of file Switch.H.

Constructor & Destructor Documentation

◆ Switch() [1/13]

constexpr Switch ( )
inlineconstexprnoexcept

Null constructible as false.

Definition at line 108 of file Switch.H.

◆ Switch() [2/13]

constexpr Switch ( const switchType  sw)
inlineconstexprnoexcept

Construct from enumerated value.

Definition at line 114 of file Switch.H.

◆ Switch() [3/13]

constexpr Switch ( const bool  b)
inlineconstexprnoexcept

Construct from bool.

Definition at line 120 of file Switch.H.

◆ Switch() [4/13]

constexpr Switch ( const int  i)
inlineconstexprnoexcept

Construct from int (treat integer as bool value)

Definition at line 126 of file Switch.H.

◆ Switch() [5/13]

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

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

Definition at line 135 of file Switch.C.

◆ Switch() [6/13]

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

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

Definition at line 141 of file Switch.C.

◆ Switch() [7/13]

Switch ( const std::string &  str)
inlineexplicit

Construct from string - catches bad input.

Definition at line 140 of file Switch.H.

◆ Switch() [8/13]

Switch ( const char *  str)
inlineexplicit

Construct from character array - catches bad input.

Definition at line 146 of file Switch.H.

◆ Switch() [9/13]

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

Construct from string.

Optionally allow bad words, and catch the error elsewhere

Definition at line 153 of file Switch.H.

◆ Switch() [10/13]

Switch ( const char *  str,
bool  allowBad 
)
inline

Construct from character array.

Optionally allow bad words, and catch the error elsewhere

Definition at line 160 of file Switch.H.

◆ Switch() [11/13]

Switch ( const word key,
const dictionary dict 
)

Construct from dictionary lookup.

FatalError if anything is incorrect.

Parameters
keyLookup key. Uses LITERAL (not REGEX)
dictdictionary

Definition at line 148 of file Switch.C.

References dict, Foam::exit(), Foam::FatalIOError, FatalIOErrorInFunction, dictionary::get(), keyType::LITERAL, and Foam::nl.

Here is the call graph for this function:

◆ Switch() [12/13]

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

Find the key in the dictionary and return the corresponding switch value, or the default value.

FatalError if anything is incorrect.

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

Definition at line 167 of file Switch.C.

References dict, Foam::exit(), Foam::FatalIOError, FatalIOErrorInFunction, dictionary::findEntry(), entry::get(), keyType::LITERAL, and Foam::nl.

Here is the call graph for this function:

◆ Switch() [13/13]

Switch ( Istream is)
explicit

Construct from Istream.

Definition at line 195 of file Switch.C.

Member Function Documentation

◆ getOrAddToDict()

Foam::Switch getOrAddToDict ( const word name,
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
nameLookup key. Uses REGEX!
dictdictionary
defltdefault to add

Definition at line 123 of file Switch.C.

References dict, dictionary::getOrAdd(), and Foam::name().

Referenced by Switch::lookupOrAddToDict().

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

◆ name()

const char * name ( const bool  b)
staticnoexcept

A string representation of bool as "false" / "true".

Definition at line 60 of file Switch.C.

References Foam::constant::physicoChemical::b.

Referenced by boolEntry::evaluate(), and Switch::lookupOrAddToDict().

Here is the caller graph for this function:

◆ valid()

bool valid ( ) const
noexcept

True if the Switch represents a valid enumeration.

Definition at line 203 of file Switch.C.

Referenced by IOstreamOption::compressionEnum(), isoSurfaceBase::getFilterType(), isTrue(), Foam::operator>>(), and scalarRange::parse().

Here is the caller graph for this function:

◆ type()

Foam::Switch::switchType type ( ) const
noexcept

The underlying enumeration value.

Definition at line 209 of file Switch.C.

◆ c_str()

const char * c_str ( ) const
noexcept

A string representation of the Switch value.

Definition at line 215 of file Switch.C.

Referenced by Foam::operator<<().

Here is the caller graph for this function:

◆ str()

std::string str ( ) const

A string representation of the Switch value.

Definition at line 221 of file Switch.C.

◆ readIfPresent()

bool readIfPresent ( const word name,
const dictionary dict 
)

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

Parameters
nameLookup key. Uses REGEX!
dictdictionary

Definition at line 227 of file Switch.C.

References dict, Foam::name(), and dictionary::readIfPresent().

Referenced by qZeta::read().

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

◆ operator bool()

operator bool ( ) const
inlinenoexcept

Conversion to bool.

Definition at line 230 of file Switch.H.

◆ operator=() [1/2]

Switch& operator= ( const switchType  sw)
inlinenoexcept

Assignment from enumerated value.

Definition at line 236 of file Switch.H.

◆ operator=() [2/2]

Switch& operator= ( const bool  b)
inlinenoexcept

Assignment from bool.

Definition at line 243 of file Switch.H.

References Foam::constant::physicoChemical::b, Switch::FALSE, and Switch::TRUE.

◆ lookupOrAddToDict()

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

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

Parameters
nameLookup key. Uses REGEX!
dictdictionary
defltdefault to add

Definition at line 255 of file Switch.H.

References dict, Switch::getOrAddToDict(), and Switch::name().

Here is the call graph for this function:

◆ asText()

const char* asText ( ) const
inline

Deprecated(2018-03) text representation of the Switch value.

Deprecated:
(2018-03) - use c_str() method

Definition at line 266 of file Switch.H.


The documentation for this class was generated from the following files: