Compare commits
No commits in common. "5d902de564b085b322ec68a8396711f430dc0bce" and "2e180b6832978ebd7c211a863a9c2007f3a7c591" have entirely different histories.
5d902de564
...
2e180b6832
|
|
@ -5,6 +5,14 @@
|
|||
#include "memory_alloc.h"
|
||||
#include "errors.h"
|
||||
|
||||
// Arg : void *payload
|
||||
int create_event(event_t *event, va_list args)
|
||||
{
|
||||
event->payload = va_arg(args, void *);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Arg : callback_t callback
|
||||
int create_subscription(subscription_t *subscription, va_list args)
|
||||
{
|
||||
|
|
@ -13,18 +21,23 @@ int create_subscription(subscription_t *subscription, va_list args)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Arg : size_t id
|
||||
// Args : size_t id, size_t max_history_size
|
||||
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, 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;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int destroy_topic(topic_t *topic)
|
||||
{
|
||||
if(linked_list_exit(&topic->events)) return EXIT_FAILURE;
|
||||
if(linked_list_exit(&topic->subscriptions)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
@ -83,22 +96,17 @@ int event_bus_change_topic_id(event_bus_t *event_bus, size_t old_id, size_t new_
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Args : va_list_args
|
||||
// Args : void *payload
|
||||
static inline int action_execute_callback(elem_t *elem, va_list args)
|
||||
{
|
||||
subscription_t *subscription = elem->data;
|
||||
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(subscription->callback(args_copy)) return EXIT_FAILURE;
|
||||
|
||||
va_end(args_copy);
|
||||
if(subscription->callback(va_arg(args, void *))) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_publish(event_bus_t *event_bus, size_t id, ...)
|
||||
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)
|
||||
|
|
@ -109,14 +117,22 @@ int event_bus_publish(event_bus_t *event_bus, size_t id, ...)
|
|||
|
||||
topic_t *topic = topic_elem->data;
|
||||
|
||||
va_list args, args_copy;
|
||||
va_start(args, id);
|
||||
va_copy(args_copy, args);
|
||||
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;
|
||||
|
||||
if(linked_list_for_eachv(&topic->subscriptions, action_execute_callback, args_copy)) return EXIT_FAILURE;
|
||||
destroy_elem(&topic->events, removed_event_elem);
|
||||
}
|
||||
|
||||
va_end(args_copy);
|
||||
va_end(args);
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,15 @@
|
|||
#define EVENT_BUS_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include "ecs.h"
|
||||
#include "linked_list.h"
|
||||
|
||||
typedef int (*callback_t)(va_list args);
|
||||
typedef int (*callback_t)(void *payload);
|
||||
|
||||
typedef struct event_t {
|
||||
void *payload;
|
||||
} event_t;
|
||||
|
||||
int create_event(event_t *event, va_list args);
|
||||
|
||||
typedef struct subscription_t {
|
||||
callback_t callback;
|
||||
|
|
@ -15,7 +20,9 @@ int create_subscription(subscription_t *subscription, va_list args);
|
|||
|
||||
typedef struct topic_t {
|
||||
size_t id;
|
||||
linked_list_t events;
|
||||
linked_list_t subscriptions;
|
||||
size_t max_history_size;
|
||||
} topic_t;
|
||||
|
||||
int create_topic(topic_t *topic, va_list args);
|
||||
|
|
@ -32,7 +39,7 @@ int event_bus_new_topic(event_bus_t *event_bus, size_t id, size_t max_history_si
|
|||
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, ...);
|
||||
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);
|
||||
|
||||
#endif // EVENT_BUS_H
|
||||
|
|
@ -274,16 +274,4 @@ int linked_list_remove_if(linked_list_t *linked_list, const condition_t conditio
|
|||
*/
|
||||
int linked_list_for_each(const linked_list_t *linked_list, const action_t action, ...);
|
||||
|
||||
/**
|
||||
* @brief Apply an action to every elements in the list.
|
||||
* The action can be anything that doesn't destroy the element.
|
||||
*
|
||||
* @param linked_list Pointer to an linked list
|
||||
* @param action Action to apply
|
||||
* @param args Argument to pass to the action in the form of a va_list
|
||||
*
|
||||
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
|
||||
*/
|
||||
int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args);
|
||||
|
||||
#endif // LINKED_LIST_H
|
||||
|
|
|
|||
|
|
@ -478,31 +478,3 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action
|
|||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
assert("Action cannot be NULL" && action);
|
||||
|
||||
elem_t *current_elem = linked_list->first;
|
||||
|
||||
while(current_elem)
|
||||
{
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(action(current_elem, args_copy))
|
||||
{
|
||||
va_end(args_copy);
|
||||
va_end(args);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
va_end(args_copy);
|
||||
|
||||
current_elem = current_elem->next;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue