List detected component classes
[babeltrace.git] / plugins / component-factory.c
index 8dc6103d9f530ebfa3b90ca0019ad94756b90de2..65840d4cac79e883b6a025846d2405b099b3d9a2 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <babeltrace/plugin/component-factory.h>
 #include <babeltrace/plugin/component-factory-internal.h>
+#include <babeltrace/plugin/component-class-internal.h>
 #include <babeltrace/plugin/source-internal.h>
 #include <babeltrace/plugin/sink-internal.h>
 #include <babeltrace/babeltrace-internal.h>
@@ -37,6 +38,7 @@
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <gmodule.h>
+#include <stdbool.h>
 
 #define NATIVE_PLUGIN_SUFFIX ".so"
 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
@@ -110,7 +112,7 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
        }
 
        /* Load plugin and make sure it defines the required entry points */
-       plugin = bt_plugin_create(module);
+       plugin = bt_plugin_create(module, path);
        if (!plugin) {
                ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN;
                if (!g_module_close(module)) {
@@ -120,8 +122,10 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
                goto end;
        }
 
+       factory->current_plugin = plugin;
        component_status = bt_plugin_register_component_classes(plugin,
                        factory);
+       factory->current_plugin = NULL;
        if (component_status != BT_COMPONENT_STATUS_OK) {
                switch (component_status) {
                case BT_COMPONENT_STATUS_NOMEM:
@@ -135,7 +139,6 @@ bt_component_factory_load_file(struct bt_component_factory *factory,
                BT_PUT(plugin);
                goto end;
        }
-       g_ptr_array_add(factory->plugins, plugin);
 end:
        return ret;
 }
@@ -241,9 +244,6 @@ void bt_component_factory_destroy(struct bt_object *obj)
        assert(obj);
        factory = container_of(obj, struct bt_component_factory, base);
 
-       if (factory->plugins) {
-               g_ptr_array_free(factory->plugins, TRUE);
-       }
        if (factory->component_classes) {
                g_ptr_array_free(factory->component_classes, TRUE);
        }
@@ -260,11 +260,6 @@ struct bt_component_factory *bt_component_factory_create(void)
        }
 
        bt_object_init(factory, bt_component_factory_destroy);
-       factory->plugins = g_ptr_array_new_with_free_func(
-               (GDestroyNotify) bt_put);
-       if (!factory->plugins) {
-               goto error;
-       }
        factory->component_classes = g_ptr_array_new_with_free_func(
                (GDestroyNotify) bt_put);
        if (!factory->component_classes) {
@@ -277,11 +272,84 @@ error:
        return factory;
 }
 
-struct bt_object *bt_component_factory_get_components(
+int bt_component_factory_get_component_class_count(
                struct bt_component_factory *factory)
 {
-       assert(0);
+       return factory ? factory->component_classes->len : -1;
+}
+
+struct bt_component_class *bt_component_factory_get_component_class_index(
+               struct bt_component_factory *factory, int index)
+{
+       struct bt_component_class *component_class = NULL;
+
+       if (!factory || index < 0 || index >= factory->component_classes->len) {
+               goto end;
+       }
+
+       component_class = bt_get(g_ptr_array_index(
+                       factory->component_classes, index));
+end:
+       return component_class;
+}
+
+struct bt_component_class *bt_component_factory_get_component_class(
+               struct bt_component_factory *factory,
+               const char *plugin_name, enum bt_component_type type,
+               const char *component_name)
+{
+       size_t i;
+       struct bt_component_class *component_class = NULL;
+
+       if (!factory || (!plugin_name && !component_name &&
+                       type == BT_COMPONENT_TYPE_UNKNOWN)) {
+               /* At least one criterion must be provided. */
+               goto no_match;
+       }
+
+       for (i = 0; i < factory->component_classes->len; i++) {
+               struct bt_plugin *plugin = NULL;
+
+               component_class = g_ptr_array_index(factory->component_classes,
+                               i);
+               plugin = bt_component_class_get_plugin(component_class);
+               assert(plugin);
+
+               if (type != BT_COMPONENT_TYPE_UNKNOWN) {
+                       if (type != bt_component_class_get_type(
+                                       component_class)) {
+                               continue;
+                       }
+               }
+
+               if (plugin_name) {
+                       const char *cur_plugin_name = bt_plugin_get_name(
+                                       plugin);
+
+                       assert(cur_plugin_name);
+                       if (strcmp(plugin_name, cur_plugin_name)) {
+                               continue;
+                       }
+               }
+
+               if (component_name) {
+                       const char *cur_cc_name = bt_component_class_get_name(
+                                       component_class);
+
+                       assert(cur_cc_name);
+                       if (strcmp(component_name, cur_cc_name)) {
+                               continue;
+                       }
+               }
+
+               /* All criteria met. */
+               goto match;
+       }
+
+no_match:
        return NULL;
+match:
+       return bt_get(component_class);
 }
 
 enum bt_component_factory_status bt_component_factory_load(
@@ -312,19 +380,11 @@ end:
        return ret;
 }
 
+static
 enum bt_component_factory_status
-bt_component_factory_register_source_component_class(
-               struct bt_component_factory *factory, const char *name,
-               bt_component_source_init_cb init)
-{
-       assert(0);
-       return BT_COMPONENT_FACTORY_STATUS_ERROR;
-}
-
-enum bt_component_factory_status
-bt_component_factory_register_sink_component_class(
-               struct bt_component_factory *factory, const char *name,
-               bt_component_sink_init_cb init)
+add_component_class(struct bt_component_factory *factory, const char *name,
+                   const char *description, bt_component_init_cb init,
+                   enum bt_component_type type)
 {
        struct bt_component_class *class;
        enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK;
@@ -334,7 +394,27 @@ bt_component_factory_register_sink_component_class(
                goto end;
        }
 
-       
+       class = bt_component_class_create(type, name, description,
+                       factory->current_plugin);
+       g_ptr_array_add(factory->component_classes, class);
 end:
        return ret;
 }
+
+enum bt_component_factory_status
+bt_component_factory_register_source_component_class(
+               struct bt_component_factory *factory, const char *name,
+               const char *description, bt_component_init_cb init)
+{
+       return add_component_class(factory, name, description, init,
+                       BT_COMPONENT_TYPE_SOURCE);
+}
+
+enum bt_component_factory_status
+bt_component_factory_register_sink_component_class(
+               struct bt_component_factory *factory, const char *name,
+               const char *description, bt_component_init_cb init)
+{
+       return add_component_class(factory, name, description, init,
+                       BT_COMPONENT_TYPE_SINK);
+}
This page took 0.025423 seconds and 4 git commands to generate.