Commit | Line | Data |
---|---|---|
6fa6bfa1 SM |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
fb25b9e3 | 25 | %include <babeltrace2/graph/message-iterator.h> |
e4b56bb9 SM |
26 | %include <babeltrace2/graph/port-output-message-iterator.h> |
27 | %include <babeltrace2/graph/self-component-port-input-message-iterator.h> | |
28 | %include <babeltrace2/graph/self-message-iterator.h> | |
6fa6bfa1 SM |
29 | |
30 | /* Helper functions for Python */ | |
31 | %{ | |
fb25b9e3 | 32 | static PyObject *bt_bt2_get_user_component_from_user_msg_iter( |
6fa6bfa1 SM |
33 | bt_self_message_iterator *self_message_iterator) |
34 | { | |
35 | bt_self_component *self_component = bt_self_message_iterator_borrow_component(self_message_iterator); | |
36 | PyObject *py_comp; | |
37 | ||
38 | BT_ASSERT(self_component); | |
39 | py_comp = bt_self_component_get_data(self_component); | |
40 | BT_ASSERT(py_comp); | |
41 | ||
42 | /* Return new reference */ | |
43 | Py_INCREF(py_comp); | |
44 | return py_comp; | |
45 | } | |
46 | ||
47 | static inline | |
48 | PyObject *create_pylist_from_messages(bt_message_array_const messages, | |
49 | uint64_t message_count) | |
50 | { | |
51 | uint64_t i; | |
52 | PyObject *py_msg_list = PyList_New(message_count); | |
fb25b9e3 | 53 | |
6fa6bfa1 | 54 | BT_ASSERT(py_msg_list); |
fb25b9e3 | 55 | |
6fa6bfa1 SM |
56 | for (i = 0; i < message_count; i++) { |
57 | PyList_SET_ITEM(py_msg_list, i, | |
58 | SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]), | |
59 | SWIGTYPE_p_bt_message, 0)); | |
60 | } | |
61 | ||
62 | return py_msg_list; | |
63 | } | |
64 | ||
65 | static | |
fb25b9e3 | 66 | PyObject *get_msg_range_common(bt_message_iterator_next_status status, |
6fa6bfa1 SM |
67 | bt_message_array_const messages, uint64_t message_count) |
68 | { | |
69 | PyObject *py_status; | |
70 | PyObject *py_return_tuple; | |
752f4edf | 71 | PyObject *py_msg_list; |
fb25b9e3 | 72 | |
6fa6bfa1 SM |
73 | py_return_tuple = PyTuple_New(2); |
74 | BT_ASSERT(py_return_tuple); | |
752f4edf SM |
75 | |
76 | /* Set tuple[0], status. */ | |
77 | py_status = SWIG_From_long_SS_long(status); | |
6fa6bfa1 | 78 | PyTuple_SET_ITEM(py_return_tuple, 0, py_status); |
752f4edf SM |
79 | |
80 | /* Set tuple[1], message list on success, None otherwise. */ | |
81 | if (status == __BT_FUNC_STATUS_OK) { | |
82 | py_msg_list = create_pylist_from_messages(messages, message_count); | |
83 | } else { | |
84 | py_msg_list = Py_None; | |
85 | Py_INCREF(py_msg_list); | |
86 | } | |
87 | ||
6fa6bfa1 | 88 | PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list); |
752f4edf | 89 | |
6fa6bfa1 SM |
90 | return py_return_tuple; |
91 | } | |
92 | ||
fb25b9e3 | 93 | static PyObject *bt_bt2_self_component_port_input_get_msg_range( |
6fa6bfa1 SM |
94 | bt_self_component_port_input_message_iterator *iter) |
95 | { | |
96 | bt_message_array_const messages; | |
97 | uint64_t message_count = 0; | |
fb25b9e3 | 98 | bt_message_iterator_next_status status; |
6fa6bfa1 | 99 | |
fb25b9e3 PP |
100 | status = bt_self_component_port_input_message_iterator_next(iter, |
101 | &messages, &message_count); | |
102 | return get_msg_range_common(status, messages, message_count); | |
6fa6bfa1 SM |
103 | } |
104 | ||
fb25b9e3 | 105 | static PyObject *bt_bt2_port_output_get_msg_range( |
6fa6bfa1 SM |
106 | bt_port_output_message_iterator *iter) |
107 | { | |
108 | bt_message_array_const messages; | |
109 | uint64_t message_count = 0; | |
fb25b9e3 | 110 | bt_message_iterator_next_status status; |
6fa6bfa1 | 111 | |
fb25b9e3 PP |
112 | status = bt_port_output_message_iterator_next(iter, &messages, |
113 | &message_count); | |
114 | return get_msg_range_common(status, messages, message_count); | |
6fa6bfa1 SM |
115 | } |
116 | %} | |
117 | ||
fb25b9e3 | 118 | PyObject *bt_bt2_get_user_component_from_user_msg_iter( |
6fa6bfa1 | 119 | bt_self_message_iterator *self_message_iterator); |
fb25b9e3 | 120 | PyObject *bt_bt2_self_component_port_input_get_msg_range( |
6fa6bfa1 | 121 | bt_self_component_port_input_message_iterator *iter); |
fb25b9e3 | 122 | PyObject *bt_bt2_port_output_get_msg_range( |
6fa6bfa1 | 123 | bt_port_output_message_iterator *iter); |