| 1 | package FFI::Platypus::Record; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use 5.008004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use FFI::Platypus; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use constant 1.32 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 9 | our @EXPORT = qw( record_layout record_layout_1 ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # ABSTRACT: FFI support for structured records data | ||||||||||||||||||||||||||||
| 14 | 9 | our $VERSION = '2.12'; # VERSION | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub record_layout_1 | ||||||||||||||||||||||||||||
| 18 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
record_layout_1: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | if(@_ % 2 == 0) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | 3 | my $ffi = FFI::Platypus->new( api => 2); | |||||||||||||||||||||||||||
| 22 | 3 | unshift @_, $ffi; | |||||||||||||||||||||||||||
| 23 | 3 | goto &record_layout; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | ▶ | elsif(defined $_[0] && ref($_[0]) eq 'ARRAY') | |||||||||||||||||||||||||||
|
Condition66%2/3
defined $_[0] and ref $_[0] eq "ARRAY"
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "ARRAY"
defined $_[0]
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | 1 | my @args = @{ shift @_ }; | |||||||||||||||||||||||||||
| 28 | 1 | unshift @args, api => 2; | |||||||||||||||||||||||||||
| 29 | 1 | unshift @_, \@args; | |||||||||||||||||||||||||||
| 30 | 1 | goto &record_layout; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 1 | ▶ | elsif(defined $_[0] && eval { $_[0]->isa('FFI::Platypus') }) | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $_[0] and eval {
do {
$_[0]->isa("FFI::Platypus")
}
}
MC/DC0%0/2
defined $_[0] and eval {
do {
$_[0]->isa("FFI::Platypus")
}
}
defined $_[0]
eval {
do {
$_[0]->isa("FFI::Platypus")
}
}
Truth table33%1/3
A and B
A= defined $_[0]B= eval {
do {
$_[0]->isa("FFI::Platypus")
}
}
| |||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||
| 34 | 1 | goto &record_layout; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else | ||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 0 | croak "odd number of arguments, but first argument is not either an array reference or Platypus instance"; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub record_layout | ||||||||||||||||||||||||||||
| 44 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
record_layout: called CC=24 SCAR=32.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 24 | my $ffi; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 24 | ▶ | if(defined $_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | 24 | ▶ | if(ref($_[0]) eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 2 | my @args = @{ shift() }; | |||||||||||||||||||||||||||
| 52 | 2 | $ffi = FFI::Platypus->new(@args); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 22 | elsif(eval { $_[0]->isa('FFI::Platypus') }) | |||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 7 | $ffi = shift; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 24 | ▶ | $ffi ||= FFI::Platypus->new; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ffi ||= "FFI::Platypus"->new
MC/DC0%0/2
$ffi ||= "FFI::Platypus"->new
$ffi
"FFI::Platypus"->new
Truth table66%2/3
A ||= B
A= $ffiB= "FFI::Platypus"->new
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 24 | my $offset = 0; | |||||||||||||||||||||||||||
| 63 | 24 | my $record_align = 0; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 24 | ▶ | croak "uneven number of arguments!" if scalar(@_) % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 24 | my($caller, $filename, $line) = caller; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 24 | ▶ | if($caller->can("_ffi_record_size") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$caller->can("_ffi_record_size") or $caller->can("ffi_record_size")
MC/DC0%0/2
$caller->can("_ffi_record_size") or $caller->can("ffi_record_size")
$caller->can("_ffi_record_size")
$caller->can("ffi_record_size")
Truth table33%1/3
A or B
A= $caller->can("_ffi_record_size")B= $caller->can("ffi_record_size")
| |||||||||||||||||||||||||||||
| 70 | || $caller->can("ffi_record_size")) | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 0 | croak "record already defined for the class $caller"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 24 | my @destroy; | |||||||||||||||||||||||||||
| 76 | my @ffi_types; | ||||||||||||||||||||||||||||
| 77 | 24 | my $has_string; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 24 | ▶ | while(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 92 | my $spec = shift; | |||||||||||||||||||||||||||
| 82 | 92 | my $name = shift; | |||||||||||||||||||||||||||
| 83 | 92 | my $type = $ffi->{tp}->parse( $spec, { member => 1 } ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 92 | ▶ | croak "illegal name $name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name =~ /^[A-Za-z_][A-Za-z_0-9]*$/ or $name eq ":"
MC/DC0%0/2
$name =~ /^[A-Za-z_][A-Za-z_0-9]*$/ or $name eq ":"
$name =~ /^[A-Za-z_][A-Za-z_0-9]*$/
$name eq ":"
Truth table66%2/3
A or B
A= $name =~ /^[A-Za-z_][A-Za-z_0-9]*$/B= $name eq ":"
| |||||||||||||||||||||||||||||
| 86 | unless $name =~ /^[A-Za-z_][A-Za-z_0-9]*$/ | ||||||||||||||||||||||||||||
| 87 | || $name eq ':'; | ||||||||||||||||||||||||||||
| 88 | 92 | ▶ | croak "accessor/method $name already exists" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | if $caller->can($name); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 91 | my $size = $type->sizeof; | |||||||||||||||||||||||||||
| 92 | 91 | my $align = $type->alignof; | |||||||||||||||||||||||||||
| 93 | 91 | ▶ | $record_align = $align if $align > $record_align; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 91 | my $meta = $type->meta; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 91 | ▶ | $offset++ while $offset % $align; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 91 | my $count; | |||||||||||||||||||||||||||
| 100 | my $ffi_type; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 91 | ▶ | if($meta->{type} eq 'record') # this means fixed string atm | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||
| 104 | 9 | $ffi_type = 'sint8'; | |||||||||||||||||||||||||||
| 105 | 9 | $count = $size; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | else | ||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | 82 | $ffi_type = $meta->{ffi_type}; | |||||||||||||||||||||||||||
| 110 | 82 | $count = $meta->{element_count}; | |||||||||||||||||||||||||||
| 111 | 82 | ▶ | $count = 1 unless defined $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 82 | ▶ | $has_string = 1 if $meta->{type} eq 'string'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 91 | push @ffi_types, $ffi_type for 1..$count; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 91 | ▶ | if($name ne ':') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 66 | ▶ | if($meta->{type} eq 'string' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$meta->{"type"} eq "string" and $meta->{"access"} eq "rw"
MC/DC100%2/2
$meta->{"type"} eq "string" and $meta->{"access"} eq "rw"
$meta->{"type"} eq "string"
$meta->{"access"} eq "rw"
Truth table100%3/3
A and B
A= $meta->{"type"} eq "string"B= $meta->{"access"} eq "rw"
| |||||||||||||||||||||||||||||
| 122 | && $meta->{access} eq 'rw') | ||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | 8 | push @destroy, eval '# line '. __LINE__ . ' "' . __FILE__ . qq("\n) .qq{ | |||||||||||||||||||||||||||
| 125 | sub { | ||||||||||||||||||||||||||||
| 126 | shift->$name(undef); | ||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||
| 129 | 8 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 66 | my $full_name = join '::', $caller, $name; | |||||||||||||||||||||||||||
| 133 | 66 | my $error_str = _accessor | |||||||||||||||||||||||||||
| 134 | $full_name, | ||||||||||||||||||||||||||||
| 135 | "$filename:$line", | ||||||||||||||||||||||||||||
| 136 | $type, | ||||||||||||||||||||||||||||
| 137 | $offset; | ||||||||||||||||||||||||||||
| 138 | 66 | ▶ | croak("$error_str ($spec $name)") if $error_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 91 | $offset += $size; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 23 | my $size = $offset; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | 23 | constant->import("${caller}::_ffi_record_size", $size); | |||||||||||||||||||||||||||
| 148 | 23 | constant->import("${caller}::_ffi_record_align", $record_align); | |||||||||||||||||||||||||||
| 149 | 23 | *{join '::', $caller, '_ffi_record_ro'} = \&_ffi_record_ro; | |||||||||||||||||||||||||||
| 150 | 23 | ▶ | *{join '::', $caller, 'new'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 151 | 21 | my $class = shift; | |||||||||||||||||||||||||||
| 152 | 21 | ▶ | my $args = ref($_[0]) ? [%{$_[0]}] : \@_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 21 | ▶ | croak "uneven number of arguments to record constructor" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | if @$args % 2; | ||||||||||||||||||||||||||||
| 155 | 21 | my $record = "\0" x $class->_ffi_record_size; | |||||||||||||||||||||||||||
| 156 | 21 | my $self = bless \$record, $class; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 21 | ▶ | while(@$args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 23 | my $key = shift @$args; | |||||||||||||||||||||||||||
| 161 | 23 | my $value = shift @$args; | |||||||||||||||||||||||||||
| 162 | 23 | $self->$key($value); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 21 | $self; | |||||||||||||||||||||||||||
| 166 | 23 | }; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||
| 169 | 23 | require FFI::Platypus::Record::Meta; | |||||||||||||||||||||||||||
| 170 | 23 | my $ffi_meta = FFI::Platypus::Record::Meta->new( | |||||||||||||||||||||||||||
| 171 | \@ffi_types, | ||||||||||||||||||||||||||||
| 172 | !$has_string, | ||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||
| 174 | 23 | ▶ | *{join '::', $caller, '_ffi_meta'} = sub { $ffi_meta }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 23 | ▶ | my $destroy_sub = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 23 | ▶ | if(@destroy) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | { | ||||||||||||||||||||||||||||
| 181 | 5 | ▶ | $destroy_sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 182 | 5 | ▶ | return if _ffi_record_ro($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 5 | $_->($_[0]) for @destroy; | |||||||||||||||||||||||||||
| 184 | 4 | }; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 23 | do { | |||||||||||||||||||||||||||
| 187 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | 23 | *{"${caller}::DESTROY"} = $destroy_sub; | |||||||||||||||||||||||||||
| 189 | }; | ||||||||||||||||||||||||||||
| 190 | 23 | (); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 9 | 1; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||