-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "event.h" | ||
|
||
#include "seika/memory.h" | ||
#include "seika/assert.h" | ||
#include "seika/data_structures/array_utils.h" | ||
|
||
//--- Observer ---// | ||
SkaObserver* ska_observer_new(SkaObserverOnNotify onNotifyFunc) { | ||
SKA_ASSERT(onNotifyFunc != NULL); | ||
SkaObserver* observer = SKA_MEM_ALLOCATE(SkaObserver); | ||
observer->on_notify = onNotifyFunc; | ||
return observer; | ||
} | ||
|
||
void ska_observer_delete(SkaObserver* observer) { | ||
SKA_MEM_FREE(observer); | ||
} | ||
|
||
//--- Event ---// | ||
SkaEvent* ska_event_new() { | ||
SkaEvent* event = SKA_MEM_ALLOCATE(SkaEvent); | ||
event->observerCount = 0; | ||
return event; | ||
} | ||
|
||
void ska_event_delete(SkaEvent* event) { | ||
SKA_MEM_FREE(event); | ||
} | ||
|
||
bool ska_event_register_observer(SkaEvent* event, SkaObserver* observer) { | ||
SKA_ASSERT(event != NULL); | ||
SKA_ASSERT(observer != NULL); | ||
SKA_ASSERT_FMT(event->observerCount + 1 < SKA_MAX_OBSERVERS, "Reached max observer count, consider increasing 'SKA_MAX_OBSERVERS'!"); | ||
event->observers[event->observerCount++] = observer; | ||
return true; | ||
} | ||
|
||
bool ska_event_unregister_observer(SkaEvent* event, SkaObserver* observer) { | ||
SKA_ARRAY_UTILS_REMOVE_ARRAY_ITEM(event->observers, event->observerCount, observer, NULL); | ||
return true; | ||
} | ||
|
||
void ska_event_notify_observers(SkaEvent* event, SkaSubjectNotifyPayload* payload) { | ||
for (size_t i = 0; i < event->observerCount; i++) { | ||
event->observers[i]->on_notify(payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
#define SKA_MAX_OBSERVERS 8 | ||
|
||
typedef struct SkaSubjectNotifyPayload { | ||
void* data; // Primary data, be sure to cast properly | ||
} SkaSubjectNotifyPayload; | ||
|
||
typedef void (*SkaObserverOnNotify)(SkaSubjectNotifyPayload*); | ||
|
||
// An observer that can subscribe to a subject | ||
typedef struct SkaObserver { | ||
SkaObserverOnNotify on_notify; | ||
} SkaObserver; | ||
|
||
SkaObserver* ska_observer_new(SkaObserverOnNotify onNotifyFunc); | ||
void ska_observer_delete(SkaObserver* observer); | ||
|
||
// A subscribable event | ||
typedef struct SkaEvent { | ||
size_t observerCount; | ||
SkaObserver* observers[SKA_MAX_OBSERVERS]; | ||
} SkaEvent; | ||
|
||
SkaEvent* ska_event_new(); | ||
void ska_event_delete(SkaEvent* event); | ||
bool ska_event_register_observer(SkaEvent* event, SkaObserver* observer); | ||
bool ska_event_unregister_observer(SkaEvent* event, SkaObserver* observer); | ||
void ska_event_notify_observers(SkaEvent* event, SkaSubjectNotifyPayload* payload); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters