Add ports to the source, filter and sink component interfaces
[babeltrace.git] / lib / component / filter.c
index 153e38f2c69f4c83b6522f043ef98a54219b06fe..231907096313e57197501fcf54e2e67905d11a89 100644 (file)
 
 #include <babeltrace/compiler.h>
 #include <babeltrace/values.h>
-#include <babeltrace/component/component-input-internal.h>
 #include <babeltrace/component/component-filter-internal.h>
 #include <babeltrace/component/component-internal.h>
 #include <babeltrace/component/component-class-internal.h>
 #include <babeltrace/component/notification/notification.h>
 #include <babeltrace/component/notification/iterator-internal.h>
 
-enum bt_component_status bt_component_filter_set_minimum_input_count(
+enum bt_component_status bt_component_filter_add_iterator(
                struct bt_component *component,
-               unsigned int minimum)
+               struct bt_notification_iterator *iterator)
 {
-       struct bt_component_filter *filter;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       struct bt_component_class_filter *filter_class;
 
-       if (!component) {
+       if (!component || !iterator) {
                ret = BT_COMPONENT_STATUS_INVALID;
                goto end;
        }
@@ -52,210 +51,308 @@ enum bt_component_status bt_component_filter_set_minimum_input_count(
                goto end;
        }
 
-       if (!component->initializing) {
-               ret = BT_COMPONENT_STATUS_INVALID;
-               goto end;
+       /* TODO validate iterator count limits. */
+
+       filter_class = container_of(component->class,
+                       struct bt_component_class_filter, parent);
+       if (filter_class->methods.add_iterator) {
+               ret = filter_class->methods.add_iterator(component, iterator);
+               if (ret != BT_COMPONENT_STATUS_OK) {
+                       goto end;
+               }
        }
 
-       filter = container_of(component, struct bt_component_filter, parent);
-       filter->input.min_count = minimum;
 end:
        return ret;
 }
 
-enum bt_component_status bt_component_filter_set_maximum_input_count(
-               struct bt_component *component,
-               unsigned int maximum)
+struct bt_notification_iterator *bt_component_filter_create_notification_iterator(
+               struct bt_component *component)
 {
-       struct bt_component_filter *filter;
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       return bt_component_create_iterator(component, NULL);
+}
 
-       if (!component) {
-               ret = BT_COMPONENT_STATUS_INVALID;
-               goto end;
-       }
+struct bt_notification_iterator *bt_component_filter_create_notification_iterator_with_init_method_data(
+               struct bt_component *component, void *init_method_data)
+{
+       return bt_component_create_iterator(component, init_method_data);
+}
 
-       if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
-               ret = BT_COMPONENT_STATUS_UNSUPPORTED;
-               goto end;
-       }
+static
+void bt_component_filter_destroy(struct bt_component *component)
+{
+       struct bt_component_filter *filter = container_of(component,
+                       struct bt_component_filter, parent);
 
-       if (!component->initializing) {
-               ret = BT_COMPONENT_STATUS_INVALID;
-               goto end;
+       if (filter->input_ports) {
+               g_ptr_array_free(filter->input_ports, TRUE);
        }
 
-       filter = container_of(component, struct bt_component_filter, parent);
-       filter->input.max_count = maximum;
-end:
-       return ret;
+       if (filter->output_ports) {
+               g_ptr_array_free(filter->output_ports, TRUE);
+       }
 }
 
-enum bt_component_status
-bt_component_filter_get_input_count(struct bt_component *component,
-               unsigned int *count)
+BT_HIDDEN
+struct bt_component *bt_component_filter_create(
+               struct bt_component_class *class, struct bt_value *params)
 {
-       struct bt_component_filter *filter;
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       int ret;
+       struct bt_component_filter *filter = NULL;
+       enum bt_component_status status;
 
-       if (!component || !count) {
-               ret = BT_COMPONENT_STATUS_INVALID;
+       filter = g_new0(struct bt_component_filter, 1);
+       if (!filter) {
                goto end;
        }
 
-       if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
-               ret = BT_COMPONENT_STATUS_UNSUPPORTED;
-               goto end;
+       filter->parent.class = bt_get(class);
+       status = bt_component_init(&filter->parent, bt_component_filter_destroy);
+       if (status != BT_COMPONENT_STATUS_OK) {
+               goto error;
+       }
+
+       ret = bt_component_init_input_ports(&filter->parent,
+                       &filter->input_ports);
+       if (ret) {
+               goto error;
+       }
+
+       ret = bt_component_init_output_ports(&filter->parent,
+                       &filter->output_ports);
+       if (ret) {
+               goto error;
        }
 
-       filter = container_of(component, struct bt_component_filter, parent);
-       *count = (unsigned int) filter->input.iterators->len;
 end:
-       return ret;
+       return filter ? &filter->parent : NULL;
+error:
+       BT_PUT(filter);
+       goto end;
 }
 
-enum bt_component_status
-bt_component_filter_get_input_iterator(struct bt_component *component,
-               unsigned int input, struct bt_notification_iterator **iterator)
+BT_HIDDEN
+enum bt_component_status bt_component_filter_validate(
+               struct bt_component *component)
 {
-       struct bt_component_filter *filter;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
 
-       if (!component || !iterator) {
+       if (!component) {
                ret = BT_COMPONENT_STATUS_INVALID;
                goto end;
        }
 
-       if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
-               ret = BT_COMPONENT_STATUS_UNSUPPORTED;
+       if (!component->class) {
+               ret = BT_COMPONENT_STATUS_INVALID;
                goto end;
        }
 
-       filter = container_of(component, struct bt_component_filter, parent);
-       if (input >= (unsigned int) filter->input.iterators->len) {
+       if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
                ret = BT_COMPONENT_STATUS_INVALID;
                goto end;
        }
 
-       *iterator = bt_get(g_ptr_array_index(filter->input.iterators, input));
+       /* Enforce iterator limits. */
 end:
        return ret;
 }
 
-enum bt_component_status bt_component_filter_add_iterator(
-               struct bt_component *component,
-               struct bt_notification_iterator *iterator)
+int bt_component_filter_get_input_port_count(struct bt_component *component)
 {
+       int ret;
        struct bt_component_filter *filter;
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
-       struct bt_component_class_filter *filter_class;
 
-       if (!component || !iterator) {
-               ret = BT_COMPONENT_STATUS_INVALID;
+       if (!component) {
+               ret = -1;
                goto end;
        }
 
-       if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
-               ret = BT_COMPONENT_STATUS_UNSUPPORTED;
+       if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               ret = -1;
                goto end;
        }
 
        filter = container_of(component, struct bt_component_filter, parent);
-       if (filter->input.iterators->len == filter->input.max_count) {
-               ret = BT_COMPONENT_STATUS_UNSUPPORTED;
+       ret = filter->input_ports->len;
+end:
+       return ret;
+}
+
+struct bt_port *bt_component_filter_get_input_port(
+               struct bt_component *component, const char *name)
+{
+       struct bt_component_filter *filter;
+       struct bt_port *ret_port = NULL;
+
+       if (!component || !name ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
                goto end;
        }
 
-       filter_class = container_of(component->class, struct bt_component_class_filter, parent);
+       filter = container_of(component, struct bt_component_filter, parent);
+       ret_port = bt_component_get_port(filter->input_ports, name);
+end:
+       return ret_port;
+}
 
-       if (filter_class->methods.add_iterator) {
-               ret = filter_class->methods.add_iterator(component, iterator);
-               if (ret != BT_COMPONENT_STATUS_OK) {
-                       goto end;
-               }
+struct bt_port *bt_component_filter_get_input_port_at_index(
+               struct bt_component *component, int index)
+{
+       struct bt_port *port = NULL;
+       struct bt_component_filter *filter;
+
+       if (!component ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               goto end;
        }
 
-       g_ptr_array_add(filter->input.iterators, bt_get(iterator));
+       filter = container_of(component, struct bt_component_filter, parent);
+       port = bt_component_get_port_at_index(filter->input_ports, index);
 end:
-       return ret;
+       return port;
 }
 
-struct bt_notification_iterator *bt_component_filter_create_notification_iterator(
+struct bt_port *bt_component_filter_get_default_input_port(
                struct bt_component *component)
 {
-       return bt_component_create_iterator(component, NULL);
+       return bt_component_filter_get_input_port(component,
+                       DEFAULT_INPUT_PORT_NAME);
 }
 
-struct bt_notification_iterator *bt_component_filter_create_notification_iterator_with_init_method_data(
-               struct bt_component *component, void *init_method_data)
+struct bt_port *bt_component_filter_add_input_port(
+               struct bt_component *component, const char *name)
 {
-       return bt_component_create_iterator(component, init_method_data);
+       struct bt_port *port;
+       struct bt_component_filter *filter;
+
+       if (!component ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               port = NULL;
+               goto end;
+       }
+
+       filter = container_of(component, struct bt_component_filter, parent);
+       port = bt_component_add_port(component, filter->input_ports,
+                       BT_PORT_TYPE_INPUT, name);
+end:
+       return port;
 }
 
-static
-void bt_component_filter_destroy(struct bt_component *component)
+enum bt_component_status bt_component_filter_remove_input_port(
+               struct bt_component *component, const char *name)
 {
-       struct bt_component_filter *filter = container_of(component,
-                       struct bt_component_filter, parent);
+       enum bt_component_status status;
+       struct bt_component_filter *filter;
 
-       component_input_fini(&filter->input);
+       if (!component ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               status = BT_COMPONENT_STATUS_INVALID;
+               goto end;
+       }
+
+       filter = container_of(component, struct bt_component_filter, parent);
+       status = bt_component_remove_port(component, filter->input_ports,
+                       name);
+end:
+       return status;
 }
 
-BT_HIDDEN
-struct bt_component *bt_component_filter_create(
-               struct bt_component_class *class, struct bt_value *params)
+int bt_component_filter_get_output_port_count(struct bt_component *component)
 {
-       struct bt_component_filter *filter = NULL;
-       enum bt_component_status ret;
+       int ret;
+       struct bt_component_filter *filter;
 
-       filter = g_new0(struct bt_component_filter, 1);
-       if (!filter) {
+       if (!component) {
+               ret = -1;
                goto end;
        }
 
-       filter->parent.class = bt_get(class);
-       ret = bt_component_init(&filter->parent, bt_component_filter_destroy);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto error;
+       if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               ret = -1;
+               goto end;
        }
 
-       if (component_input_init(&filter->input)) {
-               goto error;
-       }
+       filter = container_of(component, struct bt_component_filter, parent);
+       ret = filter->output_ports->len;
 end:
-       return filter ? &filter->parent : NULL;
-error:
-       BT_PUT(filter);
-       goto end;
+       return ret;
 }
 
-BT_HIDDEN
-enum bt_component_status bt_component_filter_validate(
-               struct bt_component *component)
+struct bt_port *bt_component_filter_get_output_port(
+               struct bt_component *component, const char *name)
 {
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_component_filter *filter;
+       struct bt_port *ret_port = NULL;
 
-       if (!component) {
-               ret = BT_COMPONENT_STATUS_INVALID;
+       if (!component || !name ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
                goto end;
        }
 
-       if (!component->class) {
-               ret = BT_COMPONENT_STATUS_INVALID;
+       filter = container_of(component, struct bt_component_filter, parent);
+       ret_port = bt_component_get_port(filter->output_ports, name);
+end:
+       return ret_port;
+}
+
+struct bt_port *bt_component_filter_get_output_port_at_index(
+               struct bt_component *component, int index)
+{
+       struct bt_port *port = NULL;
+       struct bt_component_filter *filter;
+
+       if (!component ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
                goto end;
        }
 
-       if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
-               ret = BT_COMPONENT_STATUS_INVALID;
+       filter = container_of(component, struct bt_component_filter, parent);
+       port = bt_component_get_port_at_index(filter->output_ports, index);
+end:
+       return port;
+}
+
+struct bt_port *bt_component_filter_get_default_output_port(
+               struct bt_component *component)
+{
+       return bt_component_filter_get_output_port(component,
+                       DEFAULT_OUTPUT_PORT_NAME);
+}
+
+struct bt_port *bt_component_filter_add_output_port(
+               struct bt_component *component, const char *name)
+{
+       struct bt_port *port;
+       struct bt_component_filter *filter;
+
+       if (!component ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               port = NULL;
                goto end;
        }
 
        filter = container_of(component, struct bt_component_filter, parent);
-       ret = component_input_validate(&filter->input);
-       if (ret) {
+       port = bt_component_add_port(component, filter->output_ports,
+                       BT_PORT_TYPE_OUTPUT, name);
+end:
+       return port;
+}
+
+enum bt_component_status bt_component_filter_remove_output_port(
+               struct bt_component *component, const char *name)
+{
+       enum bt_component_status status;
+       struct bt_component_filter *filter;
+
+       if (!component ||
+                       component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
+               status = BT_COMPONENT_STATUS_INVALID;
                goto end;
        }
+
+       filter = container_of(component, struct bt_component_filter, parent);
+       status = bt_component_remove_port(component, filter->output_ports,
+                       name);
 end:
-       return ret;
+       return status;
 }
This page took 0.028632 seconds and 4 git commands to generate.