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
----
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 $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 = 'int32_t' if (!$subtype);
@ -279,9 +285,12 @@ sub render_item_number {
push @lines_rb, 'number 8, true';
} elsif ($subtype eq 's-float') {
push @lines_rb, 'float';
return;
} else {
print "no render number $subtype\n";
return;
}
$lines_rb[$#lines_rb] .= ", $initvalue" if ($initvalue);
}
sub render_item_compound {

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

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