File Coverage

lib/SDL2/Utils.pm
Criterion Covered Total %
statement 91 95 95.7
branch 8 12 66.6
condition n/a
subroutine 19 20 95.0
pod 0 6 0.0
total 118 133 88.7


line stmt bran cond sub pod time code
1             package SDL2::Utils {
2              
3             # FFI utilities
4 2     2   16 use strictures 2;
  2         23  
  2         101  
5 2     2   506 use experimental 'signatures';
  2         4  
  2         16  
6 2     2   256 use base 'Exporter::Tiny';
  2         3  
  2         209  
7             our @EXPORT = qw[attach define deprecate has enum ffi];
8 2     2   1086 use Alien::libsdl2;
  2         104943  
  2         33  
9 2     2   51926 use FFI::CheckLib;
  2         6757  
  2         251  
10 2     2   2866 use FFI::Platypus 1.46;
  2         18966  
  2         178  
11 2     2   1387 use FFI::Platypus::Memory qw[malloc strcpy free];
  2         33922  
  2         265  
12 2     2   1465 use FFI::C;
  2         7781  
  2         93  
13 2     2   1320 use FFI::C::Def;
  2         40174  
  2         98  
14 2     2   1341 use FFI::C::ArrayDef;
  2         5326  
  2         79  
15 2     2   1260 use FFI::C::StructDef;
  2         10183  
  2         94  
16 2     2   1926 use FFI::Platypus::Closure;
  2         1880  
  2         1316  
17              
18 0     0 0 0 sub deprecate ($str) {
  0         0  
  0         0  
19 0 0       0 warnings::warn( 'deprecated', $str ) if warnings::enabled('deprecated');
20             }
21              
22 720     720 0 895 sub ffi () {
  720         733  
23 720         1607 CORE::state $ffi;
24 720 100       1503 if ( !defined $ffi ) {
25 2         36 $ffi = FFI::Platypus->new(
26             api => 1,
27             experimental => 2,
28             lib => [ Alien::libsdl2->dynamic_libs ]
29             );
30 2         6511 FFI::C->ffi($ffi);
31             }
32 720         3134 $ffi;
33             }
34              
35 10     10 0 23 sub enum (%args) {
  10         72  
  10         16  
36 10         36 my ($package) = caller();
37 10         23 $package = 'SDL2::FFI';
38 10         37 for my $tag ( keys %args ) {
39 66         316 FFI::C->enum( $tag => $args{$tag}, { package => $package } );
40 66         360 push @{ $SDL2::FFI::EXPORT_TAGS{ lc substr $tag, 4 } },
41 66 50       44874 sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} };
  536 100       1263  
  66         142  
42             }
43             }
44              
45 30     30 0 1086 sub attach (%args) {
  30         85  
  30         45  
46 30         102 my ($package) = caller();
47 30         63 $package = 'SDL2::FFI';
48 30         130 for my $tag ( sort keys %args ) {
49 34         52 for my $func ( sort keys %{ $args{$tag} } ) {
  34         448  
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 492         979 ffi->attach( [ $func => $package . '::' . $func ] => @{ $args{$tag}{$func} } );
  492         1477  
55 492         86104 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $func;
  492         1485  
56             }
57             }
58             }
59              
60 124     124 0 295 sub has (%args) { # Should be hash-like
  124         845  
  124         200  
61 124         431 my ($package) = caller;
62 124         280 my $type = $package;
63 124         703 $type =~ s[^SDL2::][SDL_];
64 124         293 $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 124         437 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 16     16 0 34 sub define (%args) {
  16         46  
  16         27  
81 16         58 my ($package) = caller();
82 16         40 $package = 'SDL2::FFI';
83 16         61 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     2   19 sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] }
  2     70   4  
  2         483  
  70         111  
  70         1286  
91             ->() :
92             constant->import( $package . '::' . $_->[0] => $_->[1] )
93 16 100       25 for @{ $args{$tag} };
  16         6327  
94 16         61 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} },
95 16 50       45 sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} };
  374         827  
  16         50  
96             }
97             }
98             };
99             1;