2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H
8 #define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H
10 static bt_graph_listener_func_status
port_added_listener(
11 const void *component
,
12 swig_type_info
*component_swig_type
,
13 bt_component_class_type component_class_type
,
15 swig_type_info
*port_swig_type
,
16 bt_port_type port_type
,
19 PyObject
*py_component_ptr
= NULL
;
20 PyObject
*py_port_ptr
= NULL
;
21 PyObject
*py_res
= NULL
;
22 bt_graph_listener_func_status status
;
24 py_component_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(component
), component_swig_type
, 0);
25 if (!py_component_ptr
) {
26 BT_LOGF_STR("Failed to create component SWIG pointer object.");
27 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
31 py_port_ptr
= SWIG_NewPointerObj(SWIG_as_voidptr(port
), port_swig_type
, 0);
33 BT_LOGF_STR("Failed to create port SWIG pointer object.");
34 status
= __BT_FUNC_STATUS_MEMORY_ERROR
;
38 py_res
= PyObject_CallFunction(py_callable
, "(OiOi)",
39 py_component_ptr
, component_class_type
, py_port_ptr
, port_type
);
41 loge_exception_append_cause_clear(
42 "Graph's port added listener (Python)",
44 status
= __BT_FUNC_STATUS_ERROR
;
48 BT_ASSERT(py_res
== Py_None
);
49 status
= __BT_FUNC_STATUS_OK
;
53 Py_XDECREF(py_port_ptr
);
54 Py_XDECREF(py_component_ptr
);
59 bt_graph_listener_func_status
60 source_component_output_port_added_listener(const bt_component_source
*component_source
,
61 const bt_port_output
*port_output
, void *py_callable
)
63 return port_added_listener(
64 component_source
, SWIGTYPE_p_bt_component_source
, BT_COMPONENT_CLASS_TYPE_SOURCE
,
65 port_output
, SWIGTYPE_p_bt_port_output
, BT_PORT_TYPE_OUTPUT
, py_callable
);
69 bt_graph_listener_func_status
70 filter_component_input_port_added_listener(const bt_component_filter
*component_filter
,
71 const bt_port_input
*port_input
, void *py_callable
)
73 return port_added_listener(
74 component_filter
, SWIGTYPE_p_bt_component_filter
, BT_COMPONENT_CLASS_TYPE_FILTER
,
75 port_input
, SWIGTYPE_p_bt_port_input
, BT_PORT_TYPE_INPUT
, py_callable
);
79 bt_graph_listener_func_status
80 filter_component_output_port_added_listener(const bt_component_filter
*component_filter
,
81 const bt_port_output
*port_output
, void *py_callable
)
83 return port_added_listener(
84 component_filter
, SWIGTYPE_p_bt_component_filter
, BT_COMPONENT_CLASS_TYPE_FILTER
,
85 port_output
, SWIGTYPE_p_bt_port_output
, BT_PORT_TYPE_OUTPUT
, py_callable
);
89 bt_graph_listener_func_status
90 sink_component_input_port_added_listener(const bt_component_sink
*component_sink
,
91 const bt_port_input
*port_input
, void *py_callable
)
93 return port_added_listener(
94 component_sink
, SWIGTYPE_p_bt_component_sink
, BT_COMPONENT_CLASS_TYPE_SINK
,
95 port_input
, SWIGTYPE_p_bt_port_input
, BT_PORT_TYPE_INPUT
, py_callable
);
99 PyObject
*bt_bt2_graph_add_port_added_listener(struct bt_graph
*graph
,
100 PyObject
*py_callable
)
102 PyObject
*py_listener_ids
= NULL
;
103 PyObject
*py_listener_id
= NULL
;
104 bt_listener_id listener_id
;
105 bt_graph_add_listener_status status
;
106 const char * const module_name
=
107 "graph_add_port_added_listener() (Python)";
110 BT_ASSERT(py_callable
);
113 * Behind the scene, we will be registering 4 different listeners and
114 * return all of their ids.
116 py_listener_ids
= PyTuple_New(4);
117 if (!py_listener_ids
) {
118 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
119 "Failed to allocate one PyTuple.");
123 /* source output port */
124 status
= bt_graph_add_source_component_output_port_added_listener(
125 graph
, source_component_output_port_added_listener
,
126 py_callable
, &listener_id
);
127 if (status
!= __BT_FUNC_STATUS_OK
) {
129 * bt_graph_add_source_component_output_port_added_listener has
130 * already logged/appended an error cause.
135 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
136 if (!py_listener_id
) {
137 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
138 "Failed to allocate one PyLong.");
142 PyTuple_SET_ITEM(py_listener_ids
, 0, py_listener_id
);
143 py_listener_id
= NULL
;
145 /* filter input port */
146 status
= bt_graph_add_filter_component_input_port_added_listener(
147 graph
, filter_component_input_port_added_listener
,
148 py_callable
, &listener_id
);
149 if (status
!= __BT_FUNC_STATUS_OK
) {
151 * bt_graph_add_filter_component_input_port_added_listener has
152 * already logged/appended an error cause.
157 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
158 if (!py_listener_id
) {
159 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
160 "Failed to allocate one PyLong.");
164 PyTuple_SET_ITEM(py_listener_ids
, 1, py_listener_id
);
165 py_listener_id
= NULL
;
167 /* filter output port */
168 status
= bt_graph_add_filter_component_output_port_added_listener(
169 graph
, filter_component_output_port_added_listener
,
170 py_callable
, &listener_id
);
171 if (status
!= __BT_FUNC_STATUS_OK
) {
173 * bt_graph_add_filter_component_output_port_added_listener has
174 * already logged/appended an error cause.
179 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
180 if (!py_listener_id
) {
181 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
182 "Failed to allocate one PyLong.");
186 PyTuple_SET_ITEM(py_listener_ids
, 2, py_listener_id
);
187 py_listener_id
= NULL
;
189 /* sink input port */
190 status
= bt_graph_add_sink_component_input_port_added_listener(
191 graph
, sink_component_input_port_added_listener
,
192 py_callable
, &listener_id
);
193 if (status
!= __BT_FUNC_STATUS_OK
) {
195 * bt_graph_add_sink_component_input_port_added_listener has
196 * already logged/appended an error cause.
201 py_listener_id
= PyLong_FromUnsignedLongLong(listener_id
);
202 if (!py_listener_id
) {
203 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name
,
204 "Failed to allocate one PyLong.");
208 PyTuple_SET_ITEM(py_listener_ids
, 3, py_listener_id
);
209 py_listener_id
= NULL
;
213 Py_XDECREF(py_listener_ids
);
214 py_listener_ids
= Py_None
;
215 Py_INCREF(py_listener_ids
);
218 Py_XDECREF(py_listener_id
);
219 return py_listener_ids
;
223 bt_graph_add_component_status
224 bt_bt2_graph_add_source_component(
226 const bt_component_class_source
*component_class
,
227 const char *name
, const bt_value
*params
,
228 PyObject
*obj
, bt_logging_level log_level
,
229 const bt_component_source
**component
)
231 return bt_graph_add_source_component_with_initialize_method_data(graph
,
232 component_class
, name
, params
, obj
== Py_None
? NULL
: obj
,
233 log_level
, component
);
237 bt_graph_add_component_status
238 bt_bt2_graph_add_filter_component(
240 const bt_component_class_filter
*component_class
,
241 const char *name
, const bt_value
*params
,
242 PyObject
*obj
, bt_logging_level log_level
,
243 const bt_component_filter
**component
)
245 return bt_graph_add_filter_component_with_initialize_method_data(graph
,
246 component_class
, name
, params
, obj
== Py_None
? NULL
: obj
,
247 log_level
, component
);
251 bt_graph_add_component_status
252 bt_bt2_graph_add_sink_component(
254 const bt_component_class_sink
*component_class
,
255 const char *name
, const bt_value
*params
,
256 PyObject
*obj
, bt_logging_level log_level
,
257 const bt_component_sink
**component
)
259 return bt_graph_add_sink_component_with_initialize_method_data(graph
,
260 component_class
, name
, params
, obj
== Py_None
? NULL
: obj
,
261 log_level
, component
);
264 #endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_GRAPH_I_H */