🌻 📖 FFI::C::UnionDef

NAME

FFI::C::UnionDef - Union data definition for FFI

VERSION

version 0.15

SYNOPSIS

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,

DESCRIPTION

This class creates a def for a C union.

CONSTRUCTOR

new

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

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

members

This should be an array reference containing name, type pairs. For a union, the order doesn't matter.

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 union, returns a FFI::C::Union.

You can optionally initialize member values using %init.

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.