templated function pointers?

R

Ryan

I'm writing a templated class and would like to have a user definable
function to apply algorithmic modifiers. Something alone these lines:



typedef template <typename T> T (*ObjectModifier)(T);



template <typename T>

class Object

{

public:

SetModifier( ObjectModifier om );

ApplyModifier();



private:

T data;

};



The above doesn't actually work though, i.e. it won't compile. But I think
it does a pretty good job of showing the direction I want to go in...is
there a way to do this?



Thanks!
 
J

John Harrison

Ryan said:
I'm writing a templated class and would like to have a user definable
function to apply algorithmic modifiers. Something alone these lines:



typedef template <typename T> T (*ObjectModifier)(T);



template <typename T>

class Object

{

public:

SetModifier( ObjectModifier om );

ApplyModifier();



private:

T data;

};



The above doesn't actually work though, i.e. it won't compile. But I think
it does a pretty good job of showing the direction I want to go in...is
there a way to do this?



Thanks!

Seems easy enough, but maybe I'm missing the point.

template <typename T>
class Object
{
public:
SetModifier( T (*om)(T) );
ApplyModifier();

private:
T data;
};

john
 
A

AirPete

The PocketFrog library by Thierry Tremblay does a good job of this with its
PixelShader class (if I understand what you're trying to do).
You can get the source here: pocketfrog.droneship.com

Here's a few snippets:

struct PixelShader
{
// Override this method to do your custom raster operations.
// src is the source color color specified to the primitive)
// dest is the content of the graphic buffer (dest color)
virtual Pixel operator()( Pixel src, Pixel dest ) const = 0;
};

class Rasterizer : noncopyable
{
public:
/* ... */
void SetDefaultShader();
void SetPixelShader( PixelShader* shader );
/* ... */
};

struct OrPixelShader : PixelShader
{
virtual Pixel operator()( Pixel src, Pixel dest ) const
{
return src | dest;
}
};


It's used like this:

Rasterizer* raster = /* ... */;
OrPixelShader ops;
raster->SetPixelShader(&ops);
/* draw stuff */
raster->SetDefaultShader();

Internally, Rasterizer does something like this:
dest[x][y] = (*currps)(src[x][y], dest[x][y]);

You could also extend this to have a modifier stack with a std::deque,
similar to what gmax and 3d Studio Max do.

- Pete
 
T

tom_usenet

I'm writing a templated class and would like to have a user definable
function to apply algorithmic modifiers. Something alone these lines:

typedef template <typename T> T (*ObjectModifier)(T);

template <typename T>
class Object
{
public:
SetModifier( ObjectModifier om );
ApplyModifier();

private:
T data;
};

The above doesn't actually work though, i.e. it won't compile. But I think
it does a pretty good job of showing the direction I want to go in...is
there a way to do this?

If the modifier need only be a function then:

template <typename T>
class Object
{
public:
typedef T (*ObjectModifier)(T);
void SetModifier(ObjectModifier om)
{
modifier = om;
}
void ApplyModifier()
{
data = modifier(data);
}

private:
T data;
ObjectModifier modifier;
};

but if you want the modifier to be any functor, you need:

#include <boost/function.hpp>
using boost::function;

template <typename T>
class Object
{
public:
typedef function<T(T)> ObjectModifier;
void SetModifier(ObjectModifier om)
{
modifier = om;
}
void ApplyModifier()
{
data = modifier(data);
}

private:
T data;
ObjectModifier modifier;
};

See www.boost.org

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
W

Wouter Lievens

Ryan said:
I'm writing a templated class and would like to have a user definable
function to apply algorithmic modifiers. Something alone these lines:



typedef template <typename T> T (*ObjectModifier)(T);



template <typename T>

class Object

{

public:

SetModifier( ObjectModifier om );

ApplyModifier();



private:

T data;

};



The above doesn't actually work though, i.e. it won't compile. But I think
it does a pretty good job of showing the direction I want to go in...is
there a way to do this?

Put a typedef inside the class declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top