Commit | Line | Data |
---|---|---|
811644b8 PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | ||
894a8df5 | 23 | from bt2 import native_bt, object |
79935628 SM |
24 | |
25 | ||
26 | def _bt2_connection(): | |
27 | from bt2 import connection as bt2_connection | |
28 | ||
29 | return bt2_connection | |
811644b8 PP |
30 | |
31 | ||
5813b3a3 | 32 | def _create_from_const_ptr_and_get_ref(ptr, port_type): |
2c67587a SM |
33 | cls = _PORT_TYPE_TO_PYCLS.get(port_type, None) |
34 | ||
35 | if cls is None: | |
d24d5663 | 36 | raise TypeError('unknown port type: {}'.format(port_type)) |
2c67587a SM |
37 | |
38 | return cls._create_from_ptr_and_get_ref(ptr) | |
39 | ||
40 | ||
41 | def _create_self_from_ptr_and_get_ref(ptr, port_type): | |
42 | cls = _PORT_TYPE_TO_USER_PYCLS.get(port_type, None) | |
43 | ||
44 | if cls is None: | |
d24d5663 | 45 | raise TypeError('unknown port type: {}'.format(port_type)) |
2c67587a SM |
46 | |
47 | return cls._create_from_ptr_and_get_ref(ptr) | |
48 | ||
49 | ||
5813b3a3 | 50 | class _PortConst(object._SharedObject): |
894a8df5 SM |
51 | @classmethod |
52 | def _get_ref(cls, ptr): | |
53 | ptr = cls._as_port_ptr(ptr) | |
54 | return native_bt.port_get_ref(ptr) | |
811644b8 | 55 | |
894a8df5 SM |
56 | @classmethod |
57 | def _put_ref(cls, ptr): | |
58 | ptr = cls._as_port_ptr(ptr) | |
59 | return native_bt.port_put_ref(ptr) | |
811644b8 PP |
60 | |
61 | @property | |
62 | def name(self): | |
894a8df5 SM |
63 | ptr = self._as_port_ptr(self._ptr) |
64 | name = native_bt.port_get_name(ptr) | |
65 | assert name is not None | |
66 | return name | |
811644b8 PP |
67 | |
68 | @property | |
69 | def connection(self): | |
894a8df5 SM |
70 | ptr = self._as_port_ptr(self._ptr) |
71 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
811644b8 PP |
72 | |
73 | if conn_ptr is None: | |
74 | return | |
75 | ||
79935628 | 76 | return _bt2_connection()._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 PP |
77 | |
78 | @property | |
79 | def is_connected(self): | |
80 | return self.connection is not None | |
81 | ||
811644b8 | 82 | |
5813b3a3 | 83 | class _InputPortConst(_PortConst): |
894a8df5 | 84 | _as_port_ptr = staticmethod(native_bt.port_input_as_port_const) |
811644b8 PP |
85 | |
86 | ||
5813b3a3 | 87 | class _OutputPortConst(_PortConst): |
894a8df5 | 88 | _as_port_ptr = staticmethod(native_bt.port_output_as_port_const) |
dc43190b | 89 | |
dc43190b | 90 | |
5813b3a3 | 91 | class _UserComponentPort(_PortConst): |
894a8df5 SM |
92 | @classmethod |
93 | def _as_port_ptr(cls, ptr): | |
94 | ptr = cls._as_self_port_ptr(ptr) | |
95 | return native_bt.self_component_port_as_port(ptr) | |
811644b8 PP |
96 | |
97 | @property | |
98 | def connection(self): | |
894a8df5 SM |
99 | ptr = self._as_port_ptr(self._ptr) |
100 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
811644b8 PP |
101 | |
102 | if conn_ptr is None: | |
103 | return | |
104 | ||
79935628 | 105 | return _bt2_connection()._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 | 106 | |
2e00bc76 SM |
107 | @property |
108 | def user_data(self): | |
109 | ptr = self._as_self_port_ptr(self._ptr) | |
110 | return native_bt.self_component_port_get_data(ptr) | |
111 | ||
811644b8 | 112 | |
5813b3a3 | 113 | class _UserComponentInputPort(_UserComponentPort, _InputPortConst): |
cfbd7cf3 FD |
114 | _as_self_port_ptr = staticmethod( |
115 | native_bt.self_component_port_input_as_self_component_port | |
116 | ) | |
811644b8 PP |
117 | |
118 | ||
5813b3a3 | 119 | class _UserComponentOutputPort(_UserComponentPort, _OutputPortConst): |
cfbd7cf3 FD |
120 | _as_self_port_ptr = staticmethod( |
121 | native_bt.self_component_port_output_as_self_component_port | |
122 | ) | |
2c67587a SM |
123 | |
124 | ||
125 | _PORT_TYPE_TO_PYCLS = { | |
5813b3a3 FD |
126 | native_bt.PORT_TYPE_INPUT: _InputPortConst, |
127 | native_bt.PORT_TYPE_OUTPUT: _OutputPortConst, | |
2c67587a SM |
128 | } |
129 | ||
130 | ||
131 | _PORT_TYPE_TO_USER_PYCLS = { | |
132 | native_bt.PORT_TYPE_INPUT: _UserComponentInputPort, | |
133 | native_bt.PORT_TYPE_OUTPUT: _UserComponentOutputPort, | |
134 | } |