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