Add `-internal` suffix to all internal header files
[babeltrace.git] / lib / component / iterator.c
1 /*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 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-internal.h>
30 #include <babeltrace/ref.h>
31 #include <babeltrace/graph/component.h>
32 #include <babeltrace/graph/component-source-internal.h>
33 #include <babeltrace/graph/component-class-internal.h>
34 #include <babeltrace/graph/notification-iterator.h>
35 #include <babeltrace/graph/notification-iterator-internal.h>
36 #include <babeltrace/graph/notification-internal.h>
37
38 static
39 void bt_notification_iterator_destroy(struct bt_object *obj)
40 {
41 struct bt_notification_iterator *iterator;
42 struct bt_component_class *comp_class;
43
44 assert(obj);
45 iterator = container_of(obj, struct bt_notification_iterator,
46 base);
47 assert(iterator->component);
48 comp_class = iterator->component->class;
49
50 /* Call user-defined destroy method */
51 switch (comp_class->type) {
52 case BT_COMPONENT_CLASS_TYPE_SOURCE:
53 {
54 struct bt_component_class_source *source_class;
55
56 source_class = container_of(comp_class, struct bt_component_class_source, parent);
57
58 if (source_class->methods.iterator.finalize) {
59 source_class->methods.iterator.finalize(
60 bt_private_notification_iterator_from_notification_iterator(iterator));
61 }
62 break;
63 }
64 case BT_COMPONENT_CLASS_TYPE_FILTER:
65 {
66 struct bt_component_class_filter *filter_class;
67
68 filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
69
70 if (filter_class->methods.iterator.finalize) {
71 filter_class->methods.iterator.finalize(
72 bt_private_notification_iterator_from_notification_iterator(iterator));
73 }
74 break;
75 }
76 default:
77 /* Unreachable */
78 assert(0);
79 }
80
81 BT_PUT(iterator->current_notification);
82 BT_PUT(iterator->component);
83 g_free(iterator);
84 }
85
86 BT_HIDDEN
87 struct bt_notification_iterator *bt_notification_iterator_create(
88 struct bt_component *component)
89 {
90 enum bt_component_class_type type;
91 struct bt_notification_iterator *iterator = NULL;
92
93 if (!component) {
94 goto end;
95 }
96
97 type = bt_component_get_class_type(component);
98 switch (type) {
99 case BT_COMPONENT_CLASS_TYPE_SOURCE:
100 case BT_COMPONENT_CLASS_TYPE_FILTER:
101 break;
102 default:
103 goto end;
104 }
105
106 iterator = g_new0(struct bt_notification_iterator, 1);
107 if (!iterator) {
108 goto end;
109 }
110
111 iterator->component = bt_get(component);
112 bt_object_init(iterator, bt_notification_iterator_destroy);
113 end:
114 return iterator;
115 }
116
117 BT_HIDDEN
118 enum bt_notification_iterator_status bt_notification_iterator_validate(
119 struct bt_notification_iterator *iterator)
120 {
121 enum bt_notification_iterator_status ret =
122 BT_NOTIFICATION_ITERATOR_STATUS_OK;
123
124 if (!iterator) {
125 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
126 goto end;
127 }
128 end:
129 return ret;
130 }
131
132 void *bt_private_notification_iterator_get_user_data(
133 struct bt_private_notification_iterator *private_iterator)
134 {
135 struct bt_notification_iterator *iterator =
136 bt_notification_iterator_from_private(private_iterator);
137
138 return iterator ? iterator->user_data : NULL;
139 }
140
141 enum bt_notification_iterator_status
142 bt_private_notification_iterator_set_user_data(
143 struct bt_private_notification_iterator *private_iterator,
144 void *data)
145 {
146 enum bt_notification_iterator_status ret =
147 BT_NOTIFICATION_ITERATOR_STATUS_OK;
148 struct bt_notification_iterator *iterator =
149 bt_notification_iterator_from_private(private_iterator);
150
151 if (!iterator) {
152 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
153 goto end;
154 }
155
156 iterator->user_data = data;
157 end:
158 return ret;
159 }
160
161 struct bt_notification *bt_notification_iterator_get_notification(
162 struct bt_notification_iterator *iterator)
163 {
164 struct bt_notification *notification = NULL;
165
166 if (!iterator) {
167 goto end;
168 }
169
170 notification = bt_get(iterator->current_notification);
171
172 end:
173 return notification;
174 }
175
176 enum bt_notification_iterator_status
177 bt_notification_iterator_next(struct bt_notification_iterator *iterator)
178 {
179 struct bt_private_notification_iterator *priv_iterator =
180 bt_private_notification_iterator_from_notification_iterator(iterator);
181 bt_component_class_notification_iterator_next_method next_method = NULL;
182 struct bt_notification_iterator_next_return next_return;
183 enum bt_notification_iterator_status status =
184 BT_NOTIFICATION_ITERATOR_STATUS_OK;
185
186 if (!iterator) {
187 status = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
188 goto end;
189 }
190
191 assert(iterator->component);
192 assert(iterator->component->class);
193
194 switch (iterator->component->class->type) {
195 case BT_COMPONENT_CLASS_TYPE_SOURCE:
196 {
197 struct bt_component_class_source *source_class =
198 container_of(iterator->component->class,
199 struct bt_component_class_source, parent);
200
201 assert(source_class->methods.iterator.next);
202 next_method = source_class->methods.iterator.next;
203 break;
204 }
205 case BT_COMPONENT_CLASS_TYPE_FILTER:
206 {
207 struct bt_component_class_filter *filter_class =
208 container_of(iterator->component->class,
209 struct bt_component_class_filter, parent);
210
211 assert(filter_class->methods.iterator.next);
212 next_method = filter_class->methods.iterator.next;
213 break;
214 }
215 default:
216 assert(false);
217 break;
218 }
219
220 assert(next_method);
221 next_return = next_method(priv_iterator);
222 if (next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
223 if (!next_return.notification) {
224 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
225 goto end;
226 }
227
228 BT_MOVE(iterator->current_notification,
229 next_return.notification);
230 bt_notification_freeze(iterator->current_notification);
231 }
232
233 end:
234 return next_return.status;
235 }
236
237 struct bt_component *bt_notification_iterator_get_component(
238 struct bt_notification_iterator *iterator)
239 {
240 return bt_get(iterator->component);
241 }
242
243 struct bt_private_component *
244 bt_private_notification_iterator_get_private_component(
245 struct bt_private_notification_iterator *private_iterator)
246 {
247 return bt_private_component_from_component(
248 bt_notification_iterator_get_component(
249 bt_notification_iterator_from_private(private_iterator)));
250 }
251
252 enum bt_notification_iterator_status bt_notification_iterator_seek_time(
253 struct bt_notification_iterator *iterator,
254 enum bt_notification_iterator_seek_origin seek_origin,
255 int64_t time)
256 {
257 enum bt_notification_iterator_status ret =
258 BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
259 return ret;
260 }
This page took 0.034933 seconds and 4 git commands to generate.