Libtensorflow/Output.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 99 CC 4 SCAR 13.4
CC
4
Cov
0%
_op_stringify
12.0
_as_array
6.0
New
2.0
CRAP 3.8
1 package AI::TensorFlow::Libtensorflow::Output;
2 # ABSTRACT: Output of operation as (operation, index) pair
3 $AI::TensorFlow::Libtensorflow::Output::VERSION = '0.0.7';
4 # See L<AI::TensorFlow::Libtensorflow::Input> for similar.
5 # In fact, they are mostly the same, but keeping the classes separate for now
6 # in case the upstream API changes.
7  
8 use strict;
9 use warnings;
10 use namespace::autoclean;
11 use FFI::Platypus::Record;
12 use AI::TensorFlow::Libtensorflow::Lib::FFIType::Variant::RecordArrayRef;
13  
14 use AI::TensorFlow::Libtensorflow::Lib;
15 my $ffi = AI::TensorFlow::Libtensorflow::Lib->ffi;
16 $ffi->mangler(AI::TensorFlow::Libtensorflow::Lib->mangler_default);
17  
18 record_layout_1($ffi,
19 'opaque' => '_oper', # 8 (on 64-bit)
20 'int' => '_index', # 4
21  
22 # padding to make sizeof(record) == 16
23 # but only on machines where sizeof(opaque) is 8 bytes
24 # See also:
25 # Convert::Binary::C->new( Alignment => 8 )
26 # ->parse( ... )
27 # ->sizeof( ... )
28 $ffi->sizeof('opaque') == 8 ? (
29 'char[4]' => ':',
30 ) : (),
31 );
32 $ffi->type('record(AI::TensorFlow::Libtensorflow::Output)', 'TF_Output');
33  
34 sub New {
35 my ($class, $args) = @_;
36  
37 my $record = $class->new({
38 _oper => $ffi->cast( 'TF_Operation', 'opaque', delete $args->{oper} ),
39 _index => delete $args->{index},
40 });
41 }
42  
43 sub oper { $ffi->cast('opaque', 'TF_Operation', $_[0]->_oper ) }
44 sub index { $_[0]->_index }
45  
46 use FFI::C::ArrayDef;
47 use FFI::C::StructDef;
48 my $sdef = FFI::C::StructDef->new($ffi,
49 name => 'TF_Output_struct',
50 members => [
51 _oper => 'opaque',
52 _index => 'int',
53 __ignore => 'char[4]',
54 ],
55 );
56 my $adef = FFI::C::ArrayDef->new($ffi,
57 name => 'TF_Output_struct_array',
58 members => [ 'TF_Output_struct' ]
59 );
60 sub _adef { $adef; }
61 sub _as_array {
62 my $class = shift;
63 my $output = $class->_adef->create(0 + @_);
64 for my $idx (0..@_-1) {
65 next unless defined $_[$idx];
66 $class->_copy_to_other( $_[$idx], $output->[$idx] );
67 }
68 $output;
69 }
70 sub _from_array {
71 my ($class, $array) = @_;
72 [
73 map {
74 my $record = $class->new;
75 $class->_copy_to_other($array->[$_], $record);
76 $record;
77 } 0..$array->count-1
78 ]
79 }
80 sub _copy_to_other {
81 my ($class, $this, $that) = @_;
82 $that->_oper ($this->_oper);
83 $that->_index($this->_index);
84 }
85  
86 $ffi->load_custom_type(
87 RecordArrayRef( 'OutputArrayPtr',
88 record_module => __PACKAGE__, with_size => 0,
89 ),
90 => 'TF_Output_array');
91 $ffi->load_custom_type(
92 RecordArrayRef( 'OutputArrayPtrSz',
93 record_module => __PACKAGE__, with_size => 1,
94 ),
95 => 'TF_Output_array_sz');
96  
97 use overload
98 '""' => \&_op_stringify;
99  
100 sub _op_stringify {
101 join ":", (
102 ( defined $_[0]->_oper ? $_[0]->oper->Name : '<undefined operation>' ),
103 ( defined $_[0]->index ? $_[0]->index : '<no index>' )
104 );
105 }
106  
107 sub _data_printer {
108 my ($self, $ddp) = @_;
109  
110 my %data = (
111 oper => $self->oper,
112 index => $self->index,
113 );
114  
115 return sprintf('%s %s',
116 $ddp->maybe_colorize(ref $self, 'class' ),
117 $ddp->parse(\%data) );
118 };
119  
120 1;
121  
122 __END__