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