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>
35 void bt_component_class_destroy(struct bt_object
*obj
)
37 struct bt_component_class
*class;
41 class = container_of(obj
, struct bt_component_class
, base
);
43 /* Call destroy listeners in reverse registration order */
44 for (i
= class->destroy_listeners
->len
- 1; i
>= 0; i
--) {
45 struct bt_component_class_destroy_listener
*listener
=
46 &g_array_index(class->destroy_listeners
,
47 struct bt_component_class_destroy_listener
,
50 listener
->func(class, listener
->data
);
54 g_string_free(class->name
, TRUE
);
56 if (class->description
) {
57 g_string_free(class->description
, TRUE
);
59 if (class->destroy_listeners
) {
60 g_array_free(class->destroy_listeners
, TRUE
);
67 int bt_component_class_init(struct bt_component_class
*class,
68 enum bt_component_class_type type
, const char *name
)
72 bt_object_init(class, bt_component_class_destroy
);
74 class->name
= g_string_new(name
);
79 class->description
= g_string_new(NULL
);
80 if (!class->description
) {
84 class->destroy_listeners
= g_array_new(FALSE
, TRUE
,
85 sizeof(struct bt_component_class_destroy_listener
));
86 if (!class->destroy_listeners
) {
100 struct bt_component_class
*bt_component_class_source_create(const char *name
,
101 bt_component_class_source_init_iterator_method init_iterator_method
)
103 struct bt_component_class_source
*source_class
= NULL
;
106 if (!name
|| !init_iterator_method
) {
110 source_class
= g_new0(struct bt_component_class_source
, 1);
115 ret
= bt_component_class_init(&source_class
->parent
,
116 BT_COMPONENT_CLASS_TYPE_SOURCE
, name
);
119 * If bt_component_class_init() fails, the component
120 * class is put, therefore its memory is already
127 source_class
->methods
.init_iterator
= init_iterator_method
;
130 return &source_class
->parent
;
133 struct bt_component_class
*bt_component_class_filter_create(const char *name
,
134 bt_component_class_filter_init_iterator_method init_iterator_method
)
136 struct bt_component_class_filter
*filter_class
= NULL
;
139 if (!name
|| !init_iterator_method
) {
143 filter_class
= g_new0(struct bt_component_class_filter
, 1);
148 ret
= bt_component_class_init(&filter_class
->parent
,
149 BT_COMPONENT_CLASS_TYPE_FILTER
, name
);
152 * If bt_component_class_init() fails, the component
153 * class is put, therefore its memory is already
160 filter_class
->methods
.init_iterator
= init_iterator_method
;
163 return &filter_class
->parent
;
166 struct bt_component_class
*bt_component_class_sink_create(const char *name
,
167 bt_component_class_sink_consume_method consume_method
)
169 struct bt_component_class_sink
*sink_class
= NULL
;
172 if (!name
|| !consume_method
) {
176 sink_class
= g_new0(struct bt_component_class_sink
, 1);
181 ret
= bt_component_class_init(&sink_class
->parent
,
182 BT_COMPONENT_CLASS_TYPE_SINK
, name
);
185 * If bt_component_class_init() fails, the component
186 * class is put, therefore its memory is already
193 sink_class
->methods
.consume
= consume_method
;
196 return &sink_class
->parent
;
199 int bt_component_class_set_init_method(
200 struct bt_component_class
*component_class
,
201 bt_component_class_init_method init_method
)
205 if (!component_class
|| !init_method
) {
210 component_class
->methods
.init
= init_method
;
216 int bt_component_class_set_destroy_method(
217 struct bt_component_class
*component_class
,
218 bt_component_class_destroy_method destroy_method
)
222 if (!component_class
|| !destroy_method
) {
227 component_class
->methods
.destroy
= destroy_method
;
233 extern int bt_component_class_set_description(
234 struct bt_component_class
*component_class
,
235 const char *description
)
239 if (!component_class
|| !description
) {
244 g_string_assign(component_class
->description
, description
);
250 const char *bt_component_class_get_name(
251 struct bt_component_class
*component_class
)
253 return component_class
? component_class
->name
->str
: NULL
;
256 enum bt_component_class_type
bt_component_class_get_type(
257 struct bt_component_class
*component_class
)
259 return component_class
? component_class
->type
:
260 BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
263 const char *bt_component_class_get_description(
264 struct bt_component_class
*component_class
)
266 return component_class
&& component_class
->description
?
267 component_class
->description
->str
: NULL
;
271 int bt_component_class_add_destroy_listener(struct bt_component_class
*class,
272 bt_component_class_destroy_listener_func func
, void *data
)
275 struct bt_component_class_destroy_listener listener
;
277 if (!class || !func
) {
282 listener
.func
= func
;
283 listener
.data
= data
;
284 g_array_append_val(class->destroy_listeners
, listener
);
290 extern int bt_component_class_sink_set_add_iterator_method(
291 struct bt_component_class
*component_class
,
292 bt_component_class_sink_add_iterator_method add_iterator_method
)
294 struct bt_component_class_sink
*sink_class
;
297 if (!component_class
|| !add_iterator_method
||
298 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
303 sink_class
= container_of(component_class
,
304 struct bt_component_class_sink
, parent
);
305 sink_class
->methods
.add_iterator
= add_iterator_method
;
311 extern int bt_component_class_filter_set_add_iterator_method(
312 struct bt_component_class
*component_class
,
313 bt_component_class_filter_add_iterator_method add_iterator_method
)
315 struct bt_component_class_filter
*filter_class
;
318 if (!component_class
|| !add_iterator_method
||
319 component_class
->type
!=
320 BT_COMPONENT_CLASS_TYPE_FILTER
) {
325 filter_class
= container_of(component_class
,
326 struct bt_component_class_filter
, parent
);
327 filter_class
->methods
.add_iterator
= add_iterator_method
;