File Coverage

lib/SDL2/Utils.pm
Criterion Covered Total %
statement 86 90 95.5
branch 8 12 66.6
condition n/a
subroutine 19 20 95.0
pod 0 6 0.0
total 113 128 88.2


line stmt bran cond sub pod time code
1             package SDL2::Utils {
2              
3             # FFI utilities
4 2     2   10 use strictures 2;
  2         10  
  2         57  
5 2     2   1246 use experimental 'signatures';
  2         6112  
  2         9  
6 2     2   293 use base 'Exporter::Tiny';
  2         4  
  2         961  
7             our @EXPORT = qw[attach define deprecate has enum ffi];
8 2     2   7054 use FFI::CheckLib;
  2         4961  
  2         140  
9 2     2   2139 use FFI::Platypus 1.46;
  2         15989  
  2         57  
10 2     2   892 use FFI::C;
  2         5630  
  2         55  
11 2     2   792 use Alien::libsdl2;
  2         79129  
  2         26  
12              
13             #use FFI::C::StructDef;
14 2     2   40214 use FFI::Platypus::Memory qw[malloc strcpy free];
  2         26381  
  2         153  
15 2     2   908 use FFI::C::Def;
  2         29870  
  2         67  
16 2     2   887 use FFI::C::StructDef;
  2         8114  
  2         61  
17 2     2   836 use FFI::C::ArrayDef;
  2         3921  
  2         51  
18 2     2   1299 use FFI::Platypus::Closure;
  2         1398  
  2         995  
19              
20 0     0 0 0 sub deprecate ($str) {
  0         0  
  0         0  
21 0 0       0 warnings::warn( 'deprecated', $str ) if warnings::enabled('deprecated');
22             }
23              
24 712     712 0 641 sub ffi () {
  712         533  
25 712         585 CORE::state $ffi;
26 712 100       1074 if ( !defined $ffi ) {
27 2         27 $ffi = FFI::Platypus->new(
28             api => 1,
29             experimental => 2,
30             lib => [ Alien::libsdl2->dynamic_libs ]
31             );
32 2         5226 FFI::C->ffi($ffi);
33             }
34 712         1955 $ffi;
35             }
36              
37 14     14 0 27 sub enum (%args) {
  14         47  
  14         13  
38 14         31 my ($package) = caller();
39 14         39 for my $tag ( keys %args ) {
40 46         207 FFI::C->enum( $tag => $args{$tag}, { package => 'SDL2::FFI' } );
41             $SDL2::FFI::EXPORT_TAGS{ lc substr $tag, 4 }
42 346 50       768 = [ sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ }
    100          
43 46         24828 @{ $args{$tag} } ];
  46         78  
44             }
45             }
46              
47 26     26 0 987 sub attach (%args) {
  26         55  
  26         34  
48 26         69 my ($package) = caller();
49 26         84 for my $tag ( sort keys %args ) {
50 32         41 for my $func ( sort keys %{ $args{$tag} } ) {
  32         287  
51              
52             #warn sprintf 'ffi->attach( %s => %s);', $func,
53             # Data::Dump::dump( @{ $args{$tag}{$func} } )
54             # if ref $args{$tag}{$func}[1] && ref $args{$tag}{$func}[1] eq 'ARRAY';
55 488         697 ffi->attach( [ $func => $package . '::' . $func ] => @{ $args{$tag}{$func} } );
  488         1164  
56 488         67504 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $func;
  488         1212  
57             }
58             }
59             }
60              
61 120     120 0 167 sub has (%args) { # Should be hash-like
  120         557  
  120         109  
62 120         275 my ($package) = caller;
63 120         159 my $type = $package;
64 120         445 $type =~ s[^SDL2::][SDL_];
65 120         192 $type =~ s[::][_]g;
66              
67             #$class =~ s[^SDL_(.+)$]['SDL2::' . ucfirst $1]e;
68             #warn sprintf '%-20s => %-20s%s', $name, $class, (
69             # -f sub ($package) { $package =~ m[::(.+)]; './lib/SDL2/' . $1 . '.pod' }
70             # ->($class) ? '' : ' (undocumented)'
71             #);
72 120         220 FFI::C::StructDef->new(
73             ffi,
74             name => $type, # C type
75             class => $package, # package
76             nullable => 1,
77             members => \@_ # Keep order rather than use %args
78             );
79             }
80              
81 14     14 0 25 sub define (%args) {
  14         35  
  14         17  
82 14         47 my ($package) = caller();
83 14         41 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   15 sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] }
  2     138   4  
  2         371  
  138         181  
  138         2041  
91             ->() :
92             constant->import( $package . '::' . $_->[0] => $_->[1] )
93 16 100       23 for @{ $args{$tag} };
  16         5086  
94              
95             #constant->import( $_ => $_ ) for @{ $Defines{$tag} };
96             $SDL2::FFI::EXPORT_TAGS{ lc substr $tag, 4 }
97 16 50       35 = [ sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} } ];
  474         808  
  16         30  
98             }
99             }
100             };
101             1;