lib, bt2: add precondition check for port name unicity
[babeltrace.git] / src / lib / graph / component.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #ifndef BABELTRACE_GRAPH_COMPONENT_INTERNAL_H
9 #define BABELTRACE_GRAPH_COMPONENT_INTERNAL_H
10
11 #include "common/macros.h"
12 #include <babeltrace2/graph/component.h>
13 #include <babeltrace2/graph/component-class.h>
14 #include "lib/object.h"
15 #include <babeltrace2/types.h>
16 #include <babeltrace2/logging.h>
17 #include "common/assert.h"
18 #include <glib.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21
22 #include "component-class.h"
23 #include "port.h"
24
25 #define BT_ASSERT_PRE_INPUT_PORT_NAME_UNIQUE(comp, name) \
26 BT_ASSERT_PRE(bt_component_port_name_is_unique(comp->input_ports, name), \
27 "Input port name is not unique: name=\"%s\", %![comp-]c", name, comp);
28
29 #define BT_ASSERT_PRE_OUTPUT_PORT_NAME_UNIQUE(comp, name) \
30 BT_ASSERT_PRE(bt_component_port_name_is_unique(comp->output_ports, name), \
31 "Output port name is not unique: name=\"%s\", %![comp-]c", name, comp);
32
33 typedef void (*bt_component_destroy_listener_func)(
34 struct bt_component *class, void *data);
35
36 struct bt_component_destroy_listener {
37 bt_component_destroy_listener_func func;
38 void *data;
39 };
40
41 struct bt_graph;
42
43 struct bt_component {
44 struct bt_object base;
45 struct bt_component_class *class;
46 GString *name;
47 bt_logging_level log_level;
48
49 /*
50 * Internal destroy function specific to a source, filter, or
51 * sink component object.
52 */
53 void (*destroy)(struct bt_component *);
54
55 /* User-defined data */
56 void *user_data;
57
58 /* Input and output ports (weak references) */
59 GPtrArray *input_ports;
60 GPtrArray *output_ports;
61
62 /* Array of struct bt_component_destroy_listener */
63 GArray *destroy_listeners;
64
65 bool initialized;
66 };
67
68 static inline
69 struct bt_graph *bt_component_borrow_graph(struct bt_component *comp)
70 {
71 BT_ASSERT_DBG(comp);
72 return (void *) bt_object_borrow_parent(&comp->base);
73 }
74
75 BT_HIDDEN
76 int bt_component_create(struct bt_component_class *component_class,
77 const char *name, bt_logging_level log_level,
78 struct bt_component **component);
79
80 BT_HIDDEN
81 enum bt_component_class_port_connected_method_status
82 bt_component_port_connected(
83 struct bt_component *comp,
84 struct bt_port *self_port, struct bt_port *other_port);
85
86 BT_HIDDEN
87 void bt_component_set_graph(struct bt_component *component,
88 struct bt_graph *graph);
89
90 BT_HIDDEN
91 uint64_t bt_component_get_input_port_count(const struct bt_component *comp);
92
93 BT_HIDDEN
94 uint64_t bt_component_get_output_port_count(const struct bt_component *comp);
95
96 BT_HIDDEN
97 struct bt_port_input *bt_component_borrow_input_port_by_index(
98 struct bt_component *comp, uint64_t index);
99
100 BT_HIDDEN
101 struct bt_port_output *bt_component_borrow_output_port_by_index(
102 struct bt_component *comp, uint64_t index);
103
104 BT_HIDDEN
105 struct bt_port_input *bt_component_borrow_input_port_by_name(
106 struct bt_component *comp, const char *name);
107
108 BT_HIDDEN
109 struct bt_port_output *bt_component_borrow_output_port_by_name(
110 struct bt_component *comp, const char *name);
111
112 BT_HIDDEN
113 enum bt_self_component_add_port_status bt_component_add_input_port(
114 struct bt_component *component, const char *name,
115 void *user_data, struct bt_port **port);
116
117 BT_HIDDEN
118 enum bt_self_component_add_port_status bt_component_add_output_port(
119 struct bt_component *component, const char *name,
120 void *user_data, struct bt_port **port);
121
122 BT_HIDDEN
123 bool bt_component_port_name_is_unique(GPtrArray *ports, const char *name);
124
125 BT_HIDDEN
126 void bt_component_remove_port(struct bt_component *component,
127 struct bt_port *port);
128
129 BT_HIDDEN
130 void bt_component_add_destroy_listener(struct bt_component *component,
131 bt_component_destroy_listener_func func, void *data);
132
133 BT_HIDDEN
134 void bt_component_remove_destroy_listener(struct bt_component *component,
135 bt_component_destroy_listener_func func, void *data);
136
137 #endif /* BABELTRACE_GRAPH_COMPONENT_INTERNAL_H */
This page took 0.034168 seconds and 4 git commands to generate.