4
4Space
I've hit something of a snag.
Problem:
In a DSP application we have data profiles with a varying number of points.
We have a number of Discrete Fourier transforms that are optimised for a
specific number of points. Currently, any client of these DFT classes must
switch on the number of points in the profile and instanciate the correct
DFT algorithm.
switch( size )
case 2000:
return new DFT2000Point();
case 3600:
return new DFT3600Point();
// etc...
Clearly this is horse poo.
I've just created a factory class that takes a profile, and returns a base
class pointer to the correct concrete type of DFT. It does this currently by
again, switching on the number of points in the profile. Still not good, but
at least the clients are not having to replicate this code. The thing that
bothers me is that this method is not closed to change. Every time we add a
new DFT class, we have to modify this switch. Clearly I'd like to avoid
this.
So, how do I do it?
My first feeling is that their is an obvious mapping between the concrete
class, and the number of points. So I should perhaps have a map:
class DFTFactory
{
public:
Map<int size, const type_info &theType>;
// .....
With a create method:
auto_ptr<DFTBase> Create( int numberOfPoints )
{
//....
So my question is... how do I create an instance of a concrete class from
its type_info?
Cheers,
4Space
Problem:
In a DSP application we have data profiles with a varying number of points.
We have a number of Discrete Fourier transforms that are optimised for a
specific number of points. Currently, any client of these DFT classes must
switch on the number of points in the profile and instanciate the correct
DFT algorithm.
switch( size )
case 2000:
return new DFT2000Point();
case 3600:
return new DFT3600Point();
// etc...
Clearly this is horse poo.
I've just created a factory class that takes a profile, and returns a base
class pointer to the correct concrete type of DFT. It does this currently by
again, switching on the number of points in the profile. Still not good, but
at least the clients are not having to replicate this code. The thing that
bothers me is that this method is not closed to change. Every time we add a
new DFT class, we have to modify this switch. Clearly I'd like to avoid
this.
So, how do I do it?
My first feeling is that their is an obvious mapping between the concrete
class, and the number of points. So I should perhaps have a map:
class DFTFactory
{
public:
Map<int size, const type_info &theType>;
// .....
With a create method:
auto_ptr<DFTBase> Create( int numberOfPoints )
{
//....
So my question is... how do I create an instance of a concrete class from
its type_info?
Cheers,
4Space