File Coverage

lib/SDL2/Utils.pm
Criterion Covered Total %
statement 94 98 95.9
branch 10 14 71.4
condition n/a
subroutine 19 20 95.0
pod 0 6 0.0
total 123 138 89.1


line stmt bran cond sub pod time code
1             package SDL2::Utils {
2              
3             # FFI utilities
4 2     2   14 use strictures 2;
  2         18  
  2         90  
5 2     2   435 use experimental 'signatures';
  2         4  
  2         16  
6 2     2   236 use base 'Exporter::Tiny';
  2         2  
  2         195  
7             our @EXPORT = qw[attach define deprecate has enum ffi];
8 2     2   1119 use Alien::libsdl2;
  2         105339  
  2         28  
9 2     2   51395 use FFI::CheckLib;
  2         6627  
  2         216  
10 2     2   2566 use FFI::Platypus 1.46;
  2         18350  
  2         107  
11 2     2   1236 use FFI::Platypus::Memory qw[malloc strcpy free];
  2         32031  
  2         224  
12 2     2   1273 use FFI::C;
  2         7460  
  2         86  
13 2     2   1155 use FFI::C::Def;
  2         38131  
  2         102  
14 2     2   1169 use FFI::C::ArrayDef;
  2         4895  
  2         68  
15 2     2   1130 use FFI::C::StructDef;
  2         10084  
  2         79  
16 2     2   1722 use FFI::Platypus::Closure;
  2         1763  
  2         1397  
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 828     828 0 1068 sub ffi () {
  828         920  
23 828         912 CORE::state $ffi;
24 828 100       1829 if ( !defined $ffi ) {
25 2         32 $ffi = FFI::Platypus->new(
26             api => 1,
27             experimental => 2,
28             lib => [ Alien::libsdl2->dynamic_libs ]
29             );
30 2         6618 FFI::C->ffi($ffi);
31             }
32 828         3559 $ffi;
33             }
34              
35 28     28 0 46 sub enum (%args) {
  28         134  
  28         46  
36 28         108 my ($package) = caller();
37 28         54 $package = 'SDL2::FFI';
38 28         92 for my $tag ( keys %args ) {
39 98         472 FFI::C->enum( $tag => $args{$tag}, { package => $package } );
40 98         72064 my $_tag = $tag; # Simple rules:
41 98         386 $_tag =~ s[^SDL_][]; # No SDL_XXXXX
42 98 100       387 $_tag = lcfirst $_tag unless $_tag =~ m[^.[A-Z]]; # Save GLattr
43 98         299 push @{ $SDL2::FFI::EXPORT_TAGS{$_tag} },
44 98 50       129 sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} };
  942 100       2141  
  98         218  
45             }
46             }
47              
48 30     30 0 3466 sub attach (%args) {
  30         87  
  30         41  
49 30         107 my ($package) = caller();
50 30         67 $package = 'SDL2::FFI';
51 30         143 for my $tag ( sort keys %args ) {
52 34         53 for my $func ( sort keys %{ $args{$tag} } ) {
  34         378  
53              
54             #warn sprintf 'ffi->attach( %s => %s);', $func,
55             # Data::Dump::dump( @{ $args{$tag}{$func} } )
56             # if ref $args{$tag}{$func}[1] && ref $args{$tag}{$func}[1] eq 'ARRAY';
57 492         954 ffi->attach( [ $func => $package . '::' . $func ] => @{ $args{$tag}{$func} } );
  492         1573  
58 492         87074 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $func;
  492         1581  
59             }
60             }
61             }
62              
63 224     224 0 440 sub has (%args) { # Should be hash-like
  224         974  
  224         321  
64 224         754 my ($package) = caller;
65 224         412 my $type = $package;
66 224         1103 $type =~ s[^SDL2::][SDL_];
67 224         511 $type =~ s[::][_]g;
68              
69             #$class =~ s[^SDL_(.+)$]['SDL2::' . ucfirst $1]e;
70             #warn sprintf '%-20s => %-20s%s', $name, $class, (
71             # -f sub ($package) { $package =~ m[::(.+)]; './lib/SDL2/' . $1 . '.pod' }
72             # ->($class) ? '' : ' (undocumented)'
73             #);
74 224         544 FFI::C::StructDef->new(
75             ffi,
76             name => $type, # C type
77             class => $package, # package
78             nullable => 1,
79             members => \@_ # Keep order rather than use %args
80             );
81             }
82              
83 26     26 0 46 sub define (%args) {
  26         70  
  26         52  
84 26         82 my ($package) = caller();
85 26         62 $package = 'SDL2::FFI';
86 26         76 for my $tag ( keys %args ) {
87              
88             #print $_->[0] . ' ' for sort { $a->[0] cmp $b->[0] } @{ $Defines{$tag} };
89             #no strict 'refs';
90             ref $_->[1] eq 'CODE' ?
91              
92             #constant->import( $package . '::' .$_->[0] => $_->[1]->() ) : #
93 2     2   18 sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] }
  2     442   4  
  2         464  
  442         643  
  442         6705  
94             ->() :
95             constant->import( $package . '::' . $_->[0] => $_->[1] )
96 26 100       40 for @{ $args{$tag} };
  26         7711  
97 26         73 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} },
98 26 50       61 sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} };
  954         1906  
  26         61  
99             }
100             }
101             };
102             1;