I want to implement a factory function to create objects. My object template is as follows:
template <typename TA, typename TB> struct MyImpl : public MyInterface {
and my factory is as follows:
MyInterface* factory(char ta, char tb) { if(ta == 'c' && tb == 'c') { return new MyImpl<char, char>(); } if(ta == 'c' && tb == 's') { return new MyImpl<char, short>(); } if(ta == 's' && tb == 'c') { return new MyImpl<short, char>(); }
The factory function should accept non-static char ( ta , tb ) data, since it cannot be determined at compile time, and I think this whole point is factory. In fact, ta and tb read from a file (or network).
I want a simpler solution to avoid the annoying two-tier switch.
I think my question is similar to how-would-one-write-a-meta-if-else-if-in-c , except that I cannot use static parameters.
Perhaps I just need to drop the C macros and use some macro tricks to compress my current code?
Thanks in advance!
UPDATE
Reply to @Rob:
My actual code would be more complex with many other things in it, and it would be harder to read and not be related in many aspects. I am trying to get the pseudocode correctly. If there is any problem, please let me know :-).
Reply to @Dynguss:
My problem is that, in my actual implementation, the factory (ta, tb) parameters would be large in the range, for example 10 X ta and 20 X tb, and the combination of ta and tb would be very long in rows, and hard to maintain. Therefore, I need at least some way to facilitate the combination.