blib/lib/SDL2/Utils.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 119 CC 7 SCAR 16.2
CC
7
Cov
0%
enum
12.0
define
12.0
deprecate
6.0
CRAP 5.0
1 package SDL2::Utils {
2  
3 # FFI utilities
4 use strictures 2;
5 use experimental 'signatures';
6 use base 'Exporter::Tiny';
7 our @EXPORT = qw[attach define deprecate has enum ffi];
8 use Alien::libsdl2;
9 use FFI::CheckLib;
10 use FFI::Platypus 1.46;
11 use FFI::Platypus::Memory qw[malloc strcpy free];
12 use FFI::C;
13 use FFI::C::Def;
14 use FFI::C::ArrayDef;
15 use FFI::C::StructDef;
16 use FFI::Platypus::Closure;
17  
18 sub deprecate ($str) {
19 warnings::warn( 'deprecated', $str ) if warnings::enabled('deprecated');
20 }
21  
22 sub ffi () {
23 CORE::state $ffi;
24 if ( !defined $ffi ) {
25 $ffi = FFI::Platypus->new(
26 api => 1,
27 experimental => 2,
28 lib => [ Alien::libsdl2->dynamic_libs ]
29 );
30 FFI::C->ffi($ffi);
31 }
32 $ffi;
33 }
34  
35 sub enum (%args) {
36 my ($package) = caller();
37 $package = 'SDL2::FFI';
38 for my $tag ( keys %args ) {
39 FFI::C->enum( $tag => $args{$tag}, { package => $package } );
40 push @{ $SDL2::FFI::EXPORT_TAGS{ lc substr $tag, 4 } },
41 sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} };
42 }
43 }
44  
45 sub attach (%args) {
46 my ($package) = caller();
47 $package = 'SDL2::FFI';
48 for my $tag ( sort keys %args ) {
49 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 ffi->attach( [ $func => $package . '::' . $func ] => @{ $args{$tag}{$func} } );
55 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $func;
56 }
57 }
58 }
59  
60 sub has (%args) { # Should be hash-like
61 my ($package) = caller;
62 my $type = $package;
63 $type =~ s[^SDL2::][SDL_];
64 $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 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) {
81 my ($package) = caller();
82 $package = 'SDL2::FFI';
83 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 sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] }
91 ->() :
92 constant->import( $package . '::' . $_->[0] => $_->[1] )
93 for @{ $args{$tag} };
94 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} },
95 sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} };
96 }
97 }
98 };
99 1;