| 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> |
| 30 | #include <babeltrace/ref.h> |
| 31 | #include <babeltrace/component/component.h> |
| 32 | #include <babeltrace/component/component-source-internal.h> |
| 33 | #include <babeltrace/component/component-class-internal.h> |
| 34 | #include <babeltrace/component/notification/iterator.h> |
| 35 | #include <babeltrace/component/notification/iterator-internal.h> |
| 36 | |
| 37 | static |
| 38 | void bt_notification_iterator_destroy(struct bt_object *obj) |
| 39 | { |
| 40 | struct bt_notification_iterator *iterator; |
| 41 | struct bt_component_class *comp_class; |
| 42 | |
| 43 | assert(obj); |
| 44 | iterator = container_of(obj, struct bt_notification_iterator, |
| 45 | base); |
| 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; |
| 61 | } |
| 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 | |
| 78 | BT_PUT(iterator->component); |
| 79 | g_free(iterator); |
| 80 | } |
| 81 | |
| 82 | BT_HIDDEN |
| 83 | struct bt_notification_iterator *bt_notification_iterator_create( |
| 84 | struct bt_component *component) |
| 85 | { |
| 86 | enum bt_component_class_type type; |
| 87 | struct bt_notification_iterator *iterator = NULL; |
| 88 | |
| 89 | if (!component) { |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | type = bt_component_get_class_type(component); |
| 94 | switch (type) { |
| 95 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 96 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 97 | break; |
| 98 | default: |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | iterator = g_new0(struct bt_notification_iterator, 1); |
| 103 | if (!iterator) { |
| 104 | goto end; |
| 105 | } |
| 106 | |
| 107 | iterator->component = bt_get(component); |
| 108 | bt_object_init(iterator, bt_notification_iterator_destroy); |
| 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 | { |
| 117 | enum bt_notification_iterator_status ret = |
| 118 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 119 | |
| 120 | if (!iterator) { |
| 121 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
| 122 | goto end; |
| 123 | } |
| 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 | |
| 141 | if (!iterator) { |
| 142 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | iterator->user_data = data; |
| 147 | end: |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | struct bt_notification *bt_notification_iterator_get_notification( |
| 152 | struct bt_notification_iterator *iterator) |
| 153 | { |
| 154 | bt_component_class_notification_iterator_get_method get_method = NULL; |
| 155 | |
| 156 | assert(iterator); |
| 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); |
| 188 | } |
| 189 | |
| 190 | enum bt_notification_iterator_status |
| 191 | bt_notification_iterator_next(struct bt_notification_iterator *iterator) |
| 192 | { |
| 193 | bt_component_class_notification_iterator_next_method next_method = NULL; |
| 194 | |
| 195 | assert(iterator); |
| 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); |
| 227 | } |
| 228 | |
| 229 | struct bt_component *bt_notification_iterator_get_component( |
| 230 | struct bt_notification_iterator *iterator) |
| 231 | { |
| 232 | return bt_get(iterator->component); |
| 233 | } |
| 234 | |
| 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 | { |
| 240 | enum bt_notification_iterator_status ret = |
| 241 | BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED; |
| 242 | return ret; |
| 243 | } |