4 * Babeltrace Plugin Component
6 * Copyright 2015 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/component/component.h>
30 #include <babeltrace/component/component-internal.h>
31 #include <babeltrace/component/source-internal.h>
32 #include <babeltrace/component/filter-internal.h>
33 #include <babeltrace/component/notification/iterator-internal.h>
34 #include <babeltrace/component/sink-internal.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/compiler.h>
37 #include <babeltrace/ref.h>
40 struct bt_component
* (* const component_create_funcs
[])(
41 struct bt_component_class
*, struct bt_value
*) = {
42 [BT_COMPONENT_TYPE_SOURCE
] = bt_component_source_create
,
43 [BT_COMPONENT_TYPE_SINK
] = bt_component_sink_create
,
44 [BT_COMPONENT_TYPE_FILTER
] = bt_component_filter_create
,
48 enum bt_component_status (* const component_validation_funcs
[])(
49 struct bt_component
*) = {
50 [BT_COMPONENT_TYPE_SOURCE
] = bt_component_source_validate
,
51 [BT_COMPONENT_TYPE_SINK
] = bt_component_sink_validate
,
52 [BT_COMPONENT_TYPE_FILTER
] = bt_component_filter_validate
,
56 void bt_component_destroy(struct bt_object
*obj
)
58 struct bt_component
*component
= NULL
;
59 struct bt_component_class
*component_class
= NULL
;
65 component
= container_of(obj
, struct bt_component
, base
);
66 component_class
= component
->class;
69 * User data is destroyed first, followed by the concrete component
72 if (component
->user_destroy
) {
73 component
->user_destroy(component
);
76 if (component
->destroy
) {
77 component
->destroy(component
);
80 g_string_free(component
->name
, TRUE
);
81 bt_put(component_class
);
86 enum bt_component_status
bt_component_init(struct bt_component
*component
,
87 bt_component_destroy_cb destroy
)
89 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
92 ret
= BT_COMPONENT_STATUS_INVALID
;
96 component
->destroy
= destroy
;
102 enum bt_component_type
bt_component_get_type(struct bt_component
*component
)
104 return component
? component
->class->type
: BT_COMPONENT_TYPE_UNKNOWN
;
108 struct bt_notification_iterator
*bt_component_create_iterator(
109 struct bt_component
*component
)
111 enum bt_notification_iterator_status ret_iterator
;
112 enum bt_component_type component_type
;
113 struct bt_notification_iterator
*iterator
= NULL
;
119 component_type
= bt_component_get_type(component
);
120 if (component_type
!= BT_COMPONENT_TYPE_SOURCE
&&
121 component_type
!= BT_COMPONENT_TYPE_FILTER
) {
122 /* Unsupported operation. */
126 iterator
= bt_notification_iterator_create(component
);
131 switch (component_type
) {
132 case BT_COMPONENT_TYPE_SOURCE
:
134 struct bt_component_source
*source
;
135 enum bt_component_status ret_component
;
137 source
= container_of(component
, struct bt_component_source
, parent
);
138 assert(source
->init_iterator
);
139 ret_component
= source
->init_iterator(component
, iterator
);
140 if (ret_component
!= BT_COMPONENT_STATUS_OK
) {
147 case BT_COMPONENT_TYPE_FILTER
:
149 struct bt_component_filter
*filter
;
150 enum bt_component_status ret_component
;
152 filter
= container_of(component
, struct bt_component_filter
, parent
);
153 assert(filter
->init_iterator
);
154 ret_component
= filter
->init_iterator(component
, iterator
);
155 if (ret_component
!= BT_COMPONENT_STATUS_OK
) {
165 ret_iterator
= bt_notification_iterator_validate(iterator
);
166 if (ret_iterator
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
176 struct bt_component
*bt_component_create(
177 struct bt_component_class
*component_class
, const char *name
,
178 struct bt_value
*params
)
181 struct bt_component
*component
= NULL
;
182 enum bt_component_type type
;
184 if (!component_class
) {
188 type
= bt_component_class_get_type(component_class
);
189 if (type
<= BT_COMPONENT_TYPE_UNKNOWN
||
190 type
> BT_COMPONENT_TYPE_FILTER
) {
194 component
= component_create_funcs
[type
](component_class
, params
);
199 bt_object_init(component
, bt_component_destroy
);
200 component
->name
= g_string_new(name
);
201 if (!component
->name
) {
206 component
->initializing
= true;
207 ret
= component_class
->init(component
, params
);
208 component
->initializing
= false;
209 if (ret
!= BT_COMPONENT_STATUS_OK
) {
213 ret
= component_validation_funcs
[type
](component
);
214 if (ret
!= BT_COMPONENT_STATUS_OK
) {
222 const char *bt_component_get_name(struct bt_component
*component
)
224 const char *ret
= NULL
;
230 ret
= component
->name
->str
;
235 enum bt_component_status
bt_component_set_name(struct bt_component
*component
,
238 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
240 if (!component
|| !name
|| name
[0] == '\0') {
241 ret
= BT_COMPONENT_STATUS_INVALID
;
245 g_string_assign(component
->name
, name
);
250 struct bt_component_class
*bt_component_get_class(
251 struct bt_component
*component
)
253 return component
? bt_get(component
->class) : NULL
;
256 void *bt_component_get_private_data(struct bt_component
*component
)
258 return component
? component
->user_data
: NULL
;
261 enum bt_component_status
262 bt_component_set_private_data(struct bt_component
*component
,
265 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
267 if (!component
|| !component
->initializing
) {
268 ret
= BT_COMPONENT_STATUS_INVALID
;
272 component
->user_data
= data
;
277 enum bt_component_status
bt_component_set_destroy_cb(
278 struct bt_component
*component
, bt_component_destroy_cb destroy
)
280 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
282 if (!component
|| !component
->initializing
) {
283 ret
= BT_COMPONENT_STATUS_INVALID
;
287 component
->user_destroy
= destroy
;