plugin-dev.h: put selector (type) close to union in structures
[babeltrace.git] / lib / component / component-class.c
1 /*
2 * component-class.c
3 *
4 * Babeltrace Plugin Component Class
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/component/component-class-internal.h>
31 #include <babeltrace/ref.h>
32 #include <stdbool.h>
33 #include <glib.h>
34
35 static
36 void bt_component_class_destroy(struct bt_object *obj)
37 {
38 struct bt_component_class *class;
39 int i;
40
41 assert(obj);
42 class = container_of(obj, struct bt_component_class, base);
43
44 /* Call destroy listeners in reverse registration order */
45 for (i = class->destroy_listeners->len - 1; i >= 0; i--) {
46 struct bt_component_class_destroy_listener *listener =
47 &g_array_index(class->destroy_listeners,
48 struct bt_component_class_destroy_listener,
49 i);
50
51 listener->func(class, listener->data);
52 }
53
54 if (class->name) {
55 g_string_free(class->name, TRUE);
56 }
57 if (class->description) {
58 g_string_free(class->description, TRUE);
59 }
60 if (class->destroy_listeners) {
61 g_array_free(class->destroy_listeners, TRUE);
62 }
63
64 g_free(class);
65 }
66
67 static
68 int bt_component_class_init(struct bt_component_class *class,
69 enum bt_component_class_type type, const char *name)
70 {
71 int ret = 0;
72
73 bt_object_init(class, bt_component_class_destroy);
74 class->type = type;
75 class->name = g_string_new(name);
76 if (!class->name) {
77 goto error;
78 }
79
80 class->description = g_string_new(NULL);
81 if (!class->description) {
82 goto error;
83 }
84
85 class->destroy_listeners = g_array_new(FALSE, TRUE,
86 sizeof(struct bt_component_class_destroy_listener));
87 if (!class->destroy_listeners) {
88 goto error;
89 }
90
91 goto end;
92
93 error:
94 BT_PUT(class);
95 ret = -1;
96
97 end:
98 return ret;
99 }
100
101 struct bt_component_class *bt_component_class_source_create(const char *name,
102 bt_component_class_source_init_iterator_method init_iterator_method)
103 {
104 struct bt_component_class_source *source_class = NULL;
105 int ret;
106
107 if (!name || !init_iterator_method) {
108 goto end;
109 }
110
111 source_class = g_new0(struct bt_component_class_source, 1);
112 if (!source_class) {
113 goto end;
114 }
115
116 ret = bt_component_class_init(&source_class->parent,
117 BT_COMPONENT_CLASS_TYPE_SOURCE, name);
118 if (ret) {
119 /*
120 * If bt_component_class_init() fails, the component
121 * class is put, therefore its memory is already
122 * freed.
123 */
124 source_class = NULL;
125 goto end;
126 }
127
128 source_class->methods.init_iterator = init_iterator_method;
129
130 end:
131 return &source_class->parent;
132 }
133
134 struct bt_component_class *bt_component_class_filter_create(const char *name,
135 bt_component_class_filter_init_iterator_method init_iterator_method)
136 {
137 struct bt_component_class_filter *filter_class = NULL;
138 int ret;
139
140 if (!name || !init_iterator_method) {
141 goto end;
142 }
143
144 filter_class = g_new0(struct bt_component_class_filter, 1);
145 if (!filter_class) {
146 goto end;
147 }
148
149 ret = bt_component_class_init(&filter_class->parent,
150 BT_COMPONENT_CLASS_TYPE_FILTER, name);
151 if (ret) {
152 /*
153 * If bt_component_class_init() fails, the component
154 * class is put, therefore its memory is already
155 * freed.
156 */
157 filter_class = NULL;
158 goto end;
159 }
160
161 filter_class->methods.init_iterator = init_iterator_method;
162
163 end:
164 return &filter_class->parent;
165 }
166
167 struct bt_component_class *bt_component_class_sink_create(const char *name,
168 bt_component_class_sink_consume_method consume_method)
169 {
170 struct bt_component_class_sink *sink_class = NULL;
171 int ret;
172
173 if (!name || !consume_method) {
174 goto end;
175 }
176
177 sink_class = g_new0(struct bt_component_class_sink, 1);
178 if (!sink_class) {
179 goto end;
180 }
181
182 ret = bt_component_class_init(&sink_class->parent,
183 BT_COMPONENT_CLASS_TYPE_SINK, name);
184 if (ret) {
185 /*
186 * If bt_component_class_init() fails, the component
187 * class is put, therefore its memory is already
188 * freed.
189 */
190 sink_class = NULL;
191 goto end;
192 }
193
194 sink_class->methods.consume = consume_method;
195
196 end:
197 return &sink_class->parent;
198 }
199
200 int bt_component_class_set_init_method(
201 struct bt_component_class *component_class,
202 bt_component_class_init_method init_method)
203 {
204 int ret = 0;
205
206 if (!component_class || component_class->frozen || !init_method) {
207 ret = -1;
208 goto end;
209 }
210
211 component_class->methods.init = init_method;
212
213 end:
214 return ret;
215 }
216
217 int bt_component_class_set_destroy_method(
218 struct bt_component_class *component_class,
219 bt_component_class_destroy_method destroy_method)
220 {
221 int ret = 0;
222
223 if (!component_class || component_class->frozen || !destroy_method) {
224 ret = -1;
225 goto end;
226 }
227
228 component_class->methods.destroy = destroy_method;
229
230 end:
231 return ret;
232 }
233
234 extern int bt_component_class_set_description(
235 struct bt_component_class *component_class,
236 const char *description)
237 {
238 int ret = 0;
239
240 if (!component_class || component_class->frozen || !description) {
241 ret = -1;
242 goto end;
243 }
244
245 g_string_assign(component_class->description, description);
246
247 end:
248 return ret;
249 }
250
251 const char *bt_component_class_get_name(
252 struct bt_component_class *component_class)
253 {
254 return component_class ? component_class->name->str : NULL;
255 }
256
257 enum bt_component_class_type bt_component_class_get_type(
258 struct bt_component_class *component_class)
259 {
260 return component_class ? component_class->type :
261 BT_COMPONENT_CLASS_TYPE_UNKNOWN;
262 }
263
264 const char *bt_component_class_get_description(
265 struct bt_component_class *component_class)
266 {
267 return component_class && component_class->description ?
268 component_class->description->str : NULL;
269 }
270
271 BT_HIDDEN
272 int bt_component_class_add_destroy_listener(struct bt_component_class *class,
273 bt_component_class_destroy_listener_func func, void *data)
274 {
275 int ret = 0;
276 struct bt_component_class_destroy_listener listener;
277
278 if (!class || class->frozen || !func) {
279 ret = -1;
280 goto end;
281 }
282
283 listener.func = func;
284 listener.data = data;
285 g_array_append_val(class->destroy_listeners, listener);
286
287 end:
288 return ret;
289 }
290
291 extern int bt_component_class_sink_set_add_iterator_method(
292 struct bt_component_class *component_class,
293 bt_component_class_sink_add_iterator_method add_iterator_method)
294 {
295 struct bt_component_class_sink *sink_class;
296 int ret = 0;
297
298 if (!component_class || component_class->frozen ||
299 !add_iterator_method ||
300 component_class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
301 ret = -1;
302 goto end;
303 }
304
305 sink_class = container_of(component_class,
306 struct bt_component_class_sink, parent);
307 sink_class->methods.add_iterator = add_iterator_method;
308
309 end:
310 return ret;
311 }
312
313 extern int bt_component_class_filter_set_add_iterator_method(
314 struct bt_component_class *component_class,
315 bt_component_class_filter_add_iterator_method add_iterator_method)
316 {
317 struct bt_component_class_filter *filter_class;
318 int ret = 0;
319
320 if (!component_class || component_class->frozen ||
321 !add_iterator_method ||
322 component_class->type !=
323 BT_COMPONENT_CLASS_TYPE_FILTER) {
324 ret = -1;
325 goto end;
326 }
327
328 filter_class = container_of(component_class,
329 struct bt_component_class_filter, parent);
330 filter_class->methods.add_iterator = add_iterator_method;
331
332 end:
333 return ret;
334 }
335
336 int bt_component_class_freeze(
337 struct bt_component_class *component_class)
338 {
339 int ret = 0;
340
341 if (!component_class) {
342 ret = -1;
343 goto end;
344 }
345
346 component_class->frozen = true;
347
348 end:
349 return ret;
350 }
This page took 0.036969 seconds and 5 git commands to generate.