Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

tr1 functional and boost bind and stuff

Name: OP 2011-03-20 19:02

I'm trying to make a 2d game engine in c++. It's nearly done, only I have one problem I can't seem to find a solution to:

I have an engine class that acts as an interface for the engine proper (that is, user->engine-renderer/inputHandler/helper). Now, I want to let the user assign a function to be executed when the specified event happens:

>void addFunc(EventType e, std::tr1::function<void(void)> func);

I am using

>std::tr1::unordered_map<EventType, std::tr1::function<void(void)>> map;

to keep track of those. During my main loop, I check if a key was pressed, and if so, I call:

>map[*unicode value of pressed key*]();

Now: I need to bind member functions, and to be sure nothing weird happens (the more standard way doesn't work anyway), I use the following as a function:

>boost::bind(&class::func_to_bind, &instance_of_class)

where func_to_bind is:

>void func_to_bind(void);

so let's say the first argument of addFunc was '1' (since the second argument is the boost::bind-generated function), whenever '1' is pressed, func_to_bind has to trigger.

Instead I get std::tr1::bad_function_call thrown at me by std::tr1::function<void(void)> when it is called.

QUESTION 1:
Is there an easier way to achieve my goal of letting the engine's user bind a function to a key?

QUESTION 2:
What might cause the problem I am encountering?

Name: Anonymous 2011-03-20 23:22

>>1
1)  I'd like to say "function pointers" but then I'd have to say "oh wait ..."  In a program I did recently, where the user had an excessive degree of configuration control, I used a method similar to what you did.  One thing I had to do, however, was implement an intermediate system to keep track of keys held down so that subsequent key presses did not interrupt the activity of the currently depressed keys.  You may have done something similar, or not had the complication?

2) Quoted from http://www.boost.org/doc/libs/1_38_0/doc/html/boost/bad_function_call.html:
An exception type thrown when an instance of a function object is empty when invoked.
I have no clue about Boost so I'm going to go with the function object used in the bind call being (re)moved or destroyed after being constructed on the heap, for some reason, or not throwing an error when it is not created.  What do you expect to happen, by the way, when map[*unicode value of pressed key*](); is called on unbound input?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List