llancet has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

I'm going to write some kind of drawing library using Cairo, and I want to create a Perl wrapper for my lib. It would have some method functions like this:

class MyPage { MyPage(cairo_t* context); }

In which, functions of my code have arguments of Cairo's type. I know there's already a Cairo wrapper for perl in CPAN, so I want to have my wrapped code can work like this:

use Cairo; use MyLib; ...... my $context = Cairo::Context->create(...); my $page = MyLib::MyPage->new($context);

However, to achieve this, the XS code of mine must have the knowledge of how Cairo's XS code do the wrap. As I'm not experienced on XS, I need some suggests on it. Thanks for a lot!!

In addition, I noticed that SWIG is another choice for wrapper creation. However Cairo is wrapped using XS, so maybe I have to use XS too?

Replies are listed 'Best First'.
Re: How to let my XS module interact with existing XS module?
by llancet (Friar) on Aug 16, 2013 at 02:07 UTC
    In addition, I failed to find how Perl Cairo module wraps cairo_t. In its typemap, there's no entry for it:
    # content of "cairo-perl.typemap" const char * T_PV char_utf8 * T_PV_UTF8 const char_utf8 * T_PV_UTF8 cairo_bool_t T_UV cairo_font_extents_t * T_CAIRO_FONT_EXTENTS cairo_text_extents_t * T_CAIRO_TEXT_EXTENTS cairo_glyph_t * T_CAIRO_GLYPH cairo_text_cluster_t * T_CAIRO_TEXT_CLUSTER cairo_path_t * T_CAIRO_PATH FT_Face T_FT_FACE
      Cairo-1.061\blib\arch\Cairo\Install\cairo-perl-auto.typemap 7:cairo_t * T_CAIROPERL_GENERIC_WRAPPER 35:const cairo_t * T_CAIROPERL_GENERIC_WRAPPER
Re: How to let my XS module interact with existing XS module?
by llancet (Friar) on Aug 16, 2013 at 07:18 UTC

    I did some study on the Perl's ecology of XS system. It seems an typemap can be shared only if its author made it using ExtUtils::Typemap as a module. I also found that Cairo's main typemap is auto-generated at pre-compilation stage of the module.

    So it seems I cannot use the existing Cairo wrapper to cooperate with the wrapper of my lib. I would consider a re-design of my API to allow it working without using Cairo's types as function parameter, like this:

    typedef enum { SURFACE_PDF, SURFACE_SVG, SURFACE_IMAGE, SURFACE_PS } SurfaceType; class MyPage { MyPage(SurfaceType type); };

      It seems an typemap can be shared only if its author made it using ExtUtils::Typemap as a module

      Never heard of that. Its a file, if its installed, and you can find it, you can include it

      https://metacpan.org/module/ExtUtils::MakeMaker#TYPEMAPS

      Pango-1.221\blib\arch\Pango\Install\Files.pm Pango-1.221\blib\arch\Pango\Install\pango-perl-autogen.h Pango-1.221\blib\arch\Pango\Install\pango-perl-versions.h Pango-1.221\blib\arch\Pango\Install\pango-perl.h Pango-1.221\blib\arch\Pango\Install\pango-perl.typemap Pango-1.221\blib\arch\Pango\Install\pango.typemap

      Gtk2::Install::Files

      Seems Gtk2::Install::Files is generated by ExtUtils::Depends, so just use ExtUtils::Depends in Makefile.PL and ... less work for you :)