=head1 NAME ExtUtils::Typemaps - Read/Write/Modify Perl/XS typemap files =head1 SYNOPSIS # read/create file my $typemap = ExtUtils::Typemaps->new(file => 'typemap'); # alternatively create an in-memory typemap # $typemap = ExtUtils::Typemaps->new(); # alternatively create an in-memory typemap by parsing a string # $typemap = ExtUtils::Typemaps->new(string => $sometypemap); # add a mapping $typemap->add_typemap(ctype => 'NV', xstype => 'T_NV'); $typemap->add_inputmap( xstype => 'T_NV', code => '$var = ($type)SvNV($arg);' ); $typemap->add_outputmap( xstype => 'T_NV', code => 'sv_setnv($arg, (NV)$var);' ); $typemap->add_string(string => $typemapstring); # will be parsed and merged # remove a mapping (same for remove_typemap and remove_outputmap...) $typemap->remove_inputmap(xstype => 'SomeType'); # save a typemap to a file $typemap->write(file => 'anotherfile.map'); # merge the other typemap into this one $typemap->merge(typemap => $another_typemap); =head1 DESCRIPTION This module can read, modify, create and write Perl XS typemap files. If you don't know what a typemap is, please confer the L and L manuals. The module is not entirely round-trip safe: For example it currently simply strips all comments. The order of entries in the maps is, however, preserved. We check for duplicate entries in the typemap, but do not check for missing C entries for C or C entries since these might be hidden in a different typemap. =head1 METHODS =cut =head2 new Returns a new typemap object. Takes an optional C parameter. If set, the given file will be read. If the file doesn't exist, an empty typemap is returned. Alternatively, if the C parameter is given, the supplied string will be parsed instead of a file. =cut =head2 file Get/set the file that the typemap is written to when the C method is called. =cut =head2 add_typemap Add a C entry to the typemap. Required named arguments: The C (e.g. C 'double'>) and the C (e.g. C 'T_NV'>). Optional named arguments: C 1> forces removal/replacement of existing C entries of the same C. C 1> triggers a I<"first come first serve"> logic by which new entries that conflict with existing entries are silently ignored. As an alternative to the named parameters usage, you may pass in an C object as first argument, a copy of which will be added to the typemap. In that case, only the C or C named parameters may be used after the object. Example: $map->add_typemap($type_obj, replace => 1); =cut =head2 add_inputmap Add an C entry to the typemap. Required named arguments: The C (e.g. C 'T_NV'>) and the C to associate with it for input. Optional named arguments: C 1> forces removal/replacement of existing C entries of the same C. C 1> triggers a I<"first come first serve"> logic by which new entries that conflict with existing entries are silently ignored. As an alternative to the named parameters usage, you may pass in an C object as first argument, a copy of which will be added to the typemap. In that case, only the C or C named parameters may be used after the object. Example: $map->add_inputmap($type_obj, replace => 1); =cut =head2 add_outputmap Add an C entry to the typemap. Works exactly the same as C. =cut =head2 add_string Parses a string as a typemap and merge it into the typemap object. Required named argument: C to specify the string to parse. =cut =head2 remove_typemap Removes a C entry from the typemap. Required named argument: C to specify the entry to remove from the typemap. Alternatively, you may pass a single C object. =cut =head2 remove_inputmap Removes an C entry from the typemap. Required named argument: C to specify the entry to remove from the typemap. Alternatively, you may pass a single C object. =cut =head2 remove_inputmap Removes an C entry from the typemap. Required named argument: C to specify the entry to remove from the typemap. Alternatively, you may pass a single C object. =cut =head2 get_typemap Fetches an entry of the TYPEMAP section of the typemap. Mandatory named arguments: The C of the entry. Returns the C object for the entry if found. =cut =head2 get_inputmap Fetches an entry of the INPUT section of the typemap. Mandatory named arguments: The C of the entry or the C of the typemap that can be used to find the C. 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 object for the entry if found. =cut =head2 get_outputmap Fetches an entry of the OUTPUT section of the typemap. Mandatory named arguments: The C of the entry or the C of the typemap that can be used to resolve the C. (See above for an example.) Returns the C object for the entry if found. =cut =head2 write Write the typemap to a file. Optionally takes a C argument. If given, the typemap will be written to the specified file. If not, the typemap is written to the currently stored file name (see C<-Efile> above, this defaults to the file it was read from if any). =cut =head2 as_string Generates and returns the string form of the typemap. =cut =head2 as_embedded_typemap Generates and returns the string form of the typemap with the appropriate prefix around it for verbatim inclusion into an XS file as an embedded typemap. This will return a string like TYPEMAP: < $another_typemap_obj> or C $path_to_typemap_file> but not both. Optional arguments: C 1> to force replacement of existing typemap entries without warning or C 1> to skip entries that exist already in the typemap. =cut =head2 is_empty Returns a bool indicating whether this typemap is entirely empty. =cut =head2 list_mapped_ctypes Returns a list of the C types that are mappable by this typemap object. =cut =head2 _get_typemap_hash Returns a hash mapping the C types to the XS types: { 'char **' => 'T_PACKEDARRAY', 'bool_t' => 'T_IV', 'AV *' => 'T_AVREF', 'InputStream' => 'T_IN', 'double' => 'T_DOUBLE', # ... } This is documented because it is used by C, but it's not intended for general consumption. May be removed at any time. =cut =head2 _get_inputmap_hash Returns a hash mapping the XS types (identifiers) to the corresponding INPUT code: { 'T_CALLBACK' => ' $var = make_perl_cb_$type($arg) ', 'T_OUT' => ' $var = IoOFP(sv_2io($arg)) ', 'T_REF_IV_PTR' => ' if (sv_isa($arg, \\"${ntype}\\")) { # ... } This is documented because it is used by C, but it's not intended for general consumption. May be removed at any time. =cut =head2 _get_outputmap_hash Returns a hash mapping the XS types (identifiers) to the corresponding OUTPUT code: { 'T_CALLBACK' => ' sv_setpvn($arg, $var.context.value().chp(), $var.context.value().size()); ', 'T_OUT' => ' { GV *gv = newGVgen("$Package"); if ( do_open(gv, "+>&", 3, FALSE, 0, 0, $var) ) sv_setsv( $arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1)) ); else $arg = &PL_sv_undef; } ', # ... } This is documented because it is used by C, but it's not intended for general consumption. May be removed at any time. =cut =head2 _get_prototype_hash Returns a hash mapping the C types of the typemap to their corresponding prototypes. { 'char **' => '$', 'bool_t' => '$', 'AV *' => '$', 'InputStream' => '$', 'double' => '$', # ... } This is documented because it is used by C, but it's not intended for general consumption. May be removed at any time. =cut =head1 CAVEATS Inherits some evil code from C. =head1 SEE ALSO The parser is heavily inspired from the one in L. For details on typemaps: L, L. =head1 AUTHOR Steffen Mueller C<> =head1 COPYRIGHT & LICENSE Copyright 2009, 2010, 2011, 2012 Steffen Mueller This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut