class->destroy_listeners = NULL;
}
- if (class->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
- struct bt_component_class_source *class_src
- = container_of(class, struct bt_component_class_source,
- parent);
-
- BT_ASSERT(class_src->msg_iter_cls);
- bt_message_iterator_class_put_ref(class_src->msg_iter_cls);
- class_src->msg_iter_cls = NULL;
- } else if (class->type == BT_COMPONENT_CLASS_TYPE_FILTER) {
- struct bt_component_class_filter *class_flt
- = container_of(class, struct bt_component_class_filter,
- parent);
-
- BT_ASSERT(class_flt->msg_iter_cls);
- bt_message_iterator_class_put_ref(class_flt->msg_iter_cls);
- class_flt->msg_iter_cls = NULL;
+ if (bt_component_class_has_message_iterator_class(class)) {
+ struct bt_component_class_with_iterator_class *class_with_iter_class =
+ container_of(class, struct bt_component_class_with_iterator_class, parent);
+
+ BT_ASSERT(class_with_iter_class->msg_iter_cls);
+ bt_message_iterator_class_put_ref(class_with_iter_class->msg_iter_cls);
+ class_with_iter_class->msg_iter_cls = NULL;
}
g_free(class);
return ret;
}
+static
+int bt_component_class_with_iterator_class_init(
+ struct bt_component_class_with_iterator_class *class,
+ enum bt_component_class_type type, const char *name,
+ struct bt_message_iterator_class *message_iterator_class)
+{
+ int ret;
+
+ ret = bt_component_class_init(&class->parent, type, name);
+ if (ret != 0) {
+ goto end;
+ }
+
+ class->msg_iter_cls = message_iterator_class;
+ bt_message_iterator_class_get_ref(class->msg_iter_cls);
+ bt_message_iterator_class_freeze(class->msg_iter_cls);
+
+end:
+ return ret;
+}
+
struct bt_component_class_source *bt_component_class_source_create(
const char *name,
struct bt_message_iterator_class *message_iterator_class)
}
/* bt_component_class_init() logs errors */
- ret = bt_component_class_init(&source_class->parent,
- BT_COMPONENT_CLASS_TYPE_SOURCE, name);
+ ret = bt_component_class_with_iterator_class_init(&source_class->parent,
+ BT_COMPONENT_CLASS_TYPE_SOURCE, name, message_iterator_class);
if (ret) {
/*
* If bt_component_class_init() fails, the component
goto end;
}
- source_class->msg_iter_cls = message_iterator_class;
- bt_message_iterator_class_get_ref(source_class->msg_iter_cls);
- bt_message_iterator_class_freeze(source_class->msg_iter_cls);
-
BT_LIB_LOGI("Created source component class: %!+C", source_class);
end:
}
/* bt_component_class_init() logs errors */
- ret = bt_component_class_init(&filter_class->parent,
- BT_COMPONENT_CLASS_TYPE_FILTER, name);
+ ret = bt_component_class_with_iterator_class_init(&filter_class->parent,
+ BT_COMPONENT_CLASS_TYPE_FILTER, name, message_iterator_class);
if (ret) {
/*
* If bt_component_class_init() fails, the component
goto end;
}
- filter_class->msg_iter_cls = message_iterator_class;
- bt_message_iterator_class_get_ref(filter_class->msg_iter_cls);
- bt_message_iterator_class_freeze(filter_class->msg_iter_cls);
-
BT_LIB_LOGI("Created filter component class: %!+C", filter_class);
end:
struct bt_plugin_so_shared_lib_handle *so_handle;
};
-struct bt_component_class_source {
+struct bt_component_class_with_iterator_class {
struct bt_component_class parent;
+ bt_message_iterator_class *msg_iter_cls;
+};
+
+struct bt_component_class_source {
+ struct bt_component_class_with_iterator_class parent;
struct {
bt_component_class_source_get_supported_mip_versions_method get_supported_mip_versions;
bt_component_class_source_initialize_method init;
bt_component_class_source_query_method query;
bt_component_class_source_output_port_connected_method output_port_connected;
} methods;
- bt_message_iterator_class *msg_iter_cls;
+};
+
+struct bt_component_class_filter {
+ struct bt_component_class_with_iterator_class parent;
+ struct {
+ bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions;
+ bt_component_class_filter_initialize_method init;
+ bt_component_class_filter_finalize_method finalize;
+ bt_component_class_filter_query_method query;
+ bt_component_class_filter_input_port_connected_method input_port_connected;
+ bt_component_class_filter_output_port_connected_method output_port_connected;
+ } methods;
};
struct bt_component_class_sink {
} methods;
};
-struct bt_component_class_filter {
- struct bt_component_class parent;
- struct {
- bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions;
- bt_component_class_filter_initialize_method init;
- bt_component_class_filter_finalize_method finalize;
- bt_component_class_filter_query_method query;
- bt_component_class_filter_input_port_connected_method input_port_connected;
- bt_component_class_filter_output_port_connected_method output_port_connected;
- } methods;
- bt_message_iterator_class *msg_iter_cls;
-};
-
BT_HIDDEN
void bt_component_class_add_destroy_listener(struct bt_component_class *class,
bt_component_class_destroy_listener_func func, void *data);
}
}
+static inline
+bool bt_component_class_has_message_iterator_class(
+ struct bt_component_class *component_class)
+{
+ return component_class->type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
+ component_class->type == BT_COMPONENT_CLASS_TYPE_FILTER;
+}
+
#endif /* BABELTRACE_GRAPH_COMPONENT_CLASS_INTERNAL_H */
/* Call user-defined destroy method */
if (call_user_finalize) {
typedef void (*method_t)(void *);
- method_t method = NULL;
+ method_t method;
struct bt_component_class *comp_class =
iterator->upstream_component->class;
+ struct bt_component_class_with_iterator_class *class_with_iter_class;
- switch (comp_class->type) {
- case BT_COMPONENT_CLASS_TYPE_SOURCE:
- {
- struct bt_component_class_source *src_comp_cls =
- (void *) comp_class;
-
- method = (method_t) src_comp_cls->msg_iter_cls->methods.finalize;
- break;
- }
- case BT_COMPONENT_CLASS_TYPE_FILTER:
- {
- struct bt_component_class_filter *flt_comp_cls =
- (void *) comp_class;
-
- method = (method_t) flt_comp_cls->msg_iter_cls->methods.finalize;
- break;
- }
- default:
- /* Unreachable */
- bt_common_abort();
- }
+ BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class));
+ class_with_iter_class = container_of(comp_class,
+ struct bt_component_class_with_iterator_class, parent);
+ method = (method_t) class_with_iter_class->msg_iter_cls->methods.finalize;
if (method) {
const bt_error *saved_error;
struct bt_component *comp;
struct bt_component *upstream_comp;
struct bt_component_class *upstream_comp_cls;
+ struct bt_component_class_with_iterator_class *upstream_comp_cls_with_iter_cls;
int status;
BT_ASSERT_PRE_NON_NULL(message_iterator, "Created message iterator");
set_self_comp_port_input_msg_iterator_state(iterator,
BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
- switch (iterator->upstream_component->class->type) {
- case BT_COMPONENT_CLASS_TYPE_SOURCE:
- {
- struct bt_component_class_source *src_comp_cls =
- (void *) iterator->upstream_component->class;
-
- iterator->methods.next =
- (bt_self_component_port_input_message_iterator_next_method)
- src_comp_cls->msg_iter_cls->methods.next;
- iterator->methods.seek_ns_from_origin =
- (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
- src_comp_cls->msg_iter_cls->methods.seek_ns_from_origin;
- iterator->methods.seek_beginning =
- (bt_self_component_port_input_message_iterator_seek_beginning_method)
- src_comp_cls->msg_iter_cls->methods.seek_beginning;
- iterator->methods.can_seek_ns_from_origin =
- (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
- src_comp_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
- iterator->methods.can_seek_beginning =
- (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
- src_comp_cls->msg_iter_cls->methods.can_seek_beginning;
- break;
- }
- case BT_COMPONENT_CLASS_TYPE_FILTER:
- {
- struct bt_component_class_filter *flt_comp_cls =
- (void *) iterator->upstream_component->class;
-
- iterator->methods.next =
- (bt_self_component_port_input_message_iterator_next_method)
- flt_comp_cls->msg_iter_cls->methods.next;
- iterator->methods.seek_ns_from_origin =
- (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
- flt_comp_cls->msg_iter_cls->methods.seek_ns_from_origin;
- iterator->methods.seek_beginning =
- (bt_self_component_port_input_message_iterator_seek_beginning_method)
- flt_comp_cls->msg_iter_cls->methods.seek_beginning;
- iterator->methods.can_seek_ns_from_origin =
- (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
- flt_comp_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
- iterator->methods.can_seek_beginning =
- (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
- flt_comp_cls->msg_iter_cls->methods.can_seek_beginning;
- break;
- }
- default:
- bt_common_abort();
- }
+ /* Copy methods from the message iterator class to the message iterator. */
+ BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls));
+ upstream_comp_cls_with_iter_cls = container_of(upstream_comp_cls,
+ struct bt_component_class_with_iterator_class, parent);
+
+ iterator->methods.next =
+ (bt_self_component_port_input_message_iterator_next_method)
+ upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.next;
+ iterator->methods.seek_ns_from_origin =
+ (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
+ upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_ns_from_origin;
+ iterator->methods.seek_beginning =
+ (bt_self_component_port_input_message_iterator_seek_beginning_method)
+ upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_beginning;
+ iterator->methods.can_seek_ns_from_origin =
+ (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
+ upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
+ iterator->methods.can_seek_beginning =
+ (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
+ upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_beginning;
if (iterator->methods.seek_ns_from_origin &&
!iterator->methods.can_seek_ns_from_origin) {
can_seek_beginning_true;
}
- switch (upstream_comp_cls->type) {
- case BT_COMPONENT_CLASS_TYPE_SOURCE:
- {
- struct bt_component_class_source *src_comp_cls =
- (void *) upstream_comp_cls;
-
- init_method = src_comp_cls->msg_iter_cls->methods.initialize;
- break;
- }
- case BT_COMPONENT_CLASS_TYPE_FILTER:
- {
- struct bt_component_class_filter *flt_comp_cls =
- (void *) upstream_comp_cls;
-
- init_method = flt_comp_cls->msg_iter_cls->methods.initialize;
- break;
- }
- default:
- /* Unreachable */
- bt_common_abort();
- }
+ /* Call iterator's init method. */
+ init_method = upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.initialize;
if (init_method) {
enum bt_message_iterator_class_initialize_method_status iter_status;