1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
6 from bt2
import event_class
as bt2_event_class
7 from bt2
import packet
as bt2_packet
8 from bt2
import stream
as bt2_stream
9 from bt2
import field
as bt2_field
10 import collections
.abc
13 class _EventConst(object._UniqueObject
, collections
.abc
.Mapping
):
14 _borrow_class_ptr
= staticmethod(native_bt
.event_borrow_class_const
)
15 _borrow_packet_ptr
= staticmethod(native_bt
.event_borrow_packet_const
)
16 _borrow_stream_ptr
= staticmethod(native_bt
.event_borrow_stream_const
)
17 _borrow_common_context_field_ptr
= staticmethod(
18 native_bt
.event_borrow_common_context_field_const
20 _borrow_specific_context_field_ptr
= staticmethod(
21 native_bt
.event_borrow_specific_context_field_const
23 _borrow_payload_field_ptr
= staticmethod(native_bt
.event_borrow_payload_field_const
)
24 _create_field_from_ptr
= staticmethod(bt2_field
._create
_field
_from
_const
_ptr
)
26 _event_class_pycls
= property(lambda _
: bt2_event_class
._EventClassConst
)
27 _packet_pycls
= property(lambda _
: bt2_packet
._PacketConst
)
28 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
32 event_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
33 assert event_class_ptr
is not None
34 return self
._event
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(event_class_ptr
)
46 packet_ptr
= self
._borrow
_packet
_ptr
(self
._ptr
)
48 if packet_ptr
is None:
51 return self
._packet
_pycls
._create
_from
_ptr
_and
_get
_ref
(packet_ptr
)
55 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
56 assert stream_ptr
is not None
57 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
60 def common_context_field(self
):
61 field_ptr
= self
._borrow
_common
_context
_field
_ptr
(self
._ptr
)
66 return self
._create
_field
_from
_ptr
(
67 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
71 def specific_context_field(self
):
72 field_ptr
= self
._borrow
_specific
_context
_field
_ptr
(self
._ptr
)
77 return self
._create
_field
_from
_ptr
(
78 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
82 def payload_field(self
):
83 field_ptr
= self
._borrow
_payload
_field
_ptr
(self
._ptr
)
88 return self
._create
_field
_from
_ptr
(
89 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
92 def __getitem__(self
, key
):
94 payload_field
= self
.payload_field
96 if payload_field
is not None and key
in payload_field
:
97 return payload_field
[key
]
99 specific_context_field
= self
.specific_context_field
101 if specific_context_field
is not None and key
in specific_context_field
:
102 return specific_context_field
[key
]
104 common_context_field
= self
.common_context_field
106 if common_context_field
is not None and key
in common_context_field
:
107 return common_context_field
[key
]
110 packet_context_field
= self
.packet
.context_field
112 if packet_context_field
is not None and key
in packet_context_field
:
113 return packet_context_field
[key
]
118 # To only yield unique keys, keep a set of member names that are
119 # already yielded. Two root structure fields (for example,
120 # payload and common context) can contain immediate members
121 # which share the same name.
124 if self
.payload_field
is not None:
125 for field_name
in self
.payload_field
:
127 member_names
.add(field_name
)
129 if self
.specific_context_field
is not None:
130 for field_name
in self
.specific_context_field
:
131 if field_name
not in member_names
:
133 member_names
.add(field_name
)
135 if self
.common_context_field
is not None:
136 for field_name
in self
.common_context_field
:
137 if field_name
not in member_names
:
139 member_names
.add(field_name
)
141 if self
.packet
and self
.packet
.context_field
is not None:
142 for field_name
in self
.packet
.context_field
:
143 if field_name
not in member_names
:
145 member_names
.add(field_name
)
148 return sum(1 for _
in self
)
151 class _Event(_EventConst
):
152 _borrow_class_ptr
= staticmethod(native_bt
.event_borrow_class
)
153 _borrow_packet_ptr
= staticmethod(native_bt
.event_borrow_packet
)
154 _borrow_stream_ptr
= staticmethod(native_bt
.event_borrow_stream
)
155 _borrow_common_context_field_ptr
= staticmethod(
156 native_bt
.event_borrow_common_context_field
158 _borrow_specific_context_field_ptr
= staticmethod(
159 native_bt
.event_borrow_specific_context_field
161 _borrow_payload_field_ptr
= staticmethod(native_bt
.event_borrow_payload_field
)
162 _create_field_from_ptr
= staticmethod(bt2_field
._create
_field
_from
_ptr
)
164 _event_class_pycls
= property(lambda _
: bt2_event_class
._EventClass
)
165 _packet_pycls
= property(lambda _
: bt2_packet
._Packet
)
166 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)