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