4 * Babeltrace Plugin Component Class
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/component/component-class-internal.h>
31 #include <babeltrace/ref.h>
36 void bt_component_class_destroy(struct bt_object
*obj
)
38 struct bt_component_class
*class;
42 class = container_of(obj
, struct bt_component_class
, base
);
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
,
51 listener
->func(class, listener
->data
);
55 g_string_free(class->name
, TRUE
);
57 if (class->description
) {
58 g_string_free(class->description
, TRUE
);
60 if (class->destroy_listeners
) {
61 g_array_free(class->destroy_listeners
, TRUE
);
68 int bt_component_class_init(struct bt_component_class
*class,
69 enum bt_component_class_type type
, const char *name
)
73 bt_object_init(class, bt_component_class_destroy
);
75 class->name
= g_string_new(name
);
80 class->description
= g_string_new(NULL
);
81 if (!class->description
) {
85 class->destroy_listeners
= g_array_new(FALSE
, TRUE
,
86 sizeof(struct bt_component_class_destroy_listener
));
87 if (!class->destroy_listeners
) {
101 struct bt_component_class
*bt_component_class_source_create(const char *name
,
102 bt_component_class_notification_iterator_get_method notification_iterator_get_method
,
103 bt_component_class_notification_iterator_next_method notification_iterator_next_method
)
105 struct bt_component_class_source
*source_class
= NULL
;
108 if (!name
|| !notification_iterator_get_method
||
109 !notification_iterator_next_method
) {
113 source_class
= g_new0(struct bt_component_class_source
, 1);
118 ret
= bt_component_class_init(&source_class
->parent
,
119 BT_COMPONENT_CLASS_TYPE_SOURCE
, name
);
122 * If bt_component_class_init() fails, the component
123 * class is put, therefore its memory is already
130 source_class
->methods
.iterator
.get
= notification_iterator_get_method
;
131 source_class
->methods
.iterator
.next
= notification_iterator_next_method
;
134 return &source_class
->parent
;
137 struct bt_component_class
*bt_component_class_filter_create(const char *name
,
138 bt_component_class_notification_iterator_get_method notification_iterator_get_method
,
139 bt_component_class_notification_iterator_next_method notification_iterator_next_method
)
141 struct bt_component_class_filter
*filter_class
= NULL
;
144 if (!name
|| !notification_iterator_get_method
||
145 !notification_iterator_next_method
) {
149 filter_class
= g_new0(struct bt_component_class_filter
, 1);
154 ret
= bt_component_class_init(&filter_class
->parent
,
155 BT_COMPONENT_CLASS_TYPE_FILTER
, name
);
158 * If bt_component_class_init() fails, the component
159 * class is put, therefore its memory is already
166 filter_class
->methods
.iterator
.get
= notification_iterator_get_method
;
167 filter_class
->methods
.iterator
.next
= notification_iterator_next_method
;
170 return &filter_class
->parent
;
173 struct bt_component_class
*bt_component_class_sink_create(const char *name
,
174 bt_component_class_sink_consume_method consume_method
)
176 struct bt_component_class_sink
*sink_class
= NULL
;
179 if (!name
|| !consume_method
) {
183 sink_class
= g_new0(struct bt_component_class_sink
, 1);
188 ret
= bt_component_class_init(&sink_class
->parent
,
189 BT_COMPONENT_CLASS_TYPE_SINK
, name
);
192 * If bt_component_class_init() fails, the component
193 * class is put, therefore its memory is already
200 sink_class
->methods
.consume
= consume_method
;
203 return &sink_class
->parent
;
206 int bt_component_class_set_init_method(
207 struct bt_component_class
*component_class
,
208 bt_component_class_init_method init_method
)
212 if (!component_class
|| component_class
->frozen
|| !init_method
) {
217 component_class
->methods
.init
= init_method
;
223 int bt_component_class_set_destroy_method(
224 struct bt_component_class
*component_class
,
225 bt_component_class_destroy_method destroy_method
)
229 if (!component_class
|| component_class
->frozen
|| !destroy_method
) {
234 component_class
->methods
.destroy
= destroy_method
;
240 int bt_component_class_source_set_notification_iterator_init_method(
241 struct bt_component_class
*component_class
,
242 bt_component_class_notification_iterator_init_method notification_iterator_init_method
)
244 struct bt_component_class_source
*source_class
;
247 if (!component_class
|| component_class
->frozen
||
248 !notification_iterator_init_method
||
249 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
) {
254 source_class
= container_of(component_class
,
255 struct bt_component_class_source
, parent
);
256 source_class
->methods
.iterator
.init
= notification_iterator_init_method
;
262 int bt_component_class_source_set_notification_iterator_destroy_method(
263 struct bt_component_class
*component_class
,
264 bt_component_class_notification_iterator_destroy_method notification_iterator_destroy_method
)
266 struct bt_component_class_source
*source_class
;
269 if (!component_class
|| component_class
->frozen
||
270 !notification_iterator_destroy_method
||
271 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
) {
276 source_class
= container_of(component_class
,
277 struct bt_component_class_source
, parent
);
278 source_class
->methods
.iterator
.destroy
=
279 notification_iterator_destroy_method
;
285 int bt_component_class_source_set_notification_iterator_seek_time_method(
286 struct bt_component_class
*component_class
,
287 bt_component_class_notification_iterator_seek_time_method notification_iterator_seek_time_method
)
289 struct bt_component_class_source
*source_class
;
292 if (!component_class
|| component_class
->frozen
||
293 !notification_iterator_seek_time_method
||
294 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
) {
299 source_class
= container_of(component_class
,
300 struct bt_component_class_source
, parent
);
301 source_class
->methods
.iterator
.seek_time
=
302 notification_iterator_seek_time_method
;
308 int bt_component_class_filter_set_notification_iterator_init_method(
309 struct bt_component_class
*component_class
,
310 bt_component_class_notification_iterator_init_method notification_iterator_init_method
)
312 struct bt_component_class_filter
*filter_class
;
315 if (!component_class
|| component_class
->frozen
||
316 !notification_iterator_init_method
||
317 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
322 filter_class
= container_of(component_class
,
323 struct bt_component_class_filter
, parent
);
324 filter_class
->methods
.iterator
.init
= notification_iterator_init_method
;
330 int bt_component_class_filter_set_notification_iterator_destroy_method(
331 struct bt_component_class
*component_class
,
332 bt_component_class_notification_iterator_destroy_method notification_iterator_destroy_method
)
334 struct bt_component_class_filter
*filter_class
;
337 if (!component_class
|| component_class
->frozen
||
338 !notification_iterator_destroy_method
||
339 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
344 filter_class
= container_of(component_class
,
345 struct bt_component_class_filter
, parent
);
346 filter_class
->methods
.iterator
.destroy
=
347 notification_iterator_destroy_method
;
353 int bt_component_class_filter_set_notification_iterator_seek_time_method(
354 struct bt_component_class
*component_class
,
355 bt_component_class_notification_iterator_seek_time_method notification_iterator_seek_time_method
)
357 struct bt_component_class_filter
*filter_class
;
360 if (!component_class
|| component_class
->frozen
||
361 !notification_iterator_seek_time_method
||
362 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
367 filter_class
= container_of(component_class
,
368 struct bt_component_class_filter
, parent
);
369 filter_class
->methods
.iterator
.seek_time
=
370 notification_iterator_seek_time_method
;
376 int bt_component_class_set_description(
377 struct bt_component_class
*component_class
,
378 const char *description
)
382 if (!component_class
|| component_class
->frozen
|| !description
) {
387 g_string_assign(component_class
->description
, description
);
393 const char *bt_component_class_get_name(
394 struct bt_component_class
*component_class
)
396 return component_class
? component_class
->name
->str
: NULL
;
399 enum bt_component_class_type
bt_component_class_get_type(
400 struct bt_component_class
*component_class
)
402 return component_class
? component_class
->type
:
403 BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
406 const char *bt_component_class_get_description(
407 struct bt_component_class
*component_class
)
409 return component_class
&& component_class
->description
?
410 component_class
->description
->str
: NULL
;
414 int bt_component_class_add_destroy_listener(struct bt_component_class
*class,
415 bt_component_class_destroy_listener_func func
, void *data
)
418 struct bt_component_class_destroy_listener listener
;
420 if (!class || class->frozen
|| !func
) {
425 listener
.func
= func
;
426 listener
.data
= data
;
427 g_array_append_val(class->destroy_listeners
, listener
);
433 extern int bt_component_class_sink_set_add_iterator_method(
434 struct bt_component_class
*component_class
,
435 bt_component_class_sink_add_iterator_method add_iterator_method
)
437 struct bt_component_class_sink
*sink_class
;
440 if (!component_class
|| component_class
->frozen
||
441 !add_iterator_method
||
442 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
447 sink_class
= container_of(component_class
,
448 struct bt_component_class_sink
, parent
);
449 sink_class
->methods
.add_iterator
= add_iterator_method
;
455 extern int bt_component_class_filter_set_add_iterator_method(
456 struct bt_component_class
*component_class
,
457 bt_component_class_filter_add_iterator_method add_iterator_method
)
459 struct bt_component_class_filter
*filter_class
;
462 if (!component_class
|| component_class
->frozen
||
463 !add_iterator_method
||
464 component_class
->type
!=
465 BT_COMPONENT_CLASS_TYPE_FILTER
) {
470 filter_class
= container_of(component_class
,
471 struct bt_component_class_filter
, parent
);
472 filter_class
->methods
.add_iterator
= add_iterator_method
;
478 int bt_component_class_freeze(
479 struct bt_component_class
*component_class
)
483 if (!component_class
) {
488 component_class
->frozen
= true;