FFI::C::ArrayDef(3pm)

FFI::C::ArrayDef3pm

Array data definition for ffi

Usage

In your C code:

 #include <stdio.h>
 
 typedef struct {
   double x, y;
 } point_t;
 
 void
 print_rectangle(point_t rec[2])
 {
   printf("[[%g %g] [%g %g]]\n",
     rec[0].x, rec[0].y,
     rec[1].x, rec[1].y
   );
 }

In your Perl code:

 use FFI::Platypus 1.00;
 use FFI::C::ArrayDef;
 use FFI::C::StructDef;
 
 my $ffi = FFI::Platypus->new( api => 1 );
 # See FFI::Platypus::Bundle for how bundle works.
 $ffi->bundle;
 
 my $point_def = FFI::C::StructDef->new(
   $ffi,
   name  => 'point_t',
   class => 'Point',
   members => [
     x => 'double',
     y => 'double',
   ],
 );
 
 my $rect_def = FFI::C::ArrayDef->new(
   $ffi,
   name    => 'rectangle_t',
   class   => 'Rectangle',
   members => [
     $point_def, 2,
   ]
 );
 
 $ffi->attach( print_rectangle => ['rectangle_t'] );
 
 my $rect = Rectangle->new([
   { x => 1.5,  y => 2.0  },
   { x => 3.14, y => 11.0 },
 ]);
 
 print_rectangle($rect);  # [[1.5 2] [3.14 11]]
 
 # move rectangle on the y axis
 $rect->[$_]->y( $rect->[$_]->y + 1.0 ) for 0..1;
 
 print_rectangle($rect);  # [[1.5 3] [3.14 12]]

Version

version 0.15

Overview

This class creates a def for a C array of structured data. Usually the def contains a FFI::C::StructDef or FFI::C::UnionDef and optionally a number of elements.

Constructor

new

 my $def = FFI::C::ArrayDef->new(%opts);
 my $def = FFI::C::ArrayDef->new($ffi, %opts);

For standard def options, see FFI::C::Def.

members

This should be an array reference the member type, and optionally the number of elements. Examples:

 my $struct = FFI::C::StructDef->new(...);
 
 my $fixed = FFI::C::ArrayDef->new(
   members => [ $struct, 10 ],
 );
 
 my $var = FFI::C::ArrayDef->new(
   members => [ $struct ],
 );

Methods

create

 my $instance = $def->create;
 my $instance = $def->class->new;          # if class was specified
 my $instance = $def->create($count);
 my $instance = $def->class->new($count);  # if class was specified
 my $instance = $def->create(\@init);
 my $instance = $def->class->new(\@init);  # if class was specified

This creates an instance of the array. If $count is given, this is used for the element count, possibly overriding what was specified when the def was created. If the def doesn't have an element count specified, then you MUST provide it here. Returns a FFI::C::Array.

You can optionally initialize member values using @init.

Releases

Unchanged in 2 releases since Debian 12 (Bookworm).

  1. 12–13

Author

Graham Ollis <[email protected]>

From libffi-c-perl 0.15-2 in Debian 13 (Trixie). Parsed by carta from the page's man source.