Class: Idl::EnumerationType

Inherits:
Type
  • Object
show all
Defined in:
lib/idl/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_name, element_names, element_values) ⇒ EnumerationType

Returns a new instance of EnumerationType.

Parameters:

  • type_name (String)

    The name of the enum class

  • element_names (Array<String>)

    The names of the elements, in the same order as element_values

  • element_values (Array<Integer>)

    The values of the elements, in the same order as element_names



543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/idl/type.rb', line 543

def initialize(type_name, element_names, element_values)
  width = element_values.max.bit_length
  width = 1 if width.zero? # can happen if only enum member has value 0
  super(:enum, width:)

  @name = type_name
  @element_names = element_names
  @element_values = element_values
  raise "unexpected" unless element_names.is_a?(Array)

  @ref_type = Type.new(:enum_ref, enum_class: self)
end

Instance Attribute Details

#element_namesArray<String> (readonly)

Returns The names of the enumeration elements, in the same order as element_values.

Returns:

  • (Array<String>)

    The names of the enumeration elements, in the same order as element_values



532
533
534
# File 'lib/idl/type.rb', line 532

def element_names
  @element_names
end

#element_valuesArray<Integer> (readonly)

Returns The values of the enumeration elements, in the same order as element_names.

Returns:

  • (Array<Integer>)

    The values of the enumeration elements, in the same order as element_names



535
536
537
# File 'lib/idl/type.rb', line 535

def element_values
  @element_values
end

#ref_typeType (readonly)

Returns The type of an reference to this Enumeration class.

Returns:

  • (Type)

    The type of an reference to this Enumeration class



538
539
540
# File 'lib/idl/type.rb', line 538

def ref_type
  @ref_type
end

#widthInteger (readonly)

Returns The bit width of the enumeration elements.

Returns:

  • (Integer)

    The bit width of the enumeration elements



529
530
531
# File 'lib/idl/type.rb', line 529

def width
  @width
end

Instance Method Details

#cloneObject



556
557
558
# File 'lib/idl/type.rb', line 556

def clone
  EnumerationType.new(@name, @element_names, @element_values)
end

#element_name(element_value) ⇒ Object



567
568
569
570
571
572
573
# File 'lib/idl/type.rb', line 567

def element_name(element_value)
  i = @element_values.index(element_value)
  raise "? #{element_value}" if i.nil?
  return nil if i.nil?

  @element_names[i]
end

#value(element_name) ⇒ Object



560
561
562
563
564
565
# File 'lib/idl/type.rb', line 560

def value(element_name)
  i = @element_names.index(element_name)
  return nil if i.nil?

  @element_values[i]
end