我正在尝试创建一个具有字符串标签和值的 Enum,并且我计划使用它从 ini 文件中读取内容。
例如,在 ini 文件中,我可能有一些double
,int
或string
type 值,前面有值的标记/名称:
SomeFloat = 0.5
SomeInteger = 5
FileName = ../Data/xor.csv
当我从文件中读取标签时,它以 a 的形式出现string
,所以我只想让它std::set
保留我的所有值......当我读取标签时,我可以将它EnumType
与将检查类型并进行正确的转换(atoi 或仅使用字符串等)
例如:
EnumType<int> someInteger;
someInteger.label = "SomeInteger";
someInteger.type = INT;
std::set<EnumType> myValues;
//
// populate the set
myValues.insert(someInteger);
//
void ProcessTagAndValue(const std::string &tag, const std::string &value)
{
switch(myValues[tag].type)
{
case INT:
myValues[tag].value = atoi(value);
break;
case DOUBLE:
//
break;
case STRING:
myValues[tag].value = value;
break;
default:
break;
}
}
enum ValueType{INT,DOUBLE,STRING];
template <class T>
struct EnumType{
std::string label;
ValueType type;
T value;
bool operator==(const EnumType &other) const {
return this->label == other.label;
}
bool operator==(const T& other ) const
{
return this->value == other;
}
T& operator=(const T& p)
{
value = p;
return value;
}
EnumType& operator=(const EnumType& p)
{
if (this != &p) { // make sure not same object
this->label = p.label;
this->value = p.value;
}
return *this;
}
};
我有几个问题:
你们能告诉我更好的解决方案吗?我不确定我是否为了我自己的利益而过于聪明,或者这是否真的是一个可行的解决方案。
如果我的解决方案是可以接受的,那么任何人都可以告诉我如何声明一组,
std::set<EnumType<...>>
以便它可以接受任何类型(int、double、string),而我实际上并不知道枚举将使用哪种类型作为值?
如果您有任何代码,那就太好了!:)