Class: Babeltrace2Gen::BTFieldClass

Inherits:
Object
  • Object
show all
Includes:
BTLocator, BTMatch, BTPrinter
Defined in:
lib/metababel/bt2_trace_class_generator.rb

Direct Known Subclasses

Array, BitArray, Bool, Default, Integer, Option, Real, String, Structure, Variant

Defined Under Namespace

Modules: Enumeration Classes: Array, BitArray, Bool, Default, Integer, Option, Real, String, Structure, Variant

Constant Summary collapse

BT_MATCH_ATTRS =
%i[type cast_type cast_type_is_struct]

Constants included from BTPrinter

Babeltrace2Gen::BTPrinter::INDENT_INCREMENT

Instance Attribute Summary collapse

Attributes included from BTLocator

#parent, #variable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BTMatch

#match?

Methods included from BTPrinter

context, #name_sanitized, pr, #scope

Methods included from BTLocator

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

Constructor Details

#initialize(parent:) ⇒ BTFieldClass

Returns a new instance of BTFieldClass.



374
375
376
# File 'lib/metababel/bt2_trace_class_generator.rb', line 374

def initialize(parent:)
  @parent = parent
end

Instance Attribute Details

#cast_typeObject

Returns the value of attribute cast_type.



372
373
374
# File 'lib/metababel/bt2_trace_class_generator.rb', line 372

def cast_type
  @cast_type
end

#cast_type_is_structObject

Returns the value of attribute cast_type_is_struct.



372
373
374
# File 'lib/metababel/bt2_trace_class_generator.rb', line 372

def cast_type_is_struct
  @cast_type_is_struct
end

#typeObject

Returns the value of attribute type.



372
373
374
# File 'lib/metababel/bt2_trace_class_generator.rb', line 372

def type
  @type
end

Class Method Details

.from_h(parent, model) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/metababel/bt2_trace_class_generator.rb', line 378

def self.from_h(parent, model)
  key = model.delete(:type)
  # /!\ Recursion
  is_match_model = parent.rec_trace_class.match

  raise "No type in #{model}" unless key || is_match_model

  h = {
    'bool' => BTFieldClass::Bool,
    'bit_array' => BTFieldClass::BitArray,
    'integer_unsigned' => BTFieldClass::Integer::Unsigned,
    'integer_signed' => BTFieldClass::Integer::Signed,
    'single' => BTFieldClass::Real::SinglePrecision,
    'double' => BTFieldClass::Real::DoublePrecision,
    'enumeration_unsigned' => BTFieldClass::Enumeration::Unsigned,
    'enumeration_signed' => BTFieldClass::Enumeration::Signed,
    'string' => BTFieldClass::String,
    'array_static' => BTFieldClass::Array::Static,
    'array_dynamic' => BTFieldClass::Array::Dynamic,
    'structure' => BTFieldClass::Structure,
    'option_without_selector_field' => BTFieldClass::Option::WithoutSelectorField,
    'option_with_selector_field_bool' => BTFieldClass::Option::WithSelectorField::Bool,
    'option_with_selector_field_unsigned' => BTFieldClass::Option::WithSelectorField::IntegerUnsigned,
    'option_with_selector_field_signed' => BTFieldClass::Option::WithSelectorField::IntegerSigned,
    'variant' => BTFieldClass::Variant,
  }.freeze

  raise "No #{key} in FIELD_CLASS_NAME_MAP" unless h.include?(key) || is_match_model

  cast_type = model.delete(:cast_type)
  cast_type_is_struct = model.delete(:cast_type_is_struct)

  fc = h.include?(key) ? h[key].from_h(parent, model) : BTFieldClass::Default.new(parent: parent)

  # Since key (:type) can be a string or a regex, we store
  # the type into the field to apply string.match?(regex)
  # in place of comparing field objects.
  fc.type = key
  fc.cast_type = cast_type if cast_type
  fc.cast_type_is_struct = cast_type_is_struct if cast_type_is_struct
  fc
end

Instance Method Details

#bt_get_variable(arg_variables, is_array: false) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/metababel/bt2_trace_class_generator.rb', line 425

def bt_get_variable(arg_variables, is_array: false)
  internal = arg_variables.fetch('internal', [])
  return internal.shift unless internal.empty?

  type =
    if is_array
      "#{element_field_class.class.instance_variable_get(:@bt_type)}*"
    else
      self.class.instance_variable_get(:@bt_type)
    end
  var = GeneratedArg.new(@cast_type || type, rec_member_class.name)

  arg_variables.fetch_append('outputs_allocated', var) if is_array
  arg_variables.fetch_append('outputs', var)
end

#get_declarator(*args, **dict) ⇒ Object

Raises:

  • (NotImplementedError)


421
422
423
# File 'lib/metababel/bt2_trace_class_generator.rb', line 421

def get_declarator(*args, **dict)
  raise NotImplementedError, self.class
end

#get_getter(field:, arg_variables:) ⇒ Object



441
442
443
444
445
446
# File 'lib/metababel/bt2_trace_class_generator.rb', line 441

def get_getter(field:, arg_variables:)
  bt_func_get = self.class.instance_variable_get(:@bt_func) % 'get'
  variable = bt_get_variable(arg_variables).name
  cast_func = @cast_type ? "(#{@cast_type})" : ''
  pr "#{variable} = #{cast_func}#{bt_func_get}(#{field});"
end

#get_setter(field:, arg_variables:) ⇒ Object



448
449
450
451
452
453
# File 'lib/metababel/bt2_trace_class_generator.rb', line 448

def get_setter(field:, arg_variables:)
  bt_func_get = self.class.instance_variable_get(:@bt_func) % 'set'
  variable = bt_get_variable(arg_variables).name
  # We always explicitly cast to the proper bebeltrace type when sending messages.
  pr "#{bt_func_get}(#{field}, (#{self.class.instance_variable_get(:@bt_type)})#{variable});"
end