Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
811644b8 PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
811644b8 | 4 | |
894a8df5 | 5 | from bt2 import native_bt, object |
79935628 SM |
6 | |
7 | ||
8 | def _bt2_connection(): | |
9 | from bt2 import connection as bt2_connection | |
10 | ||
11 | return bt2_connection | |
811644b8 PP |
12 | |
13 | ||
5813b3a3 | 14 | def _create_from_const_ptr_and_get_ref(ptr, port_type): |
2c67587a SM |
15 | cls = _PORT_TYPE_TO_PYCLS.get(port_type, None) |
16 | ||
17 | if cls is None: | |
d24d5663 | 18 | raise TypeError('unknown port type: {}'.format(port_type)) |
2c67587a SM |
19 | |
20 | return cls._create_from_ptr_and_get_ref(ptr) | |
21 | ||
22 | ||
23 | def _create_self_from_ptr_and_get_ref(ptr, port_type): | |
24 | cls = _PORT_TYPE_TO_USER_PYCLS.get(port_type, None) | |
25 | ||
26 | if cls is None: | |
d24d5663 | 27 | raise TypeError('unknown port type: {}'.format(port_type)) |
2c67587a SM |
28 | |
29 | return cls._create_from_ptr_and_get_ref(ptr) | |
30 | ||
31 | ||
5813b3a3 | 32 | class _PortConst(object._SharedObject): |
894a8df5 SM |
33 | @classmethod |
34 | def _get_ref(cls, ptr): | |
35 | ptr = cls._as_port_ptr(ptr) | |
36 | return native_bt.port_get_ref(ptr) | |
811644b8 | 37 | |
894a8df5 SM |
38 | @classmethod |
39 | def _put_ref(cls, ptr): | |
40 | ptr = cls._as_port_ptr(ptr) | |
41 | return native_bt.port_put_ref(ptr) | |
811644b8 PP |
42 | |
43 | @property | |
44 | def name(self): | |
894a8df5 SM |
45 | ptr = self._as_port_ptr(self._ptr) |
46 | name = native_bt.port_get_name(ptr) | |
47 | assert name is not None | |
48 | return name | |
811644b8 PP |
49 | |
50 | @property | |
51 | def connection(self): | |
894a8df5 SM |
52 | ptr = self._as_port_ptr(self._ptr) |
53 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
811644b8 PP |
54 | |
55 | if conn_ptr is None: | |
56 | return | |
57 | ||
79935628 | 58 | return _bt2_connection()._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 PP |
59 | |
60 | @property | |
61 | def is_connected(self): | |
62 | return self.connection is not None | |
63 | ||
811644b8 | 64 | |
5813b3a3 | 65 | class _InputPortConst(_PortConst): |
894a8df5 | 66 | _as_port_ptr = staticmethod(native_bt.port_input_as_port_const) |
811644b8 PP |
67 | |
68 | ||
5813b3a3 | 69 | class _OutputPortConst(_PortConst): |
894a8df5 | 70 | _as_port_ptr = staticmethod(native_bt.port_output_as_port_const) |
dc43190b | 71 | |
dc43190b | 72 | |
5813b3a3 | 73 | class _UserComponentPort(_PortConst): |
894a8df5 SM |
74 | @classmethod |
75 | def _as_port_ptr(cls, ptr): | |
76 | ptr = cls._as_self_port_ptr(ptr) | |
77 | return native_bt.self_component_port_as_port(ptr) | |
811644b8 PP |
78 | |
79 | @property | |
80 | def connection(self): | |
894a8df5 SM |
81 | ptr = self._as_port_ptr(self._ptr) |
82 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
811644b8 PP |
83 | |
84 | if conn_ptr is None: | |
85 | return | |
86 | ||
79935628 | 87 | return _bt2_connection()._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 | 88 | |
2e00bc76 SM |
89 | @property |
90 | def user_data(self): | |
91 | ptr = self._as_self_port_ptr(self._ptr) | |
92 | return native_bt.self_component_port_get_data(ptr) | |
93 | ||
811644b8 | 94 | |
5813b3a3 | 95 | class _UserComponentInputPort(_UserComponentPort, _InputPortConst): |
cfbd7cf3 FD |
96 | _as_self_port_ptr = staticmethod( |
97 | native_bt.self_component_port_input_as_self_component_port | |
98 | ) | |
811644b8 PP |
99 | |
100 | ||
5813b3a3 | 101 | class _UserComponentOutputPort(_UserComponentPort, _OutputPortConst): |
cfbd7cf3 FD |
102 | _as_self_port_ptr = staticmethod( |
103 | native_bt.self_component_port_output_as_self_component_port | |
104 | ) | |
2c67587a SM |
105 | |
106 | ||
107 | _PORT_TYPE_TO_PYCLS = { | |
5813b3a3 FD |
108 | native_bt.PORT_TYPE_INPUT: _InputPortConst, |
109 | native_bt.PORT_TYPE_OUTPUT: _OutputPortConst, | |
2c67587a SM |
110 | } |
111 | ||
112 | ||
113 | _PORT_TYPE_TO_USER_PYCLS = { | |
114 | native_bt.PORT_TYPE_INPUT: _UserComponentInputPort, | |
115 | native_bt.PORT_TYPE_OUTPUT: _UserComponentOutputPort, | |
116 | } |