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