Commit | Line | Data |
---|---|---|
4212232c | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
4212232c PP |
3 | * |
4 | * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
4212232c PP |
5 | */ |
6 | ||
ae2be88d SM |
7 | #ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_H |
8 | #define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_H | |
9 | ||
7c7301d5 | 10 | static |
9a2c8b8e PP |
11 | bt_message_iterator_create_from_message_iterator_status |
12 | bt_bt2_message_iterator_create_from_message_iterator( | |
e803df70 SM |
13 | bt_self_message_iterator *self_msg_iter, |
14 | bt_self_component_port_input *input_port, | |
9a2c8b8e | 15 | bt_message_iterator **message_iterator) |
e803df70 | 16 | { |
9a2c8b8e | 17 | bt_message_iterator_create_from_message_iterator_status |
e803df70 SM |
18 | status; |
19 | ||
9a2c8b8e | 20 | status = bt_message_iterator_create_from_message_iterator( |
e803df70 SM |
21 | self_msg_iter, input_port, message_iterator); |
22 | ||
9a2c8b8e | 23 | if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) { |
e803df70 SM |
24 | *message_iterator = NULL; |
25 | } | |
26 | ||
27 | return status; | |
28 | } | |
29 | ||
7c7301d5 | 30 | static |
9a2c8b8e PP |
31 | bt_message_iterator_create_from_sink_component_status |
32 | bt_bt2_message_iterator_create_from_sink_component( | |
e803df70 SM |
33 | bt_self_component_sink *self_comp, |
34 | bt_self_component_port_input *input_port, | |
9a2c8b8e | 35 | bt_message_iterator **message_iterator) |
e803df70 | 36 | { |
9a2c8b8e | 37 | bt_message_iterator_create_from_sink_component_status |
e803df70 SM |
38 | status; |
39 | ||
9a2c8b8e | 40 | status = bt_message_iterator_create_from_sink_component( |
e803df70 SM |
41 | self_comp, input_port, message_iterator); |
42 | ||
9a2c8b8e | 43 | if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { |
e803df70 SM |
44 | *message_iterator = NULL; |
45 | } | |
46 | ||
47 | return status; | |
48 | } | |
49 | ||
4212232c PP |
50 | static PyObject *bt_bt2_get_user_component_from_user_msg_iter( |
51 | bt_self_message_iterator *self_message_iterator) | |
52 | { | |
53 | bt_self_component *self_component = bt_self_message_iterator_borrow_component(self_message_iterator); | |
54 | PyObject *py_comp; | |
55 | ||
98b15851 | 56 | BT_ASSERT_DBG(self_component); |
4212232c | 57 | py_comp = bt_self_component_get_data(self_component); |
98b15851 | 58 | BT_ASSERT_DBG(py_comp); |
4212232c PP |
59 | |
60 | /* Return new reference */ | |
61 | Py_INCREF(py_comp); | |
62 | return py_comp; | |
63 | } | |
64 | ||
65 | static inline | |
66 | PyObject *create_pylist_from_messages(bt_message_array_const messages, | |
67 | uint64_t message_count) | |
68 | { | |
69 | uint64_t i; | |
70 | PyObject *py_msg_list = PyList_New(message_count); | |
71 | ||
72 | BT_ASSERT(py_msg_list); | |
73 | ||
74 | for (i = 0; i < message_count; i++) { | |
75 | PyList_SET_ITEM(py_msg_list, i, | |
76 | SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]), | |
77 | SWIGTYPE_p_bt_message, 0)); | |
78 | } | |
79 | ||
80 | return py_msg_list; | |
81 | } | |
82 | ||
83 | static | |
84 | PyObject *get_msg_range_common(bt_message_iterator_next_status status, | |
85 | bt_message_array_const messages, uint64_t message_count) | |
86 | { | |
87 | PyObject *py_status; | |
88 | PyObject *py_return_tuple; | |
89 | PyObject *py_msg_list; | |
90 | ||
91 | py_return_tuple = PyTuple_New(2); | |
92 | BT_ASSERT(py_return_tuple); | |
93 | ||
94 | /* Set tuple[0], status. */ | |
95 | py_status = SWIG_From_long_SS_long(status); | |
96 | PyTuple_SET_ITEM(py_return_tuple, 0, py_status); | |
97 | ||
98 | /* Set tuple[1], message list on success, None otherwise. */ | |
99 | if (status == __BT_FUNC_STATUS_OK) { | |
100 | py_msg_list = create_pylist_from_messages(messages, message_count); | |
101 | } else { | |
102 | py_msg_list = Py_None; | |
103 | Py_INCREF(py_msg_list); | |
104 | } | |
105 | ||
106 | PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list); | |
107 | ||
108 | return py_return_tuple; | |
109 | } | |
110 | ||
111 | static PyObject *bt_bt2_self_component_port_input_get_msg_range( | |
9a2c8b8e | 112 | bt_message_iterator *iter) |
4212232c PP |
113 | { |
114 | bt_message_array_const messages; | |
115 | uint64_t message_count = 0; | |
116 | bt_message_iterator_next_status status; | |
117 | ||
9a2c8b8e | 118 | status = bt_message_iterator_next(iter, |
4212232c PP |
119 | &messages, &message_count); |
120 | return get_msg_range_common(status, messages, message_count); | |
121 | } | |
ae2be88d SM |
122 | |
123 | #endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_MESSAGE_ITERATOR_I_H */ |