NAME

FFI::C::Array - Array instance for FFI

VERSION

version 0.15

SYNOPSIS

use FFI::C::ArrayDef;
use FFI::C::StructDef;

my $point_def = FFI::C::StructDef->new(
  name  => 'point_t',
  class => 'Point',
  members => [
    x => 'double',
    y => 'double',
  ],
);

my $rect_def = FFI::C::ArrayDef->new(
  name    => 'rectangle_t',
  class   => 'Rectangle',
  members => [
    $point_def, 2,
  ]
);

# create a rectangle using the def's create method
my $square = $rect_def->create([
  { x => 1.0, y => 1.0 },
  { x => 2.0, y => 2.0 },
]);

printf "[[%d %d][%d %d]]\n",
  $square->[0]->x, $square->[0]->y,
  $square->[1]->x, $square->[1]->y;   # [[1 1][2 2]]

# move square by 1 on the x axis
$square->[$_]->x( $square->[$_]->x + 1 ) for 0..1;

printf "[[%d %d][%d %d]]\n",
  $square->[0]->x, $square->[0]->y,
  $square->[1]->x, $square->[1]->y;   # [[2 1][3 2]]

# Create a rectange usingn the generated class
my $rect = Rectangle->new;
$rect->[0]->x(1.0);
$rect->[0]->y(1.0);
$rect->[1]->x(2.0);
$rect->[1]->y(3.0);

DESCRIPTION

This class represents an instance of a C an array. This class can be created using new on the generated class, if that was specified for the FFI::C::ArrayDef, or by using the create method on FFI::C::ArrayDef.

Each element of the array can be accessed using the get method below, or by using the object as an array reference, thanks to magical Perl ties.

CONSTRUCTOR

new

FFI::C::ArrayDef->new( class => 'User::Array::Class', ... );
my $instance = User::Array::Class->new;
my $instance = User::Array::Class->new($count);

Creates a new instance of the array. If $count is specified, that will be used as the element count, overriding the count defined by the def. If the def did not specify a count then you MUST provide a count.

METHODS

get

my $element = $instance->get($index);
my $element = $instance->[$index];

Gets the element at the given $index.

count

my $count = $instance->count;

Returns the number of elements in the array, if known.

tie

my $arrayref = $instance->tie;

Returns a Perl array reference tied to the C array.

SEE ALSO

FFI::C
FFI::C::Array
FFI::C::ArrayDef
FFI::C::Def
FFI::C::File
FFI::C::PosixFile
FFI::C::Struct
FFI::C::StructDef
FFI::C::Union
FFI::C::UnionDef
FFI::C::Util
FFI::Platypus::Record

AUTHOR

Graham Ollis <plicease@cpan.org>

COPYRIGHT AND LICENSE

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.