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> |
33b34c43 PP |
33 | #include <babeltrace/component/notification/iterator.h> |
34 | #include <babeltrace/component/notification/iterator-internal.h> | |
47e5a032 JG |
35 | |
36 | static | |
b8a06801 | 37 | void bt_notification_iterator_destroy(struct bt_object *obj) |
47e5a032 | 38 | { |
8738a040 JG |
39 | struct bt_notification_iterator *iterator; |
40 | ||
b8a06801 JG |
41 | assert(obj); |
42 | iterator = container_of(obj, struct bt_notification_iterator, | |
43 | base); | |
8738a040 | 44 | assert(iterator->user_destroy || !iterator->user_data); |
512ccb99 JG |
45 | if (iterator->user_destroy) { |
46 | iterator->user_destroy(iterator); | |
47 | } | |
413bc2c4 | 48 | BT_PUT(iterator->component); |
8738a040 | 49 | g_free(iterator); |
47e5a032 JG |
50 | } |
51 | ||
52 | BT_HIDDEN | |
53 | struct bt_notification_iterator *bt_notification_iterator_create( | |
54 | struct bt_component *component) | |
55 | { | |
d3e4dcd8 | 56 | enum bt_component_class_type type; |
47e5a032 JG |
57 | struct bt_notification_iterator *iterator = NULL; |
58 | ||
16b7b023 JG |
59 | if (!component) { |
60 | goto end; | |
61 | } | |
62 | ||
d3e4dcd8 | 63 | type = bt_component_get_class_type(component); |
16b7b023 | 64 | switch (type) { |
d3e4dcd8 PP |
65 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
66 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
16b7b023 JG |
67 | break; |
68 | default: | |
47e5a032 JG |
69 | goto end; |
70 | } | |
71 | ||
72 | iterator = g_new0(struct bt_notification_iterator, 1); | |
73 | if (!iterator) { | |
74 | goto end; | |
75 | } | |
76 | ||
413bc2c4 | 77 | iterator->component = bt_get(component); |
b8a06801 | 78 | bt_object_init(iterator, bt_notification_iterator_destroy); |
47e5a032 JG |
79 | end: |
80 | return iterator; | |
81 | } | |
82 | ||
83 | BT_HIDDEN | |
84 | enum bt_notification_iterator_status bt_notification_iterator_validate( | |
85 | struct bt_notification_iterator *iterator) | |
86 | { | |
8738a040 | 87 | enum bt_notification_iterator_status ret = |
ea8d3e58 | 88 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
47e5a032 JG |
89 | |
90 | if (!iterator || !iterator->get || !iterator->next) { | |
91 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
92 | goto end; | |
93 | } | |
94 | end: | |
95 | return ret; | |
96 | } | |
97 | ||
8738a040 JG |
98 | enum bt_notification_iterator_status bt_notification_iterator_set_get_cb( |
99 | struct bt_notification_iterator *iterator, | |
100 | bt_notification_iterator_get_cb get) | |
101 | { | |
33bceaf8 | 102 | enum bt_notification_iterator_status ret = |
ea8d3e58 | 103 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
8738a040 JG |
104 | |
105 | if (!iterator || !get) { | |
106 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
107 | goto end; | |
108 | } | |
109 | ||
ea8d3e58 JG |
110 | iterator->get = get; |
111 | end: | |
112 | return ret; | |
113 | } | |
114 | ||
115 | enum bt_notification_iterator_status | |
116 | bt_notification_iterator_set_next_cb(struct bt_notification_iterator *iterator, | |
117 | bt_notification_iterator_next_cb next) | |
118 | { | |
119 | enum bt_notification_iterator_status ret = | |
120 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
121 | ||
122 | if (!iterator || !next) { | |
123 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
124 | goto end; | |
125 | } | |
126 | ||
127 | iterator->next = next; | |
128 | end: | |
129 | return ret; | |
130 | } | |
131 | ||
d631a9e7 JG |
132 | enum bt_notification_iterator_status |
133 | bt_notification_iterator_set_seek_time_cb( | |
134 | struct bt_notification_iterator *iterator, | |
135 | bt_notification_iterator_seek_time_cb seek_time) | |
136 | { | |
137 | enum bt_notification_iterator_status ret = | |
138 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
139 | ||
140 | if (!iterator || !seek_time) { | |
141 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
142 | goto end; | |
143 | } | |
144 | ||
145 | iterator->seek_time = seek_time; | |
146 | end: | |
147 | return ret; | |
148 | } | |
149 | ||
ea8d3e58 JG |
150 | enum bt_notification_iterator_status |
151 | bt_notification_iterator_set_destroy_cb( | |
152 | struct bt_notification_iterator *iterator, | |
153 | bt_notification_iterator_destroy_cb destroy) | |
154 | { | |
155 | enum bt_notification_iterator_status ret = | |
156 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
157 | ||
158 | if (!iterator || !destroy) { | |
159 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
160 | goto end; | |
161 | } | |
162 | ||
163 | iterator->user_destroy = destroy; | |
164 | end: | |
165 | return ret; | |
166 | } | |
167 | ||
168 | void *bt_notification_iterator_get_private_data( | |
169 | struct bt_notification_iterator *iterator) | |
170 | { | |
171 | return iterator ? iterator->user_data : NULL; | |
172 | } | |
173 | ||
174 | enum bt_notification_iterator_status | |
175 | bt_notification_iterator_set_private_data( | |
176 | struct bt_notification_iterator *iterator, void *data) | |
177 | { | |
178 | enum bt_notification_iterator_status ret = | |
179 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
180 | ||
181 | if (!iterator || !data) { | |
182 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
183 | goto end; | |
184 | } | |
185 | ||
186 | iterator->user_data = data; | |
8738a040 JG |
187 | end: |
188 | return ret; | |
189 | } | |
413bc2c4 | 190 | |
53d45b87 JG |
191 | struct bt_notification *bt_notification_iterator_get_notification( |
192 | struct bt_notification_iterator *iterator) | |
193 | { | |
194 | assert(iterator); | |
195 | assert(iterator->get); | |
196 | return iterator->get(iterator); | |
197 | } | |
198 | ||
199 | enum bt_notification_iterator_status | |
200 | bt_notification_iterator_next(struct bt_notification_iterator *iterator) | |
201 | { | |
202 | assert(iterator); | |
203 | assert(iterator->next); | |
204 | return iterator->next(iterator); | |
205 | } | |
206 | ||
413bc2c4 JG |
207 | struct bt_component *bt_notification_iterator_get_component( |
208 | struct bt_notification_iterator *iterator) | |
209 | { | |
210 | return bt_get(iterator->component); | |
211 | } | |
212 | ||
9531634f JG |
213 | enum bt_notification_iterator_status bt_notification_iterator_seek_time( |
214 | struct bt_notification_iterator *iterator, | |
215 | enum bt_notification_iterator_seek_origin seek_origin, | |
216 | int64_t time) | |
217 | { | |
b7726e32 MD |
218 | enum bt_notification_iterator_status ret = |
219 | BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED; | |
9531634f JG |
220 | return ret; |
221 | } |