Refactor the component class and component API
[babeltrace.git] / lib / component / filter.c
CommitLineData
34ac9eaf
JG
1/*
2 * filter.c
3 *
4 * Babeltrace Filter Component
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29#include <babeltrace/compiler.h>
30#include <babeltrace/values.h>
33b34c43
PP
31#include <babeltrace/component/input.h>
32#include <babeltrace/component/filter-internal.h>
33#include <babeltrace/component/component-internal.h>
d3e4dcd8 34#include <babeltrace/component/component-class-internal.h>
33b34c43
PP
35#include <babeltrace/component/notification/notification.h>
36#include <babeltrace/component/notification/iterator-internal.h>
34ac9eaf 37
34a9ed19
JG
38enum bt_component_status bt_component_filter_set_minimum_input_count(
39 struct bt_component *component,
40 unsigned int minimum)
41{
42 struct bt_component_filter *filter;
43 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
44
45 if (!component) {
46 ret = BT_COMPONENT_STATUS_INVALID;
47 goto end;
48 }
49
d3e4dcd8 50 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
51 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
52 goto end;
53 }
54
55 if (!component->initializing) {
56 ret = BT_COMPONENT_STATUS_INVALID;
57 goto end;
58 }
59
60 filter = container_of(component, struct bt_component_filter, parent);
61 filter->input.min_count = minimum;
62end:
63 return ret;
64}
65
66enum bt_component_status bt_component_filter_set_maximum_input_count(
67 struct bt_component *component,
68 unsigned int maximum)
69{
70 struct bt_component_filter *filter;
71 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
72
73 if (!component) {
74 ret = BT_COMPONENT_STATUS_INVALID;
75 goto end;
76 }
77
d3e4dcd8 78 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
79 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
80 goto end;
81 }
82
83 if (!component->initializing) {
84 ret = BT_COMPONENT_STATUS_INVALID;
85 goto end;
86 }
87
88 filter = container_of(component, struct bt_component_filter, parent);
89 filter->input.max_count = maximum;
90end:
91 return ret;
92}
93
94enum bt_component_status
95bt_component_filter_get_input_count(struct bt_component *component,
96 unsigned int *count)
97{
98 struct bt_component_filter *filter;
99 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
100
101 if (!component || !count) {
102 ret = BT_COMPONENT_STATUS_INVALID;
103 goto end;
104 }
105
d3e4dcd8 106 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
107 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
108 goto end;
109 }
110
111 filter = container_of(component, struct bt_component_filter, parent);
112 *count = (unsigned int) filter->input.iterators->len;
113end:
114 return ret;
115}
116
117enum bt_component_status
118bt_component_filter_get_input_iterator(struct bt_component *component,
119 unsigned int input, struct bt_notification_iterator **iterator)
120{
121 struct bt_component_filter *filter;
122 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
123
124 if (!component || !iterator) {
125 ret = BT_COMPONENT_STATUS_INVALID;
126 goto end;
127 }
128
d3e4dcd8 129 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
130 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
131 goto end;
132 }
133
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;
137 goto end;
138 }
139
140 *iterator = bt_get(g_ptr_array_index(filter->input.iterators, input));
141end:
142 return ret;
143}
144
145enum bt_component_status bt_component_filter_add_iterator(
146 struct bt_component *component,
147 struct bt_notification_iterator *iterator)
148{
149 struct bt_component_filter *filter;
150 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
d3e4dcd8 151 struct bt_component_class_filter *filter_class;
34a9ed19
JG
152
153 if (!component || !iterator) {
154 ret = BT_COMPONENT_STATUS_INVALID;
155 goto end;
156 }
157
d3e4dcd8 158 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
159 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
160 goto end;
161 }
162
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;
166 goto end;
167 }
168
d3e4dcd8
PP
169 filter_class = container_of(component->class, struct bt_component_class_filter, parent);
170
171 if (filter_class->methods.add_iterator) {
172 ret = filter_class->methods.add_iterator(component, iterator);
34a9ed19
JG
173 if (ret != BT_COMPONENT_STATUS_OK) {
174 goto end;
175 }
176 }
177
178 g_ptr_array_add(filter->input.iterators, bt_get(iterator));
179end:
180 return ret;
181}
182
183struct bt_notification_iterator *bt_component_filter_create_iterator(
184 struct bt_component *component)
185{
5645cd95 186 return bt_component_create_iterator(component);
34a9ed19 187}
526fc31a
JG
188
189static
190void bt_component_filter_destroy(struct bt_component *component)
191{
192 struct bt_component_filter *filter = container_of(component,
193 struct bt_component_filter, parent);
194
195 component_input_fini(&filter->input);
196}
197
198BT_HIDDEN
199struct bt_component *bt_component_filter_create(
200 struct bt_component_class *class, struct bt_value *params)
201{
202 struct bt_component_filter *filter = NULL;
203 enum bt_component_status ret;
204
205 filter = g_new0(struct bt_component_filter, 1);
206 if (!filter) {
207 goto end;
208 }
209
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) {
213 goto error;
214 }
215
216 if (component_input_init(&filter->input)) {
217 goto error;
218 }
219end:
220 return filter ? &filter->parent : NULL;
221error:
222 BT_PUT(filter);
223 goto end;
224}
225
226BT_HIDDEN
227enum bt_component_status bt_component_filter_validate(
228 struct bt_component *component)
229{
230 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
231 struct bt_component_filter *filter;
232
233 if (!component) {
234 ret = BT_COMPONENT_STATUS_INVALID;
235 goto end;
236 }
237
238 if (!component->class) {
239 ret = BT_COMPONENT_STATUS_INVALID;
240 goto end;
241 }
242
d3e4dcd8 243 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
526fc31a
JG
244 ret = BT_COMPONENT_STATUS_INVALID;
245 goto end;
246 }
247
248 filter = container_of(component, struct bt_component_filter, parent);
526fc31a
JG
249 ret = component_input_validate(&filter->input);
250 if (ret) {
251 goto end;
252 }
253end:
254 return ret;
255}
This page took 0.034413 seconds and 4 git commands to generate.