I'm trying to make an event system The user of this system have to execute this to add an event for input for e.g:
addEventHandler(Event::KEYBOARD_INPUT, reinterpret_cast<void*>(&std::bind(&Game::On_Input, this)));
the function passed gonna be called when the event is fired, these functions are stored in std::vectors till now there is no problem.But there is different function signatures for different events.. and since std::function is a type itself I can't let this function accept all events I have tried to cast the std::bind to void* then cast it back to std::function but it wont work since impl is undefined (0xCCCCCCCC)
The current code:
void EventManager::addEventHandler(Event event, void* p_Fun)
{
if (event == Event::KEYBOARD_INPUT) {
m_InputCallbacks.push_back(*reinterpret_cast<std::function<void(Keycode, unsigned int)>*>(p_Fun));
}
/*else if(...){ // other events other push backs in other vectors
}*/
}
and the function stored called this way :
void EventManager::HandleEvents(SDL_Event& Event)
{
while (PollEvent(&Event) != 0) {
if (Event.type == KEYDOWN || Event.type == KEYUP) {
//On_Input(Event.key.keysym.sym, Event.type);
for (auto inputCallbackFunc : m_InputCallbacks) {
inputCallbackFunc(Event.key.keysym.sym, Event.type);//CRASH HERE!
}
}
}
}
Please note that this version only handles input I don't know how many events gonna be added in the future (Collision, multiplayer connections, etc..) that's why i'd like to automate the proccess throught addEventHandler function which will check the following: 1) whats the event type and cast the void function pointer to that event call back function signature 2) store them in the appropriate std::vector
So all in all how to make AddEventHandler accept every std::bind being passed to it that can be stored and called later?