Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b PP |
2 | # |
3 | # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com> | |
81447b5b | 4 | |
838a5a52 | 5 | from bt2 import native_bt, object |
3fb99a22 | 6 | from bt2 import field as bt2_field |
79935628 SM |
7 | |
8 | ||
9 | def _bt2_stream(): | |
10 | from bt2 import stream as bt2_stream | |
11 | ||
12 | return bt2_stream | |
81447b5b PP |
13 | |
14 | ||
f0a42b33 | 15 | class _PacketConst(object._SharedObject): |
9dee90bd SM |
16 | @staticmethod |
17 | def _get_ref(ptr): | |
18 | native_bt.packet_get_ref(ptr) | |
19 | ||
20 | @staticmethod | |
21 | def _put_ref(ptr): | |
22 | native_bt.packet_put_ref(ptr) | |
23 | ||
f0a42b33 FD |
24 | _borrow_stream_ptr = staticmethod(native_bt.packet_borrow_stream_const) |
25 | _borrow_context_field_ptr = staticmethod( | |
26 | native_bt.packet_borrow_context_field_const | |
27 | ) | |
79935628 | 28 | _stream_pycls = property(lambda _: _bt2_stream()._StreamConst) |
f0a42b33 | 29 | _create_field_from_ptr = staticmethod(bt2_field._create_field_from_const_ptr) |
2ae9f48c | 30 | |
81447b5b PP |
31 | @property |
32 | def stream(self): | |
f0a42b33 | 33 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
2ae9f48c | 34 | assert stream_ptr is not None |
f0a42b33 | 35 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b | 36 | |
81447b5b PP |
37 | @property |
38 | def context_field(self): | |
f0a42b33 | 39 | field_ptr = self._borrow_context_field_ptr(self._ptr) |
81447b5b PP |
40 | |
41 | if field_ptr is None: | |
42 | return | |
43 | ||
f0a42b33 | 44 | return self._create_field_from_ptr( |
cfbd7cf3 FD |
45 | field_ptr, self._ptr, self._get_ref, self._put_ref |
46 | ) | |
f0a42b33 FD |
47 | |
48 | ||
49 | class _Packet(_PacketConst): | |
50 | _borrow_stream_ptr = staticmethod(native_bt.packet_borrow_stream) | |
51 | _borrow_context_field_ptr = staticmethod(native_bt.packet_borrow_context_field) | |
79935628 | 52 | _stream_pycls = property(lambda _: _bt2_stream()._Stream) |
f0a42b33 | 53 | _create_field_from_ptr = staticmethod(bt2_field._create_field_from_ptr) |