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 |
3fb99a22 | 24 | from bt2 import connection as bt2_connection |
811644b8 PP |
25 | |
26 | ||
2c67587a SM |
27 | def _create_from_ptr_and_get_ref(ptr, port_type): |
28 | cls = _PORT_TYPE_TO_PYCLS.get(port_type, None) | |
29 | ||
30 | if cls is None: | |
d24d5663 | 31 | raise TypeError('unknown port type: {}'.format(port_type)) |
2c67587a SM |
32 | |
33 | return cls._create_from_ptr_and_get_ref(ptr) | |
34 | ||
35 | ||
36 | def _create_self_from_ptr_and_get_ref(ptr, port_type): | |
37 | cls = _PORT_TYPE_TO_USER_PYCLS.get(port_type, None) | |
38 | ||
39 | if cls is None: | |
d24d5663 | 40 | raise TypeError('unknown port type: {}'.format(port_type)) |
2c67587a SM |
41 | |
42 | return cls._create_from_ptr_and_get_ref(ptr) | |
43 | ||
44 | ||
78288f58 | 45 | class _Port(object._SharedObject): |
894a8df5 SM |
46 | @classmethod |
47 | def _get_ref(cls, ptr): | |
48 | ptr = cls._as_port_ptr(ptr) | |
49 | return native_bt.port_get_ref(ptr) | |
811644b8 | 50 | |
894a8df5 SM |
51 | @classmethod |
52 | def _put_ref(cls, ptr): | |
53 | ptr = cls._as_port_ptr(ptr) | |
54 | return native_bt.port_put_ref(ptr) | |
811644b8 PP |
55 | |
56 | @property | |
57 | def name(self): | |
894a8df5 SM |
58 | ptr = self._as_port_ptr(self._ptr) |
59 | name = native_bt.port_get_name(ptr) | |
60 | assert name is not None | |
61 | return name | |
811644b8 PP |
62 | |
63 | @property | |
64 | def connection(self): | |
894a8df5 SM |
65 | ptr = self._as_port_ptr(self._ptr) |
66 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
811644b8 PP |
67 | |
68 | if conn_ptr is None: | |
69 | return | |
70 | ||
c7e5224b | 71 | return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 PP |
72 | |
73 | @property | |
74 | def is_connected(self): | |
75 | return self.connection is not None | |
76 | ||
811644b8 PP |
77 | |
78 | class _InputPort(_Port): | |
894a8df5 | 79 | _as_port_ptr = staticmethod(native_bt.port_input_as_port_const) |
811644b8 PP |
80 | |
81 | ||
82 | class _OutputPort(_Port): | |
894a8df5 | 83 | _as_port_ptr = staticmethod(native_bt.port_output_as_port_const) |
dc43190b | 84 | |
dc43190b | 85 | |
894a8df5 SM |
86 | class _UserComponentPort(_Port): |
87 | @classmethod | |
88 | def _as_port_ptr(cls, ptr): | |
89 | ptr = cls._as_self_port_ptr(ptr) | |
90 | return native_bt.self_component_port_as_port(ptr) | |
811644b8 PP |
91 | |
92 | @property | |
93 | def connection(self): | |
894a8df5 SM |
94 | ptr = self._as_port_ptr(self._ptr) |
95 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
811644b8 PP |
96 | |
97 | if conn_ptr is None: | |
98 | return | |
99 | ||
c7e5224b | 100 | return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 | 101 | |
2e00bc76 SM |
102 | @property |
103 | def user_data(self): | |
104 | ptr = self._as_self_port_ptr(self._ptr) | |
105 | return native_bt.self_component_port_get_data(ptr) | |
106 | ||
811644b8 | 107 | |
894a8df5 | 108 | class _UserComponentInputPort(_UserComponentPort, _InputPort): |
cfbd7cf3 FD |
109 | _as_self_port_ptr = staticmethod( |
110 | native_bt.self_component_port_input_as_self_component_port | |
111 | ) | |
811644b8 PP |
112 | |
113 | ||
894a8df5 | 114 | class _UserComponentOutputPort(_UserComponentPort, _OutputPort): |
cfbd7cf3 FD |
115 | _as_self_port_ptr = staticmethod( |
116 | native_bt.self_component_port_output_as_self_component_port | |
117 | ) | |
2c67587a SM |
118 | |
119 | ||
120 | _PORT_TYPE_TO_PYCLS = { | |
121 | native_bt.PORT_TYPE_INPUT: _InputPort, | |
122 | native_bt.PORT_TYPE_OUTPUT: _OutputPort, | |
123 | } | |
124 | ||
125 | ||
126 | _PORT_TYPE_TO_USER_PYCLS = { | |
127 | native_bt.PORT_TYPE_INPUT: _UserComponentInputPort, | |
128 | native_bt.PORT_TYPE_OUTPUT: _UserComponentOutputPort, | |
129 | } |