Fix xController macro

This commit is contained in:
Adrien Marquès 2019-11-06 22:48:47 +01:00
parent 57025d9ef3
commit 964d982b31
2 changed files with 8 additions and 7 deletions

View File

@ -12,7 +12,7 @@ xController::~xController(){
void xController::handleEvent(SDL_Event* event){ void xController::handleEvent(SDL_Event* event){
_mutex.lock(); _mutex.lock();
map<SDL_EventType, EventListener>::iterator found = _handlers.find( (SDL_EventType) event->type ); map<SDL_EventType, EventHandler>::iterator found = _handlers.find( (SDL_EventType) event->type );
// ignore no handler found // ignore no handler found
if( found == _handlers.end() ){ if( found == _handlers.end() ){
@ -26,14 +26,14 @@ void xController::handleEvent(SDL_Event* event){
} }
// bind a new handler // bind a new handler
void xController::attachEvent(SDL_EventType t, void(*handler)(SDL_Event*) ){ void xController::attachEvent(SDL_EventType t, EventHandlerArg){
_mutex.lock(); _mutex.lock();
_handlers[ t ] = handler; _handlers[ t ] = handler;
_mutex.unlock(); _mutex.unlock();
} }
// removes an existing handler // removes an existing handler
void xController::detachEvent(SDL_EventType t, void(*handler)(SDL_Event*) ){ void xController::detachEvent(SDL_EventType t){
_mutex.lock(); _mutex.lock();
_handlers.erase(t); _handlers.erase(t);
_mutex.unlock(); _mutex.unlock();

View File

@ -6,7 +6,8 @@
#include <map> #include <map>
using namespace std; using namespace std;
#define EventListener void(*)(SDL_Event*) #define EventHandlerArg void(* handler)(SDL_Event*)
#define EventHandler void(*)(SDL_Event*)
class xController{ class xController{
@ -18,12 +19,12 @@
void handleEvent(SDL_Event* event); void handleEvent(SDL_Event* event);
// manage handlers // manage handlers
void attachEvent(SDL_EventType t, void(*handler)(SDL_Event*) ); void attachEvent(SDL_EventType t, EventHandlerArg);
void detachEvent(SDL_EventType t, void(*handler)(SDL_Event*) ); void detachEvent(SDL_EventType t);
private: private:
// event handlers // event handlers
map<SDL_EventType, EventListener> _handlers; map<SDL_EventType, EventHandler> _handlers;
// thread safety // thread safety
mutex _mutex; mutex _mutex;