Add internal BT_ASSERT() and BT_ASSERT_PRE() helpers
[babeltrace.git] / lib / graph / component-class.c
CommitLineData
fb2dcc52
JG
1/*
2 * component-class.c
3 *
4 * Babeltrace Plugin Component Class
33b34c43 5 *
3310b217 6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
fb2dcc52
JG
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
a3aacb6f
PP
29#define BT_LOG_TAG "COMP-CLASS"
30#include <babeltrace/lib-logging-internal.h>
31
3d9990ac 32#include <babeltrace/compiler-internal.h>
b2e0c907 33#include <babeltrace/graph/component-class-internal.h>
b8a06801 34#include <babeltrace/ref.h>
c55a9f58 35#include <babeltrace/types.h>
fb2dcc52
JG
36#include <glib.h>
37
38static
b8a06801 39void bt_component_class_destroy(struct bt_object *obj)
fb2dcc52
JG
40{
41 struct bt_component_class *class;
33b34c43 42 int i;
fb2dcc52 43
b8a06801
JG
44 assert(obj);
45 class = container_of(obj, struct bt_component_class, base);
33b34c43 46
a3aacb6f
PP
47 BT_LOGD("Destroying component class: "
48 "addr=%p, name=\"%s\", type=%s",
49 class, bt_component_class_get_name(class),
50 bt_component_class_type_string(class->type));
51
33b34c43
PP
52 /* Call destroy listeners in reverse registration order */
53 for (i = class->destroy_listeners->len - 1; i >= 0; i--) {
d3e4dcd8 54 struct bt_component_class_destroy_listener *listener =
33b34c43 55 &g_array_index(class->destroy_listeners,
d3e4dcd8 56 struct bt_component_class_destroy_listener,
33b34c43
PP
57 i);
58
a3aacb6f
PP
59 BT_LOGD("Calling destroy listener: func-addr=%p, data-addr=%p",
60 listener->func, listener->data);
33b34c43
PP
61 listener->func(class, listener->data);
62 }
63
fb2dcc52
JG
64 if (class->name) {
65 g_string_free(class->name, TRUE);
66 }
7c7c0433
JG
67 if (class->description) {
68 g_string_free(class->description, TRUE);
69 }
5536d9a6
PP
70 if (class->help) {
71 g_string_free(class->help, TRUE);
72 }
33b34c43
PP
73 if (class->destroy_listeners) {
74 g_array_free(class->destroy_listeners, TRUE);
75 }
b8a06801 76
fb2dcc52
JG
77 g_free(class);
78}
79
d3e4dcd8
PP
80static
81int bt_component_class_init(struct bt_component_class *class,
82 enum bt_component_class_type type, const char *name)
fb2dcc52 83{
d3e4dcd8
PP
84 int ret = 0;
85
86 bt_object_init(class, bt_component_class_destroy);
87 class->type = type;
88 class->name = g_string_new(name);
89 if (!class->name) {
a3aacb6f 90 BT_LOGE_STR("Failed to allocate a GString.");
d3e4dcd8
PP
91 goto error;
92 }
93
94 class->description = g_string_new(NULL);
95 if (!class->description) {
a3aacb6f 96 BT_LOGE_STR("Failed to allocate a GString.");
d3e4dcd8
PP
97 goto error;
98 }
99
5536d9a6
PP
100 class->help = g_string_new(NULL);
101 if (!class->help) {
a3aacb6f 102 BT_LOGE_STR("Failed to allocate a GString.");
5536d9a6
PP
103 goto error;
104 }
105
d3e4dcd8
PP
106 class->destroy_listeners = g_array_new(FALSE, TRUE,
107 sizeof(struct bt_component_class_destroy_listener));
108 if (!class->destroy_listeners) {
a3aacb6f 109 BT_LOGE_STR("Failed to allocate a GArray.");
d3e4dcd8
PP
110 goto error;
111 }
112
113 goto end;
6ba0b073 114
d3e4dcd8
PP
115error:
116 BT_PUT(class);
117 ret = -1;
118
119end:
120 return ret;
121}
122
123struct bt_component_class *bt_component_class_source_create(const char *name,
90157d89 124 bt_component_class_notification_iterator_next_method method)
d3e4dcd8
PP
125{
126 struct bt_component_class_source *source_class = NULL;
127 int ret;
128
a3aacb6f
PP
129 if (!name) {
130 BT_LOGW_STR("Invalid parameter: name is NULL.");
131 goto end;
132 }
133
90157d89 134 if (!method) {
a3aacb6f 135 BT_LOGW_STR("Invalid parameter: method is NULL.");
6ba0b073
PP
136 goto end;
137 }
fb2dcc52 138
a3aacb6f
PP
139 BT_LOGD("Creating source component class: "
140 "name=\"%s\", notif-iter-next-method-addr=%p",
90157d89 141 name, method);
d3e4dcd8
PP
142 source_class = g_new0(struct bt_component_class_source, 1);
143 if (!source_class) {
a3aacb6f 144 BT_LOGE_STR("Failed to allocate one source component class.");
fb2dcc52
JG
145 goto end;
146 }
147
a3aacb6f 148 /* bt_component_class_init() logs errors */
d3e4dcd8
PP
149 ret = bt_component_class_init(&source_class->parent,
150 BT_COMPONENT_CLASS_TYPE_SOURCE, 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 source_class = NULL;
158 goto end;
159 }
160
90157d89 161 source_class->methods.iterator.next = method;
a3aacb6f
PP
162 BT_LOGD("Created source component class: "
163 "name=\"%s\", notif-iter-next-method-addr=%p, addr=%p",
90157d89 164 name, method, &source_class->parent);
d3e4dcd8
PP
165
166end:
167 return &source_class->parent;
168}
169
170struct bt_component_class *bt_component_class_filter_create(const char *name,
90157d89 171 bt_component_class_notification_iterator_next_method method)
d3e4dcd8
PP
172{
173 struct bt_component_class_filter *filter_class = NULL;
174 int ret;
175
a3aacb6f
PP
176 if (!name) {
177 BT_LOGW_STR("Invalid parameter: name is NULL.");
fb2dcc52
JG
178 goto end;
179 }
6ba0b073 180
90157d89 181 if (!method) {
a3aacb6f
PP
182 BT_LOGW_STR("Invalid parameter: method is NULL.");
183 goto end;
184 }
185
186 BT_LOGD("Creating filter component class: "
187 "name=\"%s\", notif-iter-next-method-addr=%p",
90157d89 188 name, method);
d3e4dcd8
PP
189 filter_class = g_new0(struct bt_component_class_filter, 1);
190 if (!filter_class) {
a3aacb6f 191 BT_LOGE_STR("Failed to allocate one filter component class.");
d3e4dcd8 192 goto end;
6ba0b073
PP
193 }
194
a3aacb6f 195 /* bt_component_class_init() logs errors */
d3e4dcd8
PP
196 ret = bt_component_class_init(&filter_class->parent,
197 BT_COMPONENT_CLASS_TYPE_FILTER, name);
198 if (ret) {
199 /*
200 * If bt_component_class_init() fails, the component
201 * class is put, therefore its memory is already
202 * freed.
203 */
204 filter_class = NULL;
33b34c43
PP
205 goto end;
206 }
d3e4dcd8 207
90157d89 208 filter_class->methods.iterator.next = method;
a3aacb6f
PP
209 BT_LOGD("Created filter component class: "
210 "name=\"%s\", notif-iter-next-method-addr=%p, addr=%p",
90157d89 211 name, method, &filter_class->parent);
d3e4dcd8 212
fb2dcc52 213end:
d3e4dcd8
PP
214 return &filter_class->parent;
215}
216
217struct bt_component_class *bt_component_class_sink_create(const char *name,
90157d89 218 bt_component_class_sink_consume_method method)
d3e4dcd8
PP
219{
220 struct bt_component_class_sink *sink_class = NULL;
221 int ret;
222
a3aacb6f
PP
223 if (!name) {
224 BT_LOGW_STR("Invalid parameter: name is NULL.");
225 goto end;
226 }
227
90157d89 228 if (!method) {
a3aacb6f 229 BT_LOGW_STR("Invalid parameter: method is NULL.");
d3e4dcd8
PP
230 goto end;
231 }
232
a3aacb6f
PP
233 BT_LOGD("Creating sink component class: "
234 "name=\"%s\", consume-method-addr=%p",
90157d89 235 name, method);
d3e4dcd8
PP
236 sink_class = g_new0(struct bt_component_class_sink, 1);
237 if (!sink_class) {
a3aacb6f 238 BT_LOGE_STR("Failed to allocate one sink component class.");
d3e4dcd8
PP
239 goto end;
240 }
241
a3aacb6f 242 /* bt_component_class_init() logs errors */
d3e4dcd8
PP
243 ret = bt_component_class_init(&sink_class->parent,
244 BT_COMPONENT_CLASS_TYPE_SINK, name);
245 if (ret) {
246 /*
247 * If bt_component_class_init() fails, the component
248 * class is put, therefore its memory is already
249 * freed.
250 */
251 sink_class = NULL;
252 goto end;
253 }
254
90157d89 255 sink_class->methods.consume = method;
a3aacb6f
PP
256 BT_LOGD("Created sink component class: "
257 "name=\"%s\", consume-method-addr=%p, addr=%p",
90157d89 258 name, method, &sink_class->parent);
d3e4dcd8
PP
259
260end:
261 return &sink_class->parent;
262}
263
264int bt_component_class_set_init_method(
265 struct bt_component_class *component_class,
90157d89 266 bt_component_class_init_method method)
d3e4dcd8
PP
267{
268 int ret = 0;
269
a3aacb6f
PP
270 if (!component_class) {
271 BT_LOGW_STR("Invalid parameter: component class is NULL.");
272 ret = -1;
273 goto end;
274 }
275
90157d89 276 if (!method) {
a3aacb6f
PP
277 BT_LOGW_STR("Invalid parameter: method is NULL.");
278 ret = -1;
279 goto end;
280 }
281
282 if (component_class->frozen) {
283 BT_LOGW("Invalid parameter: component class is frozen: "
284 "addr=%p, name=\"%s\", type=%s",
285 component_class,
286 bt_component_class_get_name(component_class),
287 bt_component_class_type_string(component_class->type));
d3e4dcd8
PP
288 ret = -1;
289 goto end;
290 }
291
90157d89 292 component_class->methods.init = method;
a3aacb6f
PP
293 BT_LOGV("Set component class's initialization method: "
294 "addr=%p, name=\"%s\", type=%s, method-addr=%p",
295 component_class,
296 bt_component_class_get_name(component_class),
297 bt_component_class_type_string(component_class->type),
90157d89 298 method);
d3e4dcd8
PP
299
300end:
301 return ret;
302}
303
a67681c1 304int bt_component_class_set_query_method(
efa96d5d 305 struct bt_component_class *component_class,
90157d89 306 bt_component_class_query_method method)
efa96d5d
PP
307{
308 int ret = 0;
309
a3aacb6f
PP
310 if (!component_class) {
311 BT_LOGW_STR("Invalid parameter: component class is NULL.");
312 ret = -1;
313 goto end;
314 }
315
90157d89 316 if (!method) {
a3aacb6f
PP
317 BT_LOGW_STR("Invalid parameter: method is NULL.");
318 ret = -1;
319 goto end;
320 }
321
322 if (component_class->frozen) {
323 BT_LOGW("Invalid parameter: component class is frozen: "
324 "addr=%p, name=\"%s\", type=%s",
325 component_class,
326 bt_component_class_get_name(component_class),
327 bt_component_class_type_string(component_class->type));
efa96d5d
PP
328 ret = -1;
329 goto end;
330 }
331
90157d89 332 component_class->methods.query = method;
a3aacb6f
PP
333 BT_LOGV("Set component class's query method: "
334 "addr=%p, name=\"%s\", type=%s, method-addr=%p",
335 component_class,
336 bt_component_class_get_name(component_class),
337 bt_component_class_type_string(component_class->type),
90157d89 338 method);
efa96d5d
PP
339
340end:
341 return ret;
342}
343
72b913fb 344int bt_component_class_set_accept_port_connection_method(
2d41b99e 345 struct bt_component_class *component_class,
72b913fb 346 bt_component_class_accept_port_connection_method method)
2d41b99e
JG
347{
348 int ret = 0;
349
a3aacb6f
PP
350 if (!component_class) {
351 BT_LOGW_STR("Invalid parameter: component class is NULL.");
352 ret = -1;
353 goto end;
354 }
355
356 if (!method) {
357 BT_LOGW_STR("Invalid parameter: method is NULL.");
358 ret = -1;
359 goto end;
360 }
361
362 if (component_class->frozen) {
363 BT_LOGW("Invalid parameter: component class is frozen: "
364 "addr=%p, name=\"%s\", type=%s",
365 component_class,
366 bt_component_class_get_name(component_class),
367 bt_component_class_type_string(component_class->type));
2d41b99e
JG
368 ret = -1;
369 goto end;
370 }
371
72b913fb 372 component_class->methods.accept_port_connection = method;
a3aacb6f
PP
373 BT_LOGV("Set component class's \"accept port connection\" method: "
374 "addr=%p, name=\"%s\", type=%s, method-addr=%p",
375 component_class,
376 bt_component_class_get_name(component_class),
377 bt_component_class_type_string(component_class->type),
378 method);
72b913fb
PP
379
380end:
381 return ret;
382}
383
0d8b4d8e
PP
384int bt_component_class_set_port_connected_method(
385 struct bt_component_class *component_class,
386 bt_component_class_port_connected_method method)
387{
388 int ret = 0;
389
a3aacb6f
PP
390 if (!component_class) {
391 BT_LOGW_STR("Invalid parameter: component class is NULL.");
392 ret = -1;
393 goto end;
394 }
395
396 if (!method) {
397 BT_LOGW_STR("Invalid parameter: method is NULL.");
398 ret = -1;
399 goto end;
400 }
401
402 if (component_class->frozen) {
403 BT_LOGW("Invalid parameter: component class is frozen: "
404 "addr=%p, name=\"%s\", type=%s",
405 component_class,
406 bt_component_class_get_name(component_class),
407 bt_component_class_type_string(component_class->type));
0d8b4d8e
PP
408 ret = -1;
409 goto end;
410 }
411
412 component_class->methods.port_connected = method;
a3aacb6f
PP
413 BT_LOGV("Set component class's \"port connected\" method: "
414 "addr=%p, name=\"%s\", type=%s, method-addr=%p",
415 component_class,
416 bt_component_class_get_name(component_class),
417 bt_component_class_type_string(component_class->type),
418 method);
0d8b4d8e
PP
419
420end:
421 return ret;
422}
423
72b913fb
PP
424int bt_component_class_set_port_disconnected_method(
425 struct bt_component_class *component_class,
426 bt_component_class_port_disconnected_method method)
427{
428 int ret = 0;
429
a3aacb6f
PP
430 if (!component_class) {
431 BT_LOGW_STR("Invalid parameter: component class is NULL.");
432 ret = -1;
433 goto end;
434 }
435
436 if (!method) {
437 BT_LOGW_STR("Invalid parameter: method is NULL.");
438 ret = -1;
439 goto end;
440 }
441
442 if (component_class->frozen) {
443 BT_LOGW("Invalid parameter: component class is frozen: "
444 "addr=%p, name=\"%s\", type=%s",
445 component_class,
446 bt_component_class_get_name(component_class),
447 bt_component_class_type_string(component_class->type));
72b913fb
PP
448 ret = -1;
449 goto end;
450 }
451
452 component_class->methods.port_disconnected = method;
a3aacb6f
PP
453 BT_LOGV("Set component class's \"port disconnected\" method: "
454 "addr=%p, name=\"%s\", type=%s, method-addr=%p",
455 component_class,
456 bt_component_class_get_name(component_class),
457 bt_component_class_type_string(component_class->type),
458 method);
2d41b99e
JG
459
460end:
461 return ret;
462}
463
64cadc66 464int bt_component_class_set_finalize_method(
d3e4dcd8 465 struct bt_component_class *component_class,
90157d89 466 bt_component_class_finalize_method method)
d3e4dcd8
PP
467{
468 int ret = 0;
469
a3aacb6f
PP
470 if (!component_class) {
471 BT_LOGW_STR("Invalid parameter: component class is NULL.");
472 ret = -1;
473 goto end;
474 }
475
90157d89 476 if (!method) {
a3aacb6f
PP
477 BT_LOGW_STR("Invalid parameter: method is NULL.");
478 ret = -1;
479 goto end;
480 }
481
482 if (component_class->frozen) {
483 BT_LOGW("Invalid parameter: component class is frozen: "
484 "addr=%p, name=\"%s\", type=%s",
485 component_class,
486 bt_component_class_get_name(component_class),
487 bt_component_class_type_string(component_class->type));
d3e4dcd8
PP
488 ret = -1;
489 goto end;
490 }
491
90157d89 492 component_class->methods.finalize = method;
a3aacb6f
PP
493 BT_LOGV("Set component class's finalization method: "
494 "addr=%p, name=\"%s\", type=%s, method-addr=%p",
495 component_class,
496 bt_component_class_get_name(component_class),
497 bt_component_class_type_string(component_class->type),
90157d89 498 method);
d3e4dcd8
PP
499
500end:
501 return ret;
502}
503
d3eb6e8f
PP
504int bt_component_class_source_set_notification_iterator_init_method(
505 struct bt_component_class *component_class,
90157d89 506 bt_component_class_notification_iterator_init_method method)
d3eb6e8f
PP
507{
508 struct bt_component_class_source *source_class;
509 int ret = 0;
510
a3aacb6f
PP
511 if (!component_class) {
512 BT_LOGW_STR("Invalid parameter: component class is NULL.");
513 ret = -1;
514 goto end;
515 }
516
90157d89 517 if (!method) {
a3aacb6f
PP
518 BT_LOGW_STR("Invalid parameter: method is NULL.");
519 ret = -1;
520 goto end;
521 }
522
523 if (component_class->type != BT_COMPONENT_CLASS_TYPE_SOURCE) {
524 BT_LOGW("Invalid parameter: component class is not a source component class: "
525 "addr=%p, name=\"%s\", type=%s",
526 component_class,
527 bt_component_class_get_name(component_class),
528 bt_component_class_type_string(component_class->type));
529 ret = -1;
530 goto end;
531 }
532
533 if (component_class->frozen) {
534 BT_LOGW("Invalid parameter: component class is frozen: "
535 "addr=%p, name=\"%s\", type=%s",
536 component_class,
537 bt_component_class_get_name(component_class),
538 bt_component_class_type_string(component_class->type));
d3eb6e8f
PP
539 ret = -1;
540 goto end;
541 }
542
543 source_class = container_of(component_class,
544 struct bt_component_class_source, parent);
90157d89 545 source_class->methods.iterator.init = method;
a3aacb6f
PP
546 BT_LOGV("Set filter component class's notification iterator initialization method: "
547 "addr=%p, name=\"%s\", method-addr=%p",
548 component_class,
549 bt_component_class_get_name(component_class),
90157d89 550 method);
d3eb6e8f
PP
551
552end:
553 return ret;
554}
555
64cadc66 556int bt_component_class_source_set_notification_iterator_finalize_method(
d3eb6e8f 557 struct bt_component_class *component_class,
90157d89 558 bt_component_class_notification_iterator_finalize_method method)
d3eb6e8f
PP
559{
560 struct bt_component_class_source *source_class;
561 int ret = 0;
562
a3aacb6f
PP
563 if (!component_class) {
564 BT_LOGW_STR("Invalid parameter: component class is NULL.");
565 ret = -1;
566 goto end;
567 }
568
90157d89 569 if (!method) {
a3aacb6f
PP
570 BT_LOGW_STR("Invalid parameter: method is NULL.");
571 ret = -1;
572 goto end;
573 }
574
575 if (component_class->type != BT_COMPONENT_CLASS_TYPE_SOURCE) {
576 BT_LOGW("Invalid parameter: component class is not a source component class: "
577 "addr=%p, name=\"%s\", type=%s",
578 component_class,
579 bt_component_class_get_name(component_class),
580 bt_component_class_type_string(component_class->type));
581 ret = -1;
582 goto end;
583 }
584
585 if (component_class->frozen) {
586 BT_LOGW("Invalid parameter: component class is frozen: "
587 "addr=%p, name=\"%s\", type=%s",
588 component_class,
589 bt_component_class_get_name(component_class),
590 bt_component_class_type_string(component_class->type));
d3eb6e8f
PP
591 ret = -1;
592 goto end;
593 }
594
595 source_class = container_of(component_class,
596 struct bt_component_class_source, parent);
64cadc66 597 source_class->methods.iterator.finalize =
90157d89 598 method;
a3aacb6f
PP
599 BT_LOGV("Set filter component class's notification iterator finalization method: "
600 "addr=%p, name=\"%s\", method-addr=%p",
601 component_class,
602 bt_component_class_get_name(component_class),
90157d89 603 method);
d3eb6e8f
PP
604
605end:
606 return ret;
607}
608
d3eb6e8f
PP
609int bt_component_class_filter_set_notification_iterator_init_method(
610 struct bt_component_class *component_class,
90157d89 611 bt_component_class_notification_iterator_init_method method)
d3eb6e8f
PP
612{
613 struct bt_component_class_filter *filter_class;
614 int ret = 0;
615
a3aacb6f
PP
616 if (!component_class) {
617 BT_LOGW_STR("Invalid parameter: component class is NULL.");
618 ret = -1;
619 goto end;
620 }
621
90157d89 622 if (!method) {
a3aacb6f
PP
623 BT_LOGW_STR("Invalid parameter: method is NULL.");
624 ret = -1;
625 goto end;
626 }
627
628 if (component_class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
629 BT_LOGW("Invalid parameter: component class is not a filter component class: "
630 "addr=%p, name=\"%s\", type=%s",
631 component_class,
632 bt_component_class_get_name(component_class),
633 bt_component_class_type_string(component_class->type));
634 ret = -1;
635 goto end;
636 }
637
638 if (component_class->frozen) {
639 BT_LOGW("Invalid parameter: component class is frozen: "
640 "addr=%p, name=\"%s\", type=%s",
641 component_class,
642 bt_component_class_get_name(component_class),
643 bt_component_class_type_string(component_class->type));
d3eb6e8f
PP
644 ret = -1;
645 goto end;
646 }
647
648 filter_class = container_of(component_class,
649 struct bt_component_class_filter, parent);
90157d89 650 filter_class->methods.iterator.init = method;
a3aacb6f
PP
651 BT_LOGV("Set filter component class's notification iterator initialization method: "
652 "addr=%p, name=\"%s\", method-addr=%p",
653 component_class,
654 bt_component_class_get_name(component_class),
90157d89 655 method);
d3eb6e8f
PP
656
657end:
658 return ret;
659}
660
64cadc66 661int bt_component_class_filter_set_notification_iterator_finalize_method(
d3eb6e8f 662 struct bt_component_class *component_class,
90157d89 663 bt_component_class_notification_iterator_finalize_method method)
d3eb6e8f
PP
664{
665 struct bt_component_class_filter *filter_class;
666 int ret = 0;
667
a3aacb6f
PP
668 if (!component_class) {
669 BT_LOGW_STR("Invalid parameter: component class is NULL.");
670 ret = -1;
671 goto end;
672 }
673
90157d89 674 if (!method) {
a3aacb6f
PP
675 BT_LOGW_STR("Invalid parameter: method is NULL.");
676 ret = -1;
677 goto end;
678 }
679
680 if (component_class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
681 BT_LOGW("Invalid parameter: component class is not a filter component class: "
682 "addr=%p, name=\"%s\", type=%s",
683 component_class,
684 bt_component_class_get_name(component_class),
685 bt_component_class_type_string(component_class->type));
686 ret = -1;
687 goto end;
688 }
689
690 if (component_class->frozen) {
691 BT_LOGW("Invalid parameter: component class is frozen: "
692 "addr=%p, name=\"%s\", type=%s",
693 component_class,
694 bt_component_class_get_name(component_class),
695 bt_component_class_type_string(component_class->type));
d3eb6e8f
PP
696 ret = -1;
697 goto end;
698 }
699
700 filter_class = container_of(component_class,
701 struct bt_component_class_filter, parent);
64cadc66 702 filter_class->methods.iterator.finalize =
90157d89 703 method;
a3aacb6f
PP
704 BT_LOGV("Set filter component class's notification iterator finalization method: "
705 "addr=%p, name=\"%s\", method-addr=%p",
706 component_class,
707 bt_component_class_get_name(component_class),
90157d89 708 method);
d3eb6e8f
PP
709
710end:
711 return ret;
712}
713
d3eb6e8f 714int bt_component_class_set_description(
d3e4dcd8
PP
715 struct bt_component_class *component_class,
716 const char *description)
717{
718 int ret = 0;
719
a3aacb6f
PP
720 if (!component_class) {
721 BT_LOGW_STR("Invalid parameter: component class is NULL.");
722 ret = -1;
723 goto end;
724 }
725
726 if (!description) {
727 BT_LOGW_STR("Invalid parameter: description is NULL.");
728 ret = -1;
729 goto end;
730 }
731
732 if (component_class->frozen) {
733 BT_LOGW("Invalid parameter: component class is frozen: "
734 "addr=%p, name=\"%s\", type=%s",
735 component_class,
736 bt_component_class_get_name(component_class),
737 bt_component_class_type_string(component_class->type));
d3e4dcd8
PP
738 ret = -1;
739 goto end;
740 }
741
742 g_string_assign(component_class->description, description);
a3aacb6f
PP
743 BT_LOGV("Set component class's description: "
744 "addr=%p, name=\"%s\", type=%s",
745 component_class,
746 bt_component_class_get_name(component_class),
747 bt_component_class_type_string(component_class->type));
d3e4dcd8
PP
748
749end:
750 return ret;
fb2dcc52 751}
38b48196 752
5536d9a6
PP
753int bt_component_class_set_help(
754 struct bt_component_class *component_class,
755 const char *help)
756{
757 int ret = 0;
758
a3aacb6f
PP
759 if (!component_class) {
760 BT_LOGW_STR("Invalid parameter: component class is NULL.");
761 ret = -1;
762 goto end;
763 }
764
765 if (!help) {
766 BT_LOGW_STR("Invalid parameter: help is NULL.");
767 ret = -1;
768 goto end;
769 }
770
771 if (component_class->frozen) {
772 BT_LOGW("Invalid parameter: component class is frozen: "
773 "addr=%p, name=\"%s\", type=%s",
774 component_class,
775 bt_component_class_get_name(component_class),
776 bt_component_class_type_string(component_class->type));
5536d9a6
PP
777 ret = -1;
778 goto end;
779 }
780
781 g_string_assign(component_class->help, help);
a3aacb6f
PP
782 BT_LOGV("Set component class's help text: "
783 "addr=%p, name=\"%s\", type=%s",
784 component_class,
785 bt_component_class_get_name(component_class),
786 bt_component_class_type_string(component_class->type));
5536d9a6
PP
787
788end:
789 return ret;
790}
791
38b48196
JG
792const char *bt_component_class_get_name(
793 struct bt_component_class *component_class)
794{
795 return component_class ? component_class->name->str : NULL;
796}
797
d3e4dcd8 798enum bt_component_class_type bt_component_class_get_type(
38b48196
JG
799 struct bt_component_class *component_class)
800{
801 return component_class ? component_class->type :
d3e4dcd8 802 BT_COMPONENT_CLASS_TYPE_UNKNOWN;
38b48196
JG
803}
804
33b34c43 805const char *bt_component_class_get_description(
38b48196
JG
806 struct bt_component_class *component_class)
807{
22e22462
PP
808 return component_class && component_class->description &&
809 component_class->description->str[0] != '\0' ?
6ba0b073 810 component_class->description->str : NULL;
38b48196 811}
7c7c0433 812
5536d9a6
PP
813const char *bt_component_class_get_help(
814 struct bt_component_class *component_class)
815{
22e22462
PP
816 return component_class && component_class->help &&
817 component_class->help->str[0] != '\0' ?
5536d9a6
PP
818 component_class->help->str : NULL;
819}
820
33b34c43 821BT_HIDDEN
3230ee6b 822void bt_component_class_add_destroy_listener(struct bt_component_class *class,
33b34c43 823 bt_component_class_destroy_listener_func func, void *data)
7c7c0433 824{
d3e4dcd8 825 struct bt_component_class_destroy_listener listener;
33b34c43 826
3230ee6b
PP
827 assert(class);
828 assert(func);
33b34c43
PP
829 listener.func = func;
830 listener.data = data;
831 g_array_append_val(class->destroy_listeners, listener);
a3aacb6f
PP
832 BT_LOGV("Component class has no registered query method: "
833 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s"
834 "func-addr=%p, data-addr=%p",
835 class,
836 bt_component_class_get_name(class),
837 bt_component_class_type_string(class->type),
838 func, data);
7c7c0433 839}
d3e4dcd8 840
1e4d8103
PP
841int bt_component_class_freeze(
842 struct bt_component_class *component_class)
843{
844 int ret = 0;
845
846 if (!component_class) {
a3aacb6f 847 BT_LOGW_STR("Invalid parameter: component class is NULL.");
1e4d8103
PP
848 ret = -1;
849 goto end;
850 }
851
a3aacb6f
PP
852 if (component_class->frozen) {
853 goto end;
854 }
855
856 BT_LOGD("Freezing component class: "
857 "addr=%p, name=\"%s\", type=%s",
858 component_class,
859 bt_component_class_get_name(component_class),
860 bt_component_class_type_string(component_class->type));
c55a9f58 861 component_class->frozen = BT_TRUE;
1e4d8103
PP
862
863end:
864 return ret;
865}
This page took 0.079376 seconds and 4 git commands to generate.