Nww Data types

Discussion in 'Perl Misc' started by Guenther Sohler, Apr 27, 2005.

  1. Hallo Group,

    Due to your great Help in the last Days I was able to
    use embedded perl in c for my program. I was also able to
    register my own c functions with my perl interpreter useing newXS.
    Now I am wondering if its even possible to integrate my own
    data type. At the moment I am calculating with integers and floats and
    strings. and it works very well.
    But due to the graphical nature of my prg it would be nice, if I could
    add a new data type eg Point, which contains two floats(x and y)
    or a list of Points, (or maybe a list of strings).
    Is it possible ?
    Maybe I would finally write

    $pt=convert_point($x,$y);

    How can I implement new data tyes ?
     
    Guenther Sohler, Apr 27, 2005
    #1
    1. Advertising

  2. Also sprach Guenther Sohler:
    > Hallo Group,
    >
    > Due to your great Help in the last Days I was able to
    > use embedded perl in c for my program. I was also able to
    > register my own c functions with my perl interpreter useing newXS.
    > Now I am wondering if its even possible to integrate my own
    > data type. At the moment I am calculating with integers and floats and
    > strings. and it works very well.
    > But due to the graphical nature of my prg it would be nice, if I could
    > add a new data type eg Point, which contains two floats(x and y)
    > or a list of Points, (or maybe a list of strings).
    > Is it possible ?
    > Maybe I would finally write
    >
    > $pt=convert_point($x,$y);
    >
    > How can I implement new data tyes ?


    At this point you will probably need a class other than main to bless
    the point object into. I'd suggest writing a class in XS for a point:

    typedef struct {
    float x, y;
    } Point;

    MODULE = Point PACKAGE = Point

    Point*
    new (char *CLASS, float x, float y)
    CODE:
    {
    New(0, RETVAL, Point, 1);
    RETVAL->x = x;
    RETVAL->y = y;
    }
    OUTPUT:
    RETVAL

    float
    x (Point *pt)
    CODE:
    {
    RETVAL = pt->x;
    }
    OUTPUT:
    RETVAL

    # likewise for y and other methods

    Plus, you will need a typemap file:

    Point* POINT

    OUTPUT
    POINT
    sv_setref_pv( $arg, CLASS, (void*)$var );

    INPUT
    POINT
    if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
    $var = ($type)SvIV((SV*)SvRV( $arg ));
    else{
    warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
    XSRETURN_UNDEF;
    }

    As always, do a

    xsubpp -typemap typemap_file Point.xs

    and copy the generated functions into your C program and give it a name
    of your choice with newXS. If you insist on main::convert_point($x, $y)
    being the constructor, change the XSUB for Point::new to:

    Point*
    convert_point(float x, float y)
    PREINIT:
    char *CLASS = "Point";
    CODE:
    /* rest is unchanged */

    The point object itself will still be blessed into the class 'Point',
    though. But the programmer writing scripts for your program will usually
    not notice this because all he will do is things like this:

    my $pt = convert_point(1.0, 0.5);
    print $pt->x;

    Note that instance methods, such as x(), need to live in the Point::
    namespace, so your newXS lines now should read:

    newXS("main::convert_point", XS_Point__new, file);
    newXS("Point::x", XS_Point__x, file);
    ...

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Apr 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    606
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    596
    Henry S. Thompson
    Dec 1, 2005
  3. ramu
    Replies:
    2
    Views:
    339
    rlblaster
    Feb 20, 2006
  4. Gary Roach
    Replies:
    0
    Views:
    118
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    135
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page