A general mechanism for checking if an enum value is in range. More...
Macros | |
#define | SPARSE_ENUM_VALIDATOR(ENUM, ...) |
Generate a sp::validateEnumValue() specialisation for sparse enum type ENUM . More... | |
#define | DENSE_ENUM_VALIDATOR(ENUM, FIRST, LAST) |
Generate a sp::validateEnumValue() specialisation for the dense enum, ENUM More... | |
Functions | |
template<typename EnumType > | |
void | sp::validateEnumValue (std::underlying_type_t< EnumType >) |
Throw sp::EnumValueOutOfRange if an enum value is out of range. More... | |
A general mechanism for checking if an enum value is in range.
Mostly useful for API input validation.
#define DENSE_ENUM_VALIDATOR | ( | ENUM, | |
FIRST, | |||
LAST | |||
) |
Generate a sp::validateEnumValue()
specialisation for the dense enum, ENUM
#define SPARSE_ENUM_VALIDATOR | ( | ENUM, | |
... | |||
) |
Generate a sp::validateEnumValue()
specialisation for sparse enum type ENUM
.
This occupies more binary size and is slower than sp::DENSE_ENUM_VALIDATOR
, but is necessary for enums that have gaps between their values.
These specialisations (along with the other validation routines) are made inline because they are very short, and we don't want a huge pile of call overhead at the entry to every API function. We want it to compile to a couple of comparison instructions for each argument, not a huge song and dance with 20-ish calls.
void sp::validateEnumValue | ( | std::underlying_type_t< EnumType > | ) |
Throw sp::EnumValueOutOfRange
if an enum value is out of range.
This function is useful for validating enum values being passed in from public APIs. To use it:
SPARSE_ENUM_VALIDATOR
or DENSE_ENUM_VALIDATOR
to generate a validator for your enum typevalidateEnumValue<YourEnumType>(userProvidedInputValue);
.EnumType | The enum type to validate against |
<tt>sp::EnumValueOutOfRange</tt> | if the value is not one declared in EnumType . |