Boost logo

Boost :

From: ali f (k9eks_at_[hidden])
Date: 2006-12-07 09:38:13


Hello,

A few days ago I developed a function argument caster because of a situation
I had. Imagine you had a callback function that takes 4 arguments. x,y
position of your mouse pointer, a bool indicating if a button is pressed,
and then an int indicating the button that is pressed.

Now imagine you already have a function that takes 2 ints representing the
mouse coordinates and then renders an image at those coordinates. You can't
assign this function to the callback because the sigs don't match. So you
have a couple of options.

a) wrap the function in a struct and overload operator ()
b) change the signature (but then change all places where you've invoked the
function already)

A produced unnecessary clutter and B can be error-prone depending on how big
the project is.

So I propose a function argument caster:

bool drawfunc(int, int);
callback = args_cast< 2, bool(int,int,bool,int) >(&drawfunc);
callback( x, y, true, 3 ); // calls drawfunc(x,y)

NOTE: the '2' as the first template parameter tells args_cast how many
arguments the original function takes.

The implementation I have also allows you to rewire the arguments from the
synthsized function to the actual arguments, eg:

callback = args_cast< 2, bool(int,int,bool,int), 1, 4 >(&drawfunc);
callback( x, y, true, 3 ); // calls drawfunc(x,3)

What do you guys think?

If anyone is interested I posted the code to a game development community a
while back. http://www.devmaster.net/forums/showthread.php?t=7744

- ali


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk