Class: Xdrgen::AST::Definitions::EnumMember

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Includes:
Concerns::Contained, Concerns::Named
Defined in:
lib/xdrgen/ast/definitions/enum_member.rb

Instance Method Summary collapse

Methods included from Concerns::Named

#fully_qualified_name, #namespaces

Methods inherited from Base

#sub_type

Instance Method Details

#auto_valueObject



22
23
24
25
26
27
28
29
30
# File 'lib/xdrgen/ast/definitions/enum_member.rb', line 22

def auto_value
  index = enum.members.index(self)
  if index == 0
    0
  else
    # use the previous members value + 1
    enum.members[index - 1].value + 1
  end
end

#defined_valueObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/xdrgen/ast/definitions/enum_member.rb', line 32

def defined_value
  return if value_n.terminal?

  case value_n.val
  when Constant
    value_n.val.value
  when Identifier
    namespace.find_enum_value(value_n.val.name).defined_value
  end
end

#valueObject



9
10
11
12
13
14
15
# File 'lib/xdrgen/ast/definitions/enum_member.rb', line 9

def value
  unsigned_value = defined_value || auto_value

  # enums are signed in xdr, so...
  # convert to twos complement value
  [unsigned_value].pack("l>").unpack("l>").first
end