ruby: use enums in single-bit values too

develop
jj 2012-08-03 16:53:54 +02:00
parent 160487f7eb
commit f400591080
2 changed files with 8 additions and 8 deletions

@ -175,10 +175,10 @@ sub render_bitfield_fields {
if ($name) if ($name)
{ {
if ($count == 1) { if ($enum) {
push @lines_rb, "field(:$name, 0) { bit $shift }";
} elsif ($enum) {
push @lines_rb, "field(:$name, 0) { bits $shift, $count, $enum }"; push @lines_rb, "field(:$name, 0) { bits $shift, $count, $enum }";
} elsif ($count == 1) {
push @lines_rb, "field(:$name, 0) { bit $shift }";
} else { } else {
push @lines_rb, "field(:$name, 0) { bits $shift, $count }"; push @lines_rb, "field(:$name, 0) { bits $shift, $count }";
} }

@ -34,8 +34,8 @@ module DFHack
def float def float
Float.new Float.new
end end
def bit(shift) def bit(shift, enum=nil)
BitField.new(shift, 1) BitField.new(shift, 1, enum)
end end
def bits(shift, len, enum=nil) def bits(shift, len, enum=nil)
BitField.new(shift, len, enum) BitField.new(shift, len, enum)
@ -147,7 +147,7 @@ module DFHack
out << '>' out << '>'
end end
def inspect_field(n, o, s) def inspect_field(n, o, s)
if s.kind_of?(BitField) and s._len == 1 if s.kind_of?(BitField) and s._len == 1 and not s._enum
send(n) ? n.to_s : '' send(n) ? n.to_s : ''
elsif s.kind_of?(Pointer) elsif s.kind_of?(Pointer)
"#{n}=#{s._at(@_memaddr+o).inspect}" "#{n}=#{s._at(@_memaddr+o).inspect}"
@ -242,7 +242,7 @@ module DFHack
def _get def _get
v = DFHack.memory_read_int32(@_memaddr) >> @_shift v = DFHack.memory_read_int32(@_memaddr) >> @_shift
if @_len == 1 if @_len == 1 and not @_enum
((v & 1) == 0) ? false : true ((v & 1) == 0) ? false : true
else else
v &= _mask v &= _mask
@ -252,7 +252,7 @@ module DFHack
end end
def _set(v) def _set(v)
if @_len == 1 if @_len == 1 and (not @_enum or v == false or v == true)
# allow 'bit = 0' # allow 'bit = 0'
v = (v && v != 0 ? 1 : 0) v = (v && v != 0 ? 1 : 0)
end end