lib: graph: add "self" and some "private" APIs
[babeltrace.git] / lib / graph / port.c
index 6660c66aeebad3fdea2dcf3fc614158060d9e5ad..aa8ed98f26f03d865e670ded59fc9b8c459f2278 100644 (file)
@@ -1,8 +1,4 @@
 /*
- * port.c
- *
- * Babeltrace Port
- *
  * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
 #include <babeltrace/lib-logging-internal.h>
 
 #include <babeltrace/graph/port.h>
+#include <babeltrace/graph/self-component-port.h>
+#include <babeltrace/graph/self-component-port-input.h>
+#include <babeltrace/graph/self-component-port-output.h>
 #include <babeltrace/graph/component-internal.h>
 #include <babeltrace/graph/port-internal.h>
 #include <babeltrace/graph/connection-internal.h>
 #include <babeltrace/object-internal.h>
+#include <babeltrace/object.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/assert-internal.h>
+#include <babeltrace/assert-pre-internal.h>
 
 static
-void bt_port_destroy(struct bt_object *obj)
+void destroy_port(struct bt_object *obj)
 {
-       struct bt_port *port = container_of(obj, struct bt_port, base);
+       struct bt_port *port = (void *) obj;
 
-       BT_LOGD("Destroying port: addr=%p, name=\"%s\", comp-addr=%p",
-               port, bt_port_get_name(port), obj->parent);
+       BT_LIB_LOGD("Destroying port: %!+p", port);
 
        if (port->name) {
                g_string_free(port->name, TRUE);
+               port->name = NULL;
        }
 
        g_free(port);
 }
 
-struct bt_port *bt_port_borrow_from_private(
-               struct bt_private_port *private_port)
-{
-       return (void *) private_port;
-}
-
 BT_HIDDEN
 struct bt_port *bt_port_create(struct bt_component *parent_component,
                enum bt_port_type type, const char *name, void *user_data)
@@ -67,25 +62,17 @@ struct bt_port *bt_port_create(struct bt_component *parent_component,
        BT_ASSERT(name);
        BT_ASSERT(parent_component);
        BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
-
-       if (strlen(name) == 0) {
-               BT_LOGW_STR("Invalid parameter: name is an empty string.");
-               goto end;
-       }
-
+       BT_ASSERT(strlen(name) > 0);
        port = g_new0(struct bt_port, 1);
        if (!port) {
                BT_LOGE_STR("Failed to allocate one port.");
                goto end;
        }
 
-       BT_LOGD("Creating port for component: "
-               "comp-addr=%p, comp-name=\"%s\", port-type=%s, "
-               "port-name=\"%s\"",
-               parent_component, bt_component_get_name(parent_component),
-               bt_port_type_string(type), name);
-
-       bt_object_init_shared_with_parent(&port->base, bt_port_destroy);
+       BT_LIB_LOGD("Creating port for component: %![comp-]+c, port-type=%s, "
+               "port-name=\"%s\"", parent_component, bt_port_type_string(type),
+               name);
+       bt_object_init_shared_with_parent(&port->base, destroy_port);
        port->name = g_string_new(name);
        if (!port->name) {
                BT_LOGE_STR("Failed to allocate one GString.");
@@ -96,11 +83,8 @@ struct bt_port *bt_port_create(struct bt_component *parent_component,
        port->type = type;
        port->user_data = user_data;
        bt_object_set_parent(&port->base, &parent_component->base);
-       BT_LOGD("Created port for component: "
-               "comp-addr=%p, comp-name=\"%s\", port-type=%s, "
-               "port-name=\"%s\", port-addr=%p",
-               parent_component, bt_component_get_name(parent_component),
-               bt_port_type_string(type), name, port);
+       BT_LIB_LOGD("Created port for component: "
+               "%![comp-]+c, %![port-]+p", parent_component, port);
 
 end:
        return port;
@@ -108,51 +92,33 @@ end:
 
 const char *bt_port_get_name(struct bt_port *port)
 {
-       return port ? port->name->str : NULL;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->name->str;
 }
 
 enum bt_port_type bt_port_get_type(struct bt_port *port)
 {
-       return port ? port->type : BT_PORT_TYPE_UNKOWN;
-}
-
-struct bt_connection *bt_port_get_connection(struct bt_port *port)
-{
-       struct bt_connection *connection = NULL;
-
-       if (!port) {
-               BT_LOGW_STR("Invalid parameter: port is NULL.");
-               goto end;
-       }
-
-       if (!port->connection) {
-               /* Not an error: means disconnected */
-               goto end;
-       }
-
-       connection = bt_object_get_ref(port->connection);
-
-end:
-       return connection;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->type;
 }
 
-struct bt_component *bt_port_get_component(struct bt_port *port)
+struct bt_connection *bt_port_borrow_connection(struct bt_port *port)
 {
-       return (struct bt_component *) bt_object_get_parent(&port->base);
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->connection;
 }
 
-struct bt_private_connection *bt_private_port_get_connection(
-               struct bt_private_port *private_port)
+struct bt_component *bt_port_borrow_component(struct bt_port *port)
 {
-       return bt_private_connection_from_connection(bt_port_get_connection(
-               bt_port_borrow_from_private(private_port)));
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return (struct bt_component *) bt_object_borrow_parent(&port->base);
 }
 
-struct bt_private_component *bt_private_port_get_component(
-               struct bt_private_port *private_port)
+struct bt_self_component *bt_self_component_port_borrow_component(
+               struct bt_self_component_port *port)
 {
-       return bt_private_component_from_component(bt_port_get_component(
-               bt_port_borrow_from_private(private_port)));
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return (void *) bt_object_borrow_parent((void *) port);
 }
 
 BT_HIDDEN
@@ -165,86 +131,40 @@ void bt_port_set_connection(struct bt_port *port,
         * connection exists.
         */
        port->connection = connection;
-       BT_LOGV("Set port's connection: "
-               "port-addr=%p, port-name=\"%s\", conn-addr=%p",
-               port, bt_port_get_name(port), connection);
+       BT_LIB_LOGV("Set port's connection: %![port-]+p, %![conn-]+x", port,
+               connection);
 }
 
-enum bt_port_status bt_private_port_remove_from_component(
-               struct bt_private_port *private_port)
+enum bt_self_component_port_status bt_self_component_port_remove_from_component(
+               struct bt_self_component_port *self_port)
 {
-       enum bt_port_status status = BT_PORT_STATUS_OK;
-       struct bt_port *port = bt_port_borrow_from_private(private_port);
+       struct bt_port *port = (void *) self_port;
        struct bt_component *comp = NULL;
-       enum bt_component_status comp_status;
 
-       if (!port) {
-               BT_LOGW_STR("Invalid parameter: private port is NULL.");
-               status = BT_PORT_STATUS_INVALID;
-               goto end;
-       }
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
 
-       comp = (void *) bt_object_get_parent(&port->base);
+       comp = (void *) bt_object_borrow_parent(&port->base);
        if (!comp) {
-               BT_LOGV("Port already removed from its component: "
-                       "port-addr=%p, port-name=\"%s\", ",
-                       port, bt_port_get_name(port));
+               BT_LIB_LOGV("Port already removed from its component: %!+p",
+                       port);
                goto end;
        }
 
        /* bt_component_remove_port() logs details */
-       comp_status = bt_component_remove_port(comp, port);
-       BT_ASSERT(comp_status != BT_COMPONENT_STATUS_INVALID);
-       if (comp_status < 0) {
-               status = BT_PORT_STATUS_ERROR;
-               goto end;
-       }
+       bt_component_remove_port(comp, port);
 
 end:
-       bt_object_put_ref(comp);
-       return status;
-}
-
-enum bt_port_status bt_port_disconnect(struct bt_port *port)
-{
-       enum bt_port_status status = BT_PORT_STATUS_OK;
-
-       if (!port) {
-               BT_LOGW_STR("Invalid parameter: port is NULL.");
-               status = BT_PORT_STATUS_INVALID;
-               goto end;
-       }
-
-       if (port->connection) {
-               bt_connection_end(port->connection, true);
-               BT_LOGV("Disconnected port: "
-                       "port-addr=%p, port-name=\"%s\"",
-                       port, bt_port_get_name(port));
-       }
-
-end:
-       return status;
+       return BT_SELF_PORT_STATUS_OK;
 }
 
 bt_bool bt_port_is_connected(struct bt_port *port)
 {
-       int ret;
-
-       if (!port) {
-               BT_LOGW_STR("Invalid parameter: port is NULL.");
-               ret = -1;
-               goto end;
-       }
-
-       ret = port->connection ? 1 : 0;
-
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->connection ? BT_TRUE : BT_FALSE;
 }
 
-void *bt_private_port_get_user_data(
-               struct bt_private_port *private_port)
+void *bt_self_component_port_get_data(struct bt_self_component_port *port)
 {
-       return private_port ?
-               bt_port_borrow_from_private(private_port)->user_data : NULL;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return ((struct bt_port *) port)->user_data;
 }
This page took 0.026672 seconds and 4 git commands to generate.