views:

92

answers:

3

Hi, i have a templated class, with the following definition:

ImageRescaleDepth<PIXEL_TYPE_INPUT, PIXEL_TYPE_OUTPUT>

This class uses templates, for pretty much everything since its supposed to be generic. Anyways i need to make a command line version of this application, to do image rescaling, currently the system is setup to handle the following types:

1BIT, 2BIT, 4BIT, unsigned 8 bit, signed 8 bit, unsigned 16 bit, signed 16 bit, unsigned 32 bit, signed 32 bit, float, double.

These are passed in by command line, and i convert them to an enum.

I cannot modify the ImageRescaleDepth class since its part of a library. and i don't exactly want to create a giant switch or eliseif block, since there would be a 100 combinations. Is it possible, i can just somehow store these types as variables? Then pass them to the constructor?

+6  A: 

No, the type of a template class must be known at compile time, so the image types types have to be supplied to the template then. I have to say, that if this class is intended to perform conversions between many different formats, the use of template parameters to specify the conversion smacks of very poor design.

anon
I know.So making a switch case of 100 elements is the best solution?
UberJumper
I would use a map, keyed on a pair of from/to types (as enums or constants, not type name) to a pointer to a creation function of some sort. In other words, a factory.
anon
fa.
A map is not going to help as there is no common base class.
Martin York
I didn't say a map of base class, I said a map of creation functions.
anon
A: 

Why not to switch to OOP? You could use typeid() function to get the type name and dynamic casts then.

marpetr
Can you elaborate more?
UberJumper
I don't understand exactly what you need. Could you write how the giant switch would look like?
marpetr
I am using OOP the big switch would be for calling the corresponding constructor.
UberJumper
A: 

Maybe the use of typelists can help you. See for example boost::mpl, or simply boost/std::tuple. This is powerfull enough to generate the list of all pairs of types from your initial list. Your big switch can be handled automatically (i.e. generated) with recursive template metaprogramming (I don't know if mpl has facilities for this). However this is not trivial (I spent many hours), so if you only need to deal with this one case, it may be faster to generate the code with the help of a scripting language!

rafak