FFI::C::UnionDef(3pm)

πŸ’– FFI::C::UnionDef πŸ’–

okay so bestie, welcome to the FFI::C::UnionDef page β€” it's a 3pm page from the User Contributed Perl Documentation, and it's literally about making C union defs happen in Perl. no cap.


✨ VERSION ✨

version 0.15 β€” yes queen, that's the one.


🌈 SYNOPSIS 🌈

so first, in your C code, you'd be doing something like this:

#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);
}

and then over in your Perl code, the whole vibe is:

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,

and if you're like "what's a bundle" β€” go read FFI::Platypus::Bundle, it explains the whole thing 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so this class? it creates a def for a C union. that's the whole gig. it's giving "small but mighty" ✨


πŸ’… CONSTRUCTOR πŸ’…

new

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

for the standard def options, go peek at FFI::C::Def β€” that's where the rest of the lore lives πŸ’œ

members

this one should be an array reference stuffed with name, type pairs. and here's the cute part: for a union, the order doesn't matter. no drama, no ranking, everyone's equal πŸ’–


πŸŽ€ 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 makes an instance of the union and hands you back an FFI::C::Union. icon behavior, honestly πŸ‘‘

and you can optionally initialize member values with %init if you're feeling fancy πŸ’…


πŸ‘€ SEE ALSO πŸ‘€

the whole friend group, go say hi:

  • 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> β€” we stan 🫢


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.


and that's the whole page bestie!! this one's perl v5.36.0, dated 2023-02-06, and it's the FFI::C::UnionDef(3pm) page β€” go forth and union, ily, bye πŸ’–βœ¨πŸ§š

πŸ“¦ where this page comes from