1 # The MIT License (MIT)
3 # Copyright (c) 2013-2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
24 import babeltrace
.common
as common
27 def _create_field_declaration(field_type
, name
, scope
):
29 if type(field_type
) == bt2
.IntegerFieldType
:
30 declaration
= IntegerFieldDeclaration
.__new
__(
31 IntegerFieldDeclaration
)
32 elif type(field_type
) == bt2
.EnumerationFieldType
:
33 declaration
= EnumerationFieldDeclaration
.__new
__(
34 EnumerationFieldDeclaration
)
35 elif type(field_type
) == bt2
.ArrayFieldType
:
36 declaration
= ArrayFieldDeclaration
.__new
__(
37 ArrayFieldDeclaration
)
38 elif type(field_type
) == bt2
.SequenceFieldType
:
39 declaration
= SequenceFieldDeclaration
.__new
__(
40 SequenceFieldDeclaration
)
41 elif type(field_type
) == bt2
.FloatingPointNumberFieldType
:
42 declaration
= FloatFieldDeclaration
.__new
__(
43 FloatFieldDeclaration
)
44 elif type(field_type
) == bt2
.StructureFieldType
:
45 declaration
= StructureFieldDeclaration
.__new
__(
46 StructureFieldDeclaration
)
47 elif type(field_type
) == bt2
.StringFieldType
:
48 declaration
= StringFieldDeclaration
.__new
__(
49 StringFieldDeclaration
)
50 elif type(field_type
) == bt2
.VariantFieldType
:
51 declaration
= VariantFieldDeclaration
.__new
__(
52 VariantFieldDeclaration
)
58 declaration
._field
_type
= field_type
59 declaration
._name
= name
60 declaration
._scope
= scope
64 class FieldDeclaration
:
66 Base class for concrete field declarations.
68 This class is not meant to be instantiated by the user.
72 raise NotImplementedError("FieldDeclaration cannot be instantiated")
75 return "({0}) {1} {2}".format(common
.CTFScope
.scope_name(self
.scope
),
76 common
.CTFTypeId
.type_name(self
.type),
82 Field name, or ``None`` on error.
90 Field type (one of :class:`babeltrace.common.CTFTypeId`
94 return _OBJ_TO_TYPE_ID
[type(self
)]
99 Field scope (one of:class:`babeltrace.common.CTFScope`
106 class IntegerFieldDeclaration(FieldDeclaration
):
108 Integer field declaration.
112 raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
115 def signedness(self
):
117 0 if this integer is unsigned, 1 if signed, or -1 on error.
121 if self
._field
_type
.is_signed
:
131 Integer base (:class:`int`), or a negative value on error.
135 return self
._field
_type
.base
136 except AssertionError:
140 def byte_order(self
):
142 Integer byte order (one of
143 :class:`babeltrace.common.ByteOrder` constants).
147 byte_order
= self
._field
_type
.byte_order
148 except AssertionError:
149 return common
.ByteOrder
.BYTE_ORDER_UNKNOWN
152 return _BT2_BYTE_ORDER_TO_BYTE_ORDER
[byte_order
]
154 return common
.ByteOrder
.BYTE_ORDER_UNKNOWN
159 Integer size in bits, or a negative value on error.
163 return self
._field
_type
.size
164 except AssertionError:
170 Integer size in bits, or a negative value on error.
178 Integer encoding (one of
179 :class:`babeltrace.common.CTFStringEncoding` constants).
183 encoding
= self
._field
_type
.encoding
185 return common
.CTFStringEncoding
.UNKNOWN
188 return _BT2_ENCODING_TO_ENCODING
[encoding
]
190 return common
.CTFStringEncoding
.UNKNOWN
193 class EnumerationFieldDeclaration(FieldDeclaration
):
195 Enumeration field declaration.
199 As of this version, this class is missing some properties.
203 raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
206 class ArrayFieldDeclaration(FieldDeclaration
):
208 Static array field declaration.
212 raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
217 Fixed length of this static array (number of contained
218 elements), or a negative value on error.
222 return self
._field
_type
.length
223 except AssertionError:
227 def element_declaration(self
):
229 Field declaration of the underlying element.
233 return _create_field_declaration(
234 self
._field
_type
.element_field_type
, name
=None,
240 class SequenceFieldDeclaration(FieldDeclaration
):
242 Sequence (dynamic array) field declaration.
246 As of this version, this class is missing some properties.
250 raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
253 def element_declaration(self
):
255 Field declaration of the underlying element.
259 return _create_field_declaration(
260 self
._field
_type
.element_field_type
, name
=None,
266 class FloatFieldDeclaration(FieldDeclaration
):
268 Floating point number field declaration.
272 As of this version, this class is missing some properties.
276 raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
279 class StructureFieldDeclaration(FieldDeclaration
):
281 Structure (ordered map of field names to field declarations) field
286 As of this version, this class is missing some properties.
290 raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
293 class StringFieldDeclaration(FieldDeclaration
):
295 String (NULL-terminated array of bytes) field declaration.
299 As of this version, this class is missing some properties.
303 raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
306 class VariantFieldDeclaration(FieldDeclaration
):
308 Variant (dynamic selection between different types) field declaration.
312 As of this version, this class is missing some properties.
316 raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
320 IntegerFieldDeclaration
: common
.CTFTypeId
.INTEGER
,
321 FloatFieldDeclaration
: common
.CTFTypeId
.FLOAT
,
322 EnumerationFieldDeclaration
: common
.CTFTypeId
.ENUM
,
323 StringFieldDeclaration
: common
.CTFTypeId
.STRING
,
324 StructureFieldDeclaration
: common
.CTFTypeId
.STRUCT
,
325 ArrayFieldDeclaration
: common
.CTFTypeId
.ARRAY
,
326 SequenceFieldDeclaration
: common
.CTFTypeId
.SEQUENCE
,
327 VariantFieldDeclaration
: common
.CTFTypeId
.VARIANT
,
330 _BT2_BYTE_ORDER_TO_BYTE_ORDER
= {
331 bt2
.ByteOrder
.NATIVE
: common
.ByteOrder
.BYTE_ORDER_NATIVE
,
332 bt2
.ByteOrder
.LITTLE_ENDIAN
: common
.ByteOrder
.BYTE_ORDER_LITTLE_ENDIAN
,
333 bt2
.ByteOrder
.BIG_ENDIAN
: common
.ByteOrder
.BYTE_ORDER_BIG_ENDIAN
,
334 bt2
.ByteOrder
.NETWORK
: common
.ByteOrder
.BYTE_ORDER_NETWORK
,
337 _BT2_ENCODING_TO_ENCODING
= {
338 bt2
.Encoding
.NONE
: common
.CTFStringEncoding
.NONE
,
339 bt2
.Encoding
.ASCII
: common
.CTFStringEncoding
.ASCII
,
340 bt2
.Encoding
.UTF8
: common
.CTFStringEncoding
.UTF8
,