lib: make message iterator creation functions return a status
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_message_iterator.i.h
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
25 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
26 bt_bt2_self_component_port_input_message_iterator_create_from_message_iterator(
27 bt_self_message_iterator *self_msg_iter,
28 bt_self_component_port_input *input_port,
29 bt_self_component_port_input_message_iterator **message_iterator)
30 {
31 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
32 status;
33
34 status = bt_self_component_port_input_message_iterator_create_from_message_iterator(
35 self_msg_iter, input_port, message_iterator);
36
37 if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
38 *message_iterator = NULL;
39 }
40
41 return status;
42 }
43
44 bt_self_component_port_input_message_iterator_create_from_sink_component_status
45 bt_bt2_self_component_port_input_message_iterator_create_from_sink_component(
46 bt_self_component_sink *self_comp,
47 bt_self_component_port_input *input_port,
48 bt_self_component_port_input_message_iterator **message_iterator)
49 {
50 bt_self_component_port_input_message_iterator_create_from_sink_component_status
51 status;
52
53 status = bt_self_component_port_input_message_iterator_create_from_sink_component(
54 self_comp, input_port, message_iterator);
55
56 if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
57 *message_iterator = NULL;
58 }
59
60 return status;
61 }
62
63 static PyObject *bt_bt2_get_user_component_from_user_msg_iter(
64 bt_self_message_iterator *self_message_iterator)
65 {
66 bt_self_component *self_component = bt_self_message_iterator_borrow_component(self_message_iterator);
67 PyObject *py_comp;
68
69 BT_ASSERT(self_component);
70 py_comp = bt_self_component_get_data(self_component);
71 BT_ASSERT(py_comp);
72
73 /* Return new reference */
74 Py_INCREF(py_comp);
75 return py_comp;
76 }
77
78 static inline
79 PyObject *create_pylist_from_messages(bt_message_array_const messages,
80 uint64_t message_count)
81 {
82 uint64_t i;
83 PyObject *py_msg_list = PyList_New(message_count);
84
85 BT_ASSERT(py_msg_list);
86
87 for (i = 0; i < message_count; i++) {
88 PyList_SET_ITEM(py_msg_list, i,
89 SWIG_NewPointerObj(SWIG_as_voidptr(messages[i]),
90 SWIGTYPE_p_bt_message, 0));
91 }
92
93 return py_msg_list;
94 }
95
96 static
97 PyObject *get_msg_range_common(bt_message_iterator_next_status status,
98 bt_message_array_const messages, uint64_t message_count)
99 {
100 PyObject *py_status;
101 PyObject *py_return_tuple;
102 PyObject *py_msg_list;
103
104 py_return_tuple = PyTuple_New(2);
105 BT_ASSERT(py_return_tuple);
106
107 /* Set tuple[0], status. */
108 py_status = SWIG_From_long_SS_long(status);
109 PyTuple_SET_ITEM(py_return_tuple, 0, py_status);
110
111 /* Set tuple[1], message list on success, None otherwise. */
112 if (status == __BT_FUNC_STATUS_OK) {
113 py_msg_list = create_pylist_from_messages(messages, message_count);
114 } else {
115 py_msg_list = Py_None;
116 Py_INCREF(py_msg_list);
117 }
118
119 PyTuple_SET_ITEM(py_return_tuple, 1, py_msg_list);
120
121 return py_return_tuple;
122 }
123
124 static PyObject *bt_bt2_self_component_port_input_get_msg_range(
125 bt_self_component_port_input_message_iterator *iter)
126 {
127 bt_message_array_const messages;
128 uint64_t message_count = 0;
129 bt_message_iterator_next_status status;
130
131 status = bt_self_component_port_input_message_iterator_next(iter,
132 &messages, &message_count);
133 return get_msg_range_common(status, messages, message_count);
134 }
This page took 0.034737 seconds and 4 git commands to generate.