ruby: name nested compounds, to allow cpp_new etc

develop
jj 2012-04-27 17:59:54 +02:00
parent bc218db596
commit a7afe04f3b
2 changed files with 10 additions and 7 deletions

@ -328,8 +328,13 @@ sub render_item_compound {
my $cppns = $pns . '::' . $item->getAttribute('ld:typedef-name'); my $cppns = $pns . '::' . $item->getAttribute('ld:typedef-name');
my $subtype = $item->getAttribute('ld:subtype'); my $subtype = $item->getAttribute('ld:subtype');
my @namecomponents = split('::', $cppns);
shift @namecomponents;
my $classname = join('_', map { rb_ucase($_) } @namecomponents);
if (!$subtype || $subtype eq 'bitfield') { if (!$subtype || $subtype eq 'bitfield') {
push @lines_rb, "compound {"; push @lines_rb, "compound(:$classname) {";
indent_rb { indent_rb {
if (!$subtype) { if (!$subtype) {
render_struct_fields($item, $cppns); render_struct_fields($item, $cppns);
@ -339,10 +344,7 @@ sub render_item_compound {
}; };
push @lines_rb, "}" push @lines_rb, "}"
} elsif ($subtype eq 'enum') { } elsif ($subtype eq 'enum') {
my @namecomponents = split('::', $cppns); push @lines_rb, "class ::DFHack::$classname";
shift @namecomponents;
my $enumclassname = join('_', map { rb_ucase($_) } @namecomponents);
push @lines_rb, "class ::DFHack::$enumclassname";
indent_rb { indent_rb {
# declare constants # declare constants
render_enum_fields($item); render_enum_fields($item);
@ -350,7 +352,7 @@ sub render_item_compound {
push @lines_rb, "end\n"; push @lines_rb, "end\n";
# actual field # actual field
render_item_number($item, $enumclassname); render_item_number($item, $classname);
} else { } else {
print "no render compound $subtype\n"; print "no render compound $subtype\n";
} }

@ -82,8 +82,9 @@ class Compound < MemStruct
def global(glob) def global(glob)
Global.new(glob) Global.new(glob)
end end
def compound(&b) def compound(name=nil, &b)
m = Class.new(Compound) m = Class.new(Compound)
DFHack.const_set(name, m) if name
m.instance_eval(&b) m.instance_eval(&b)
m.new m.new
end end