Make get_(in|out)putmap more flexible
authorSteffen Mueller <smueller@cpan.org>
Fri, 18 Feb 2011 20:36:32 +0000 (21:36 +0100)
committerSteffen Mueller <smueller@cpan.org>
Tue, 12 Jul 2011 18:54:49 +0000 (20:54 +0200)
They now also accept ctypes which are resolved to xstypes via the
typemap section.

dist/ExtUtils-ParseXS/lib/ExtUtils/Typemaps.pm
dist/ExtUtils-ParseXS/t/510-t-bare.t

index 933af7e..9755293 100644 (file)
@@ -449,7 +449,14 @@ Fetches an entry of the INPUT section of the
 typemap.
 
 Mandatory named arguments: The C<xstype> of the
-entry.
+entry or the C<ctype> of the typemap that can be used to find
+the C<xstype>. To wit, the following pieces of code
+are equivalent:
+
+  my $type = $typemap->get_typemap(ctype => $ctype)
+  my $input_map = $typemap->get_inputmap(xstype => $type->xstype);
+
+  my $input_map = $typemap->get_inputmap(ctype => $ctype);
 
 Returns the C<ExtUtils::Typemaps::InputMap>
 object for the entry if found.
@@ -460,7 +467,16 @@ sub get_inputmap {
   my $self = shift;
   my %args = @_;
   my $xstype = $args{xstype};
-  die("Need xstype argument") if not defined $xstype;
+  my $ctype  = $args{ctype};
+  die("Need xstype or ctype argument")
+    if not defined $xstype
+    and not defined $ctype;
+  die("Need xstype OR ctype arguments, not both")
+    if defined $xstype and defined $ctype;
+
+  if (defined $ctype) {
+    $xstype = $self->get_typemap(ctype => $ctype)->xstype;
+  }
 
   my $index = $self->{input_lookup}{$xstype};
   return() if not defined $index;
@@ -473,7 +489,8 @@ Fetches an entry of the OUTPUT section of the
 typemap.
 
 Mandatory named arguments: The C<xstype> of the
-entry.
+entry or the C<ctype> of the typemap that can be used to
+resolve the C<xstype>. (See above for an example.)
 
 Returns the C<ExtUtils::Typemaps::InputMap>
 object for the entry if found.
@@ -484,7 +501,16 @@ sub get_outputmap {
   my $self = shift;
   my %args = @_;
   my $xstype = $args{xstype};
-  die("Need xstype argument") if not defined $xstype;
+  my $ctype  = $args{ctype};
+  die("Need xstype or ctype argument")
+    if not defined $xstype
+    and not defined $ctype;
+  die("Need xstype OR ctype arguments, not both")
+    if defined $xstype and defined $ctype;
+
+  if (defined $ctype) {
+    $xstype = $self->get_typemap(ctype => $ctype)->xstype;
+  }
 
   my $index = $self->{output_lookup}{$xstype};
   return() if not defined $index;
index b76b10a..a58bca8 100644 (file)
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 29;
+use Test::More tests => 30;
 use ExtUtils::Typemaps;
 
 # typemap only
@@ -44,6 +44,10 @@ HERE
   my $in = $map->get_inputmap(xstype => 'T_UV');
   isa_ok($in, 'ExtUtils::Typemaps::InputMap');
   is($in->xstype, 'T_UV');
+
+  # test fetching inputmap by ctype
+  my $in2 = $map->get_inputmap(ctype => 'unsigned int');
+  is_deeply($in2, $in, "get_inputmap returns the same typemap for ctype and xstype");
 }