blib/lib/SDL2/Utils/Type/Enum.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 146 CC 22 SCAR 59.2
CC
22
Cov
0%
ffi_custom_type_api_1
506.0
BEGIN
2.0
BEGIN
2.0
CRAP 371.6
1 package SDL2::Utils::Type::Enum { # Cribbed from FFI::Platypus::Type::Enum
2 use strict;
3 use warnings;
4 use constant 1.32 ();
5 use 5.008001;
6 use Ref::Util qw( is_plain_arrayref is_plain_hashref is_ref );
7 use Scalar::Util qw( dualvar );
8 use Carp qw( croak );
9  
10 # ABSTRACT: Custom platypus type for dealing with C enumerated types
11 our $VERSION = '0.06'; # VERSION
12 our @CARP_NOT = qw( FFI::Platypus );
13  
14 sub ffi_custom_type_api_1 {
15 my %config = defined $_[2] && is_plain_hashref $_[2] ? %{ splice( @_, 2, 1 ) } : ();
16 my ( undef, undef, @values ) = @_;
17 my $index = 0;
18 my %str_lookup;
19 my %int_lookup;
20 my $prefix = defined $config{prefix} ? $config{prefix} : '';
21 $config{rev} ||= 'str';
22 ( $config{rev} =~ /^(int|str|dualvar)$/ ) or
23 croak("rev must be either 'int', 'str', or 'dualvar'");
24 $config{casing} ||= 'upper';
25 ( $config{casing} =~ /^(upper|keep)$/ ) or croak("casing must be either 'upper' or 'keep'");
26  
27 foreach my $value (@values) {
28 my $name;
29 my @aliases;
30 if ( is_plain_arrayref $value) {
31 my %opt;
32 if ( @$value % 2 ) {
33 ( $name, %opt ) = @$value;
34 }
35 else {
36 ( $name, $index, %opt ) = @$value;
37 }
38 @aliases = @{ delete $opt{alias} || [] };
39 croak("unrecognized options: @{[ sort keys %opt ]}") if %opt;
40 }
41 elsif ( !is_ref $value) {
42 $name = $value;
43 }
44 else {
45 croak("not a array ref or scalar: $value");
46 }
47 if ( $index < 0 ) {
48 $config{type} ||= 'senum';
49 }
50 if ( my $packages = $config{package} ) {
51 foreach my $package ( is_plain_arrayref $packages ? @$packages : $packages ) {
52 foreach my $name ( $name, @aliases ) {
53 my $full = join '::', $package,
54 $prefix . ( $config{casing} eq 'upper' ? uc($name) : $name );
55 no strict 'refs';
56 ref $index eq 'CODE' ? *{$full} = $index :
57 constant->import( $full, $index );
58 }
59 }
60 }
61 croak("$name declared twice") if exists $str_lookup{$name};
62 $int_lookup{$index} = $name unless exists $int_lookup{$index};
63 $str_lookup{$_} = $index for @aliases;
64 $str_lookup{$name} = $index++;
65 }
66 $config{type} ||= 'enum';
67 if ( defined $config{maps} ) {
68 if ( is_plain_arrayref $config{maps} ) {
69 @{ $config{maps} } = ( \%str_lookup, \%int_lookup, $config{type} );
70 }
71 else {
72 croak("maps is not an array reference");
73 }
74 }
75 my %type = (
76 native_type => $config{type},
77 perl_to_native => sub {
78 exists $str_lookup{ $_[0] } ? $str_lookup{ $_[0] } :
79 exists $int_lookup{ $_[0] } ? $_[0] :
80 croak("illegal enum value $_[0]");
81 }
82 );
83 if ( $config{rev} eq 'str' ) {
84 $type{native_to_perl} = sub {
85 exists $int_lookup{ $_[0] } ? $int_lookup{ $_[0] } : $_[0];
86 }
87 }
88 elsif ( $config{rev} eq 'dualvar' ) {
89 $type{native_to_perl} = sub {
90 exists $int_lookup{ $_[0] } ? dualvar( $_[0], $int_lookup{ $_[0] } ) : $_[0];
91 };
92 }
93 \%type;
94 }
95 1;
96 }