失败的证明转换字符串到枚举类
fail proof conversion of string to enum class
我有这样的枚举类(我打算稍后再添加更多选项):
enum class ViSequencePointType {
JumpToValue = 0,
RampToValue = 1
};
然后,我有一个配置文本文件,每行应该代表一个枚举值。这样的东西:
1
0
255
A
WTF
我需要解析此文件并创建一个枚举类的向量...所以我做类似的事情:
bool conversionResult = false;
int colThree = line.toInt(&conversionResult);
if(!conversionResult) {
//failed to convert to integer
} else {
ViSequencePointType pt = static_cast<ViSequencePointType>(colThree);
switch(pt) {
case ViSequencePointType::JumpToValue:
break;
case ViSequencePointType::RampToValue:
break;
default:
break;
}
对于该default
案例,编译器说
开关中的默认标签,该标签涵盖了所有枚举值
我相信这意味着如果存在文本文件中的任何无效条目,我找不到它!
那么,如何在运行时?
为了涵盖无效/毫无意义的枚举值,一种常见的做法是
- 依靠随后的枚举值被隐式分配了先前的枚举值 1 的值
- 在枚举中以最低值(隐式为0)添加
"Invalid"
枚举值,或者您可以为其分配一个低值,例如-1
) - 在枚举中添加最高值的
"Max"
枚举值
这是一个示例:
enum class ViSequencePointType
{
Invalid = -1,
JumpToValue, // is implicitly assigned enum value 0 (-1 + 1 == 0)
RampToValue, // is implicitly 1 (JumpToValue + 1)
CrawlToValue, // etc...
HopToValue,
// add new values here
Max // Max must be the last value in the enum
};
现在,当您解析输入值时,您可以检查积分值大于Invalid
,小于Max
,如果是,您知道这是有效的枚举值
ViSequencePointType parse(const std::string& value)
{
bool converted = false;
int val = line.toInt(&converted);
if(!converted)
{
// do additional conversion failure handling here if necessary
return ViSequencePointType::Invalid;
}
if (val <= static_cast<int>(ViSequencePointType::Invalid) ||
val >= static_cast<int>(ViSequencePointType::Max)
{
// do additional out of bounds handling here if necessary
return ViSequencePointType::Invalid;
}
return static_cast<ViSequencePointType>(val);
}
现在,您知道parse
的输出是一个有效的枚举值,未知/无效值的输出用枚举值Invalid
。
相关文章:
- 重载 std::字符串运算符+ 用于打印枚举名称
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 重载输入 (>>) 运算符以获取枚举字符串
- C++如何将字符串数组中的一个点设置为枚举变量
- 将整数值转换为以枚举为基础的字符串
- 将 Qt 属性枚举值打印为键字符串
- 如何在没有映射的情况下在枚举和字符串之间进行转换?
- 是否有任何操纵器将枚举值打印为字符串?
- C 中的枚举类型字符串类别
- 一种创建将字符串常量返回给枚举的类的廉价方法,反之亦然
- 失败的证明转换字符串到枚举类
- 使用 C++11 可变参数模板初始化枚举到字符串映射
- C++将多个字符串更改为枚举
- 从字符串转换时,如何组合多个枚举
- 翻译枚举值qt的字符串表示
- 如何重载<<以输出枚举的字符串值
- 在 QML 中使用 c++ 枚举作为字符串
- 使用地图将字符串与枚举连接
- 具有字符串的类中的枚举
- 将枚举字符串(不是值)传递给宏