FFI::C::ArrayDef(3pm)

πŸ’– FFI::C::ArrayDef πŸ’–

okay bestie, so this is FFI::C::ArrayDef β€” a User Contributed Perl Documentation page, and it's literally the glow-up your C arrays of structs have been waiting for. pull up a chair, we're doing this 🫢


✨ NAME ✨

FFI::C::ArrayDef β€” array data definition for FFI. that's it, that's the name, slay.


🌸 VERSION 🌸

version 0.15 β€” yes, we're on that version, iconic behavior.


πŸŽ€ SYNOPSIS πŸŽ€

first up, your C code, and yes we're keeping it exactly as-is because it's giving ✨

#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
  );
}

and now the Perl side, where the magic actually happens πŸ’…

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]]

like?? we built a rectangle, printed it, moved it up the y axis, printed it again. main character energy fr fr πŸ‘‘


πŸ’œ DESCRIPTION πŸ’œ

so what does this class actually do? it creates a def for a C array of structured data. usually that def is holding a FFI::C::StructDef or a FFI::C::UnionDef, and optionally a number of elements. that's the whole vibe. no cap.


πŸ¦‹ CONSTRUCTOR πŸ¦‹

new

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

for the standard def options, go peek at FFI::C::Def β€” iykyk.

members

this one should be an array reference: the member type first, and optionally the number of elements after it. examples, bestie:

my $struct = FFI::C::StructDef->new(...);

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

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

so $fixed is your locked-in-count situation, and $var is the flexible girl who doesn't commit to a number yet. both valid. both slay. πŸ’…


🌈 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, that's what gets used for the element count β€” and it can possibly override whatever was specified back when the def was created. and listen, important: if the def doesn't have an element count specified, then you MUST provide it here. no skipping, no shortcuts, it's required, periodt.

returns a FFI::C::Array.

and you can optionally initialize member values using @init. cute, right? πŸ’–


⭐ 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

go read them, bestie, they're all part of the same little friend group 🫢


πŸ‘‘ AUTHOR πŸ‘‘

Graham Ollis <plicease@cpan.org> β€” the legend himself, we love to see it 🌟


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.

so yeah, free software, do your thing, just respect the terms okay? πŸ’œ


and that's the whole page, bestie πŸŽ€ this was FFI::C::ArrayDef, perl v5.36.0, dated 2023-02-06, and it came to you straight from the User Contributed Perl Documentation corner of the universe. mwah, go write some arrays πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from