Jump to content
  • Advertisement
Sign in to follow this  
JonConley

Map of a function Pointer with arbitrary member functions

This topic is 2394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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).

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!