For the past few days I've been trying to come up with a robust event handling system for the game (using a component based entity system, C++, OpenGL) I've been toying with.
class EventDispatcher {
typedef void (*CallbackFunction)(Event* event);
typedef std::unordered_map<TypeInfo, std::list<CallbackFunction>, hash_TypeInfo > TypeCallbacksMap;
EventQueue* global_queue_;
TypeCallbacksMap callbacks_;
...
}
global_queue_ is a pointer to a wrapper EventQueue of std::queue<Event*> where Event is a pure virtual class. For every type of event I want to handle, I create a new derived class of Event, e.g. SetPositionEvent.
TypeInfo is a wrapper on type_info.
When I initialize my data, I bind functions to events in an unordered_map using TypeInfo(typeid(Event)) as the key that corresponds to a std::list of function pointers. When an event is dispatched, I iterate over the list calling the functions on that event. Those functions then static_cast the event pointer to the actual event type, so the event dispatcher needs to know very little.
The actual functions that are being bound are functions for my component managers. For instance, SetPositionEvent would be handled by
void PositionManager::HandleSetPositionEvent(Event* event) {
SetPositionEvent* s_p_event = static_cast<SetPositionEvent*>(event);
...
}
The problem I'm running into is that to store a pointer to this function, it has to be static (or so everything leads me to believe.) In a perfect world, I want to store pointers member functions of a component manager that is defined in a script or whatever. It looks like I can store the instance of the component manager as well, but the typedef for this function is no longer simple and I can't find an example of how to do it.
Is there a way to store a pointer to a member function of a class (along with a class instance, or, I guess a pointer to a class instance)?
Is there an easier way to address this problem?