Commit | Line | Data |
---|---|---|
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 | ||
29 | #include <babeltrace/compiler.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 | |
38 | static | |
b8a06801 | 39 | void 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 | ||
86 | BT_HIDDEN | |
87 | struct 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 |
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 | { | |
8738a040 | 121 | enum bt_notification_iterator_status ret = |
ea8d3e58 | 122 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
47e5a032 | 123 | |
d3eb6e8f | 124 | if (!iterator) { |
ea8d3e58 JG |
125 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
126 | goto end; | |
127 | } | |
ea8d3e58 JG |
128 | end: |
129 | return ret; | |
130 | } | |
131 | ||
890882ef PP |
132 | void *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 | ||
141 | enum bt_notification_iterator_status | |
890882ef PP |
142 | bt_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) { |
ea8d3e58 JG |
152 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
153 | goto end; | |
154 | } | |
155 | ||
156 | iterator->user_data = data; | |
8738a040 JG |
157 | end: |
158 | return ret; | |
159 | } | |
413bc2c4 | 160 | |
53d45b87 JG |
161 | struct 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 |
172 | end: |
173 | return notification; | |
53d45b87 JG |
174 | } |
175 | ||
176 | enum bt_notification_iterator_status | |
177 | bt_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; |
41a2b7ae PP |
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 | } | |
d3eb6e8f | 190 | |
d3eb6e8f PP |
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); | |
41a2b7ae PP |
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); | |
e7fa96c3 | 230 | bt_notification_freeze(iterator->current_notification); |
41a2b7ae PP |
231 | } |
232 | ||
233 | end: | |
234 | return next_return.status; | |
53d45b87 JG |
235 | } |
236 | ||
413bc2c4 JG |
237 | struct bt_component *bt_notification_iterator_get_component( |
238 | struct bt_notification_iterator *iterator) | |
239 | { | |
240 | return bt_get(iterator->component); | |
241 | } | |
242 | ||
91457551 PP |
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 | ||
9531634f JG |
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 | { | |
b7726e32 MD |
257 | enum bt_notification_iterator_status ret = |
258 | BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED; | |
9531634f JG |
259 | return ret; |
260 | } |