FFI::C::ArrayDef - Array data definition for FFI
version 0.15
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]]
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.
my $def = FFI::C::ArrayDef->new(%opts); my $def = FFI::C::ArrayDef->new($ffi, %opts);
For standard def options, see FFI::C::Def.
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 ], );
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
.
Graham Ollis <plicease@cpan.org>
This software is copyright (c) 2020-2022 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.