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_source_init_iterator_method init_iterator_method
)
104 struct bt_component_class_source
*source_class
= NULL
;
107 if (!name
|| !init_iterator_method
) {
111 source_class
= g_new0(struct bt_component_class_source
, 1);
116 ret
= bt_component_class_init(&source_class
->parent
,
117 BT_COMPONENT_CLASS_TYPE_SOURCE
, name
);
120 * If bt_component_class_init() fails, the component
121 * class is put, therefore its memory is already
128 source_class
->methods
.init_iterator
= init_iterator_method
;
131 return &source_class
->parent
;
134 struct bt_component_class
*bt_component_class_filter_create(const char *name
,
135 bt_component_class_filter_init_iterator_method init_iterator_method
)
137 struct bt_component_class_filter
*filter_class
= NULL
;
140 if (!name
|| !init_iterator_method
) {
144 filter_class
= g_new0(struct bt_component_class_filter
, 1);
149 ret
= bt_component_class_init(&filter_class
->parent
,
150 BT_COMPONENT_CLASS_TYPE_FILTER
, name
);
153 * If bt_component_class_init() fails, the component
154 * class is put, therefore its memory is already
161 filter_class
->methods
.init_iterator
= init_iterator_method
;
164 return &filter_class
->parent
;
167 struct bt_component_class
*bt_component_class_sink_create(const char *name
,
168 bt_component_class_sink_consume_method consume_method
)
170 struct bt_component_class_sink
*sink_class
= NULL
;
173 if (!name
|| !consume_method
) {
177 sink_class
= g_new0(struct bt_component_class_sink
, 1);
182 ret
= bt_component_class_init(&sink_class
->parent
,
183 BT_COMPONENT_CLASS_TYPE_SINK
, name
);
186 * If bt_component_class_init() fails, the component
187 * class is put, therefore its memory is already
194 sink_class
->methods
.consume
= consume_method
;
197 return &sink_class
->parent
;
200 int bt_component_class_set_init_method(
201 struct bt_component_class
*component_class
,
202 bt_component_class_init_method init_method
)
206 if (!component_class
|| component_class
->frozen
|| !init_method
) {
211 component_class
->methods
.init
= init_method
;
217 int bt_component_class_set_destroy_method(
218 struct bt_component_class
*component_class
,
219 bt_component_class_destroy_method destroy_method
)
223 if (!component_class
|| component_class
->frozen
|| !destroy_method
) {
228 component_class
->methods
.destroy
= destroy_method
;
234 extern int bt_component_class_set_description(
235 struct bt_component_class
*component_class
,
236 const char *description
)
240 if (!component_class
|| component_class
->frozen
|| !description
) {
245 g_string_assign(component_class
->description
, description
);
251 const char *bt_component_class_get_name(
252 struct bt_component_class
*component_class
)
254 return component_class
? component_class
->name
->str
: NULL
;
257 enum bt_component_class_type
bt_component_class_get_type(
258 struct bt_component_class
*component_class
)
260 return component_class
? component_class
->type
:
261 BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
264 const char *bt_component_class_get_description(
265 struct bt_component_class
*component_class
)
267 return component_class
&& component_class
->description
?
268 component_class
->description
->str
: NULL
;
272 int bt_component_class_add_destroy_listener(struct bt_component_class
*class,
273 bt_component_class_destroy_listener_func func
, void *data
)
276 struct bt_component_class_destroy_listener listener
;
278 if (!class || class->frozen
|| !func
) {
283 listener
.func
= func
;
284 listener
.data
= data
;
285 g_array_append_val(class->destroy_listeners
, listener
);
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
)
295 struct bt_component_class_sink
*sink_class
;
298 if (!component_class
|| component_class
->frozen
||
299 !add_iterator_method
||
300 component_class
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
305 sink_class
= container_of(component_class
,
306 struct bt_component_class_sink
, parent
);
307 sink_class
->methods
.add_iterator
= add_iterator_method
;
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
)
317 struct bt_component_class_filter
*filter_class
;
320 if (!component_class
|| component_class
->frozen
||
321 !add_iterator_method
||
322 component_class
->type
!=
323 BT_COMPONENT_CLASS_TYPE_FILTER
) {
328 filter_class
= container_of(component_class
,
329 struct bt_component_class_filter
, parent
);
330 filter_class
->methods
.add_iterator
= add_iterator_method
;
336 int bt_component_class_freeze(
337 struct bt_component_class
*component_class
)
341 if (!component_class
) {
346 component_class
->frozen
= true;