473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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!
Jul 22 '05 #1
4 1266

"Ryan" <pa******@techie.com> wrote in message
news:10*************@corp.supernews.com...
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
Jul 22 '05 #2
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" <pa******@techie.com> wrote in message
news:10*************@corp.supernews.com...
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!

Jul 22 '05 #3
On Tue, 3 Feb 2004 08:36:00 -0800, "Ryan" <pa******@techie.com> wrote:
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
Jul 22 '05 #4
"Ryan" <pa******@techie.com> schreef in bericht
news:10*************@corp.supernews.com...
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.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
6
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
2
by: Amadeus W. M. | last post by:
I have a bunch of templated functions: template <class Type_t> double f2(Type_t x) { return 2*x; } template <class Type_t> double f3(Type_t x) { return 3*x; }
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
2
by: mattjgalloway | last post by:
I'm having some problems with a templated member function of a templated class. Unfortunately I can't replicate it with a simple example so I know something odd must be going on!!! Basically it's...
4
by: Jim Langston | last post by:
This should illistrate what I am trying to do: template <class T> T SomeFunction( T parm ) { return parm; } template <class T> class SomeClass
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.