Added subscription data to event bus.
This commit is contained in:
parent
5d902de564
commit
67c5c18fa2
|
|
@ -9,6 +9,7 @@
|
|||
int create_subscription(subscription_t *subscription, va_list args)
|
||||
{
|
||||
subscription->callback = va_arg(args, callback_t);
|
||||
subscription->data = va_arg(args, void *);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -45,9 +46,9 @@ int event_bus_exit(event_bus_t *event_bus)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_new_topic(event_bus_t *event_bus, size_t id, size_t max_history_size)
|
||||
int event_bus_new_topic(event_bus_t *event_bus, size_t id)
|
||||
{
|
||||
elem_t *topic_elem = create_elem(&event_bus->topics, id, max_history_size);
|
||||
elem_t *topic_elem = create_elem(&event_bus->topics, id);
|
||||
if(!topic_elem) return EXIT_FAILURE;
|
||||
|
||||
if(linked_list_push_back(&event_bus->topics, topic_elem)) return EXIT_FAILURE;
|
||||
|
|
@ -91,7 +92,7 @@ static inline int action_execute_callback(elem_t *elem, va_list args)
|
|||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(subscription->callback(args_copy)) return EXIT_FAILURE;
|
||||
if(subscription->callback(args_copy, subscription->data)) return EXIT_FAILURE;
|
||||
|
||||
va_end(args_copy);
|
||||
|
||||
|
|
@ -121,7 +122,7 @@ int event_bus_publish(event_bus_t *event_bus, size_t id, ...)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback)
|
||||
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback, void *data)
|
||||
{
|
||||
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
|
||||
if(!topic_elem)
|
||||
|
|
@ -132,7 +133,7 @@ int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback)
|
|||
|
||||
topic_t *topic = topic_elem->data;
|
||||
|
||||
elem_t *subscription_elem = create_elem(&topic->subscriptions, callback);
|
||||
elem_t *subscription_elem = create_elem(&topic->subscriptions, callback, data);
|
||||
if(!subscription_elem) return EXIT_FAILURE;
|
||||
|
||||
if(linked_list_push_back(&topic->subscriptions, subscription_elem)) return EXIT_FAILURE;
|
||||
|
|
|
|||
|
|
@ -5,10 +5,11 @@
|
|||
#include "ecs.h"
|
||||
#include "linked_list.h"
|
||||
|
||||
typedef int (*callback_t)(va_list args);
|
||||
typedef int (*callback_t)(va_list event_args, void *subscribtion_args);
|
||||
|
||||
typedef struct subscription_t {
|
||||
callback_t callback;
|
||||
void *data;
|
||||
} subscription_t;
|
||||
|
||||
int create_subscription(subscription_t *subscription, va_list args);
|
||||
|
|
@ -28,11 +29,11 @@ typedef struct event_bus_t {
|
|||
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_new_topic(event_bus_t *event_bus, size_t id);
|
||||
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_subscribe(event_bus_t *event_bus, size_t id, callback_t callback);
|
||||
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback, void *data);
|
||||
|
||||
#endif // EVENT_BUS_H
|
||||
Loading…
Reference in New Issue