2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* Output argument typemap for connection output (always appends) */
26 %typemap(in, numinputs=0)
27 (const bt_connection **)
28 (bt_connection *temp_conn = NULL) {
33 (const bt_connection **) {
35 /* SWIG_Python_AppendOutput() steals the created object */
36 $result = SWIG_Python_AppendOutput($result,
37 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
38 SWIGTYPE_p_bt_connection, 0));
40 /* SWIG_Python_AppendOutput() steals Py_None */
42 $result = SWIG_Python_AppendOutput($result, Py_None);
46 /* Output argument typemap for component output (always appends) */
47 %typemap(in, numinputs=0)
48 (const bt_component_source **)
49 (bt_component_source *temp_comp = NULL) {
53 %typemap(in, numinputs=0)
54 (const bt_component_filter **)
55 (bt_component_filter *temp_comp = NULL) {
59 %typemap(in, numinputs=0)
60 (const bt_component_sink **)
61 (bt_component_sink *temp_comp = NULL) {
65 %typemap(argout) (const bt_component_source **) {
67 /* SWIG_Python_AppendOutput() steals the created object */
68 $result = SWIG_Python_AppendOutput($result,
69 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
70 SWIGTYPE_p_bt_component_source, 0));
72 /* SWIG_Python_AppendOutput() steals Py_None */
74 $result = SWIG_Python_AppendOutput($result, Py_None);
78 %typemap(argout) (const bt_component_filter **) {
80 /* SWIG_Python_AppendOutput() steals the created object */
81 $result = SWIG_Python_AppendOutput($result,
82 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
83 SWIGTYPE_p_bt_component_filter, 0));
85 /* SWIG_Python_AppendOutput() steals Py_None */
87 $result = SWIG_Python_AppendOutput($result, Py_None);
91 %typemap(argout) (const bt_component_sink **) {
93 /* SWIG_Python_AppendOutput() steals the created object */
94 $result = SWIG_Python_AppendOutput($result,
95 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
96 SWIGTYPE_p_bt_component_sink, 0));
98 /* SWIG_Python_AppendOutput() steals Py_None */
100 $result = SWIG_Python_AppendOutput($result, Py_None);
104 %include <babeltrace2/graph/graph-const.h>
105 %include <babeltrace2/graph/graph.h>
107 /* Helper functions for Python */
111 void graph_listener_removed(void *py_callable)
113 BT_ASSERT(py_callable);
114 Py_DECREF(py_callable);
117 static bt_graph_listener_func_status port_added_listener(
118 const void *component,
119 swig_type_info *component_swig_type,
120 bt_component_class_type component_class_type,
122 swig_type_info *port_swig_type,
123 bt_port_type port_type,
126 PyObject *py_component_ptr = NULL;
127 PyObject *py_port_ptr = NULL;
128 PyObject *py_res = NULL;
129 bt_graph_listener_func_status status;
131 py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0);
132 if (!py_component_ptr) {
133 BT_LOGF_STR("Failed to create component SWIG pointer object.");
134 status = __BT_FUNC_STATUS_MEMORY_ERROR;
138 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0);
140 BT_LOGF_STR("Failed to create port SWIG pointer object.");
141 status = __BT_FUNC_STATUS_MEMORY_ERROR;
145 py_res = PyObject_CallFunction(py_callable, "(OiOi)",
146 py_component_ptr, component_class_type, py_port_ptr, port_type);
150 status = __BT_FUNC_STATUS_ERROR;
154 BT_ASSERT(py_res == Py_None);
155 status = __BT_FUNC_STATUS_OK;
159 Py_XDECREF(py_port_ptr);
160 Py_XDECREF(py_component_ptr);
165 bt_graph_listener_func_status
166 source_component_output_port_added_listener(const bt_component_source *component_source,
167 const bt_port_output *port_output, void *py_callable)
169 return port_added_listener(
170 component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
171 port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable);
175 bt_graph_listener_func_status
176 filter_component_input_port_added_listener(const bt_component_filter *component_filter,
177 const bt_port_input *port_input, void *py_callable)
179 return port_added_listener(
180 component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
181 port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable);
185 bt_graph_listener_func_status
186 filter_component_output_port_added_listener(const bt_component_filter *component_filter,
187 const bt_port_output *port_output, void *py_callable)
189 return port_added_listener(
190 component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
191 port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable);
195 bt_graph_listener_func_status
196 sink_component_input_port_added_listener(const bt_component_sink *component_sink,
197 const bt_port_input *port_input, void *py_callable)
199 return port_added_listener(
200 component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
201 port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable);
205 PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph,
206 PyObject *py_callable)
208 PyObject *py_listener_ids = NULL;
209 PyObject *py_listener_id = NULL;
211 bt_graph_add_listener_status status;
214 BT_ASSERT(py_callable);
217 * Behind the scene, we will be registering 4 different listeners and
218 * return all of their ids.
220 py_listener_ids = PyTuple_New(4);
221 if (!py_listener_ids) {
225 /* source output port */
226 status = bt_graph_add_source_component_output_port_added_listener(
227 graph, source_component_output_port_added_listener,
228 graph_listener_removed, py_callable, &listener_id);
229 if (status != __BT_FUNC_STATUS_OK) {
233 py_listener_id = PyLong_FromLong(listener_id);
234 if (!py_listener_id) {
238 PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id);
239 py_listener_id = NULL;
241 /* filter input port */
242 status = bt_graph_add_filter_component_input_port_added_listener(
243 graph, filter_component_input_port_added_listener,
244 graph_listener_removed, py_callable, &listener_id);
245 if (status != __BT_FUNC_STATUS_OK) {
249 py_listener_id = PyLong_FromLong(listener_id);
250 if (!py_listener_id) {
254 PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id);
255 py_listener_id = NULL;
257 /* filter output port */
258 status = bt_graph_add_filter_component_output_port_added_listener(
259 graph, filter_component_output_port_added_listener,
260 graph_listener_removed, py_callable, &listener_id);
261 if (status != __BT_FUNC_STATUS_OK) {
265 py_listener_id = PyLong_FromLong(listener_id);
266 if (!py_listener_id) {
270 PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id);
271 py_listener_id = NULL;
273 /* sink input port */
274 status = bt_graph_add_sink_component_input_port_added_listener(
275 graph, sink_component_input_port_added_listener,
276 graph_listener_removed, py_callable, &listener_id);
277 if (status != __BT_FUNC_STATUS_OK) {
281 py_listener_id = PyLong_FromLong(listener_id);
282 if (!py_listener_id) {
287 PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id);
288 py_listener_id = NULL;
290 Py_INCREF(py_callable);
291 Py_INCREF(py_callable);
292 Py_INCREF(py_callable);
293 Py_INCREF(py_callable);
298 Py_XDECREF(py_listener_ids);
299 py_listener_ids = Py_None;
300 Py_INCREF(py_listener_ids);
304 Py_XDECREF(py_listener_id);
305 return py_listener_ids;
309 bt_graph_listener_func_status ports_connected_listener(
310 const void *upstream_component,
311 swig_type_info *upstream_component_swig_type,
312 bt_component_class_type upstream_component_class_type,
313 const bt_port_output *upstream_port,
314 const void *downstream_component,
315 swig_type_info *downstream_component_swig_type,
316 bt_component_class_type downstream_component_class_type,
317 const bt_port_input *downstream_port,
320 PyObject *py_upstream_component_ptr = NULL;
321 PyObject *py_upstream_port_ptr = NULL;
322 PyObject *py_downstream_component_ptr = NULL;
323 PyObject *py_downstream_port_ptr = NULL;
324 PyObject *py_res = NULL;
325 bt_graph_listener_func_status status;
327 py_upstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(upstream_component),
328 upstream_component_swig_type, 0);
329 if (!py_upstream_component_ptr) {
330 BT_LOGF_STR("Failed to create upstream component SWIG pointer object.");
331 status = __BT_FUNC_STATUS_MEMORY_ERROR;
335 py_upstream_port_ptr = SWIG_NewPointerObj(
336 SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port_output, 0);
337 if (!py_upstream_port_ptr) {
338 BT_LOGF_STR("Failed to create upstream port SWIG pointer object.");
339 status = __BT_FUNC_STATUS_MEMORY_ERROR;
343 py_downstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(downstream_component),
344 downstream_component_swig_type, 0);
345 if (!py_downstream_component_ptr) {
346 BT_LOGF_STR("Failed to create downstream component SWIG pointer object.");
347 status = __BT_FUNC_STATUS_MEMORY_ERROR;
351 py_downstream_port_ptr = SWIG_NewPointerObj(
352 SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port_input, 0);
353 if (!py_downstream_port_ptr) {
354 BT_LOGF_STR("Failed to create downstream port SWIG pointer object.");
355 status = __BT_FUNC_STATUS_MEMORY_ERROR;
359 py_res = PyObject_CallFunction(py_callable, "(OiOOiO)",
360 py_upstream_component_ptr, upstream_component_class_type,
361 py_upstream_port_ptr,
362 py_downstream_component_ptr, downstream_component_class_type,
363 py_downstream_port_ptr);
367 status = __BT_FUNC_STATUS_ERROR;
371 BT_ASSERT(py_res == Py_None);
372 status = __BT_FUNC_STATUS_OK;
375 Py_XDECREF(py_upstream_component_ptr);
376 Py_XDECREF(py_upstream_port_ptr);
377 Py_XDECREF(py_downstream_component_ptr);
378 Py_XDECREF(py_downstream_port_ptr);
384 bt_graph_listener_func_status source_filter_component_ports_connected_listener(
385 const bt_component_source *source_component,
386 const bt_component_filter *filter_component,
387 const bt_port_output *upstream_port,
388 const bt_port_input *downstream_port, void *py_callable)
390 return ports_connected_listener(
391 source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
393 filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
399 bt_graph_listener_func_status source_sink_component_ports_connected_listener(
400 const bt_component_source *source_component,
401 const bt_component_sink *sink_component,
402 const bt_port_output *upstream_port,
403 const bt_port_input *downstream_port, void *py_callable)
405 return ports_connected_listener(
406 source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
408 sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
414 bt_graph_listener_func_status filter_filter_component_ports_connected_listener(
415 const bt_component_filter *filter_component_left,
416 const bt_component_filter *filter_component_right,
417 const bt_port_output *upstream_port,
418 const bt_port_input *downstream_port, void *py_callable)
420 return ports_connected_listener(
421 filter_component_left, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
423 filter_component_right, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
429 bt_graph_listener_func_status filter_sink_component_ports_connected_listener(
430 const bt_component_filter *filter_component,
431 const bt_component_sink *sink_component,
432 const bt_port_output *upstream_port,
433 const bt_port_input *downstream_port, void *py_callable)
435 return ports_connected_listener(
436 filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
438 sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
444 PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph,
445 PyObject *py_callable)
447 PyObject *py_listener_ids = NULL;
448 PyObject *py_listener_id = NULL;
450 bt_graph_add_listener_status status;
453 BT_ASSERT(py_callable);
455 /* Behind the scene, we will be registering 4 different listeners and
456 * return all of their ids. */
457 py_listener_ids = PyTuple_New(4);
458 if (!py_listener_ids) {
462 /* source -> filter connection */
463 status = bt_graph_add_source_filter_component_ports_connected_listener(
464 graph, source_filter_component_ports_connected_listener,
465 graph_listener_removed, py_callable, &listener_id);
466 if (status != __BT_FUNC_STATUS_OK) {
470 py_listener_id = PyLong_FromLong(listener_id);
471 if (!py_listener_id) {
475 PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id);
476 py_listener_id = NULL;
478 /* source -> sink connection */
479 status = bt_graph_add_source_sink_component_ports_connected_listener(
480 graph, source_sink_component_ports_connected_listener,
481 graph_listener_removed, py_callable, &listener_id);
482 if (status != __BT_FUNC_STATUS_OK) {
486 py_listener_id = PyLong_FromLong(listener_id);
487 if (!py_listener_id) {
491 PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id);
492 py_listener_id = NULL;
494 /* filter -> filter connection */
495 status = bt_graph_add_filter_filter_component_ports_connected_listener(
496 graph, filter_filter_component_ports_connected_listener,
497 graph_listener_removed, py_callable, &listener_id);
498 if (status != __BT_FUNC_STATUS_OK) {
502 py_listener_id = PyLong_FromLong(listener_id);
503 if (!py_listener_id) {
507 PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id);
508 py_listener_id = NULL;
510 /* filter -> sink connection */
511 status = bt_graph_add_filter_sink_component_ports_connected_listener(
512 graph, filter_sink_component_ports_connected_listener,
513 graph_listener_removed, py_callable, &listener_id);
514 if (status != __BT_FUNC_STATUS_OK) {
518 py_listener_id = PyLong_FromLong(listener_id);
519 if (!py_listener_id) {
523 PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id);
524 py_listener_id = NULL;
526 Py_INCREF(py_callable);
527 Py_INCREF(py_callable);
528 Py_INCREF(py_callable);
529 Py_INCREF(py_callable);
534 Py_XDECREF(py_listener_ids);
535 py_listener_ids = Py_None;
536 Py_INCREF(py_listener_ids);
540 Py_XDECREF(py_listener_id);
541 return py_listener_ids;
545 PyObject *bt_bt2_graph_add_port_added_listener(struct bt_graph *graph,
546 PyObject *py_callable);
547 PyObject *bt_bt2_graph_add_ports_connected_listener(struct bt_graph *graph,
548 PyObject *py_callable);