Skip to main content
GameDev.net gamedev.net
🔒 Locked

Map of a function Pointer with arbitrary member functions

Started by JonConley Nov 5, 2011 at 7:30 PM 3 replies 1.1k views
Original Post
JonConley
JonConley
I am trying to design an input handler for SDL, the biggest problem with just checking for a keydown event is that once I do something else while still holding down the key it will no longer register as a keydown.
So I am trying to make a generic handler that I can register a function to a key using a map of SDLKey and a function pointer. Now this becomes the issue. I am trying to use boost and the functions that I will be binding will be member functions (such as for moving a camera or even rotating it).

Now I am not sure how to do this since to create the map I believe I need to know the arguments for the function pointer.
map<SDLKey, void(*)(<pointer to class that I am registering>, otherArgument(probably eventually boost::any) >

I want to be able to have a function in the input handler that just takes a pointer to whatever object that will have its member function called and then the function that will be called.


class foo
{
public:
void MoveForward(float mag){...}
};

foo a;
keyHandler.bind(SDL_w, &foo::MoveForward, &a, <pointer to what data is sent>);



Now I was wondering if anyone could help me figure out how to do this, I have used boost before but only for threads and random numbers (generating fractals).
SiCrane
SiCrane
Since you're already using boost, take a look at boost::function and boost::bind. In particular you should be able to use boost::bind to create member functions pre-bound to whatever object you want to hook to your keyboard handler.
JonConley
JonConley

Since you're already using boost, take a look at boost::function and boost::bind. In particular you should be able to use boost::bind to create member functions pre-bound to whatever object you want to hook to your keyboard handler.



Thank you that led me to a mini tutorial on the boost::function documentation for member functions and actually seems to be able to do exactly what I need to do.
It seems to be a mix of boost and std::mem_fun.


boost::function<int (int)> f;
X x;
f = std::bind1st(
std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
SiCrane
SiCrane
One thing to note is that std::bind1st doesn't play happily with some member functions like those that have reference parameters. You may want to consider using boost::bind instead.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.