Public Types | Public Member Functions | Static Public Member Functions | List of all members
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...

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

Static Public Member Functions

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. More...
 
static const char * name (const bool b) noexcept
 A string representation of bool as "false" / "true". More...
 
static Switch find (const std::string &str)
 Find switchType for the given string, returning as a Switch that can be tested for good() or bad(). More...
 
static bool found (const std::string &str)
 Test if there is a switch type corresponding to the given string. More...
 
static Switch lookupOrAddToDict (const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
 Same as getOrAddToDict() More...
 

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 77 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 for determining 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 91 of file Switch.H.

Constructor & Destructor Documentation

◆ Switch() [1/15]

Switch ( const Switch )
defaultnoexcept

Copy construct.

◆ Switch() [2/15]

constexpr Switch ( )
inlinenoexcept

Default construct as false.

Definition at line 141 of file Switch.H.

◆ Switch() [3/15]

constexpr Switch ( const switchType  sw)
inlinenoexcept

Implicit construct from enumerated value.

Definition at line 149 of file Switch.H.

◆ Switch() [4/15]

constexpr Switch ( const bool  b)
inlinenoexcept

Implicit construct from bool.

Definition at line 157 of file Switch.H.

◆ Switch() [5/15]

constexpr Switch ( const int  i)
inlinenoexcept

Implicit construct from int (treat integer as bool value)

Definition at line 165 of file Switch.H.

◆ Switch() [6/15]

Switch ( const std::string &  str)
explicit

Construct from string - catches bad input.

Use static find() method for a failsafe alternative

Definition at line 172 of file Switch.C.

◆ Switch() [7/15]

Switch ( const char *  str)
explicit

Construct from character array - catches bad input.

Use static find() method for a failsafe alternative

Definition at line 178 of file Switch.C.

◆ Switch() [8/15]

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 196 of file Switch.C.

◆ Switch() [9/15]

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 202 of file Switch.C.

◆ Switch() [10/15]

Switch ( const token tok)
explicit

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

Definition at line 208 of file Switch.C.

References token::boolToken(), token::good(), token::isBool(), token::isLabel(), token::isWord(), token::labelToken(), and token::wordToken().

Here is the call graph for this function:

◆ Switch() [11/15]

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 231 of file Switch.C.

References dict, Foam::exit(), Foam::FatalIOError, FatalIOErrorInFunction, dictionary::get(), Switch::good(), Foam::glTF::key(), keyType::LITERAL, and Foam::printTokenError().

Here is the call graph for this function:

◆ Switch() [12/15]

Switch ( const word key,
const dictionary dict,
const Switch  deflt,
const bool  failsafe = 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 failsafe downgrades the FatalIOError to an IOWarning.

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

Definition at line 255 of file Switch.C.

References Switch::c_str(), dict, Foam::endl(), Foam::exit(), Foam::FatalIOError, FatalIOErrorInFunction, Switch::good(), IOWarningInFunction, Foam::glTF::key(), keyType::LITERAL, Foam::printTokenError(), and dictionary::readIfPresent().

Here is the call graph for this function:

◆ Switch() [13/15]

Switch ( Istream is)
explicit

Construct from Istream by reading a token.

Definition at line 288 of file Switch.C.

◆ Switch() [14/15]

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

Definition at line 184 of file Switch.C.

◆ Switch() [15/15]

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

Definition at line 190 of file Switch.C.

Member Function Documentation

◆ operator=() [1/3]

Switch& operator= ( const Switch )
defaultnoexcept

Copy assignment.

◆ getOrAddToDict()

Foam::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

Definition at line 160 of file Switch.C.

References dict, dictionary::getOrAdd(), Foam::glTF::key(), and keyType::LITERAL.

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 141 of file Switch.C.

References Foam::constant::physicoChemical::b, and Foam::PtrListOps::names().

Referenced by surfaceNoise::calculate(), debugWriter::debugWriter(), boolEntry::evaluate(), Switch::lookupOrAddToDict(), ensightMesh::options::print(), profilingInformation::write(), mappedPatchBase::write(), and attachDetach::writeDict().

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

◆ find()

Foam::Switch find ( const std::string &  str)
static

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

Definition at line 147 of file Switch.C.

Referenced by isTrue(), scalarRange::parse(), and error::useAbort().

Here is the caller graph for this function:

◆ found()

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

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

Definition at line 153 of file Switch.C.

◆ good()

bool good ( ) const
noexcept

True if the Switch represents a valid enumeration.

Definition at line 296 of file Switch.C.

Referenced by argList::argList(), Switch::bad(), IOstreamOption::compressionEnum(), Switch::FOAM_DEPRECATED_FOR(), isoSurfaceParams::getFilterType(), isTrue(), Foam::operator>>(), scalarRange::parse(), and Switch::Switch().

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 277 of file Switch.H.

References Switch::good().

Here is the call graph for this function:

◆ type()

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

The underlying enumeration value.

Definition at line 302 of file Switch.C.

◆ negate()

void negate ( )
noexcept

Flip the type, so OFF becomes ON, etc.

Ignored if the Switch is INVALID

Definition at line 308 of file Switch.C.

◆ c_str()

const char * c_str ( ) const
noexcept

A C-string representation of the Switch value.

Definition at line 318 of file Switch.C.

References Foam::PtrListOps::names().

Referenced by Foam::operator<<(), and Switch::Switch().

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.

Definition at line 324 of file Switch.C.

References Foam::PtrListOps::names().

Here is the call 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

Definition at line 331 of file Switch.C.

References dict, Foam::glTF::key(), keyType::LITERAL, and dictionary::readIfPresent().

Referenced by qZeta::read(), and adjointkOmegaSST::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 316 of file Switch.H.

◆ operator=() [2/3]

Switch& operator= ( const switchType  sw)
inlinenoexcept

Assignment from enumerated value.

Definition at line 324 of file Switch.H.

◆ operator=() [3/3]

Switch& operator= ( const bool  b)
inlinenoexcept

Assignment from bool.

Definition at line 333 of file Switch.H.

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

◆ FOAM_DEPRECATED_FOR()

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

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

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

Definition at line 363 of file Switch.H.

References Switch::good().

Here is the call graph for this function:

◆ lookupOrAddToDict()

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

Same as getOrAddToDict()

Definition at line 373 of file Switch.H.

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

Here is the call graph for this function:

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