stefan.bruck...@gmail.com wrote:
On Feb 21, 8:50 am, John Harrison <john_androni...@hotmail.com> wrote:
Piyo wrote:
Darn, you beat me to it!! :) Interestingly enough, we came up
with the same solution. BTW, I forgot (so I went and included
it in my solution) do you need to do ->template in the call
to member function y()? (See below).
I can't remember the rules for ->template. I know it's purpose is to
disambiguate the syntax for the compiler, so I add it if the compiler is
complaining. If the compiler isn't complaining I figure there is nothing
to disambiguate.
john
Thanks for your replies. Maybe I should explain the scenario in a bit
more detail:
Class Base is actually something called a Resource, which can be an
Array, Image, Volume, etc.
Class A is one type of Resource, e.g a Volume. The voxels of the
volume can have different types (char,int,float,...) and number of
components (1,2,...).
Class B wants to perform some processing on a resource, for any type
and any number of components. The algorithm will, in general, be
independent of type and components (but there also might be the need
for partial specialization for some types/number of components).
All class B gets is a pointer to a resource and now it wants to call
some member of the resource class which determines the actual type of
resource and its voxel type and components and calls and appropriate
templated member which I want to specify. The actual implementation of
the algorithm is performance critical, so everything should be
statically typed after this point.
--Stefan
So here is my new suggestion. Based on this description, you seem to
be pushing the responsibility of knowing how to apply the algorithm
properly onto the algorithm. This is not the best way to do it. Since
your algorithm is independent of type and components, you can
encapsulate it as a templated functor. Then inside each concrete
Resource, you instantiate as many as Processors as you need for
each component you need to process and do the processing.
I sure hope this helps :)
-------------------------------------------------------------------
#include <boost/function.hpp>
template<typename T1>
class Proc
{
public:
T1 operator()( const T1 &val )
{
// process
}
};
class Resource
{
virtual void applyProc() = 0;
};
template<typename T1>
class Array
{
virtual void applyProc()
{
Proc<T1> processor;
for( unsigned int i=0; i < m_internal.size(); ++i )
{
m_internal[i] = processor( m_internal[i] );
}
}
private:
std::vector<T1> m_internal;
};
template<typename T1, typename T2>
class Image
{
virtual void applyProc()
{
Proc<T1> processor1;
Proc<T2> processor2;
for( unsigned int i=0; i < m_internal.size(); ++i )
{
m_first[i] = processor1( m_first[i] );
m_second[i] = processor2( m_second[i] );
}
}
private:
std::vector<T1> m_first;
std::vector<T2> m_second;
};
template<typename T1, typename T2, typename T3>
class Volume
{
// .. you get the picture
};- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Well ... hmmmm ... the problem is that different algorithms are
performed on resources from basically all over the place. A plugin to
the application might implement a specific algorithms, for example. In
Lets say, I now have Proc1, Proc2 and Proc3, all do different things.
At runtime, any one of them could be applied to a resource.