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> |
33b34c43 | 31 | #include <babeltrace/component/component.h> |
d71dcf2c | 32 | #include <babeltrace/component/component-source-internal.h> |
d3eb6e8f | 33 | #include <babeltrace/component/component-class-internal.h> |
33b34c43 PP |
34 | #include <babeltrace/component/notification/iterator.h> |
35 | #include <babeltrace/component/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 | ||
57 | if (source_class->methods.iterator.destroy) { | |
58 | source_class->methods.iterator.destroy(iterator); | |
59 | } | |
60 | break; | |
512ccb99 | 61 | } |
d3eb6e8f PP |
62 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
63 | { | |
64 | struct bt_component_class_filter *filter_class; | |
65 | ||
66 | filter_class = container_of(comp_class, struct bt_component_class_filter, parent); | |
67 | ||
68 | if (filter_class->methods.iterator.destroy) { | |
69 | filter_class->methods.iterator.destroy(iterator); | |
70 | } | |
71 | break; | |
72 | } | |
73 | default: | |
74 | /* Unreachable */ | |
75 | assert(0); | |
76 | } | |
77 | ||
413bc2c4 | 78 | BT_PUT(iterator->component); |
8738a040 | 79 | g_free(iterator); |
47e5a032 JG |
80 | } |
81 | ||
82 | BT_HIDDEN | |
83 | struct bt_notification_iterator *bt_notification_iterator_create( | |
84 | struct bt_component *component) | |
85 | { | |
d3e4dcd8 | 86 | enum bt_component_class_type type; |
47e5a032 JG |
87 | struct bt_notification_iterator *iterator = NULL; |
88 | ||
16b7b023 JG |
89 | if (!component) { |
90 | goto end; | |
91 | } | |
92 | ||
d3e4dcd8 | 93 | type = bt_component_get_class_type(component); |
16b7b023 | 94 | switch (type) { |
d3e4dcd8 PP |
95 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
96 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
16b7b023 JG |
97 | break; |
98 | default: | |
47e5a032 JG |
99 | goto end; |
100 | } | |
101 | ||
102 | iterator = g_new0(struct bt_notification_iterator, 1); | |
103 | if (!iterator) { | |
104 | goto end; | |
105 | } | |
106 | ||
413bc2c4 | 107 | iterator->component = bt_get(component); |
b8a06801 | 108 | bt_object_init(iterator, bt_notification_iterator_destroy); |
47e5a032 JG |
109 | end: |
110 | return iterator; | |
111 | } | |
112 | ||
113 | BT_HIDDEN | |
114 | enum bt_notification_iterator_status bt_notification_iterator_validate( | |
115 | struct bt_notification_iterator *iterator) | |
116 | { | |
8738a040 | 117 | enum bt_notification_iterator_status ret = |
ea8d3e58 | 118 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
47e5a032 | 119 | |
d3eb6e8f | 120 | if (!iterator) { |
ea8d3e58 JG |
121 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
122 | goto end; | |
123 | } | |
ea8d3e58 JG |
124 | end: |
125 | return ret; | |
126 | } | |
127 | ||
128 | void *bt_notification_iterator_get_private_data( | |
129 | struct bt_notification_iterator *iterator) | |
130 | { | |
131 | return iterator ? iterator->user_data : NULL; | |
132 | } | |
133 | ||
134 | enum bt_notification_iterator_status | |
135 | bt_notification_iterator_set_private_data( | |
136 | struct bt_notification_iterator *iterator, void *data) | |
137 | { | |
138 | enum bt_notification_iterator_status ret = | |
139 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
140 | ||
d3eb6e8f | 141 | if (!iterator) { |
ea8d3e58 JG |
142 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
143 | goto end; | |
144 | } | |
145 | ||
146 | iterator->user_data = data; | |
8738a040 JG |
147 | end: |
148 | return ret; | |
149 | } | |
413bc2c4 | 150 | |
53d45b87 JG |
151 | struct bt_notification *bt_notification_iterator_get_notification( |
152 | struct bt_notification_iterator *iterator) | |
153 | { | |
d3eb6e8f PP |
154 | bt_component_class_notification_iterator_get_method get_method = NULL; |
155 | ||
53d45b87 | 156 | assert(iterator); |
d3eb6e8f PP |
157 | assert(iterator->component); |
158 | assert(iterator->component->class); | |
159 | ||
160 | switch (iterator->component->class->type) { | |
161 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
162 | { | |
163 | struct bt_component_class_source *source_class = | |
164 | container_of(iterator->component->class, | |
165 | struct bt_component_class_source, parent); | |
166 | ||
167 | assert(source_class->methods.iterator.get); | |
168 | get_method = source_class->methods.iterator.get; | |
169 | break; | |
170 | } | |
171 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
172 | { | |
173 | struct bt_component_class_filter *filter_class = | |
174 | container_of(iterator->component->class, | |
175 | struct bt_component_class_filter, parent); | |
176 | ||
177 | assert(filter_class->methods.iterator.get); | |
178 | get_method = filter_class->methods.iterator.get; | |
179 | break; | |
180 | } | |
181 | default: | |
182 | assert(false); | |
183 | break; | |
184 | } | |
185 | ||
186 | assert(get_method); | |
187 | return get_method(iterator); | |
53d45b87 JG |
188 | } |
189 | ||
190 | enum bt_notification_iterator_status | |
191 | bt_notification_iterator_next(struct bt_notification_iterator *iterator) | |
192 | { | |
d3eb6e8f PP |
193 | bt_component_class_notification_iterator_next_method next_method = NULL; |
194 | ||
53d45b87 | 195 | assert(iterator); |
d3eb6e8f PP |
196 | assert(iterator->component); |
197 | assert(iterator->component->class); | |
198 | ||
199 | switch (iterator->component->class->type) { | |
200 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
201 | { | |
202 | struct bt_component_class_source *source_class = | |
203 | container_of(iterator->component->class, | |
204 | struct bt_component_class_source, parent); | |
205 | ||
206 | assert(source_class->methods.iterator.next); | |
207 | next_method = source_class->methods.iterator.next; | |
208 | break; | |
209 | } | |
210 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
211 | { | |
212 | struct bt_component_class_filter *filter_class = | |
213 | container_of(iterator->component->class, | |
214 | struct bt_component_class_filter, parent); | |
215 | ||
216 | assert(filter_class->methods.iterator.next); | |
217 | next_method = filter_class->methods.iterator.next; | |
218 | break; | |
219 | } | |
220 | default: | |
221 | assert(false); | |
222 | break; | |
223 | } | |
224 | ||
225 | assert(next_method); | |
226 | return next_method(iterator); | |
53d45b87 JG |
227 | } |
228 | ||
413bc2c4 JG |
229 | struct bt_component *bt_notification_iterator_get_component( |
230 | struct bt_notification_iterator *iterator) | |
231 | { | |
232 | return bt_get(iterator->component); | |
233 | } | |
234 | ||
9531634f JG |
235 | enum bt_notification_iterator_status bt_notification_iterator_seek_time( |
236 | struct bt_notification_iterator *iterator, | |
237 | enum bt_notification_iterator_seek_origin seek_origin, | |
238 | int64_t time) | |
239 | { | |
b7726e32 MD |
240 | enum bt_notification_iterator_status ret = |
241 | BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED; | |
9531634f JG |
242 | return ret; |
243 | } |