Skip to content

Commit

Permalink
show how to use typemaps - fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Mar 25, 2019
1 parent 16b67fc commit 2860272
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 15 deletions.
17 changes: 9 additions & 8 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
Changes
MANIFEST
MANIFEST.SKIP
Makefile.PL
README
TESTED
LICENSE
inc/ILCPPConfig/OldCompilerGuess.pm
inc/ILCPPConfig/CompilerGuess.pm
inc/ILCPPConfig/OldCompilerGuess.pm
lib/Inline/CPP.pm
lib/Inline/CPP.pod
lib/Inline/CPP/Parser/RecDescent.pm
lib/Inline/CPP/Config.pm.in
lib/Inline/CPP/Parser/RecDescent.pm
LICENSE
Makefile.PL
MANIFEST
MANIFEST.SKIP
README
t/00load_prereqs.t
t/01fn_cans.t
t/02basic.t
Expand Down Expand Up @@ -50,12 +49,14 @@ t/grammar/16varlst.t
t/grammar/17memberarray.t
t/grammar/18namespace.t
t/grammar/19multidimarray.t
t/grammar/typemap.09purevt
t/namespace/01empty_ns_is_main.t
t/namespace/02main_ns_is_main.t
t/namespace/03baz_ns_is_baz.t
t/namespace/04invoke_from_non_main.t
t/namespace/05invalid_ns.t
t/namespace/06inherit.t
TESTED
xt/05pod.t
xt/07perlcritic.t
xt/09pod_coverage.t
Expand Down
2 changes: 1 addition & 1 deletion lib/Inline/CPP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ BADTYPE: while (1) {
warn "No typemap for type $badtype. "
. "Skipping $thing->{return_type} $thing->{name}("
. join(', ', @{ $thing->{arg_types} }) . ")\n"
if 0;
if 1;
return 0;
}

Expand Down
56 changes: 56 additions & 0 deletions lib/Inline/CPP.pod
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,62 @@ Inline::Struct does not understand any C++ features, so constructors
and member functions are not supported. See L<Inline::Struct> for more
details.

=head2 Example

Say you want to use a C++ standard C<string> type. A C++ method that
takes or returns such will be ignored unless you tell Perl how to map
it to and from Perl data types.

Put this in a file called F<typemap>:

TYPEMAP
string T_CPPSTRING

INPUT

T_CPPSTRING
$var = ($type)SvPV_nolen($arg)

OUTPUT

T_CPPSTRING
sv_setpv((SV*)$arg, $var.c_str());

Then this script will work:

use Inline CPP => config => typemaps => "typemap";
use Inline CPP => <<'END';

#ifdef __INLINE_CPP_STANDARD_HEADERS
#include <string>
#else
#include <string.h>
#endif

#ifdef __INLINE_CPP_NAMESPACE_STD
using namespace std;
#endif

class Abstract {
public:
virtual string greet2() {
string retval = "yo";
return retval;
}
};

class Impl : public Abstract {
public:
Impl() {}
~Impl() {}
};
END

my $o = Impl->new;
print $o->greet2, "\n";

See F<t/grammar/09purevt.t> for this within the test suite.

=head1 <iostream>, Standard Headers, C++ Namespaces, and Portability Solutions

Inline::CPP automatically includes <iostream>, or <iostream.h>, depending on the
Expand Down
32 changes: 26 additions & 6 deletions t/grammar/09purevt.t
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
use strict;
use warnings;
use Test::More;
use FindBin;

# this is needed to avoid false passes if was done first without 'info'
use Inline CPP => config => force_build => 1, clean_after_build => 0;
use Inline CPP => config => force_build => 1, clean_after_build => 0,
typemaps => "$FindBin::Bin/typemap.09purevt",
;

# Test pure virtual functions (abstract classes).
use Inline CPP => <<'END';
#ifdef __INLINE_CPP_STANDARD_HEADERS
#include <string>
#else
#include <string.h>
#endif
#ifdef __INLINE_CPP_NAMESPACE_STD
using namespace std;
#endif
class Abstract {
public:
virtual char *text() = 0;
virtual int greet(char *name) {
printf("# Hello, %s.\n", name);
return 17;
}
printf("# Hello, %s.\n", name);
return 17;
}
virtual string greet2() {
string retval = "yo";
return retval;
}
};
class Impl : public Abstract {
Expand All @@ -23,8 +40,6 @@ class Impl : public Abstract {
~Impl() {}
virtual char *text() { return "Hello from Impl!"; }
};
END

my $o = new_ok( 'Impl' );
Expand All @@ -38,6 +53,11 @@ is(
"Inherited member function from parent."
);

is(
$o->greet2, "yo",
"Inherited string member function from parent."
);

my $p;
eval{ $p = Abstract->new(); };
if( $@ ) {
Expand Down
12 changes: 12 additions & 0 deletions t/grammar/typemap.09purevt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TYPEMAP
string T_CPPSTRING

INPUT

T_CPPSTRING
$var = ($type)SvPV_nolen($arg)

OUTPUT

T_CPPSTRING
sv_setpv((SV*)$arg, $var.c_str());

0 comments on commit 2860272

Please sign in to comment.