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: OP 2011-03-21 11:38

Well, since the thread is still open...

>>10
This wasn't the whole function: I'm checking if the key exists before attempting to call the corresponding function. As I said, I was basically trying to call map[(char)1234] instead of map[(char)43] (just an example) when I had registered the function to map[(char)43]. And functional is a functor already, which is probably better to use here than a function pointer.

------------------------------
a new problem has arisen!

While trying to clean-up my code, I have decided to create function pointers to the members of the engine's components. So (and I still don't know how to quote here, but:)

[quote]void addFunc(EventType e, std::tr1::function<void(void)> func);[/quote]
is moved back to my event handler, for example.

I have, in my engine (the interface):
[quote]const std::tr1::function<void(EventType, EventFunc)> addFunc;[/quote]

initialized with

[quote]addFunc(boost::bind<void>(&EventHandler::addFunc, eventHandlerInstancePtr, _1, _2))[/quote]

in the engine's initialization list.

I call it in my main with:

[quote]engine.addFunc(KEY_W, boost::bind<void>(&Player::move, playerInstancePtr, UP));[/quote]

but that gives me an access violation error reading 0xffffffffffffffff.

Name: OP 2011-03-21 13:26

>>15
It crashes at the line directly following engine.addFunc(KEY_W, boost::bind<void>(&Player::move, playerInstancePtr, UP)); in my main.cpp; adding or commenting function calls confirm that. That's all my debugger is willing to tell me (the engine is a DLL).

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