ENH: add 'default' as possible Switch state, but not as input/output
- in some circumstances we need to pass a bool value upwards to the caller and know if the true/false value was set based on real input or is a default value. Eg, in the object::read() we might normally have enabled_(dict.readIfPresent(key, true)); but would lose information about why the value is true/false. We can change that by using enabled_(dict.readIfPresent<Switch>(key, Switch::DEFAULT_ON)); After which we can use this information is testing. if ( child.enabled().nonDefault() ? child.enabled() : parent.enabled() ) { ... } And thus enable output if the parent requested it explicitly or by default and it has not been explicitly disabled in the child. No difference when testing as a bool and the text representation of DEFAULT_ON / DEFAULT_OFF will simply be "true" / "false". ENH: add construction of Switch from dictionary (similar to Enum)
Showing
... | ... | @@ -3,7 +3,7 @@ |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox | ||
\\ / O peration | | ||
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation | ||
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. | ||
\\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd. | ||
------------------------------------------------------------------------------- | ||
License | ||
This file is part of OpenFOAM. | ||
... | ... | @@ -81,7 +81,9 @@ public: |
NO = 2 /*!< "no" */, YES = 3 /*!< "yes" */, | ||
OFF = 4 /*!< "off" */, ON = 5 /*!< "on" */, | ||
NONE = 6 /*!< "none" */, | ||
INVALID = 8 /*!< "invalid" */ | ||
INVALID = 8 /*!< "invalid" */, | ||
DEFAULT_OFF = 0x10 /*!< off/false (as default value) */, | ||
DEFAULT_ON = 0x11 /*!< on/true (as default value) */ | ||
}; | ||
... | ... | @@ -92,6 +94,7 @@ private: |
//- The logic and enumerated text representation stored in a byte | ||
unsigned char switch_; | ||
// Static Member Functions | ||
//- Return enum value for input string | ||
... | ... | @@ -152,6 +155,24 @@ public: |
switch_(parse(str, allowBad)) | ||
{} | ||
//- Construct from dictionary lookup. | ||
// FatalError if anything is incorrect. | ||
Switch | ||
( | ||
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) | ||
const dictionary& dict //!< dictionary | ||
); | ||
//- Find the key in the dictionary and return the corresponding | ||
//- switch value, or the default value. | ||
// FatalError if anything is incorrect. | ||
Switch | ||
( | ||
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) | ||
const dictionary& dict, //!< dictionary | ||
const Switch defaultValue //!< fallback if not found | ||
); | ||
//- Construct from Istream | ||
explicit Switch(Istream& is); | ||
... | ... | @@ -159,7 +180,7 @@ public: |
// Helpers | ||
//- Construct from dictionary, supplying default value so that if the | ||
// value is not found, it is added into the dictionary. | ||
//- value is not found, it is added into the dictionary. | ||
static Switch lookupOrAddToDict | ||
( | ||
const word& name, | ||
... | ... | @@ -170,9 +191,18 @@ public: |
// Member Functions | ||
//- Return true if the Switch has a valid value | ||
//- True if the Switch has a valid value | ||
bool valid() const noexcept; | ||
//- The underlying enumeration value | ||
switchType type() const noexcept; | ||
//- Underlying enumeration is DEFAULT_ON or DEFAULT_OFF | ||
|
||
bool isDefault() const noexcept; | ||
//- Underlying enumeration is not DEFAULT_ON or DEFAULT_OFF | ||
bool nonDefault() const noexcept; | ||
//- A string representation of the Switch value | ||
const char* c_str() const noexcept; | ||
... | ... |