FFI::C::UnionDef(3pm)

FFI::C::UnionDef3pm

Union data definition for ffi

Usage

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,

Version

version 0.15

Overview

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.

Releases

Unchanged in 2 releases since Debian 12 (Bookworm).

  1. 12–13

Author

Graham Ollis <[email protected]>

From libffi-c-perl 0.15-2 in Debian 13 (Trixie). Parsed by carta from the page's man source.