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