Re: How to avoid complex switches?
On 28 Mrz., 19:32, none <""mort\"@(none)"> wrote:
I have a class that takes a few template parameters:
template<typename A, typename B, typename C,typename D>
class MyClass {
// ...
};
The types A,B,C and D are selected from a user specified input file (prop=
erties file):
A = 1
B = 2
C = 1
D = 3
I then parse this file an need to create MyClass with the correct types:
void createMyClass (int a, int b, int c, int d) {
switch ( a ) {
case 1 :
typedef test::Green ColorType;
switch ( b ) {
case 1 :
typedef test::Water MediumType;
switch ( c ) {
case 1 :
typedef test::Linear InterpolationType=
;
MyClass<ColorTyper, MediumType, Interp=
olationType > myClass;
break;
case 2 :
typedef test::Cubic InterpolationType;
MyClass<ColorTyper, MediumType, Interp=
olationType > myClass;
break;
default :
}
break;
case 2 :
// ....
break;
default :
}
break;
case 2 :
typedef test::Blue ColorType;
switch ( b ) {
case 1 :
typedef test::Water MediumType;
switch ( c ) {
case 1 :
typedef test::Linear InterpolationType=
;
MyClass<ColorTyper, MediumType, Interp=
olationType > myClass;
break;
case 2 :
typedef test::Cubic InterpolationType;
MyClass<ColorTyper, MediumType, Interp=
olationType > myClass;
break;
default :
}
break;
case 2 :
break;
default :
}
break;
default :
}
}
But this switch grows extremely large when the number of choices for each=
type grows and is also
very ugly/error prone.
[snip]
As far as I can see in the other postings of this thread, you can
hardly get around typing an expontentially growing amount of code as
your choices increase, even if you use those fancy creator patterns
and whatnot. Since using (run-time) polymorphism is out of the
question for you, you may think about writing a small script that
simply generates your switch statement (as yacc and flex do).
Regards,
Stuart