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> |
47e5a032 JG |
31 | #include <babeltrace/plugin/component.h> |
32 | #include <babeltrace/plugin/source-internal.h> | |
33 | #include <babeltrace/plugin/notification/iterator.h> | |
34 | #include <babeltrace/plugin/notification/iterator-internal.h> | |
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 | { | |
16b7b023 | 56 | enum bt_component_type type; |
47e5a032 JG |
57 | struct bt_notification_iterator *iterator = NULL; |
58 | ||
16b7b023 JG |
59 | if (!component) { |
60 | goto end; | |
61 | } | |
62 | ||
63 | type = bt_component_get_type(component); | |
64 | switch (type) { | |
65 | case BT_COMPONENT_TYPE_SOURCE: | |
66 | case BT_COMPONENT_TYPE_FILTER: | |
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 | ||
132 | enum bt_notification_iterator_status | |
133 | bt_notification_iterator_set_destroy_cb( | |
134 | struct bt_notification_iterator *iterator, | |
135 | bt_notification_iterator_destroy_cb destroy) | |
136 | { | |
137 | enum bt_notification_iterator_status ret = | |
138 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
139 | ||
140 | if (!iterator || !destroy) { | |
141 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
142 | goto end; | |
143 | } | |
144 | ||
145 | iterator->user_destroy = destroy; | |
146 | end: | |
147 | return ret; | |
148 | } | |
149 | ||
150 | void *bt_notification_iterator_get_private_data( | |
151 | struct bt_notification_iterator *iterator) | |
152 | { | |
153 | return iterator ? iterator->user_data : NULL; | |
154 | } | |
155 | ||
156 | enum bt_notification_iterator_status | |
157 | bt_notification_iterator_set_private_data( | |
158 | struct bt_notification_iterator *iterator, void *data) | |
159 | { | |
160 | enum bt_notification_iterator_status ret = | |
161 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
162 | ||
163 | if (!iterator || !data) { | |
164 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; | |
165 | goto end; | |
166 | } | |
167 | ||
168 | iterator->user_data = data; | |
8738a040 JG |
169 | end: |
170 | return ret; | |
171 | } | |
413bc2c4 | 172 | |
53d45b87 JG |
173 | struct bt_notification *bt_notification_iterator_get_notification( |
174 | struct bt_notification_iterator *iterator) | |
175 | { | |
176 | assert(iterator); | |
177 | assert(iterator->get); | |
178 | return iterator->get(iterator); | |
179 | } | |
180 | ||
181 | enum bt_notification_iterator_status | |
182 | bt_notification_iterator_next(struct bt_notification_iterator *iterator) | |
183 | { | |
184 | assert(iterator); | |
185 | assert(iterator->next); | |
186 | return iterator->next(iterator); | |
187 | } | |
188 | ||
413bc2c4 JG |
189 | struct bt_component *bt_notification_iterator_get_component( |
190 | struct bt_notification_iterator *iterator) | |
191 | { | |
192 | return bt_get(iterator->component); | |
193 | } | |
194 | ||
9531634f JG |
195 | enum bt_notification_iterator_status bt_notification_iterator_seek_time( |
196 | struct bt_notification_iterator *iterator, | |
197 | enum bt_notification_iterator_seek_origin seek_origin, | |
198 | int64_t time) | |
199 | { | |
b7726e32 MD |
200 | enum bt_notification_iterator_status ret = |
201 | BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED; | |
9531634f JG |
202 | return ret; |
203 | } |