Class: Babeltrace2Gen::BTFieldClass::Array::Dynamic

Inherits:
Babeltrace2Gen::BTFieldClass::Array show all
Extended by:
Babeltrace2Gen::BTFromH
Defined in:
lib/metababel/bt2_trace_class_generator.rb

Defined Under Namespace

Modules: WithLengthField

Constant Summary

Constants inherited from Babeltrace2Gen::BTFieldClass

BT_MATCH_ATTRS

Constants included from Babeltrace2Gen::BTPrinter

Babeltrace2Gen::BTPrinter::INDENT_INCREMENT

Instance Attribute Summary

Attributes inherited from Babeltrace2Gen::BTFieldClass::Array

#element_field_class

Attributes inherited from Babeltrace2Gen::BTFieldClass

#cast_type, #cast_type_is_struct, #type

Attributes included from Babeltrace2Gen::BTLocator

#parent, #variable

Instance Method Summary collapse

Methods included from Babeltrace2Gen::BTFromH

from_h

Methods inherited from Babeltrace2Gen::BTFieldClass

#bt_get_variable, from_h

Methods included from Babeltrace2Gen::BTMatch

#match?

Methods included from Babeltrace2Gen::BTPrinter

context, #name_sanitized, pr, #scope

Methods included from Babeltrace2Gen::BTLocator

#rec_event_class, #rec_member_class, #rec_stream_class, #rec_trace_class, #resolve_path

Constructor Details

#initialize(parent:, element_field_class:, length_field_path: nil) ⇒ Dynamic

Returns a new instance of Dynamic.



720
721
722
723
724
725
726
# File 'lib/metababel/bt2_trace_class_generator.rb', line 720

def initialize(parent:, element_field_class:, length_field_path: nil)
  super(parent: parent, element_field_class: element_field_class)
  return unless length_field_path

  extend(WithLengthField)
  @length_field_path = length_field_path
end

Instance Method Details

#get_declarator(trace_class:, variable:) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/metababel/bt2_trace_class_generator.rb', line 728

def get_declarator(trace_class:, variable:)
  element_field_class_variable = "#{variable}_field_class"
  scope do
    pr "bt_field_class *#{element_field_class_variable};"
    @element_field_class.get_declarator(trace_class: trace_class, variable: element_field_class_variable)
    if @length_field_path
      element_field_class_variable_length = "#{element_field_class_variable}_length"
      pr "bt_field_class *#{element_field_class_variable_length};"
      scope do
        element_field_class_variable_length_sm = "#{element_field_class_variable_length}_sm"
        pr "bt_field_class_structure_member *#{element_field_class_variable_length_sm};"
        field_class, id = resolve_path(@length_field_path)
        id.scan(/(\w+)|(\d+)/).each do |name, index|
          # String
          if name
            pr "#{element_field_class_variable_length_sm} = bt_field_class_structure_borrow_member_by_name(#{field_class.variable}, \"#{name}\");"
          else
            pr "#{element_field_class_variable_length_sm} = bt_field_class_structure_borrow_member_by_index(#{field_class.variable}, #{index});"
          end
        end
        pr "#{element_field_class_variable_length} = bt_field_class_structure_member_borrow_field_class(#{element_field_class_variable_length_sm});"
      end
      pr "#{variable} = bt_field_class_array_dynamic_create(#{trace_class}, #{element_field_class_variable}, #{element_field_class_variable_length});"
      pr "bt_field_class_put_ref(#{element_field_class_variable});"
    else
      pr "#{variable} = bt_field_class_array_dynamic_create(#{trace_class}, #{element_field_class_variable}, NULL);"
    end
  end
end

#get_getter(field:, arg_variables:) ⇒ Object



772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/metababel/bt2_trace_class_generator.rb', line 772

def get_getter(field:, arg_variables:)
  length = "#{field}_length"
  pr "uint64_t #{length} = bt_field_array_get_length(#{field});"
  usr_var = bt_get_variable(arg_variables, is_array: true)
  pr "#{usr_var.name} = (#{usr_var.type}) malloc(#{length} * sizeof(#{usr_var.name}));"
  pr "for(uint64_t _i=0; _i < #{length} ; _i++)"
  scope do
    v = "#{field}_e"
    pr "const bt_field* #{v} = bt_field_array_borrow_element_field_by_index_const(#{field}, _i);"
    arg_variables.fetch_append('internal', GeneratedArg.new('', "#{usr_var.name}[_i]"))
    @element_field_class.get_getter(field: v, arg_variables: arg_variables)
  end
end

#get_setter(field:, arg_variables:) ⇒ Object



758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/metababel/bt2_trace_class_generator.rb', line 758

def get_setter(field:, arg_variables:)
  field_class, id = resolve_path(@length_field_path)
  length_field = field_class[id]
  pr "bt_field_array_dynamic_set_length(#{field}, #{length_field.name});"
  usr_var = bt_get_variable(arg_variables, is_array: true)
  pr "for(uint64_t _i=0; _i < #{length_field.name} ; _i++)"
  scope do
    v = "#{field}_e"
    pr "bt_field* #{v} = bt_field_array_borrow_element_field_by_index(#{field}, _i);"
    arg_variables.fetch_append('internal', GeneratedArg.new('', "#{usr_var.name}[_i]"))
    @element_field_class.get_setter(field: v, arg_variables: arg_variables)
  end
end