🌻 📖 FFI::C::StructDef

NAME

FFI::C::StructDef - Structured data definition for FFI

VERSION

version 0.15

SYNOPSIS

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]

DESCRIPTION

This class creates a def for a C struct.

CONSTRUCTOR

new

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

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

members

This should be an array reference containing name, type pairs, in the order that they will be stored in the struct.

trim_string

If true, fixed-length strings should be treated as null terminated strings and be trimmed.

METHODS

create

 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.

trim_string

 my $bool = $def->trim_string;

Returns true if fixed-length strings should be treated as null terminated strings and be trimmed.

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.