"destroy" method -> "finalize" method
[babeltrace.git] / lib / component / 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
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
37static
b8a06801 38void 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
413bc2c4 80 BT_PUT(iterator->component);
8738a040 81 g_free(iterator);
47e5a032
JG
82}
83
84BT_HIDDEN
85struct bt_notification_iterator *bt_notification_iterator_create(
86 struct bt_component *component)
87{
d3e4dcd8 88 enum bt_component_class_type type;
47e5a032
JG
89 struct bt_notification_iterator *iterator = NULL;
90
16b7b023
JG
91 if (!component) {
92 goto end;
93 }
94
d3e4dcd8 95 type = bt_component_get_class_type(component);
16b7b023 96 switch (type) {
d3e4dcd8
PP
97 case BT_COMPONENT_CLASS_TYPE_SOURCE:
98 case BT_COMPONENT_CLASS_TYPE_FILTER:
16b7b023
JG
99 break;
100 default:
47e5a032
JG
101 goto end;
102 }
103
104 iterator = g_new0(struct bt_notification_iterator, 1);
105 if (!iterator) {
106 goto end;
107 }
108
413bc2c4 109 iterator->component = bt_get(component);
b8a06801 110 bt_object_init(iterator, bt_notification_iterator_destroy);
47e5a032
JG
111end:
112 return iterator;
113}
114
115BT_HIDDEN
116enum bt_notification_iterator_status bt_notification_iterator_validate(
117 struct bt_notification_iterator *iterator)
118{
8738a040 119 enum bt_notification_iterator_status ret =
ea8d3e58 120 BT_NOTIFICATION_ITERATOR_STATUS_OK;
47e5a032 121
d3eb6e8f 122 if (!iterator) {
ea8d3e58
JG
123 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
124 goto end;
125 }
ea8d3e58
JG
126end:
127 return ret;
128}
129
890882ef
PP
130void *bt_private_notification_iterator_get_user_data(
131 struct bt_private_notification_iterator *private_iterator)
ea8d3e58 132{
890882ef
PP
133 struct bt_notification_iterator *iterator =
134 bt_notification_iterator_from_private(private_iterator);
135
ea8d3e58
JG
136 return iterator ? iterator->user_data : NULL;
137}
138
139enum bt_notification_iterator_status
890882ef
PP
140bt_private_notification_iterator_set_user_data(
141 struct bt_private_notification_iterator *private_iterator,
142 void *data)
ea8d3e58
JG
143{
144 enum bt_notification_iterator_status ret =
145 BT_NOTIFICATION_ITERATOR_STATUS_OK;
890882ef
PP
146 struct bt_notification_iterator *iterator =
147 bt_notification_iterator_from_private(private_iterator);
ea8d3e58 148
d3eb6e8f 149 if (!iterator) {
ea8d3e58
JG
150 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
151 goto end;
152 }
153
154 iterator->user_data = data;
8738a040
JG
155end:
156 return ret;
157}
413bc2c4 158
53d45b87
JG
159struct bt_notification *bt_notification_iterator_get_notification(
160 struct bt_notification_iterator *iterator)
161{
890882ef
PP
162 struct bt_private_notification_iterator *priv_iterator =
163 bt_private_notification_iterator_from_notification_iterator(iterator);
d3eb6e8f
PP
164 bt_component_class_notification_iterator_get_method get_method = NULL;
165
53d45b87 166 assert(iterator);
d3eb6e8f
PP
167 assert(iterator->component);
168 assert(iterator->component->class);
169
170 switch (iterator->component->class->type) {
171 case BT_COMPONENT_CLASS_TYPE_SOURCE:
172 {
173 struct bt_component_class_source *source_class =
174 container_of(iterator->component->class,
175 struct bt_component_class_source, parent);
176
177 assert(source_class->methods.iterator.get);
178 get_method = source_class->methods.iterator.get;
179 break;
180 }
181 case BT_COMPONENT_CLASS_TYPE_FILTER:
182 {
183 struct bt_component_class_filter *filter_class =
184 container_of(iterator->component->class,
185 struct bt_component_class_filter, parent);
186
187 assert(filter_class->methods.iterator.get);
188 get_method = filter_class->methods.iterator.get;
189 break;
190 }
191 default:
192 assert(false);
193 break;
194 }
195
196 assert(get_method);
890882ef 197 return get_method(priv_iterator);
53d45b87
JG
198}
199
200enum bt_notification_iterator_status
201bt_notification_iterator_next(struct bt_notification_iterator *iterator)
202{
890882ef
PP
203 struct bt_private_notification_iterator *priv_iterator =
204 bt_private_notification_iterator_from_notification_iterator(iterator);
d3eb6e8f
PP
205 bt_component_class_notification_iterator_next_method next_method = NULL;
206
53d45b87 207 assert(iterator);
d3eb6e8f
PP
208 assert(iterator->component);
209 assert(iterator->component->class);
210
211 switch (iterator->component->class->type) {
212 case BT_COMPONENT_CLASS_TYPE_SOURCE:
213 {
214 struct bt_component_class_source *source_class =
215 container_of(iterator->component->class,
216 struct bt_component_class_source, parent);
217
218 assert(source_class->methods.iterator.next);
219 next_method = source_class->methods.iterator.next;
220 break;
221 }
222 case BT_COMPONENT_CLASS_TYPE_FILTER:
223 {
224 struct bt_component_class_filter *filter_class =
225 container_of(iterator->component->class,
226 struct bt_component_class_filter, parent);
227
228 assert(filter_class->methods.iterator.next);
229 next_method = filter_class->methods.iterator.next;
230 break;
231 }
232 default:
233 assert(false);
234 break;
235 }
236
237 assert(next_method);
890882ef 238 return next_method(priv_iterator);
53d45b87
JG
239}
240
413bc2c4
JG
241struct bt_component *bt_notification_iterator_get_component(
242 struct bt_notification_iterator *iterator)
243{
244 return bt_get(iterator->component);
245}
246
9531634f
JG
247enum bt_notification_iterator_status bt_notification_iterator_seek_time(
248 struct bt_notification_iterator *iterator,
249 enum bt_notification_iterator_seek_origin seek_origin,
250 int64_t time)
251{
b7726e32
MD
252 enum bt_notification_iterator_status ret =
253 BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
9531634f
JG
254 return ret;
255}
This page took 0.038128 seconds and 4 git commands to generate.