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