| 1 | 2 | package SDL2::Utils { | |||||||||||||
| 2 | |||||||||||||||
| 3 | # FFI utilities | ||||||||||||||
| 4 | 2 | ▶ | use strictures 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use experimental 'signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use base 'Exporter::Tiny'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | our @EXPORT = qw[attach define deprecate has enum ffi]; | |||||||||||||
| 8 | 2 | ▶ | use Alien::libsdl2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use FFI::CheckLib; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use FFI::Platypus 1.46; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use FFI::Platypus::Memory qw[malloc strcpy free]; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use FFI::C; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 2 | ▶ | use FFI::C::Def; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 2 | ▶ | use FFI::C::ArrayDef; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 2 | ▶ | use FFI::C::StructDef; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 2 | ▶ | use FFI::Platypus::Closure; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | ▶ | sub deprecate ($str) { | |||||||||||||
|
Subroutine
deprecate: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 19 | 0 | ▶ | warnings::warn( 'deprecated', $str ) if warnings::enabled('deprecated'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | } | ||||||||||||||
| 21 | |||||||||||||||
| 22 | ▶ | sub ffi () { | |||||||||||||
|
Subroutine
ffi: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||||||
| 23 | 2 | CORE::state $ffi; | |||||||||||||
| 24 | 2 | ▶ | if ( !defined $ffi ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 2 | $ffi = FFI::Platypus->new( | |||||||||||||
| 26 | api => 1, | ||||||||||||||
| 27 | experimental => 2, | ||||||||||||||
| 28 | lib => [ Alien::libsdl2->dynamic_libs ] | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | 0 | FFI::C->ffi($ffi); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | 0 | $ffi; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | ▶ | sub enum (%args) { | |||||||||||||
|
Subroutine
enum: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 36 | 0 | my ($package) = caller(); | |||||||||||||
| 37 | 0 | $package = 'SDL2::FFI'; | |||||||||||||
| 38 | 0 | for my $tag ( keys %args ) { | |||||||||||||
| 39 | 0 | FFI::C->enum( $tag => $args{$tag}, { package => $package } ); | |||||||||||||
| 40 | 0 | push @{ $SDL2::FFI::EXPORT_TAGS{ lc substr $tag, 4 } }, | |||||||||||||
| 41 | 0 | ▶ | sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} }; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | ▶ | sub attach (%args) { | |||||||||||||
|
Subroutine
attach: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 46 | 0 | my ($package) = caller(); | |||||||||||||
| 47 | 0 | $package = 'SDL2::FFI'; | |||||||||||||
| 48 | 0 | for my $tag ( sort keys %args ) { | |||||||||||||
| 49 | 0 | for my $func ( sort keys %{ $args{$tag} } ) { | |||||||||||||
| 50 | |||||||||||||||
| 51 | #warn sprintf 'ffi->attach( %s => %s);', $func, | ||||||||||||||
| 52 | # Data::Dump::dump( @{ $args{$tag}{$func} } ) | ||||||||||||||
| 53 | # if ref $args{$tag}{$func}[1] && ref $args{$tag}{$func}[1] eq 'ARRAY'; | ||||||||||||||
| 54 | 0 | ffi->attach( [ $func => $package . '::' . $func ] => @{ $args{$tag}{$func} } ); | |||||||||||||
| 55 | 0 | push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $func; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | ▶ | sub has (%args) { # Should be hash-like | |||||||||||||
|
Subroutine
has: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 61 | 2 | my ($package) = caller; | |||||||||||||
| 62 | 2 | my $type = $package; | |||||||||||||
| 63 | 2 | $type =~ s[^SDL2::][SDL_]; | |||||||||||||
| 64 | 2 | $type =~ s[::][_]g; | |||||||||||||
| 65 | |||||||||||||||
| 66 | #$class =~ s[^SDL_(.+)$]['SDL2::' . ucfirst $1]e; | ||||||||||||||
| 67 | #warn sprintf '%-20s => %-20s%s', $name, $class, ( | ||||||||||||||
| 68 | # -f sub ($package) { $package =~ m[::(.+)]; './lib/SDL2/' . $1 . '.pod' } | ||||||||||||||
| 69 | # ->($class) ? '' : ' (undocumented)' | ||||||||||||||
| 70 | #); | ||||||||||||||
| 71 | 2 | FFI::C::StructDef->new( | |||||||||||||
| 72 | ffi, | ||||||||||||||
| 73 | name => $type, # C type | ||||||||||||||
| 74 | class => $package, # package | ||||||||||||||
| 75 | nullable => 1, | ||||||||||||||
| 76 | members => \@_ # Keep order rather than use %args | ||||||||||||||
| 77 | ); | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | ▶ | sub define (%args) { | |||||||||||||
|
Subroutine
define: not called CC=5 SCAR=29.7
Pod
undocumented
| |||||||||||||||
| 81 | 0 | my ($package) = caller(); | |||||||||||||
| 82 | 0 | $package = 'SDL2::FFI'; | |||||||||||||
| 83 | 0 | for my $tag ( keys %args ) { | |||||||||||||
| 84 | |||||||||||||||
| 85 | #print $_->[0] . ' ' for sort { $a->[0] cmp $b->[0] } @{ $Defines{$tag} }; | ||||||||||||||
| 86 | #no strict 'refs'; | ||||||||||||||
| 87 | ref $_->[1] eq 'CODE' ? | ||||||||||||||
| 88 | |||||||||||||||
| 89 | #constant->import( $package . '::' .$_->[0] => $_->[1]->() ) : # | ||||||||||||||
| 90 | 2 | ▶ | sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||||||
| 91 | ->() : | ||||||||||||||
| 92 | constant->import( $package . '::' . $_->[0] => $_->[1] ) | ||||||||||||||
| 93 | 0 | ▶ | for @{ $args{$tag} }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 0 | push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, | |||||||||||||
| 95 | 0 | ▶ | sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | }; | ||||||||||||||
| 99 | 2 | 1; | |||||||||||||