-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathosc_vtable.c
220 lines (195 loc) · 6.48 KB
/
osc_vtable.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2011, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
/** \file osc_vtable.c
\author John MacCallum
*/
#include <string.h>
#include <stdio.h>
#include "osc.h"
#include "osc_mem.h"
#include "osc_error.h"
#include "osc_bundle_s.h"
#include "osc_hashtab.h"
#include "osc_vtable.h"
#include "osc_vtable.r"
t_osc_vtable *osc_vtable_alloc(int num_entries){
t_osc_vtable *vtab = osc_mem_alloc(sizeof(t_osc_vtable));
vtab->entries = NULL;
vtab->num_entries = 0;
vtab->num_slots = 0;
if(num_entries > 0){
vtab->entries = osc_mem_alloc(num_entries * sizeof(t_osc_vtable_entry));
vtab->num_entries = 0;
vtab->num_slots = num_entries;
}
vtab->hashtab = osc_hashtab_new(num_entries, NULL);
vtab->delegation_context = NULL;
vtab->delegation_method = NULL;
return vtab;
}
void osc_vtable_free(t_osc_vtable *vtab){
int i;
for(i = 0; i < osc_vtable_getNumEntries(vtab); i++){
t_osc_vtable_entry *e = osc_vtable_getEntryByIndex(vtab, i);
if(e->selector){
osc_mem_free(e->selector);
}
}
if(vtab->entries){
osc_mem_free(vtab->entries);
}
if(vtab->hashtab){
osc_hashtab_destroy(vtab->hashtab);
}
}
void osc_vtable_setDelegationMethod(t_osc_vtable *vtab,
t_osc_vtable_delegation_method delegation_method,
void *delegation_context)
{
vtab->delegation_method = delegation_method;
vtab->delegation_context = delegation_context;
}
t_osc_err osc_vtable_addEntry(t_osc_vtable *vtab,
char *selector,
t_osc_vtable_method method,
void *context)
{
if(vtab->num_entries >= vtab->num_slots){
vtab->entries = osc_mem_resize(vtab->entries, ++(vtab->num_slots) * sizeof(t_osc_vtable_entry));
if(!(vtab->entries)){
return OSC_ERR_OUTOFMEM;
}
}
t_osc_vtable_entry *entry = osc_vtable_getEntryBySelector(vtab, selector);
if(entry){
// already an entry with this selector. blow away the old one.
entry->method = method;
entry->context = context;
return OSC_ERR_NONE;
}
entry = &(vtab->entries[vtab->num_entries]);
vtab->num_entries++;
if(selector){
int len = strlen(selector);
entry->selector = osc_mem_alloc(len + 1);
strncpy(entry->selector, selector, len);
entry->selector[len] = '\0';
}
entry->method = method;
entry->context = context;
if(!(vtab->hashtab)){
vtab->hashtab = osc_hashtab_new(-1, NULL);
}
// osc_hashtab_store doesn't make a copy of the key, so make sure to use the selector that we have
// allocated memory for.
osc_hashtab_store(vtab->hashtab, strlen(entry->selector), entry->selector, entry);
return OSC_ERR_NONE;
}
int osc_vtable_getNumEntries(t_osc_vtable *vtab){
return vtab->num_entries;
}
int osc_vtable_getNumSlots(t_osc_vtable *vtab){
return vtab->num_slots;
}
t_osc_vtable_entry *osc_vtable_getEntryByIndex(t_osc_vtable *vtab, int index){
if(index >= vtab->num_entries){
return NULL;
}
return &(vtab->entries[index]);
}
t_osc_vtable_entry *osc_vtable_getEntryBySelector(t_osc_vtable *vtab, char *selector){
return osc_hashtab_lookup(vtab->hashtab, strlen(selector), selector);
}
int osc_vtable_getIndexForEntry(t_osc_vtable *vtab, t_osc_vtable_entry *e){
if(e){
return (e - vtab->entries);
}
return -1;
}
int osc_vtable_getIndexForSelector(t_osc_vtable *vtab, char *selector){
t_osc_vtable_entry *e = osc_hashtab_lookup(vtab->hashtab, strlen(selector), selector);
return osc_vtable_getIndexForEntry(vtab, e);
}
char *osc_vtable_entry_getSelector(t_osc_vtable_entry *e){
return e->selector;
}
t_osc_err osc_vtable_rebindSelector(t_osc_vtable *vtab, char *selector, t_osc_vtable_method new_method){
t_osc_vtable_entry *e = osc_vtable_getEntryBySelector(vtab, selector);
if(!e){
return OSC_ERR_INVAL;
}
e->method = new_method;
return OSC_ERR_NONE;
}
t_osc_err osc_vtable_rebindSelectorWithContext(t_osc_vtable *vtab,
char *selector,
t_osc_vtable_method new_method,
void *new_context)
{
t_osc_vtable_entry *e = osc_vtable_getEntryBySelector(vtab, selector);
if(!e){
return OSC_ERR_INVAL;
}
e->method = new_method;
e->context = new_context;
return OSC_ERR_NONE;
}
t_osc_err osc_vtable_renameEntry(t_osc_vtable *vtab, char *old_selector, char *new_selector){
t_osc_vtable_entry *e = osc_vtable_getEntryBySelector(vtab, old_selector);
if(!e){
return OSC_ERR_INVAL;
}
int oldlen = strlen(old_selector);
int newlen = strlen(new_selector);
if(newlen > oldlen){
e->selector = osc_mem_resize(e->selector, newlen + 1);
}
strncpy(e->selector, new_selector, newlen + 1);
osc_hashtab_remove(vtab->hashtab, oldlen, old_selector, NULL);
osc_hashtab_store(vtab->hashtab, newlen, new_selector, (void *)e);
return OSC_ERR_NONE;
}
int osc_vtable_callWithEntry(t_osc_vtable *vtab,
t_osc_vtable_entry *e,
long bndllen,
char *bndl,
t_osc_bndl_s *partial_matches,
t_osc_bndl_s *complete_matches)
{
e->method(e, bndllen, bndl, partial_matches, complete_matches, e->context);
return 0;
}
int osc_vtable_call(t_osc_vtable *vtab,
char *selector,
long bndllen,
char *bndl,
t_osc_bndl_s *partial_matches,
t_osc_bndl_s *complete_matches)
{
t_osc_vtable_entry *e = osc_hashtab_lookup(vtab->hashtab, strlen(selector), selector);
if(!e){
return 1;
}
return osc_vtable_callWithEntry(vtab, e, bndllen, bndl, partial_matches, complete_matches);
}
int osc_vtable_delegate(t_osc_vtable *vtab, long bndllen, char *bndl, t_osc_bndl_s *unmatched){
vtab->delegation_method(bndllen, bndl, unmatched, vtab->delegation_context);
return 0;
}