Rename *create_iterator*() -> *create_notification_iterator*()
[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>
262de837 31#include <babeltrace/component/component-input-internal.h>
d71dcf2c 32#include <babeltrace/component/component-filter-internal.h>
33b34c43 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
c725abdd 183struct bt_notification_iterator *bt_component_filter_create_notification_iterator(
34a9ed19
JG
184 struct bt_component *component)
185{
8b0ce102
PP
186 return bt_component_create_iterator(component, NULL);
187}
188
c725abdd 189struct bt_notification_iterator *bt_component_filter_create_notification_iterator_with_init_method_data(
8b0ce102
PP
190 struct bt_component *component, void *init_method_data)
191{
192 return bt_component_create_iterator(component, init_method_data);
34a9ed19 193}
526fc31a
JG
194
195static
196void bt_component_filter_destroy(struct bt_component *component)
197{
198 struct bt_component_filter *filter = container_of(component,
199 struct bt_component_filter, parent);
200
201 component_input_fini(&filter->input);
202}
203
204BT_HIDDEN
205struct bt_component *bt_component_filter_create(
206 struct bt_component_class *class, struct bt_value *params)
207{
208 struct bt_component_filter *filter = NULL;
209 enum bt_component_status ret;
210
211 filter = g_new0(struct bt_component_filter, 1);
212 if (!filter) {
213 goto end;
214 }
215
216 filter->parent.class = bt_get(class);
217 ret = bt_component_init(&filter->parent, bt_component_filter_destroy);
218 if (ret != BT_COMPONENT_STATUS_OK) {
219 goto error;
220 }
221
222 if (component_input_init(&filter->input)) {
223 goto error;
224 }
225end:
226 return filter ? &filter->parent : NULL;
227error:
228 BT_PUT(filter);
229 goto end;
230}
231
232BT_HIDDEN
233enum bt_component_status bt_component_filter_validate(
234 struct bt_component *component)
235{
236 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
237 struct bt_component_filter *filter;
238
239 if (!component) {
240 ret = BT_COMPONENT_STATUS_INVALID;
241 goto end;
242 }
243
244 if (!component->class) {
245 ret = BT_COMPONENT_STATUS_INVALID;
246 goto end;
247 }
248
d3e4dcd8 249 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
526fc31a
JG
250 ret = BT_COMPONENT_STATUS_INVALID;
251 goto end;
252 }
253
254 filter = container_of(component, struct bt_component_filter, parent);
526fc31a
JG
255 ret = component_input_validate(&filter->input);
256 if (ret) {
257 goto end;
258 }
259end:
260 return ret;
261}
This page took 0.034735 seconds and 4 git commands to generate.