FFI::C::UnionDef - Union data definition for FFI
version 0.15
In your C code:
#include <stdint.h> #include <stdio.h> typedef union { uint8_t u8; uint16_t u16; uint32_t u32; } anyint_t; void print_anyint_as_u32(anyint_t *any) { printf("0x%x\n", any->u32); }
In your Perl code:
use FFI::Platypus 1.00; use FFI::C::UnionDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $def = FFI::C::UnionDef->new( $ffi, name => 'anyint_t', class => 'AnyInt', members => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', ], ); $ffi->attach( print_anyint_as_u32 => ['anyint_t'] ); my $int = AnyInt->new({ u8 => 42 }); print_anyint_as_u32($int); # 0x2a on Intel,
This class creates a def for a C union
.
my $def = FFI::C::UnionDef->new(%opts); my $def = FFI::C::UnionDef->new($ffi, %opts);
For standard def options, see FFI::C::Def.
This should be an array reference containing name, type pairs. For a union, the order doesn't matter.
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 union
, returns a FFI::C::Union.
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.