Connecting Tech Pros Worldwide Help | Site Map

templated function pointers?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:06 AM
Ryan
Guest
 
Posts: n/a
Default templated function pointers?

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, 06:06 AM
John Harrison
Guest
 
Posts: n/a
Default 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, 06:06 AM
AirPete
Guest
 
Posts: n/a
Default 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, 06:06 AM
tom_usenet
Guest
 
Posts: n/a
Default 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, 06:06 AM
Wouter Lievens
Guest
 
Posts: n/a
Default 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.



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.