-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_emitter.template.h
107 lines (96 loc) · 2.96 KB
/
event_emitter.template.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once
#include "event_id.h"
#include "<%= listener_decl %>.h"
#include "list.h"
<% if domain != "logger" -%>
#include "logger_event_emitter.h"
<% end -%>
<% actions.each do |a| -%>
typedef struct <%= a.elem_decl %> {
list_t list_entry;
void *context;
<%= a.callback_decl %> callback;
} <%= a.elem_decl %>_t;
<% end -%>
typedef struct <%= emitter_decl %> {
<% actions.each do |a| -%>
list_head_t <%= a.name %>_listeners;
<% end -%>
<% if domain != "logger" -%>
logger_event_emitter_t *shared_logger_emitter;
<% end -%>
} <%= emitter_decl %>_t;
static inline int <%= domain %>_event_attach_listener(<%= emitter_decl %>_t *self, <%= listener_decl %>_t *listener)
{
<% actions.each do |a| -%>
if (listener->on_<%= a.name %>) {
<%= a.elem_decl %>_t *elem = NULL;
TALLOC(elem);
elem->context = listener->context;
elem->callback = listener->on_<%= a.name %>;
list_append(&self-><%= a.name %>_listeners, &elem->list_entry);
}
<% end -%>
return SUCCESS;
error:
return NG;
}
<% if domain != "logger" -%>
static inline void <%= domain %>_event_set_logger_emitter(<%= emitter_decl %>_t *self, logger_event_emitter_t *logger_emitter)
{
self->shared_logger_emitter = logger_emitter;
}
<% end -%>
<% actions.each do |a| -%>
// <%= a.desc %>
static inline void <%= domain %>_event_emit_<%= a.name %>(<%= emitter_decl %>_t *self<%= expand_cargs(a.args) %>)
{
<% if domain != "logger" and a.logging -%>
if (self->shared_logger_emitter) {
logger_event_emit_event_trace(self->shared_logger_emitter, EVENT_DOMAIN_ID_<%= domain.upcase %>, <%= a.id %>);
}
<% end -%>
<%= a.elem_decl %>_t *elem = NULL;
list_foreach (&self-><%= a.name %>_listeners, elem) {
elem->callback(elem->context<%= expand_cparams(a.args) %>);
}
}
<% end -%>
static inline void <%= domain %>_event_detach_listener(<%= emitter_decl %>_t *self, void *context)
{
<% actions.each do |a| -%>
{
<%= a.elem_decl %>_t *elem = NULL;
list_foreach (&self-><%= a.name %>_listeners, elem) {
if (elem->context == context) {
list_remove(&self-><%= a.name %>_listeners, &elem->list_entry);
FREE(elem);
}
}
}
<% end -%>
}
<% actions.each do |a| -%>
static inline void <%= domain %>_event_detach_listener_on_<%= a.name %>(<%= emitter_decl %>_t *self, void *context)
{
<%= a.elem_decl %>_t *elem = NULL;
list_foreach (&self-><%= a.name %>_listeners, elem) {
if (elem->context == context) {
list_remove(&self-><%= a.name %>_listeners, &elem->list_entry);
FREE(elem);
}
}
}
<% end -%>
static inline void <%= domain %>_event_detach_all(<%= emitter_decl %>_t *self)
{
<% actions.each do |a| -%>
{
<%= a.elem_decl %>_t *elem = NULL;
list_foreach (&self-><%= a.name %>_listeners, elem) {
list_remove(&self-><%= a.name %>_listeners, &elem->list_entry);
FREE(elem);
}
}
<% end -%>
}