-
Notifications
You must be signed in to change notification settings - Fork 2
/
registry.c
197 lines (152 loc) · 4.25 KB
/
registry.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "registry.h"
struct aem_registry *aem_registry_init(struct aem_registry *reg)
{
aem_stack_init(®->stk);
reg->on_get_miss = NULL;
reg->dtor = NULL;
reg->flags = 0;
return reg;
}
void aem_registry_dtor(struct aem_registry *reg)
{
if (!reg)
return;
AEM_STACK_FOREACH(i, ®->stk) {
aem_registry_remove(reg, i);
}
aem_stack_dtor(®->stk);
}
struct aem_registrable *aem_registrable_init(struct aem_registrable *item)
{
aem_stringbuf_init(&item->name);
item->reg = NULL;
item->id = -1;
return item;
}
void aem_registrable_dtor(struct aem_registrable *item)
{
if (!item)
return;
if (item->id >= 0)
aem_registrable_deregister(item);
aem_assert(!item->reg);
aem_stringbuf_dtor(&item->name);
}
const char *aem_registrable_name(struct aem_registrable *item)
{
if (!item)
return NULL;
return aem_stringbuf_get(&item->name);
}
ssize_t aem_registrable_id(struct aem_registrable *item)
{
if (!item)
return -1;
return item->id;
}
ssize_t aem_registrable_register(struct aem_registrable *item, struct aem_registry *reg)
{
// Bail if already registered
if (item->id >= 0)
return item->id;
if (reg->flags & AEM_REGISTRY_NO_DUPS) {
struct aem_registrable *item2 = aem_registry_by_name(reg, aem_stringslice_new_str(&item->name));
if (item2) {
//aem_logf_ctx(AEM_LOG_BUG, "Duplicate registration name: \"%s\" already taken by #%zd", aem_registrable_name(item), aem_registrable_id(item2));
return -1;
}
}
//aem_assert(item->name.n);
aem_assert(reg);
item->reg = reg;
aem_assert(item->id == -1);
size_t id = aem_stack_assign_empty(&item->reg->stk, item);
item->id = id;
return id;
}
void aem_registrable_deregister(struct aem_registrable *item)
{
if (!item)
return;
// Bail if not registered
if (item->id == -1)
return;
aem_assert(item->id >= 0);
aem_assert(item->reg);
// Deregister the item, and verify we didn't somehow deregister the wrong one.
aem_assert(aem_stack_remove(&item->reg->stk, item->id) == item);
item->id = -1;
item->reg = NULL;
}
struct aem_registrable *aem_registry_by_id(struct aem_registry *reg, ssize_t id)
{
if (!reg)
return NULL;
if (id < 0)
return NULL;
struct aem_registrable *item = aem_stack_index(®->stk, id);
return item;
}
struct aem_registrable *aem_registry_by_name(struct aem_registry *reg, struct aem_stringslice name)
{
if (!reg)
return NULL;
// TODO: Use a hash table
AEM_STACK_FOREACH(i, ®->stk) {
// TODO: struct aem_registrable *item = reg->stk.s[(i + hash(name)) % reg->stk.n];
struct aem_registrable *item = reg->stk.s[i];
if (!item)
continue;
if (!aem_stringslice_cmp(aem_stringslice_new_str(&item->name), name))
return item;
}
return NULL;
}
struct aem_registrable *aem_registry_lookup(struct aem_registry *reg, struct aem_stringslice key)
{
if (!reg)
return NULL;
// Try to look up by key
struct aem_registrable *by_name = aem_registry_by_name(reg, key);
// Get a '#' if there is one
int prefer_id = aem_stringslice_match(&key, "#");
// If we found one by key that didn't start with "#", return it now
if (!prefer_id && by_name)
return by_name;
// Try to look up by ID
int id;
struct aem_registrable *by_id = aem_stringslice_match_int_base(&key, 10, &id) && !aem_stringslice_ok(key) ? aem_registry_by_id(reg, id) : NULL;
// If we couldn't find one by ID, return the one we found by key
if (!by_id)
return by_name;
return by_id;
}
struct aem_registrable *aem_registry_get(struct aem_registry *reg, struct aem_stringslice name)
{
if (!reg)
return NULL;
struct aem_registrable *item = aem_registry_by_name(reg, name);
// If we found nothing but have an on_get_miss callback, see if it can get us something.
if (!item && reg->on_get_miss) {
item = reg->on_get_miss(reg, name);
// If it returned something, assert that it is registered with this registry.
if (item)
aem_assert(item->reg == reg);
}
return item;
}
int aem_registry_remove(struct aem_registry *reg, size_t id)
{
aem_assert(reg);
struct aem_registrable *item = aem_registry_by_id(reg, id);
if (!item)
return -1;
if (reg->dtor) {
reg->dtor(item);
return 0;
} else {
aem_logf_ctx(AEM_LOG_BUG, "Removing item %s (#%zd) from registry with no ->dtor method!", aem_registrable_name(item), aem_registrable_id(item));
aem_registrable_deregister(item);
}
return 1;
}