Connecting Tech Pros Worldwide Help | Site Map

templated function pointers?

  #1  
Old July 22nd, 2005, 07:06 AM
Ryan
Guest
 
Posts: n/a
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!


  #2  
Old July 22nd, 2005, 07:06 AM
John Harrison
Guest
 
Posts: n/a

re: templated function pointers?



"Ryan" <palefire@techie.com> wrote in message
news:101vjfvdvk5dj7c@corp.supernews.com...[color=blue]
> 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[/color]
think[color=blue]
> it does a pretty good job of showing the direction I want to go in...is
> there a way to do this?
>
>
>
> Thanks!
>[/color]

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


  #3  
Old July 22nd, 2005, 07:06 AM
AirPete
Guest
 
Posts: n/a

re: templated function pointers?


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


"Ryan" <palefire@techie.com> wrote in message
news:101vjfvdvk5dj7c@corp.supernews.com...[color=blue]
> 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[/color]
think[color=blue]
> it does a pretty good job of showing the direction I want to go in...is
> there a way to do this?
>
>
>
> Thanks!
>
>[/color]


  #4  
Old July 22nd, 2005, 07:06 AM
tom_usenet
Guest
 
Posts: n/a

re: templated function pointers?


On Tue, 3 Feb 2004 08:36:00 -0800, "Ryan" <palefire@techie.com> wrote:
[color=blue]
>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?[/color]

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
  #5  
Old July 22nd, 2005, 07:06 AM
Wouter Lievens
Guest
 
Posts: n/a

re: templated function pointers?


"Ryan" <palefire@techie.com> schreef in bericht
news:101vjfvdvk5dj7c@corp.supernews.com...[color=blue]
> 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[/color]
think[color=blue]
> it does a pretty good job of showing the direction I want to go in...is
> there a way to do this?[/color]

Put a typedef inside the class declaration.



Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing member function pointers to a function tbringley@gmail.com answers 18 February 26th, 2007 05:55 PM
Passing member function pointers are template parameters Dilip answers 3 November 6th, 2006 11:25 PM
templated function as parameter of another templated function Amadeus W. M. answers 2 July 4th, 2006 10:55 PM
templates and function pointers Andy White answers 2 October 20th, 2005 07:05 AM