lib: create common base for bt_component_class_{source,filter}
[babeltrace.git] / src / lib / graph / iterator.c
index 3ef5fd57d226a6e17d7a69157dedf79a1831fec7..c7ab253c3aaae3edc836a8429d6039e0d50931f3 100644 (file)
@@ -69,6 +69,7 @@
 #include "component-source.h"
 #include "connection.h"
 #include "graph.h"
+#include "message-iterator-class.h"
 #include "message/discarded-items.h"
 #include "message/event.h"
 #include "message/iterator.h"
@@ -169,7 +170,6 @@ void bt_self_component_port_input_message_iterator_try_finalize(
                struct bt_self_component_port_input_message_iterator *iterator)
 {
        uint64_t i;
-       typedef void (*method_t)(void *);
        bool call_user_finalize = true;
 
        BT_ASSERT(iterator);
@@ -209,31 +209,16 @@ void bt_self_component_port_input_message_iterator_try_finalize(
 
        /* Call user-defined destroy method */
        if (call_user_finalize) {
-               method_t method = NULL;
+               typedef void (*method_t)(void *);
+               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->methods.msg_iter_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->methods.msg_iter_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;
@@ -318,10 +303,7 @@ int create_self_component_input_port_message_iterator(
                struct bt_self_component_port_input *self_port,
                struct bt_self_component_port_input_message_iterator **message_iterator)
 {
-       typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t)(
-                       void *, void *, void *, void *);
-
-       init_method_t init_method = NULL;
+       bt_message_iterator_class_initialize_method init_method = NULL;
        struct bt_self_component_port_input_message_iterator *iterator =
                NULL;
        struct bt_self_component_port_input_message_iterator *downstream_msg_iter =
@@ -331,6 +313,7 @@ int create_self_component_input_port_message_iterator(
        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");
@@ -401,54 +384,26 @@ int create_self_component_input_port_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->methods.msg_iter_next;
-               iterator->methods.seek_ns_from_origin =
-                       (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
-                               src_comp_cls->methods.msg_iter_seek_ns_from_origin;
-               iterator->methods.seek_beginning =
-                       (bt_self_component_port_input_message_iterator_seek_beginning_method)
-                               src_comp_cls->methods.msg_iter_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->methods.msg_iter_can_seek_ns_from_origin;
-               iterator->methods.can_seek_beginning =
-                       (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
-                               src_comp_cls->methods.msg_iter_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->methods.msg_iter_next;
-               iterator->methods.seek_ns_from_origin =
-                       (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
-                               flt_comp_cls->methods.msg_iter_seek_ns_from_origin;
-               iterator->methods.seek_beginning =
-                       (bt_self_component_port_input_message_iterator_seek_beginning_method)
-                               flt_comp_cls->methods.msg_iter_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->methods.msg_iter_can_seek_ns_from_origin;
-               iterator->methods.can_seek_beginning =
-                       (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
-                               flt_comp_cls->methods.msg_iter_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) {
@@ -464,36 +419,18 @@ int create_self_component_input_port_message_iterator(
                                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 =
-                       (init_method_t) src_comp_cls->methods.msg_iter_initialize;
-               break;
-       }
-       case BT_COMPONENT_CLASS_TYPE_FILTER:
-       {
-               struct bt_component_class_filter *flt_comp_cls =
-                       (void *) upstream_comp_cls;
-
-               init_method =
-                       (init_method_t) flt_comp_cls->methods.msg_iter_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_component_class_message_iterator_initialize_method_status iter_status;
+               enum bt_message_iterator_class_initialize_method_status iter_status;
 
                BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
-               iter_status = init_method(iterator, &iterator->config, upstream_comp,
-                       upstream_port);
+               iter_status = init_method(
+                       (struct bt_self_message_iterator *) iterator,
+                       &iterator->config,
+                       (struct bt_self_component *) upstream_comp,
+                       (struct bt_self_component_port_output *) upstream_port);
                BT_LOGD("User method returned: status=%s",
                        bt_common_func_status_string(iter_status));
                BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status);
@@ -871,12 +808,12 @@ end:
  */
 
 static
-enum bt_component_class_message_iterator_next_method_status
+enum bt_message_iterator_class_next_method_status
 call_iterator_next_method(
                struct bt_self_component_port_input_message_iterator *iterator,
                bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
 {
-       enum bt_component_class_message_iterator_next_method_status status;
+       enum bt_message_iterator_class_next_method_status status;
 
        BT_ASSERT_DBG(iterator->methods.next);
        BT_LOGD_STR("Calling user's \"next\" method.");
@@ -1670,7 +1607,7 @@ end:
  */
 
 static
-enum bt_component_class_message_iterator_next_method_status post_auto_seek_next(
+enum bt_message_iterator_class_next_method_status post_auto_seek_next(
                struct bt_self_component_port_input_message_iterator *iterator,
                bt_message_array_const msgs, uint64_t capacity,
                uint64_t *count)
@@ -1763,7 +1700,7 @@ bt_self_component_port_input_message_iterator_seek_ns_from_origin(
 
        /* Check if the iterator can seek by itself.  If not we'll use autoseek. */
        if (iterator->methods.can_seek_ns_from_origin) {
-               bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
+               bt_message_iterator_class_can_seek_ns_from_origin_method_status
                        can_seek_status;
 
                can_seek_status =
@@ -1805,8 +1742,7 @@ bt_self_component_port_input_message_iterator_seek_ns_from_origin(
                 * particular time.  We will seek to the beginning and fast
                 * forward to the right place.
                 */
-               enum bt_component_class_message_iterator_can_seek_beginning_method_status
-                       can_seek_status;
+               enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
                bt_bool can_seek_beginning;
 
                can_seek_status = iterator->methods.can_seek_beginning(iterator,
This page took 0.027084 seconds and 4 git commands to generate.