blib/lib/ExtUtils/XSpp/Typemap.pm

filter: Statementstmt 83.1% 84 / 101 Branchbran 72.7% 16 / 22 Conditioncond 58.3% 7 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 77.7% 21 / 27 Podpod 43.4% 10 / 23 totaltotal 73.4% 141 / 192 CC 23 SCAR 15.7
CC
23
Cov
79%
get_typemap_for_type
11.8
get_xs_typemap_code_for_all_typemaps
10.2
_enable_default_xs_typemaps
5.0
CRAP 4.8
« blib/lib/ExtUtils/XSpp/Node/Class.pm ↑ Summary blib/lib/ExtUtils/XSpp/Node.pm »
1 package ExtUtils::XSpp::Typemap;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 21 ▶use ExtUtils::Typemaps;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 require ExtUtils::XSpp::Node::Type;
8 21 require ExtUtils::XSpp::Typemap::parsed;
9 21 require ExtUtils::XSpp::Typemap::simple;
10 21 require ExtUtils::XSpp::Typemap::reference;
11  
12 21 my %TypemapsByName;
13  
14 =head1 NAME
15  
16 ExtUtils::XSpp::Typemap - map types
17  
18 =cut
19  
20 3131 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
21 3131 my $class = shift;
22 3131 my $this = bless {}, $class;
23  
24 3131 $this->init( @_ );
25  
26 3131 return $this;
27 }
28  
29 28 ▶sub create {
Subroutine create: called CC=2 SCAR=7.9
Pod undocumented
30 28 my( $name, @args ) = @_;
31  
32 28 ▶ if( my $template = $TypemapsByName{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $template = $TypemapsByName{$name}) { } 0 28 28
33 0 my $package = ref $template;
34  
35 0 return $package->new( base => $template, @args );
36 } else {
37 28 my $package = "ExtUtils::XSpp::Typemap::" . $name;
38  
39 28 return $package->new( @args );
40 }
41 }
42  
43 =head1 METHODS
44  
45 =head2 ExtUtils::XSpp::Typemap::type
46  
47 Returns the ExtUtils::XSpp::Node::Type that is used for this typemap.
48  
49 =cut
50  
51 122 ▶sub type { $_[0]->{TYPE} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
52  
53 =head2 ExtUtils::XSpp::Typemap::xs_type()
54  
55 (Optional) XS typemap identifier (e.g. T_IV) for this C++ type.
56  
57 =head2 ExtUtils::XSpp::Typemap::xs_input_code()
58  
59 (Optional) XS input code for the associated XS typemap.
60  
61 =head2 ExtUtils::XSpp::Typemap::xs_output_code()
62  
63 (Optional) XS output code for the associated XS typemap.
64  
65 =head2 ExtUtils::XSpp::Typemap::cpp_type()
66  
67 Returns the C++ type to be used for the local variable declaration.
68  
69 =head2 ExtUtils::XSpp::Typemap::input_code( perl_argument_name, cpp_var_name1, ... )
70  
71 Code to put the contents of the perl_argument (typically ST(x)) into
72 the C++ variable(s).
73  
74 =head2 ExtUtils::XSpp::Typemap::output_code( perl_variable, c_variable )
75  
76 =head2 ExtUtils::XSpp::Typemap::cleanup_code( perl_variable, c_variable )
77  
78 =head2 ExtUtils::XSpp::Typemap::call_parameter_code( parameter_name )
79  
80 =head2 ExtUtils::XSpp::Typemap::call_function_code( function_call_code, return_variable )
81  
82 Allows modifying the code used in the function/method call. The first
83 parameter has the form C<THIS->method( <args> )>, the second
84 parameter is a variable to hold the return value.
85  
86 =cut
87  
88 0 ▶sub init { }
Subroutine init: not called CC=1 SCAR=0.0
Pod undocumented
89  
90 1609 ▶sub xs_type { $_[0]->{XS_TYPE} }
Subroutine xs_type: called CC=1 SCAR=0.0
Pod documented
91 13 ▶sub xs_input_code { $_[0]->{XS_INPUT_CODE} }
Subroutine xs_input_code: called CC=1 SCAR=0.0
Pod documented
92 13 ▶sub xs_output_code { $_[0]->{XS_OUTPUT_CODE} }
Subroutine xs_output_code: called CC=1 SCAR=0.0
Pod documented
93 3333 ▶sub name { $_[0]->{NAME} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
94 0 ▶sub cpp_type { die; }
Subroutine cpp_type: not called CC=1 SCAR=6.9
Pod documented
95 0 ▶sub input_code { die; }
Subroutine input_code: not called CC=1 SCAR=6.9
Pod documented
96 111 ▶sub precall_code { undef }
Subroutine precall_code: called CC=1 SCAR=0.0
Pod undocumented
97 0 ▶sub output_code { undef }
Subroutine output_code: not called CC=1 SCAR=6.9
Pod documented
98 71 ▶sub cleanup_code { undef }
Subroutine cleanup_code: called CC=1 SCAR=0.0
Pod documented
99 0 ▶sub call_parameter_code { undef }
Subroutine call_parameter_code: not called CC=1 SCAR=6.9
Pod documented
100 0 ▶sub call_function_code { undef }
Subroutine call_function_code: not called CC=1 SCAR=6.9
Pod documented
101 140 ▶sub output_list { undef }
Subroutine output_list: called CC=1 SCAR=0.0
Pod undocumented
102  
103 my @Typemaps;
104 21 my $Default_output_code = 'sv_setref_pv( $arg, xsp_constructor_class("${my $ntt = $type; $ntt =~ s{^const\s+|[ \t*]+$}{}g; \\$ntt}"), (void*)$var );';
105 21 my $Default_input_code = <<'INPUTCODE';
106 if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
107 $var = ($type)SvIV((SV*)SvRV( $arg ));
108 else{
109 warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
110 XSRETURN_UNDEF;
111 }
112 INPUTCODE
113  
114  
115 # add typemaps for basic C types
116 21 add_default_typemaps();
117  
118 3021 ▶sub add_typemap_for_type {
Subroutine add_typemap_for_type: called CC=2 SCAR=6.9
Pod undocumented
119 3021 my( $type, $typemap ) = @_;
120  
121 3021 unshift @Typemaps, [ $type, $typemap ];
122 3021 ▶ $TypemapsByName{$typemap->name} = $typemap if $typemap->name;
Branch: 1 branch
BranchTrueFalseTotal
if $typemap->name 188 2833 3021
123 }
124  
125 166 ▶sub reset_typemaps {
Subroutine reset_typemaps: called CC=1 SCAR=0.0
Pod undocumented
126 166 @Typemaps = ();
127 166 add_default_typemaps();
128 }
129  
130 # a weak typemap does not override an already existing typemap for the
131 # same type
132 108 ▶sub add_weak_typemap_for_type {
Subroutine add_weak_typemap_for_type: called CC=3 SCAR=14.2
Pod undocumented
133 108 my( $type, $typemap ) = @_;
134 108 push @Typemaps, [ $type, $typemap ];
135 108 ▶ $TypemapsByName{$typemap->name} ||= $typemap if $typemap->name;
Branch: 1 branch
BranchTrueFalseTotal
if $typemap->name 0 108 108
Condition0%0/3
$TypemapsByName{$typemap->name} ||= $typemap
l!l&&r!l&&!r
000
MC/DC0%0/2
$TypemapsByName{$typemap->name} ||= $typemap
$TypemapsByName{$typemap->name} $typemap
Truth table0%0/3
A ||= B
A= $TypemapsByName{$typemap->name}B= $typemap
ABresult
1X1
011
000
136 }
137  
138 227 ▶sub get_typemap_for_type {
Subroutine get_typemap_for_type: called CC=5 SCAR=24.7
Pod undocumented
139 227 my $type = shift;
140  
141 227 foreach my $t ( @Typemaps ) {
142 2407 ▶ return ${$t}[1] if $t->[0]->equals( $type );
Branch: 1 branch
BranchTrueFalseTotal
if $t->[0]->equals($type) 227 2180 2407
143 }
144  
145 # construct verbose error message:
146 0 my $errmsg = "No typemap for type " . $type->print
147 . "\nThere are typemaps for the following types:\n";
148 0 my @types;
149 0 foreach my $t (@Typemaps) {
150 0 push @types, " - " . $t->[0]->print . "\n";
151 }
152  
153 0 ▶ if (@types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@types) { } 0 0 0
154 0 $errmsg .= join('', @types);
155 }
156 else {
157 0 $errmsg .= " (none)\n";
158 }
159 0 $errmsg .= "Did you forget to declare your type in an XS++ typemap?";
160  
161 0 Carp::confess( $errmsg );
162 }
163  
164 87 ▶sub get_xs_typemap_code_for_all_typemaps {
Subroutine get_xs_typemap_code_for_all_typemaps: called CC=10 SCAR=23.2
Pod undocumented
165 87 my $typemaps = ExtUtils::Typemaps->new;
166  
167 # process typemaps in reverse order, so newer ones take precedence
168 87 my @xs_typemaps = grep $_->[1]->xs_type, reverse @Typemaps;
169 87 ▶ return unless @xs_typemaps;
Branch: 1 branch
BranchTrueFalseTotal
unless @xs_typemaps 83 4 87
170  
171 4 my %xs_types;
172 4 ▶ foreach my $typemap (grep $_->[1]->cpp_type && $_->[1]->cpp_type ne '_', @xs_typemaps) {
Condition100%3/3
$_->[1]->cpp_type && $_->[1]->cpp_type ne '_'
!ll&&!rl&&r
117
MC/DC100%2/2
$_->[1]->cpp_type && $_->[1]->cpp_type ne '_'
$_->[1]->cpp_type $_->[1]->cpp_type ne '_'
Truth table100%3/3
A && B
A= $_->[1]->cpp_typeB= $_->[1]->cpp_type ne '_'
ABresult
0X0
100
111
173 7 my $xstype = $typemap->[1]->xs_type;
174  
175 7 $xs_types{$typemap->[1]->cpp_type} = $xstype;
176 7 $typemaps->add_typemap(
177 ctype => $typemap->[1]->cpp_type,
178 xstype => $xstype,
179 replace => 1,
180 );
181 }
182  
183 # avoid adding INPUT/OUTPUT sections for unused mappings
184 4 %xs_types = reverse %xs_types;
185 4 ▶ foreach my $typemap (grep $xs_types{$_->[1]->xs_type || ''}, @xs_typemaps) {
Condition50%1/2
$_->[1]->xs_type || ''
l!l
90
MC/DC0%0/1
$_->[1]->xs_type || ''
$_->[1]->xs_type
Truth table50%1/2
A
A= $_->[1]->xs_type
Aresult
11
00
186 9 my $xstype = $typemap->[1]->xs_type;
187  
188 9 ▶ $typemaps->add_inputmap(
Branch: 1 branch
BranchTrueFalseTotal
if $typemap->[1]->xs_input_code 4 5 9
189 xstype => $xstype,
190 code => $typemap->[1]->xs_input_code,
191 replace => 1,
192 ) if $typemap->[1]->xs_input_code;
193  
194 9 ▶ $typemaps->add_outputmap(
Branch: 1 branch
BranchTrueFalseTotal
if $typemap->[1]->xs_output_code 4 5 9
195 xstype => $xstype,
196 code => $typemap->[1]->xs_output_code,
197 replace => 1,
198 ) if $typemap->[1]->xs_output_code;
199 }
200  
201 4 ▶ return '' if $typemaps->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $typemaps->is_empty 0 4 4
202 4 my $code = $typemaps->as_string;
203 4 my $end_marker = 'END';
204 4 ▶ while ($code =~ /^\Q$end_marker\E\s*$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\Q$end_marker\E\s*$/m) 0 4 4
205 0 $end_marker .= '_';
206 }
207 4 return "TYPEMAP: <<$end_marker\n$code\n$end_marker\n";
208 }
209  
210 # adds default typemaps for C* and C&
211 54 ▶sub add_class_default_typemaps {
Subroutine add_class_default_typemaps: called CC=1 SCAR=0.0
Pod undocumented
212 54 my( $name ) = @_;
213  
214 54 my $ptr = ExtUtils::XSpp::Node::Type->new
215 ( base => $name,
216 pointer => 1,
217 );
218 54 my $ref = ExtUtils::XSpp::Node::Type->new
219 ( base => $name,
220 reference => 1,
221 );
222  
223 54 my $xs_type = $TypemapsByName{object}->xs_type;
224  
225 54 add_weak_typemap_for_type
226 ( $ptr, ExtUtils::XSpp::Typemap::simple->new( type => $ptr, xs_type => $xs_type ) );
227 54 add_weak_typemap_for_type
228 ( $ref, ExtUtils::XSpp::Typemap::reference->new( type => $ref, xs_type => $xs_type ) );
229 }
230  
231 187 ▶sub add_default_typemaps {
Subroutine add_default_typemaps: called CC=2 SCAR=6.9
Pod undocumented
232 # void, integral and floating point types
233 187 foreach my $t ( 'char', 'short', 'int', 'long', 'bool',
234 'unsigned char', 'unsigned short', 'unsigned int',
235 'unsigned long', 'void',
236 'float', 'double', 'long double' ) {
237 2431 my $type = ExtUtils::XSpp::Node::Type->new( base => $t );
238  
239 2431 ExtUtils::XSpp::Typemap::add_typemap_for_type
240 ( $type, ExtUtils::XSpp::Typemap::simple->new( type => $type ) );
241 }
242  
243 # char*, const char*
244 187 my $char_p = ExtUtils::XSpp::Node::Type->new
245 ( base => 'char',
246 pointer => 1,
247 );
248  
249 187 ExtUtils::XSpp::Typemap::add_typemap_for_type
250 ( $char_p, ExtUtils::XSpp::Typemap::simple->new( type => $char_p ) );
251  
252 187 my $const_char_p = ExtUtils::XSpp::Node::Type->new
253 ( base => 'char',
254 pointer => 1,
255 const => 1,
256 );
257  
258 187 ExtUtils::XSpp::Typemap::add_typemap_for_type
259 ( $const_char_p, ExtUtils::XSpp::Typemap::simple->new( type => $const_char_p ) );
260  
261 # objects
262 187 my $dummy_type = ExtUtils::XSpp::Node::Type->new( base => '' );
263 187 my $obj_typemap = ExtUtils::XSpp::Typemap::parsed->new(
264 name => 'object',
265 type => $dummy_type,
266 xs_input_code => $Default_input_code,
267 xs_output_code => $Default_output_code,
268 );
269  
270 187 ExtUtils::XSpp::Typemap::add_typemap_for_type( $dummy_type, $obj_typemap )
271 }
272  
273 1 ▶sub _enable_default_xs_typemaps {
Subroutine _enable_default_xs_typemaps: called CC=5 SCAR=16.1
274 1 foreach my $t ( reverse @Typemaps ) {
275 16 ▶ if( ($t->[1]->name || '') eq 'object' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($t->[1]->name || "") eq "object") 1 15 16
Condition100%2/2
$t->[1]->name || ''
l!l
115
MC/DC100%1/1
$t->[1]->name || ''
$t->[1]->name
Truth table100%2/2
A
A= $t->[1]->name
Aresult
11
00
276 1 ▶ $t->[1]{XS_TYPE} ||= 'O_OBJECT';
Condition50%1/2
$t->[1]{"XS_TYPE"} ||= "O_OBJECT"
l!l
01
MC/DC0%0/1
$t->[1]{"XS_TYPE"} ||= "O_OBJECT"
$t->[1]{"XS_TYPE"}
Truth table50%1/2
A
A= $t->[1]{"XS_TYPE"}
Aresult
11
00
277 1 last;
278 }
279 }
280 }
281  
282 21 1;
« blib/lib/ExtUtils/XSpp/Node/Class.pm ↑ Summary blib/lib/ExtUtils/XSpp/Node.pm »