Set notification iterator methods to the component class
[babeltrace.git] / lib / component / component.c
index 4ad897c7cb32c8f359edfe673c2cbc2f404808df..d6921a42da4b2e7235b0661be163df38330d9556 100644 (file)
 
 #include <babeltrace/component/component.h>
 #include <babeltrace/component/component-internal.h>
-#include <babeltrace/component/source-internal.h>
-#include <babeltrace/component/filter-internal.h>
+#include <babeltrace/component/component-class-internal.h>
+#include <babeltrace/component/component-source-internal.h>
+#include <babeltrace/component/component-filter-internal.h>
+#include <babeltrace/component/component-sink-internal.h>
 #include <babeltrace/component/notification/iterator-internal.h>
-#include <babeltrace/component/sink-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/ref.h>
 static
 struct bt_component * (* const component_create_funcs[])(
                struct bt_component_class *, struct bt_value *) = {
-       [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_create,
-       [BT_COMPONENT_TYPE_SINK] = bt_component_sink_create,
-       [BT_COMPONENT_TYPE_FILTER] = bt_component_filter_create,
+       [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
+       [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
+       [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
 };
 
 static
 enum bt_component_status (* const component_validation_funcs[])(
                struct bt_component *) = {
-       [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate,
-       [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate,
-       [BT_COMPONENT_TYPE_FILTER] = bt_component_filter_validate,
+       [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_validate,
+       [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_validate,
+       [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_validate,
 };
 
 static
@@ -69,8 +70,8 @@ void bt_component_destroy(struct bt_object *obj)
         * User data is destroyed first, followed by the concrete component
         * instance.
         */
-       if (component->user_destroy) {
-               component->user_destroy(component);
+       if (component->class->methods.destroy) {
+               component->class->methods.destroy(component);
        }
 
        if (component->destroy) {
@@ -84,7 +85,7 @@ void bt_component_destroy(struct bt_object *obj)
 
 BT_HIDDEN
 enum bt_component_status bt_component_init(struct bt_component *component,
-               bt_component_destroy_cb destroy)
+               bt_component_class_destroy_method destroy)
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
 
@@ -98,10 +99,10 @@ end:
        return ret;
 }
 
-BT_HIDDEN
-enum bt_component_type bt_component_get_type(struct bt_component *component)
+enum bt_component_class_type bt_component_get_class_type(
+               struct bt_component *component)
 {
-       return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN;
+       return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN;
 }
 
 BT_HIDDEN
@@ -109,16 +110,17 @@ 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;
+       enum bt_component_class_type type;
        struct bt_notification_iterator *iterator = NULL;
+       struct bt_component_class *class = component->class;
 
        if (!component) {
                goto error;
        }
 
-       component_type = bt_component_get_type(component);
-       if (component_type != BT_COMPONENT_TYPE_SOURCE &&
-                       component_type != BT_COMPONENT_TYPE_FILTER) {
+       type = bt_component_get_class_type(component);
+       if (type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
+                       type != BT_COMPONENT_CLASS_TYPE_FILTER) {
                /* Unsupported operation. */
                goto error;
        }
@@ -128,31 +130,31 @@ struct bt_notification_iterator *bt_component_create_iterator(
                goto error;
        }
 
-       switch (component_type) {
-       case BT_COMPONENT_TYPE_SOURCE:
+       switch (type) {
+       case BT_COMPONENT_CLASS_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) {
+               struct bt_component_class_source *source_class;
+               enum bt_notification_iterator_status status;
+
+               source_class = container_of(class, struct bt_component_class_source, parent);
+               assert(source_class->methods.iterator.init);
+               status = source_class->methods.iterator.init(component,
+                               iterator);
+               if (status < 0) {
                        goto error;
                }
                break;
-
-               break;
        }
-       case BT_COMPONENT_TYPE_FILTER:
+       case BT_COMPONENT_CLASS_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) {
+               struct bt_component_class_filter *filter_class;
+               enum bt_notification_iterator_status status;
+
+               filter_class = container_of(class, struct bt_component_class_filter, parent);
+               assert(filter_class->methods.iterator.init);
+               status = filter_class->methods.iterator.init(component,
+                               iterator);
+               if (status < 0) {
                        goto error;
                }
                break;
@@ -173,21 +175,21 @@ error:
        return iterator;
 }
 
-struct bt_component *bt_component_create(
+struct bt_component *bt_component_create_with_init_method_data(
                struct bt_component_class *component_class, const char *name,
-               struct bt_value *params)
+               struct bt_value *params, void *init_method_data)
 {
        int ret;
        struct bt_component *component = NULL;
-       enum bt_component_type type;
+       enum bt_component_class_type type;
 
        if (!component_class) {
                goto end;
        }
 
        type = bt_component_class_get_type(component_class);
-       if (type <= BT_COMPONENT_TYPE_UNKNOWN ||
-                       type > BT_COMPONENT_TYPE_FILTER) {
+       if (type <= BT_COMPONENT_CLASS_TYPE_UNKNOWN ||
+                       type > BT_COMPONENT_CLASS_TYPE_FILTER) {
                goto end;
        }
 
@@ -204,21 +206,37 @@ struct bt_component *bt_component_create(
        }
 
        component->initializing = true;
-       ret = component_class->init(component, params);
-       component->initializing = false;
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               BT_PUT(component);
-               goto end;
+
+       if (component_class->methods.init) {
+               ret = component_class->methods.init(component, params,
+                       init_method_data);
+               component->initializing = false;
+               if (ret != BT_COMPONENT_STATUS_OK) {
+                       BT_PUT(component);
+                       goto end;
+               }
        }
+
+       component->initializing = false;
        ret = component_validation_funcs[type](component);
        if (ret != BT_COMPONENT_STATUS_OK) {
                BT_PUT(component);
                goto end;
        }
+
+       bt_component_class_freeze(component->class);
 end:
        return component;
 }
 
+struct bt_component *bt_component_create(
+               struct bt_component_class *component_class, const char *name,
+               struct bt_value *params)
+{
+       return bt_component_create_with_init_method_data(component_class, name,
+               params, NULL);
+}
+
 const char *bt_component_get_name(struct bt_component *component)
 {
        const char *ret = NULL;
@@ -273,18 +291,3 @@ bt_component_set_private_data(struct bt_component *component,
 end:
        return ret;
 }
-
-enum bt_component_status bt_component_set_destroy_cb(
-               struct bt_component *component, bt_component_destroy_cb destroy)
-{
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
-
-       if (!component || !component->initializing) {
-               ret = BT_COMPONENT_STATUS_INVALID;
-               goto end;
-       }
-
-       component->user_destroy = destroy;
-end:
-       return ret;
-}
This page took 0.028504 seconds and 4 git commands to generate.