1 |
#! perl |
2 |
|
3 |
# used to automatically create accessors/mutators and method interfaces |
4 |
# to C++ classes for Perl. |
5 |
|
6 |
my $class = shift; |
7 |
my $curclass = ""; |
8 |
my (@scalar_member, @array_member); |
9 |
|
10 |
for my $file (@ARGV) { |
11 |
open my $fh, "<:utf8", $file |
12 |
or die "$file: $!"; |
13 |
|
14 |
while (<$fh>) { |
15 |
next if /^\s*\//; # skip lines starting with / |
16 |
if ($curclass eq $class) { |
17 |
while (/ACC\s*\(R([WO])\s*,\s*([^)\]]+)(?:\[(\S+)\])?\)/g) { |
18 |
if ($3) { |
19 |
push @array_member, [$1, $2, $3]; |
20 |
} else { |
21 |
push @scalar_member, [$1, $2]; |
22 |
} |
23 |
} |
24 |
if (/MTH \s+ ([^(]*?) \s* ([A-Za-z_0-9]+) \s* \(([^)]*)\)/x) { |
25 |
push @method_member, [$1, $2, $3]; |
26 |
} |
27 |
} |
28 |
while (/INTERFACE_CLASS\s*\((\S+)\)/g) { |
29 |
$curclass = $1; |
30 |
} |
31 |
} |
32 |
} |
33 |
|
34 |
for (@method_member) { |
35 |
my ($rettype, $name, $params) = @$_; |
36 |
|
37 |
if ($rettype =~ s/static\s+//) { |
38 |
my $args = join ", ", $params =~ m/.*?([a-zA-Z_0-9]+)(?:,\s*|$)/g; |
39 |
if ($rettype ne "void") { |
40 |
print "$rettype\n$name ($params)\n", |
41 |
"\tCODE:\n", |
42 |
"\tRETVAL = $class\::$name ($args);\n", |
43 |
"\tOUTPUT:\n", |
44 |
"\tRETVAL\n"; |
45 |
} else { |
46 |
print "$rettype\n$name ($params)\n", |
47 |
"\tCODE:\n", |
48 |
"\t$class\::$name ($args);\n", |
49 |
} |
50 |
} else { |
51 |
print "$rettype\n$class\::$name ($params)\n"; |
52 |
} |
53 |
|
54 |
print "\n"; |
55 |
} |
56 |
|
57 |
if (@scalar_member) { |
58 |
print "SV *$scalar_member[0][1] ($class *self, SV *newval = 0)\n"; |
59 |
print "\tPROTOTYPE: \$;\$\n"; |
60 |
|
61 |
if (@scalar_member > 1) { |
62 |
print "\tALIAS:\n"; |
63 |
for (1 .. $#scalar_member) { |
64 |
print "\t\t$scalar_member[$_][1]\t= $_\n"; |
65 |
} |
66 |
} |
67 |
|
68 |
print "\tCODE:\n"; |
69 |
|
70 |
# read |
71 |
print "\tif (GIMME_V == G_VOID)\n", |
72 |
"\t RETVAL = &PL_sv_undef;\n", |
73 |
"\telse\n", |
74 |
"\t switch (ix)\n", |
75 |
"\t {\n", |
76 |
(map "\t case $_: RETVAL = to_sv (self->$scalar_member[$_][1]); break;\n", |
77 |
0 .. $#scalar_member), |
78 |
"\t default: croak (\"scalar_member is write-only\");\n", |
79 |
"\t };\n"; |
80 |
|
81 |
# write |
82 |
print "\tif (newval)\n", |
83 |
"\t switch (ix)\n", |
84 |
"\t {\n", |
85 |
(map "\t case $_: sv_to (newval, self->$scalar_member[$_][1]); break;\n", |
86 |
grep $scalar_member[$_][0] eq "W", |
87 |
0 .. $#scalar_member), |
88 |
"\t default: croak (\"scalar_member is read-only\");\n", |
89 |
"\t };\n"; |
90 |
|
91 |
print "\tOUTPUT: RETVAL\n\n"; |
92 |
} |
93 |
|
94 |
if (@array_member) { |
95 |
print "SV *$array_member[0][1] ($class *self, int idx, SV *newval = 0)\n"; |
96 |
print "\tPROTOTYPE: \$;\$\n"; |
97 |
|
98 |
if (@array_member > 1) { |
99 |
print "\tALIAS:\n"; |
100 |
for (1 .. $#array_member) { |
101 |
print "\t\t$array_member[$_][1]\t= $_\n"; |
102 |
} |
103 |
} |
104 |
|
105 |
print "\tCODE:\n"; |
106 |
|
107 |
print "\tif (idx < 0) croak (\"negative array index\");\n"; |
108 |
|
109 |
# range |
110 |
print "\t switch (ix)\n", |
111 |
"\t {\n", |
112 |
(map "\t case $_: if (idx >= $array_member[$_][2]) croak (\"array index out of bounds\"); break;\n", |
113 |
0 .. $#array_member), |
114 |
"\t };\n"; |
115 |
|
116 |
# read |
117 |
print "\tif (GIMME_V == G_VOID)\n", |
118 |
"\t RETVAL = &PL_sv_undef;\n", |
119 |
"\telse\n", |
120 |
"\t switch (ix)\n", |
121 |
"\t {\n", |
122 |
(map "\t case $_: RETVAL = to_sv (self->$array_member[$_][1] [idx]); break;\n", |
123 |
0 .. $#array_member), |
124 |
"\t default: croak (\"array_member is write-only\");\n", |
125 |
"\t };\n"; |
126 |
|
127 |
# write |
128 |
print "\tif (newval)\n", |
129 |
"\t switch (ix)\n", |
130 |
"\t {\n", |
131 |
(map "\t case $_: sv_to (newval, self->$array_member[$_][1] [idx]); break;\n", |
132 |
grep $array_member[$_][0] eq "W", |
133 |
0 .. $#array_member), |
134 |
"\t default: croak (\"array_member is read-only\");\n", |
135 |
"\t };\n"; |
136 |
|
137 |
print "\tOUTPUT: RETVAL\n\n"; |
138 |
} |
139 |
|