bt2: validate parameters to _TraceClass.create_stream_class before creating the nativ...
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
index 44a213070b76a0de39884e7ec7d82bd1732f2c8a..08d3e91f400c79e812fe091a61f9c36261c85186 100644 (file)
@@ -25,6 +25,7 @@
 from bt2 import native_bt, utils, object
 from bt2 import stream_class as bt2_stream_class
 from bt2 import field_class as bt2_field_class
+from bt2 import integer_range_set as bt2_integer_range_set
 from bt2 import trace as bt2_trace
 from bt2 import value as bt2_value
 import collections.abc
@@ -111,6 +112,25 @@ class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
 
         return utils._ListenerHandle(listener_id, self)
 
+    def remove_destruction_listener(self, listener_handle):
+        utils._check_type(listener_handle, utils._ListenerHandle)
+
+        if listener_handle._obj.addr != self.addr:
+            raise ValueError(
+                'This trace class destruction listener does not match the trace object.'
+            )
+
+        if listener_handle._listener_id is None:
+            raise ValueError(
+                'This trace class destruction listener was already removed.'
+            )
+
+        status = native_bt.trace_class_remove_destruction_listener(
+            self._ptr, listener_handle._listener_id
+        )
+        utils._handle_func_status(status)
+        listener_handle._listener_id = None
+
 
 class _TraceClass(_TraceClassConst):
     _borrow_stream_class_ptr_by_index = staticmethod(
@@ -170,6 +190,23 @@ class _TraceClass(_TraceClassConst):
         supports_discarded_packets=False,
         discarded_packets_have_default_clock_snapshots=False,
     ):
+        # Validate parameters before we create the object.
+        bt2_stream_class._StreamClass._validate_create_params(
+            name,
+            user_attributes,
+            packet_context_field_class,
+            event_common_context_field_class,
+            default_clock_class,
+            assigns_automatic_event_class_id,
+            assigns_automatic_stream_id,
+            supports_packets,
+            packets_have_beginning_default_clock_snapshot,
+            packets_have_end_default_clock_snapshot,
+            supports_discarded_events,
+            discarded_events_have_default_clock_snapshots,
+            supports_discarded_packets,
+            discarded_packets_have_default_clock_snapshots,
+        )
 
         if self.assigns_automatic_stream_class_id:
             if id is not None:
@@ -349,13 +386,26 @@ class _TraceClass(_TraceClassConst):
             user_attributes,
         )
 
-    def create_real_field_class(self, is_single_precision=False, user_attributes=None):
-        field_class_ptr = native_bt.field_class_real_create(self._ptr)
-        self._check_field_class_create_status(field_class_ptr, 'real')
+    def create_single_precision_real_field_class(self, user_attributes=None):
+        field_class_ptr = native_bt.field_class_real_single_precision_create(self._ptr)
+        self._check_field_class_create_status(field_class_ptr, 'single-precision real')
 
-        field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr)
+        field_class = bt2_field_class._SinglePrecisionRealFieldClass._create_from_ptr(
+            field_class_ptr
+        )
+
+        self._set_field_class_user_attrs(field_class, user_attributes)
+
+        return field_class
+
+    def create_double_precision_real_field_class(self, user_attributes=None):
+        field_class_ptr = native_bt.field_class_real_double_precision_create(self._ptr)
+        self._check_field_class_create_status(field_class_ptr, 'double-precision real')
+
+        field_class = bt2_field_class._DoublePrecisionRealFieldClass._create_from_ptr(
+            field_class_ptr
+        )
 
-        field_class._is_single_precision = is_single_precision
         self._set_field_class_user_attrs(field_class, user_attributes)
 
         return field_class
@@ -397,24 +447,57 @@ class _TraceClass(_TraceClassConst):
             self._ptr, elem_fc._ptr, length_fc_ptr
         )
         self._check_field_class_create_status(ptr, 'dynamic array')
-        fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
+        fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
         self._set_field_class_user_attrs(fc, user_attributes)
         return fc
 
-    def create_option_field_class(
-        self, content_fc, selector_fc=None, user_attributes=None
+    def create_option_without_selector_field_class(
+        self, content_fc, user_attributes=None
     ):
         utils._check_type(content_fc, bt2_field_class._FieldClass)
+        ptr = native_bt.field_class_option_without_selector_create(
+            self._ptr, content_fc._ptr
+        )
+        self._check_field_class_create_status(ptr, 'option')
+        fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
+        self._set_field_class_user_attrs(fc, user_attributes)
+        return fc
 
-        selector_fc_ptr = None
+    def create_option_with_bool_selector_field_class(
+        self, content_fc, selector_fc, selector_is_reversed=False, user_attributes=None
+    ):
+        utils._check_type(content_fc, bt2_field_class._FieldClass)
+        utils._check_bool(selector_is_reversed)
+        utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
+        ptr = native_bt.field_class_option_with_selector_field_bool_create(
+            self._ptr, content_fc._ptr, selector_fc._ptr
+        )
+        self._check_field_class_create_status(ptr, 'option')
+        fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
+        self._set_field_class_user_attrs(fc, user_attributes)
+        fc._selector_is_reversed = selector_is_reversed
+        return fc
 
-        if selector_fc is not None:
-            utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
-            selector_fc_ptr = selector_fc._ptr
+    def create_option_with_integer_selector_field_class(
+        self, content_fc, selector_fc, ranges, user_attributes=None
+    ):
+        utils._check_type(content_fc, bt2_field_class._FieldClass)
+        utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
+
+        if len(ranges) == 0:
+            raise ValueError('integer range set is empty')
+
+        if isinstance(selector_fc, bt2_field_class._UnsignedIntegerFieldClass):
+            utils._check_type(ranges, bt2_integer_range_set.UnsignedIntegerRangeSet)
+            ptr = native_bt.field_class_option_with_selector_field_integer_unsigned_create(
+                self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
+            )
+        else:
+            utils._check_type(ranges, bt2_integer_range_set.SignedIntegerRangeSet)
+            ptr = native_bt.field_class_option_with_selector_field_integer_signed_create(
+                self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
+            )
 
-        ptr = native_bt.field_class_option_create(
-            self._ptr, content_fc._ptr, selector_fc_ptr
-        )
         self._check_field_class_create_status(ptr, 'option')
         fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
         self._set_field_class_user_attrs(fc, user_attributes)
This page took 0.02641 seconds and 4 git commands to generate.