4 * Babeltrace Filter Component
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/values.h>
31 #include <babeltrace/component/input.h>
32 #include <babeltrace/component/component-filter-internal.h>
33 #include <babeltrace/component/component-internal.h>
34 #include <babeltrace/component/component-class-internal.h>
35 #include <babeltrace/component/notification/notification.h>
36 #include <babeltrace/component/notification/iterator-internal.h>
38 enum bt_component_status
bt_component_filter_set_minimum_input_count(
39 struct bt_component
*component
,
42 struct bt_component_filter
*filter
;
43 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
46 ret
= BT_COMPONENT_STATUS_INVALID
;
50 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_FILTER
) {
51 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
55 if (!component
->initializing
) {
56 ret
= BT_COMPONENT_STATUS_INVALID
;
60 filter
= container_of(component
, struct bt_component_filter
, parent
);
61 filter
->input
.min_count
= minimum
;
66 enum bt_component_status
bt_component_filter_set_maximum_input_count(
67 struct bt_component
*component
,
70 struct bt_component_filter
*filter
;
71 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
74 ret
= BT_COMPONENT_STATUS_INVALID
;
78 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_FILTER
) {
79 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
83 if (!component
->initializing
) {
84 ret
= BT_COMPONENT_STATUS_INVALID
;
88 filter
= container_of(component
, struct bt_component_filter
, parent
);
89 filter
->input
.max_count
= maximum
;
94 enum bt_component_status
95 bt_component_filter_get_input_count(struct bt_component
*component
,
98 struct bt_component_filter
*filter
;
99 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
101 if (!component
|| !count
) {
102 ret
= BT_COMPONENT_STATUS_INVALID
;
106 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_FILTER
) {
107 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
111 filter
= container_of(component
, struct bt_component_filter
, parent
);
112 *count
= (unsigned int) filter
->input
.iterators
->len
;
117 enum bt_component_status
118 bt_component_filter_get_input_iterator(struct bt_component
*component
,
119 unsigned int input
, struct bt_notification_iterator
**iterator
)
121 struct bt_component_filter
*filter
;
122 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
124 if (!component
|| !iterator
) {
125 ret
= BT_COMPONENT_STATUS_INVALID
;
129 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_FILTER
) {
130 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
134 filter
= container_of(component
, struct bt_component_filter
, parent
);
135 if (input
>= (unsigned int) filter
->input
.iterators
->len
) {
136 ret
= BT_COMPONENT_STATUS_INVALID
;
140 *iterator
= bt_get(g_ptr_array_index(filter
->input
.iterators
, input
));
145 enum bt_component_status
bt_component_filter_add_iterator(
146 struct bt_component
*component
,
147 struct bt_notification_iterator
*iterator
)
149 struct bt_component_filter
*filter
;
150 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
151 struct bt_component_class_filter
*filter_class
;
153 if (!component
|| !iterator
) {
154 ret
= BT_COMPONENT_STATUS_INVALID
;
158 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_FILTER
) {
159 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
163 filter
= container_of(component
, struct bt_component_filter
, parent
);
164 if (filter
->input
.iterators
->len
== filter
->input
.max_count
) {
165 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
169 filter_class
= container_of(component
->class, struct bt_component_class_filter
, parent
);
171 if (filter_class
->methods
.add_iterator
) {
172 ret
= filter_class
->methods
.add_iterator(component
, iterator
);
173 if (ret
!= BT_COMPONENT_STATUS_OK
) {
178 g_ptr_array_add(filter
->input
.iterators
, bt_get(iterator
));
183 struct bt_notification_iterator
*bt_component_filter_create_iterator(
184 struct bt_component
*component
)
186 return bt_component_create_iterator(component
);
190 void bt_component_filter_destroy(struct bt_component
*component
)
192 struct bt_component_filter
*filter
= container_of(component
,
193 struct bt_component_filter
, parent
);
195 component_input_fini(&filter
->input
);
199 struct bt_component
*bt_component_filter_create(
200 struct bt_component_class
*class, struct bt_value
*params
)
202 struct bt_component_filter
*filter
= NULL
;
203 enum bt_component_status ret
;
205 filter
= g_new0(struct bt_component_filter
, 1);
210 filter
->parent
.class = bt_get(class);
211 ret
= bt_component_init(&filter
->parent
, bt_component_filter_destroy
);
212 if (ret
!= BT_COMPONENT_STATUS_OK
) {
216 if (component_input_init(&filter
->input
)) {
220 return filter
? &filter
->parent
: NULL
;
227 enum bt_component_status
bt_component_filter_validate(
228 struct bt_component
*component
)
230 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
231 struct bt_component_filter
*filter
;
234 ret
= BT_COMPONENT_STATUS_INVALID
;
238 if (!component
->class) {
239 ret
= BT_COMPONENT_STATUS_INVALID
;
243 if (component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
244 ret
= BT_COMPONENT_STATUS_INVALID
;
248 filter
= container_of(component
, struct bt_component_filter
, parent
);
249 ret
= component_input_validate(&filter
->input
);