From 5645cd954c45d26b521655bcc09d5828118470a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 24 Nov 2016 13:58:34 -0500 Subject: [PATCH] Move component iterator creation to base component class MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- .../babeltrace/plugin/component-internal.h | 4 ++ lib/plugin-system/component.c | 71 +++++++++++++++++++ lib/plugin-system/filter.c | 35 +-------- lib/plugin-system/source.c | 35 +-------- 4 files changed, 77 insertions(+), 68 deletions(-) diff --git a/include/babeltrace/plugin/component-internal.h b/include/babeltrace/plugin/component-internal.h index 4239ac4d..9cc39b3b 100644 --- a/include/babeltrace/plugin/component-internal.h +++ b/include/babeltrace/plugin/component-internal.h @@ -60,4 +60,8 @@ enum bt_component_status bt_component_init(struct bt_component *component, BT_HIDDEN enum bt_component_type bt_component_get_type(struct bt_component *component); +BT_HIDDEN +struct bt_notification_iterator *bt_component_create_iterator( + struct bt_component *component); + #endif /* BABELTRACE_PLUGIN_COMPONENT_INTERNAL_H */ diff --git a/lib/plugin-system/component.c b/lib/plugin-system/component.c index 643e17ad..e722089e 100644 --- a/lib/plugin-system/component.c +++ b/lib/plugin-system/component.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -100,6 +102,75 @@ enum bt_component_type bt_component_get_type(struct bt_component *component) return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN; } +BT_HIDDEN +struct bt_notification_iterator *bt_component_create_iterator( + struct bt_component *component) +{ + enum bt_notification_iterator_status ret_iterator; + enum bt_component_type component_type; + struct bt_notification_iterator *iterator = NULL; + + if (!component) { + goto error; + } + + component_type = bt_component_get_type(component); + if (component_type != BT_COMPONENT_TYPE_SOURCE && + component_type != BT_COMPONENT_TYPE_FILTER) { + /* Unsupported operation. */ + goto error; + } + + iterator = bt_notification_iterator_create(component); + if (!iterator) { + goto error; + } + + switch (component_type) { + case BT_COMPONENT_TYPE_SOURCE: + { + struct bt_component_source *source; + enum bt_component_status ret_component; + + source = container_of(component, struct bt_component_source, parent); + assert(source->init_iterator); + ret_component = source->init_iterator(component, iterator); + if (ret_component != BT_COMPONENT_STATUS_OK) { + goto error; + } + break; + + break; + } + case BT_COMPONENT_TYPE_FILTER: + { + struct bt_component_filter *filter; + enum bt_component_status ret_component; + + filter = container_of(component, struct bt_component_filter, parent); + assert(filter->init_iterator); + ret_component = filter->init_iterator(component, iterator); + if (ret_component != BT_COMPONENT_STATUS_OK) { + goto error; + } + break; + } + default: + /* Unreachable. */ + assert(0); + } + + ret_iterator = bt_notification_iterator_validate(iterator); + if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { + goto error; + } + + return iterator; +error: + BT_PUT(iterator); + return iterator; +} + struct bt_component *bt_component_create( struct bt_component_class *component_class, const char *name, struct bt_value *params) diff --git a/lib/plugin-system/filter.c b/lib/plugin-system/filter.c index 5ba74051..cb60ca54 100644 --- a/lib/plugin-system/filter.c +++ b/lib/plugin-system/filter.c @@ -225,38 +225,5 @@ end: struct bt_notification_iterator *bt_component_filter_create_iterator( struct bt_component *component) { - enum bt_component_status ret_component; - enum bt_notification_iterator_status ret_iterator; - struct bt_component_filter *filter; - struct bt_notification_iterator *iterator = NULL; - - if (!component) { - goto end; - } - - if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { - goto end; - } - - iterator = bt_notification_iterator_create(component); - if (!iterator) { - goto end; - } - - filter = container_of(component, struct bt_component_filter, parent); - assert(filter->init_iterator); - ret_component = filter->init_iterator(component, iterator); - if (ret_component != BT_COMPONENT_STATUS_OK) { - goto error; - } - - ret_iterator = bt_notification_iterator_validate(iterator); - if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { - goto error; - } -end: - return iterator; -error: - BT_PUT(iterator); - return iterator; + return bt_component_create_iterator(component); } diff --git a/lib/plugin-system/source.c b/lib/plugin-system/source.c index 51b32f83..7a19bc55 100644 --- a/lib/plugin-system/source.c +++ b/lib/plugin-system/source.c @@ -108,38 +108,5 @@ end: struct bt_notification_iterator *bt_component_source_create_iterator( struct bt_component *component) { - enum bt_component_status ret_component; - enum bt_notification_iterator_status ret_iterator; - struct bt_component_source *source; - struct bt_notification_iterator *iterator = NULL; - - if (!component) { - goto end; - } - - if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SOURCE) { - goto end; - } - - iterator = bt_notification_iterator_create(component); - if (!iterator) { - goto end; - } - - source = container_of(component, struct bt_component_source, parent); - assert(source->init_iterator); - ret_component = source->init_iterator(component, iterator); - if (ret_component != BT_COMPONENT_STATUS_OK) { - goto error; - } - - ret_iterator = bt_notification_iterator_validate(iterator); - if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { - goto error; - } -end: - return iterator; -error: - BT_PUT(iterator); - return iterator; + return bt_component_create_iterator(component); } -- 2.34.1