Adds funny casting to avoid pedantic ISO C warning

This commit is contained in:
Linus Probert 2019-03-10 07:30:12 +01:00
parent fd3a625249
commit 3e43c30bcb
2 changed files with 21 additions and 19 deletions

View File

@ -22,25 +22,27 @@
static LinkedList *callbacks = NULL;
void
void
event_register_listener(EventCallback cb)
{
linkedlist_append(&callbacks, cb);
// Cast a pointer to a pointer to avoid -wpedantic iso warning
linkedlist_append(&callbacks, *(void**)(&cb));
}
void
void
event_clear_listeners(void)
{
while (callbacks)
linkedlist_pop(&callbacks);
while (callbacks)
linkedlist_pop(&callbacks);
}
void
void
event_trigger(Event *event)
{
LinkedList *cbs = callbacks;
while (cbs) {
((EventCallback) cbs->data)(event);
cbs = cbs->next;
}
}
LinkedList *cbs = callbacks;
while (cbs) {
// Reverse the cast from the register (-pedantic ISO warning)
(*(EventCallback*)(&cbs->data))(event);
cbs = cbs->next;
}
}

View File

@ -21,18 +21,18 @@
#include "player.h"
typedef enum EventType {
MONSTER_KILLED_EVENT
MONSTER_KILLED_EVENT
} EventType;
typedef struct MonsterKilledEvent {
EventType *type;
Player *player;
Monster *monster;
EventType *type;
Player *player;
Monster *monster;
} MonsterKilledEvent;
typedef union Event {
EventType type;
MonsterKilledEvent monsterKilled;
EventType type;
MonsterKilledEvent monsterKilled;
} Event;
typedef void (*EventCallback)(Event*);
@ -44,4 +44,4 @@ void
event_clear_listeners(void);
void
event_trigger(Event *event);
event_trigger(Event *event);