1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
package FFI::C::UnionDef;
use strict;
use warnings;
use 5.008001;
use FFI::C::Union;
use FFI::Platypus 1.24;
use constant _is_union => 1;
use base qw( FFI::C::StructDef );
# ABSTRACT: Union data definition for FFI
our $VERSION = '0.15'; # VERSION
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
FFI::C::UnionDef - Union data definition for FFI
=head1 VERSION
version 0.15
=head1 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,
=head1 DESCRIPTION
This class creates a def for a C C<union>.
=head1 CONSTRUCTOR
=head2 new
my $def = FFI::C::UnionDef->new(%opts);
my $def = FFI::C::UnionDef->new($ffi, %opts);
For standard def options, see L<FFI::C::Def>.
=over 4
=item members
This should be an array reference containing name, type pairs.
For a union, the order doesn't matter.
=back
=head1 METHODS
=head2 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 C<union>, returns a L<FFI::C::Union>.
You can optionally initialize member values using C<%init>.
=head1 SEE ALSO
=over 4
=item L<FFI::C>
=item L<FFI::C::Array>
=item L<FFI::C::ArrayDef>
=item L<FFI::C::Def>
=item L<FFI::C::File>
=item L<FFI::C::PosixFile>
=item L<FFI::C::Struct>
=item L<FFI::C::StructDef>
=item L<FFI::C::Union>
=item L<FFI::C::UnionDef>
=item L<FFI::C::Util>
=item L<FFI::Platypus::Record>
=back
=head1 AUTHOR
Graham Ollis <plicease@cpan.org>
=head1 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.
=cut
|