文字列ラベルと値を持つ列挙型を作成しようとしていますが、これを使用してiniファイルからデータを読み取る予定です。
たとえば、iniファイルでは、値のタグ/名前の前にいくつかの値またはタイプ値がある場合がありdouble
ますint
。string
SomeFloat = 0.5
SomeInteger = 5
FileName = ../Data/xor.csv
ファイルからタグを読み取ると、それはとして入ってくる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)を受け入れることができるように、セットを宣言する方法を誰かに教えてもらえますか?
あなたがコードを持っているなら、それは素晴らしいでしょう!:)