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
26 def _create_field_declaration(field_type
, name
, scope
):
28 if type(field_type
) == bt2
.IntegerFieldType
:
29 declaration
= IntegerFieldDeclaration
.__new
__(
30 IntegerFieldDeclaration
)
31 elif type(field_type
) == bt2
.EnumerationFieldType
:
32 declaration
= EnumerationFieldDeclaration
.__new
__(
33 EnumerationFieldDeclaration
)
34 elif type(field_type
) == bt2
.ArrayFieldType
:
35 declaration
= ArrayFieldDeclaration
.__new
__(
36 ArrayFieldDeclaration
)
37 elif type(field_type
) == bt2
.SequenceFieldType
:
38 declaration
= SequenceFieldDeclaration
.__new
__(
39 SequenceFieldDeclaration
)
40 elif type(field_type
) == bt2
.FloatingPointNumberFieldType
:
41 declaration
= FloatFieldDeclaration
.__new
__(
42 FloatFieldDeclaration
)
43 elif type(field_type
) == bt2
.StructureFieldType
:
44 declaration
= StructureFieldDeclaration
.__new
__(
45 StructureFieldDeclaration
)
46 elif type(field_type
) == bt2
.StringFieldType
:
47 declaration
= StringFieldDeclaration
.__new
__(
48 StringFieldDeclaration
)
49 elif type(field_type
) == bt2
.VariantFieldType
:
50 declaration
= VariantFieldDeclaration
.__new
__(
51 VariantFieldDeclaration
)
57 declaration
._field
_type
= field_type
58 declaration
._name
= name
59 declaration
._scope
= scope
63 class FieldDeclaration
:
65 Base class for concrete field declarations.
67 This class is not meant to be instantiated by the user.
71 raise NotImplementedError("FieldDeclaration cannot be instantiated")
74 return "({0}) {1} {2}".format(common
.CTFScope
.scope_name(self
.scope
),
75 common
.CTFTypeId
.type_name(self
.type),
81 Field name, or ``None`` on error.
89 Field type (one of :class:`babeltrace.common.CTFTypeId`
93 return _OBJ_TO_TYPE_ID
[type(self
)]
98 Field scope (one of:class:`babeltrace.common.CTFScope`
105 class IntegerFieldDeclaration(FieldDeclaration
):
107 Integer field declaration.
111 raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
114 def signedness(self
):
116 0 if this integer is unsigned, 1 if signed, or -1 on error.
120 if self
._field
_type
.is_signed
:
130 Integer base (:class:`int`), or a negative value on error.
134 return self
._field
_type
.base
135 except AssertionError:
139 def byte_order(self
):
141 Integer byte order (one of
142 :class:`babeltrace.common.ByteOrder` constants).
146 byte_order
= self
._field
_type
.byte_order
147 except AssertionError:
148 return common
.ByteOrder
.BYTE_ORDER_UNKNOWN
151 return _BT2_BYTE_ORDER_TO_BYTE_ORDER
[byte_order
]
153 return common
.ByteOrder
.BYTE_ORDER_UNKNOWN
158 Integer size in bits, or a negative value on error.
162 return self
._field
_type
.size
163 except AssertionError:
169 Integer size in bits, or a negative value on error.
177 Integer encoding (one of
178 :class:`babeltrace.common.CTFStringEncoding` constants).
182 encoding
= self
._field
_type
.encoding
184 return common
.CTFStringEncoding
.UNKNOWN
187 return _BT2_ENCODING_TO_ENCODING
[encoding
]
189 return common
.CTFStringEncoding
.UNKNOWN
192 class EnumerationFieldDeclaration(FieldDeclaration
):
194 Enumeration field declaration.
198 As of this version, this class is missing some properties.
202 raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
205 class ArrayFieldDeclaration(FieldDeclaration
):
207 Static array field declaration.
211 raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
216 Fixed length of this static array (number of contained
217 elements), or a negative value on error.
221 return self
._field
_type
.length
222 except AssertionError:
226 def element_declaration(self
):
228 Field declaration of the underlying element.
232 return _create_field_declaration(
233 self
._field
_type
.element_field_type
, name
=None,
239 class SequenceFieldDeclaration(FieldDeclaration
):
241 Sequence (dynamic array) field declaration.
245 As of this version, this class is missing some properties.
249 raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
252 def element_declaration(self
):
254 Field declaration of the underlying element.
258 return _create_field_declaration(
259 self
._field
_type
.element_field_type
, name
=None,
265 class FloatFieldDeclaration(FieldDeclaration
):
267 Floating point number field declaration.
271 As of this version, this class is missing some properties.
275 raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
278 class StructureFieldDeclaration(FieldDeclaration
):
280 Structure (ordered map of field names to field declarations) field
285 As of this version, this class is missing some properties.
289 raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
292 class StringFieldDeclaration(FieldDeclaration
):
294 String (NULL-terminated array of bytes) field declaration.
298 As of this version, this class is missing some properties.
302 raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
305 class VariantFieldDeclaration(FieldDeclaration
):
307 Variant (dynamic selection between different types) field declaration.
311 As of this version, this class is missing some properties.
315 raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
319 IntegerFieldDeclaration
: common
.CTFTypeId
.INTEGER
,
320 FloatFieldDeclaration
: common
.CTFTypeId
.FLOAT
,
321 EnumerationFieldDeclaration
: common
.CTFTypeId
.ENUM
,
322 StringFieldDeclaration
: common
.CTFTypeId
.STRING
,
323 StructureFieldDeclaration
: common
.CTFTypeId
.STRUCT
,
324 ArrayFieldDeclaration
: common
.CTFTypeId
.ARRAY
,
325 SequenceFieldDeclaration
: common
.CTFTypeId
.SEQUENCE
,
326 VariantFieldDeclaration
: common
.CTFTypeId
.VARIANT
,
329 _BT2_BYTE_ORDER_TO_BYTE_ORDER
= {
330 bt2
.ByteOrder
.NATIVE
: common
.ByteOrder
.BYTE_ORDER_NATIVE
,
331 bt2
.ByteOrder
.LITTLE_ENDIAN
: common
.ByteOrder
.BYTE_ORDER_LITTLE_ENDIAN
,
332 bt2
.ByteOrder
.BIG_ENDIAN
: common
.ByteOrder
.BYTE_ORDER_BIG_ENDIAN
,
333 bt2
.ByteOrder
.NETWORK
: common
.ByteOrder
.BYTE_ORDER_NETWORK
,
336 _BT2_ENCODING_TO_ENCODING
= {
337 bt2
.Encoding
.NONE
: common
.CTFStringEncoding
.NONE
,
338 bt2
.Encoding
.ASCII
: common
.CTFStringEncoding
.ASCII
,
339 bt2
.Encoding
.UTF8
: common
.CTFStringEncoding
.UTF8
,