Class: Idl::EnumerationType

Inherits:
Type
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/idlc/type.rb,
lib/idlc/type.rb

Constant Summary

Constants inherited from Type

Type::KINDS, Type::QUALIFIERS, Type::TYPE_FROM_KIND

Instance Attribute Summary collapse

Attributes inherited from Type

#kind, #max_width, #qualifiers, #width_ast

Instance Method Summary collapse

Methods inherited from Type

#==, #ary?, #ary_type, #comparable_to?, #const?, #convertable_to?, #default, #enum_class, #equal_to?, from_json_schema, from_typename, #global?, #integral?, #known?, #make_const, #make_const!, #make_global, #make_known, #make_signed, #mutable?, #name, #qualify, #runtime?, #signed?, #sub_type, #to_idl, #to_s, #tuple_types

Constructor Details

#initialize(type_name, element_names, element_values, builtin: false) ⇒ EnumerationType

Returns a new instance of EnumerationType.



684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/idlc/type.rb', line 684

def initialize(type_name, element_names, element_values, builtin: false)
  width = T.must(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.freeze
  @element_names = element_names.freeze
  @element_values = element_values.freeze
  raise "names and values aren't the same size" unless element_names.size == element_values.size

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

Instance Attribute Details

#element_namesObject (readonly)

Returns the value of attribute element_names.



663
664
665
# File 'lib/idlc/type.rb', line 663

def element_names
  @element_names
end

#element_valuesObject (readonly)

Returns the value of attribute element_values.



667
668
669
# File 'lib/idlc/type.rb', line 667

def element_values
  @element_values
end

#ref_typeObject (readonly)

Returns the value of attribute ref_type.



671
672
673
# File 'lib/idlc/type.rb', line 671

def ref_type
  @ref_type
end

Instance Method Details

#builtin?Boolean

Returns:

  • (Boolean)


699
# File 'lib/idlc/type.rb', line 699

def builtin? = @builtin

#cloneObject



702
703
704
# File 'lib/idlc/type.rb', line 702

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

#element_name(element_value) ⇒ Object



715
716
717
718
719
720
721
# File 'lib/idlc/type.rb', line 715

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



707
708
709
710
711
712
# File 'lib/idlc/type.rb', line 707

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

  @element_values[i]
end

#widthObject



659
# File 'lib/idlc/type.rb', line 659

def width = T.cast(@width, Integer)