FFI::C::StructDef - Structured data definition for FFI
version 0.15
In your C code:
#include <stdint.h>
#include <stdio.h>
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} color_t;
void
print_color(color_t *c)
{
printf("[%02x %02x %02x]\n",
c->red,
c->green,
c->blue
);
}
In your Perl code:
use FFI::Platypus 1.00;
use FFI::C::StructDef;
my $ffi = FFI::Platypus->new( api => 1 );
# See FFI::Platypus::Bundle for how bundle works.
$ffi->bundle;
my $def = FFI::C::StructDef->new(
$ffi,
name => 'color_t',
class => 'Color',
members => [
red => 'uint8',
green => 'uint8',
blue => 'uint8',
],
);
my $red = Color->new({ red => 255 });
my $green = Color->new({ green => 255 });
$ffi->attach( print_color => ['color_t'] );
print_color($red); # [ff 00 00]
print_color($green); # [00 ff 00]
# that red is a tad bright!
$red->red( 200 );
print_color($red); # [c8 00 00]
This class creates a def for a C struct.
my $def = FFI::C::StructDef->new(%opts); my $def = FFI::C::StructDef->new($ffi, %opts);
For standard def options, see FFI::C::Def.
This should be an array reference containing name, type pairs, in the order that they will be stored in the struct.
If true, fixed-length strings should be treated as null terminated strings and be trimmed.
my $instance = $def->create; my $instance = $def->class->new; # 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 struct, returns a FFI::C::Struct.
You can optionally initialize member values using %init.
my $bool = $def->trim_string;
Returns true if fixed-length strings should be treated as null terminated strings and be trimmed.
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.