ruby: fix new c++ obj initialization, use xml init-value

develop
jj 2012-04-26 14:16:47 +02:00
parent 7a8db179ab
commit 03925c923a
4 changed files with 45 additions and 21 deletions

@ -112,4 +112,4 @@ really a Pointer (with no 'pos' method).
Todo Todo
---- ----
Correct c++ object (de)allocation (call ctor etc) Correct c++ object (de)allocation (call ctor etc) ; ability to call vtable methods

@ -258,6 +258,12 @@ sub render_item_number {
my ($item, $pns) = @_; my ($item, $pns) = @_;
my $subtype = $item->getAttribute('ld:subtype'); my $subtype = $item->getAttribute('ld:subtype');
my $initvalue = $item->getAttribute('init-value');
$initvalue = 1 if ($initvalue and $initvalue eq 'true');
# XXX needs pre-declaration of the enum...
$initvalue = rb_ucase($item->getAttribute('type-name')) . '::' . $initvalue if ($subtype and $subtype eq 'enum' and $initvalue =~ /[a-zA-Z]/);
$subtype = $item->getAttribute('base-type') if (!$subtype or $subtype eq 'enum' or $subtype eq 'bitfield'); $subtype = $item->getAttribute('base-type') if (!$subtype or $subtype eq 'enum' or $subtype eq 'bitfield');
$subtype = 'int32_t' if (!$subtype); $subtype = 'int32_t' if (!$subtype);
@ -279,9 +285,12 @@ sub render_item_number {
push @lines_rb, 'number 8, true'; push @lines_rb, 'number 8, true';
} elsif ($subtype eq 's-float') { } elsif ($subtype eq 's-float') {
push @lines_rb, 'float'; push @lines_rb, 'float';
return;
} else { } else {
print "no render number $subtype\n"; print "no render number $subtype\n";
return;
} }
$lines_rb[$#lines_rb] .= ", $initvalue" if ($initvalue);
} }
sub render_item_compound { sub render_item_compound {

@ -4,6 +4,7 @@ class MemStruct
attr_accessor :_memaddr attr_accessor :_memaddr
def _at(addr) ; d = dup ; d._memaddr = addr ; d ; end def _at(addr) ; d = dup ; d._memaddr = addr ; d ; end
def _get ; self ; end def _get ; self ; end
def _cpp_init ; end
def inspect ; _get.inspect ; end def inspect ; _get.inspect ; end
end end
@ -25,8 +26,8 @@ class Compound < MemStruct
end end
end end
def number(bits, signed) def number(bits, signed, initvalue=nil)
Number.new(bits, signed) Number.new(bits, signed, initvalue)
end end
def float def float
Float.new Float.new
@ -101,22 +102,13 @@ class Compound < MemStruct
DFHack.memory_write_int32(ptr, vt) DFHack.memory_write_int32(ptr, vt)
# TODO call constructor # TODO call constructor
end end
new._at(ptr)._cpp_init o = new._at(ptr)
o._cpp_init
o
end end
end end
def _cpp_init def _cpp_init
_fields.each { |n, o, s| _fields_ancestors.each { |n, o, s| s._at(@_memaddr+o)._cpp_init }
case s
when StlString
DFHack.memory_stlstring_init(@_memaddr+o)
when StlVector32
# englobes all vectors
DFHack.memory_vector_init(@_memaddr+o)
when Compound
send(n)._cpp_init
end
}
self
end end
def _set(h) ; h.each { |k, v| send("_#{k}=", v) } ; end def _set(h) ; h.each { |k, v| send("_#{k}=", v) } ; end
def _fields ; self.class._fields.to_a ; end def _fields ; self.class._fields.to_a ; end
@ -152,10 +144,11 @@ class Compound < MemStruct
end end
class Number < MemStruct class Number < MemStruct
attr_accessor :_bits, :_signed attr_accessor :_bits, :_signed, :_initvalue
def initialize(bits, signed) def initialize(bits, signed, initvalue)
@_bits = bits @_bits = bits
@_signed = signed @_signed = signed
@_initvalue = initvalue
end end
def _get def _get
@ -177,6 +170,10 @@ class Number < MemStruct
when 64; DFHack.memory_write_int32(@_memaddr, v & 0xffffffff) ; DFHack.memory_write_int32(@memaddr+4, v>>32) when 64; DFHack.memory_write_int32(@_memaddr, v & 0xffffffff) ; DFHack.memory_write_int32(@memaddr+4, v>>32)
end end
end end
def _cpp_init
_set(@_initvalue) if @_initvalue
end
end end
class Float < MemStruct class Float < MemStruct
# _get/_set defined in ruby.cpp # _get/_set defined in ruby.cpp
@ -187,6 +184,10 @@ class Float < MemStruct
def _set(v) def _set(v)
DFHack.memory_write_float(@_memaddr, v) DFHack.memory_write_float(@_memaddr, v)
end end
def _cpp_init
_set(0.0)
end
end end
class BitField < MemStruct class BitField < MemStruct
attr_accessor :_shift, :_len attr_accessor :_shift, :_len
@ -308,6 +309,9 @@ class StaticArray < MemStruct
def _set(a) def _set(a)
a.each_with_index { |v, i| self[i] = v } a.each_with_index { |v, i| self[i] = v }
end end
def _cpp_init
_length.times { |i| _tgat(i)._cpp_init }
end
alias length _length alias length _length
alias size _length alias size _length
def _tgat(i) def _tgat(i)
@ -334,6 +338,7 @@ class StaticArray < MemStruct
to_a.inspect to_a.inspect
end end
end end
def flatten ; map { |e| e.respond_to?(:flatten) ? e.flatten : e }.flatten ; end
end end
class StaticString < MemStruct class StaticString < MemStruct
attr_accessor :_length attr_accessor :_length
@ -373,6 +378,10 @@ class StlVector32 < MemStruct
v.each_with_index { |e, i| self[i] = e } # patch entries v.each_with_index { |e, i| self[i] = e } # patch entries
end end
def _cpp_init
DFHack.memory_vector_init(@_memaddr)
end
def clear def clear
delete_at(length-1) while length > 0 delete_at(length-1) while length > 0
end end
@ -475,6 +484,10 @@ class StlString < MemStruct
def _set(v) def _set(v)
DFHack.memory_write_stlstring(@_memaddr, v) DFHack.memory_write_stlstring(@_memaddr, v)
end end
def _cpp_init
DFHack.memory_stlstring_init(@_memaddr)
end
end end
class StlDeque < MemStruct class StlDeque < MemStruct
attr_accessor :_tglen, :_tg attr_accessor :_tglen, :_tg
@ -496,6 +509,7 @@ class DfFlagarray < MemStruct
def length def length
DFHack.memory_bitarray_length(@_memaddr) DFHack.memory_bitarray_length(@_memaddr)
end end
# TODO _cpp_init
def size ; length ; end def size ; length ; end
def resize(len) def resize(len)
DFHack.memory_bitarray_resize(@_memaddr, len) DFHack.memory_bitarray_resize(@_memaddr, len)
@ -538,6 +552,7 @@ class DfArray < Compound
def length ; _length ; end def length ; _length ; end
def size ; _length ; end def size ; _length ; end
# TODO _cpp_init
def _tgat(i) def _tgat(i)
@_tg._at(_ptr + i*@_tglen) if i >= 0 and i < _length @_tg._at(_ptr + i*@_tglen) if i >= 0 and i < _length
end end

@ -228,12 +228,12 @@ module DFHack
# add an announcement # add an announcement
# color = integer, bright = bool # color = integer, bright = bool
def add_announcement(str, color=7, bright=false) def add_announcement(str, color=nil, bright=nil)
cont = false cont = false
while str.length > 0 while str.length > 0
rep = Report.cpp_new rep = Report.cpp_new
rep.color = color rep.color = color if color
rep.bright = ((bright && bright != 0) ? 1 : 0) rep.bright = ((bright && bright != 0) ? 1 : 0) if bright != nil
rep.year = cur_year rep.year = cur_year
rep.time = cur_year_tick rep.time = cur_year_tick
rep.flags.continuation = cont rep.flags.continuation = cont