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/component-class-internal.h>
32 #include <babeltrace/component/source-internal.h>
33 #include <babeltrace/component/filter-internal.h>
34 #include <babeltrace/component/notification/iterator-internal.h>
35 #include <babeltrace/component/sink-internal.h>
36 #include <babeltrace/babeltrace-internal.h>
37 #include <babeltrace/compiler.h>
38 #include <babeltrace/ref.h>
41 struct bt_component
* (* const component_create_funcs
[])(
42 struct bt_component_class
*, struct bt_value
*) = {
43 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
44 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
45 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
49 enum bt_component_status (* const component_validation_funcs
[])(
50 struct bt_component
*) = {
51 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_validate
,
52 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_validate
,
53 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_validate
,
57 void bt_component_destroy(struct bt_object
*obj
)
59 struct bt_component
*component
= NULL
;
60 struct bt_component_class
*component_class
= NULL
;
66 component
= container_of(obj
, struct bt_component
, base
);
67 component_class
= component
->class;
70 * User data is destroyed first, followed by the concrete component
73 if (component
->class->methods
.destroy
) {
74 component
->class->methods
.destroy(component
);
77 if (component
->destroy
) {
78 component
->destroy(component
);
81 g_string_free(component
->name
, TRUE
);
82 bt_put(component_class
);
87 enum bt_component_status
bt_component_init(struct bt_component
*component
,
88 bt_component_class_destroy_method destroy
)
90 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
93 ret
= BT_COMPONENT_STATUS_INVALID
;
97 component
->destroy
= destroy
;
102 enum bt_component_class_type
bt_component_get_class_type(
103 struct bt_component
*component
)
105 return component
? component
->class->type
: BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
109 struct bt_notification_iterator
*bt_component_create_iterator(
110 struct bt_component
*component
)
112 enum bt_notification_iterator_status ret_iterator
;
113 enum bt_component_class_type type
;
114 struct bt_notification_iterator
*iterator
= NULL
;
115 struct bt_component_class
*class = component
->class;
121 type
= bt_component_get_class_type(component
);
122 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
123 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
124 /* Unsupported operation. */
128 iterator
= bt_notification_iterator_create(component
);
134 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
136 struct bt_component_class_source
*source_class
;
137 enum bt_component_status ret_component
;
139 source_class
= container_of(class, struct bt_component_class_source
, parent
);
140 assert(source_class
->methods
.init_iterator
);
142 source_class
->methods
.init_iterator(component
, iterator
);
143 if (ret_component
!= BT_COMPONENT_STATUS_OK
) {
150 case BT_COMPONENT_CLASS_TYPE_FILTER
:
152 struct bt_component_class_filter
*filter_class
;
153 enum bt_component_status ret_component
;
155 filter_class
= container_of(class, struct bt_component_class_filter
, parent
);
156 assert(filter_class
->methods
.init_iterator
);
158 filter_class
->methods
.init_iterator(component
, iterator
);
159 if (ret_component
!= BT_COMPONENT_STATUS_OK
) {
169 ret_iterator
= bt_notification_iterator_validate(iterator
);
170 if (ret_iterator
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
180 struct bt_component
*bt_component_create(
181 struct bt_component_class
*component_class
, const char *name
,
182 struct bt_value
*params
)
185 struct bt_component
*component
= NULL
;
186 enum bt_component_class_type type
;
188 if (!component_class
) {
193 type
= bt_component_class_get_type(component_class
);
194 if (type
<= BT_COMPONENT_CLASS_TYPE_UNKNOWN
||
195 type
> BT_COMPONENT_CLASS_TYPE_FILTER
) {
199 component
= component_create_funcs
[type
](component_class
, params
);
204 bt_object_init(component
, bt_component_destroy
);
205 component
->name
= g_string_new(name
);
206 if (!component
->name
) {
211 component
->initializing
= true;
213 if (component_class
->methods
.init
) {
214 ret
= component_class
->methods
.init(component
, params
);
215 component
->initializing
= false;
216 if (ret
!= BT_COMPONENT_STATUS_OK
) {
222 component
->initializing
= false;
223 ret
= component_validation_funcs
[type
](component
);
224 if (ret
!= BT_COMPONENT_STATUS_OK
) {
232 const char *bt_component_get_name(struct bt_component
*component
)
234 const char *ret
= NULL
;
240 ret
= component
->name
->str
;
245 enum bt_component_status
bt_component_set_name(struct bt_component
*component
,
248 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
250 if (!component
|| !name
|| name
[0] == '\0') {
251 ret
= BT_COMPONENT_STATUS_INVALID
;
255 g_string_assign(component
->name
, name
);
260 struct bt_component_class
*bt_component_get_class(
261 struct bt_component
*component
)
263 return component
? bt_get(component
->class) : NULL
;
266 void *bt_component_get_private_data(struct bt_component
*component
)
268 return component
? component
->user_data
: NULL
;
271 enum bt_component_status
272 bt_component_set_private_data(struct bt_component
*component
,
275 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
277 if (!component
|| !component
->initializing
) {
278 ret
= BT_COMPONENT_STATUS_INVALID
;
282 component
->user_data
= data
;