lib, bt2: add precondition check for port name unicity
[babeltrace.git] / src / lib / graph / component-source.c
CommitLineData
d94d92ac 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
d94d92ac 5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
d94d92ac
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/COMPONENT-SOURCE"
c2d9d9cf 9#include "lib/logging.h"
d94d92ac 10
578e048b
MJ
11#include "common/assert.h"
12#include "lib/assert-pre.h"
13#include "compat/compiler.h"
43c59509
PP
14#include <babeltrace2/graph/self-component.h>
15#include <babeltrace2/graph/component.h>
3fadfbc0 16#include <babeltrace2/graph/graph.h>
d94d92ac 17
578e048b
MJ
18#include "component-source.h"
19#include "component.h"
20#include "port.h"
21#include "message/iterator.h"
d24d5663 22#include "lib/func-status.h"
578e048b 23
d94d92ac
PP
24BT_HIDDEN
25void bt_component_source_destroy(struct bt_component *component)
26{
27}
28
29BT_HIDDEN
30struct bt_component *bt_component_source_create(
0d72b8c3 31 const struct bt_component_class *class)
d94d92ac
PP
32{
33 struct bt_component_source *source = NULL;
34
17f3083a
SM
35 BT_ASSERT_PRE_NO_ERROR();
36
d94d92ac
PP
37 source = g_new0(struct bt_component_source, 1);
38 if (!source) {
870631a2
PP
39 BT_LIB_LOGE_APPEND_CAUSE(
40 "Failed to allocate one source component.");
d94d92ac
PP
41 goto end;
42 }
43
44end:
45 return (void *) source;
46}
47
752d0e47
SM
48const bt_component_class_source *
49bt_component_source_borrow_class_const(
50 const bt_component_source *component)
51{
52 struct bt_component_class *cls;
53
bdb288b3 54 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
752d0e47
SM
55
56 cls = component->parent.class;
57
98b15851
PP
58 BT_ASSERT_DBG(cls);
59 BT_ASSERT_DBG(cls->type == BT_COMPONENT_CLASS_TYPE_SOURCE);
752d0e47
SM
60
61 return (bt_component_class_source *) cls;
62}
63
d94d92ac 64uint64_t bt_component_source_get_output_port_count(
0d72b8c3 65 const struct bt_component_source *comp)
d94d92ac
PP
66{
67 return bt_component_get_output_port_count((void *) comp);
68}
69
0d72b8c3
PP
70const struct bt_port_output *
71bt_component_source_borrow_output_port_by_name_const(
72 const struct bt_component_source *comp, const char *name)
d94d92ac
PP
73{
74 return bt_component_borrow_output_port_by_name((void *) comp, name);
75}
76
77struct bt_self_component_port_output *
78bt_self_component_source_borrow_output_port_by_name(
79 struct bt_self_component_source *comp, const char *name)
80{
81 return (void *) bt_component_borrow_output_port_by_name(
82 (void *) comp, name);
83}
84
0d72b8c3
PP
85const struct bt_port_output *
86bt_component_source_borrow_output_port_by_index_const(
87 const struct bt_component_source *comp, uint64_t index)
d94d92ac
PP
88{
89 return bt_component_borrow_output_port_by_index((void *) comp, index);
90}
91
92struct bt_self_component_port_output *
93bt_self_component_source_borrow_output_port_by_index(
94 struct bt_self_component_source *comp, uint64_t index)
95{
96 return (void *) bt_component_borrow_output_port_by_index(
97 (void *) comp, index);
98}
99
d24d5663 100enum bt_self_component_add_port_status bt_self_component_source_add_output_port(
d94d92ac
PP
101 struct bt_self_component_source *self_comp,
102 const char *name, void *user_data,
103 struct bt_self_component_port_output **self_port)
104{
105 struct bt_component *comp = (void *) self_comp;
d24d5663 106 enum bt_self_component_add_port_status status;
d94d92ac
PP
107 struct bt_port *port = NULL;
108
17f3083a 109 BT_ASSERT_PRE_NO_ERROR();
157a98ed 110 BT_ASSERT_PRE_OUTPUT_PORT_NAME_UNIQUE(comp, name);
17f3083a 111
d94d92ac 112 /* bt_component_add_output_port() logs details and errors */
8cc56726 113 status = bt_component_add_output_port(comp, name, user_data, &port);
d24d5663 114 if (status != BT_FUNC_STATUS_OK) {
d94d92ac
PP
115 goto end;
116 }
117
118 if (self_port) {
119 /* Move reference to user */
120 *self_port = (void *) port;
d94d92ac
PP
121 }
122
123end:
124 bt_object_put_ref(port);
125 return status;
126}
c5b9b441
PP
127
128void bt_component_source_get_ref(
129 const struct bt_component_source *component_source)
130{
131 bt_object_get_ref(component_source);
132}
133
134void bt_component_source_put_ref(
135 const struct bt_component_source *component_source)
136{
137 bt_object_put_ref(component_source);
138}
This page took 0.059143 seconds and 4 git commands to generate.