Working event bus.

This commit is contained in:
Ulysse Cura 2026-08-10 12:38:15 +02:00
parent 571bb9699d
commit 2e180b6832
4 changed files with 62 additions and 21 deletions

View File

@ -3,8 +3,9 @@
#include <stdlib.h>
#include "linked_list.h"
#include "memory_alloc.h"
#include "errors.h"
// Args : void *payload
// Arg : void *payload
int create_event(event_t *event, va_list args)
{
event->payload = va_arg(args, void *);
@ -12,14 +13,7 @@ int create_event(event_t *event, va_list args)
return EXIT_SUCCESS;
}
int destroy_event(event_t *event)
{
free(event->payload);
return EXIT_SUCCESS;
}
// Args : callback_t callback
// Arg : callback_t callback
int create_subscription(subscription_t *subscription, va_list args)
{
subscription->callback = va_arg(args, callback_t);
@ -33,7 +27,7 @@ int create_topic(topic_t *topic, va_list args)
topic->id = va_arg(args, size_t);
topic->max_history_size = va_arg(args, size_t);
linked_list_init(&topic->events, sizeof(event_t), (allocator_t)create_event, (deleter_t)destroy_event);
linked_list_init(&topic->events, sizeof(event_t), (allocator_t)create_event, NULL);
linked_list_init(&topic->subscriptions, sizeof(subscription_t), (allocator_t)create_subscription, NULL);
if(linked_list_reserve(&topic->events, topic->max_history_size)) return EXIT_FAILURE;
@ -49,28 +43,32 @@ int destroy_topic(topic_t *topic)
return EXIT_SUCCESS;
}
int event_bus_init(event_bus_t *event_bus)
void event_bus_init(event_bus_t *event_bus)
{
linked_list_init(&event_bus->topics, sizeof(topic_t), (allocator_t)create_topic, (deleter_t)destroy_topic);
return EXIT_SUCCESS;
debug_printf("Initialized event bus.");
}
int event_bus_exit(event_bus_t *event_bus)
{
if(linked_list_exit(&event_bus->topics)) return EXIT_FAILURE;
debug_printf("Exited event bus.");
return EXIT_SUCCESS;
}
int event_bus_new_topic(event_bus_t *event_bus, size_t id, size_t max_history_size)
{
if(!create_elem(&event_bus->topics, id, max_history_size)) return EXIT_FAILURE;
elem_t *topic_elem = create_elem(&event_bus->topics, id, max_history_size);
if(!topic_elem) return EXIT_FAILURE;
if(linked_list_push_back(&event_bus->topics, topic_elem)) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
// Args : size_t id
// Arg : size_t id
static inline bool condition_is_topic_id(elem_t *elem, va_list args)
{
return ((topic_t *)elem->data)->id == va_arg(args, size_t);
@ -83,6 +81,21 @@ int event_bus_remove_topic(event_bus_t *event_bus, size_t id)
return EXIT_SUCCESS;
}
int event_bus_change_topic_id(event_bus_t *event_bus, size_t old_id, size_t new_id)
{
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, old_id);
if(!topic_elem)
{
error_printf("No topics with id = %lu found.", old_id);
return EXIT_FAILURE;
}
topic_t *topic = topic_elem->data;
topic->id = new_id;
return EXIT_SUCCESS;
}
// Args : void *payload
static inline int action_execute_callback(elem_t *elem, va_list args)
{
@ -96,23 +109,49 @@ static inline int action_execute_callback(elem_t *elem, va_list args)
int event_bus_publish(event_bus_t *event_bus, size_t id, void *payload)
{
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
if(!topic_elem) return EXIT_FAILURE;
if(!topic_elem)
{
error_printf("No topics with id = %lu found.", id);
return EXIT_FAILURE;
}
topic_t *topic = topic_elem->data;
if(!create_elem(&topic->events, payload)) return EXIT_FAILURE;
if(topic->events.size + 1 > topic->max_history_size)
{
elem_t *removed_event_elem = linked_list_pop_back(&topic->events);
if(!removed_event_elem) return EXIT_FAILURE;
destroy_elem(&topic->events, removed_event_elem);
}
elem_t *event_elem = create_elem(&topic->events, payload);
if(!event_elem) return EXIT_FAILURE;
if(linked_list_push_front(&topic->events, event_elem)) return EXIT_FAILURE;
if(linked_list_for_each(&topic->subscriptions, action_execute_callback, payload)) return EXIT_FAILURE;
debug_printf("Event published, id = %lu, payload = %d", id, *(bool*)payload);
return EXIT_SUCCESS;
}
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback)
{
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
if(!topic_elem) return EXIT_FAILURE;
if(!topic_elem)
{
error_printf("No topics with id = %lu found.", id);
return EXIT_FAILURE;
}
topic_t *topic = topic_elem->data;
if(!create_elem(&topic->subscriptions, callback)) return EXIT_FAILURE;
elem_t *subscription_elem = create_elem(&topic->subscriptions, callback);
if(!subscription_elem) return EXIT_FAILURE;
if(linked_list_push_back(&topic->subscriptions, subscription_elem)) return EXIT_FAILURE;
return EXIT_SUCCESS;
}

View File

@ -11,7 +11,6 @@ typedef struct event_t {
} event_t;
int create_event(event_t *event, va_list args);
int destroy_event(event_t *event);
typedef struct subscription_t {
callback_t callback;
@ -33,11 +32,12 @@ typedef struct event_bus_t {
linked_list_t topics;
} event_bus_t;
int event_bus_init(event_bus_t *event_bus);
void event_bus_init(event_bus_t *event_bus);
int event_bus_exit(event_bus_t *event_bus);
int event_bus_new_topic(event_bus_t *event_bus, size_t id, size_t max_history_size);
int event_bus_remove_topic(event_bus_t *event_bus, size_t id);
int event_bus_change_topic_id(event_bus_t *event_bus, size_t old_id, size_t new_id);
int event_bus_publish(event_bus_t *event_bus, size_t id, void *payload);
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback);

View File

@ -4,6 +4,7 @@
#include <stdbool.h>
#include "asset_manager.h"
#include "ecs.h"
#include "event_bus.h"
#include "inputs.h"
/**
@ -19,6 +20,7 @@
typedef struct game_t {
asset_manager_t asset_manager;
entity_manager_t entity_manager;
event_bus_t event_bus;
inputs_t events;
float delta_time_ms;

View File

@ -203,7 +203,7 @@ elem_t *linked_list_pop_back(linked_list_t *linked_list);
*
* @param linked_list Pointer to a linked list
*
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
* @return Pointer to element, NULL on error.
*/
elem_t *linked_list_pop_front(linked_list_t *linked_list);