I have two unrelated types: Object and Unrelated implementing the same basic interface Interface (for storing in the same container).
I have an enum class that basically maps these types to enums
enum class TypeEnum {
TYPE_OBJECT,
TYPE_UNRELATED,
};
I have a reading method, that basically down-casts from the Interface to an implementation class
template<typename DATA>
const DATA& Read(const Container<Interface>& container, TypeEnum type);
Is it possible to automate the code, such that (this is a pseudo code)
switch(type_enum) {
case TYPE_OBJECT:
return Read<Object>(container, type_enum);
case TYPE_UNRELATED:
return Read<Unrelated>(container, type_enum);
}
can be made into a one liner?
NB. I have a bunch of enum values around 50 and a number of places i want to use this idiom.
In my want-to-have way, I would prefer something aka
template<TypeName>
struct TypePicker;
template<>
struct TypePicker<TYPE_OBJECT> {
typedef Object underlying_type;
};
template<>
struct TypePicker<TYPE_UNRELATED> {
typedef Unrelated underlying_type;
};
and then
return Read<TypePicker<type_enum>::underlying_type>(container, type_enum);
However, type_enum is only known at run-time.