I have some types defined by the values of an enumerator, which represent the type of data read in from a file. I wish to do a different processing workflow based on the type of data , but it results in a lot of code duplication:
#include <iostream>
enum dataType {
type1,
type2,
type3
};
struct File {
File(dataType type):type{type}{};
dataType type;
};
void process_file(File file)
{
if(file.type == dataType::type1){ std::cout << "Do work A" << std::endl; };
if(file.type == dataType::type2){ std::cout << "Do work B" << std::endl; };
if(file.type == dataType::type3){ std::cout << "Do work C" << std::endl; };
}
int main(){
File file(dataType::type2);
process_file(file);
return 0;
}
My main problem is with having to check the value via "if" or a switch statement. Imagine there being 50 types instead of just 3 and it becomes quite a chore and error prone to check every single one.
Does anyone know of a way to deal with this? Template code is the obvious thing to try but I'm stuck using the enumerator to determine the type, so I didn't think template code was possible here, at least the attempts I've made have not been successful.