mardi 19 février 2019

fail proof conversion of string to enum class

I have an enum class like this (I am planning to add more options to it later):

enum class ViSequencePointType {
   JumpToValue = 0,
   RampToValue = 1
};

Then I have a configuration text file which each line supposed to represents one the enum values. Something like this:

1
0
255
A
WTF

I need to parse this file and create a vector of that enum class...so I do something like:

    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;
    }

for that default case the compiler says

Default label in switch which covers all enumeration values

which I believe it means if there is any invalid entry in the text file exists, I can not find it out!

So how can I approach this problem without letting any invalid enumeration slip through during runtime?

Aucun commentaire:

Enregistrer un commentaire