Tangence/Type.pm

filter: Statementstmt 96.7% 449 / 464 Branchbran 84.1% 138 / 164 Conditioncond 66.6% 32 / 48 MC/DCmcdc 38.7% 12 / 31 Subroutinesub 93.2% 69 / 74 Podpod 100% 1 / 1 totaltotal 89.6% 701 / 782 CC 103 SCAR 17.7
CC
103
Cov
92%
pack_value
15.6
unpack_value
13.9
_best_int_type_for
8.7
CRAP 5.9
« Tangence/ObjectProxy.pm ↑ Summary Tangence/Meta/Type.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2024 -- leonerd@leonerd.org.uk
5  
6 14 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Tangence::Type 0.33;
11 14 ▶class Tangence::Type :isa(Tangence::Meta::Type);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 =head1 NAME
14  
15 C<Tangence::Type> - represent a C<Tangence> value type
16  
17 =head1 DESCRIPTION
18  
19 Objects in this class represent individual types that are sent over the wire
20 in L<Tangence> messages. This is a subclass of L<Tangence::Meta::Type> which
21 provides additional methods that may be useful in server or client
22 implementations.
23  
24 =cut
25  
26 =head1 CONSTRUCTOR
27  
28 =head2 make
29  
30 $type = Tangence::Type->make( $primitive_sig );
31  
32 Returns an instance to represent a primitive type of the given signature.
33  
34 $type = Tangence::Type->make( list => $member_type );
35  
36 $type = Tangence::Type->make( dict => $member_type );
37  
38 Returns an instance to represent a list or dict aggregation containing members
39 of the given type.
40  
41 =cut
42  
43 sub make
44 1016 ▶{
Subroutine make: called CC=5 SCAR=16.3
Pod documented
45 # Subtle trickery is at work here
46 # Invoke our own superclass constructor, but pretend to be some higher
47 # subclass that's appropriate
48  
49 1016 shift;
50 1016 ▶ if( @_ == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 797 219 1016
elsif ($_[0] eq 'list') { } 140 79 219
elsif ($_[0] eq 'dict') { } 79 0 79
51 797 my ( $type ) = @_;
52 797 my $class = "Tangence::Type::Primitive::$type";
53 797 ▶ $class->can( "make" ) or die "TODO: Need $class";
Branch: 1 branch
BranchTrueFalseTotal
$class->can("make") or die "TODO: Need $class" 797 0 797
54  
55 797 return $class->SUPER::make( $type );
56 }
57 elsif( $_[0] eq "list" ) {
58 140 shift;
59 140 return Tangence::Type::List->SUPER::make( list => @_ );
60 }
61 elsif( $_[0] eq "dict" ) {
62 79 shift;
63 79 return Tangence::Type::Dict->SUPER::make( dict => @_ );
64 }
65 else {
66 0 die "TODO: Not sure how to make a Tangence::Type->make( @_ )";
67 }
68 }
69  
70 =head1 METHODS
71  
72 =head2 default_value
73  
74 $value = $type->default_value;
75  
76 Returns a value suitable to use as an initial value for object properties.
77  
78 =head2 pack_value
79  
80 $type->pack_value( $message, $value );
81  
82 Appends a value of this type to the end of a L<Tangence::Message>.
83  
84 =head2 unpack_value
85  
86 $value = $type->unpack_value( $message );
87  
88 Removes a value of this type from the start of a L<Tangence::Message>.
89  
90 =cut
91  
92 class Tangence::Type::List :isa(Tangence::Type)
93 14 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
94 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 13 ▶ method default_value { [] }
Subroutine default_value: called CC=1 SCAR=0.0
98  
99 129 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=3 SCAR=11.0
100 {
101 129 ▶ ref $value eq "ARRAY" or croak "Cannot pack a list from non-ARRAY reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "ARRAY" or croak("Cannot pack a list from non-ARRAY reference") 128 1 129
102  
103 128 $message->_pack_leader( DATA_LIST, scalar @$value );
104  
105 128 my $member_type = $self->member_type;
106 128 $member_type->pack_value( $message, $_ ) for @$value;
107 14 }
108  
109 129 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=3 SCAR=11.0
110 {
111 129 my ( $type, $num ) = $message->_unpack_leader();
112 129 ▶ $type == DATA_LIST or croak "Expected to unpack a list but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 or croak("Expected to unpack a list but did not find one") 128 1 129
113  
114 128 my $member_type = $self->member_type;
115 128 my @values;
116 128 foreach ( 1 .. $num ) {
117 171 push @values, $member_type->unpack_value( $message );
118 }
119  
120 126 return \@values;
121 14 }
122 }
123  
124 14 class Tangence::Type::Dict :isa(Tangence::Type)
125 {
126 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 0 ▶ method default_value { {} }
Subroutine default_value: not called CC=1 SCAR=1.2
130  
131 71 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=4 SCAR=13.9
132 {
133 71 ▶ ref $value eq "HASH" or croak "Cannot pack a dict from non-HASH reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "HASH" or croak("Cannot pack a dict from non-HASH reference") 70 1 71
134  
135 70 my @keys = keys %$value;
136 70 ▶ @keys = sort @keys if $Tangence::Message::SORT_HASH_KEYS;
Branch: 1 branch
BranchTrueFalseTotal
if $Tangence::Message::SORT_HASH_KEYS 19 51 70
137  
138 70 $message->_pack_leader( DATA_DICT, scalar @keys );
139  
140 70 my $member_type = $self->member_type;
141 70 $message->pack_str( $_ ), $member_type->pack_value( $message, $value->{$_} ) for @keys;
142 14 }
143  
144 71 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=3 SCAR=11.0
145 {
146 71 my ( $type, $num ) = $message->_unpack_leader();
147 71 ▶ $type == DATA_DICT or croak "Expected to unpack a dict but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 or croak("Expected to unpack a dict but did not find one") 70 1 71
148  
149 70 my $member_type = $self->member_type;
150 70 my %values;
151 70 foreach ( 1 .. $num ) {
152 153 my $key = $message->unpack_str();
153 152 $values{$key} = $member_type->unpack_value( $message );
154 }
155  
156 69 return \%values;
157 14 }
158 }
159  
160 14 class Tangence::Type::Primitive::bool :isa(Tangence::Type)
161 {
162 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 0 ▶ method default_value { "" }
Subroutine default_value: not called CC=1 SCAR=1.2
166  
167 102 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=2 SCAR=6.9
168 {
169 102 ▶ $message->_pack_leader( DATA_NUMBER, $value ? DATANUM_BOOLTRUE : DATANUM_BOOLFALSE );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 29 73 102
170 14 }
171  
172 103 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=4 SCAR=14.0
173 {
174 103 my ( $type, $num ) = $message->_unpack_leader();
175  
176 103 ▶ $type == DATA_NUMBER or croak "Expected to unpack a number(bool) but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 or croak("Expected to unpack a number(bool) but did not find one") 101 2 103
177 101 ▶ $num == DATANUM_BOOLFALSE and return !!0;
Branch: 1 branch
BranchTrueFalseTotal
$num == 0 and 72 29 101
178 29 ▶ $num == DATANUM_BOOLTRUE and return !!1;
Branch: 1 branch
BranchTrueFalseTotal
$num == 1 and 29 0 29
179 0 croak "Expected to find a DATANUM_BOOL subtype but got $num";
180 14 }
181 }
182  
183 14 class Tangence::Type::Primitive::_integral :isa(Tangence::Type)
184 {
185 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 14 ▶ use constant SUBTYPE => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 14 ▶ method default_value { 0 }
Subroutine default_value: called CC=1 SCAR=0.0
191  
192 14 my %format = (
193 DATANUM_UINT8, [ "C", 1 ],
194 DATANUM_SINT8, [ "c", 1 ],
195 DATANUM_UINT16, [ "S>", 2 ],
196 DATANUM_SINT16, [ "s>", 2 ],
197 DATANUM_UINT32, [ "L>", 4 ],
198 DATANUM_SINT32, [ "l>", 4 ],
199 DATANUM_UINT64, [ "Q>", 8 ],
200 DATANUM_SINT64, [ "q>", 8 ],
201 );
202  
203 667 ▶ sub _best_int_type_for ( $n )
Subroutine _best_int_type_for: called CC=8 SCAR=21.6
204 {
205 667 ▶ if( $n < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 5 662 667
206 5 ▶ return DATANUM_SINT8 if $n >= -0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= -128 3 2 5
207 2 ▶ return DATANUM_SINT16 if $n >= -0x8000;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= -32768 0 2 2
208 2 ▶ return DATANUM_SINT32 if $n >= -0x80000000;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= '-2147483648' 2 0 2
209 0 return DATANUM_SINT64;
210 }
211  
212 662 ▶ return DATANUM_UINT8 if $n <= 0xff;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 255 645 17 662
213 17 ▶ return DATANUM_UINT16 if $n <= 0xffff;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 65535 14 3 17
214 3 ▶ return DATANUM_UINT32 if $n <= 0xffffffff;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 4294967295 3 0 3
215 0 return DATANUM_UINT64;
216 }
217  
218 723 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=7 SCAR=19.5
219 {
220 723 ▶ defined $value or croak "cannot pack_int(undef)";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or croak("cannot pack_int(undef)") 722 1 723
221 722 ▶ ref $value and croak "$value is not a number";
Branch: 1 branch
BranchTrueFalseTotal
ref $value and croak("$value is not a number") 2 720 722
222 720 ▶ $value == $value or croak "cannot pack_int(NaN)";
Branch: 1 branch
BranchTrueFalseTotal
$value == $value or croak("cannot pack_int(NaN)") 718 2 720
223 718 ▶ $value == "+Inf" || $value == "-Inf" and croak "cannot pack_int(Inf)";
Branch: 1 branch
BranchTrueFalseTotal
$value == "+Inf" || $value == "-Inf" and croak("cannot pack_int(Inf)") 2 716 718
Condition66%2/3
$value == "+Inf" or $value == "-Inf"
l!l&&r!l&&!r
20716
MC/DC50%1/2
$value == "+Inf" or $value == "-Inf"
$value == "+Inf" $value == "-Inf"
Truth table66%2/3
A or B
A= $value == "+Inf"B= $value == "-Inf"
ABresult
1X1
011
000
224  
225 716 ▶ my $subtype = $self->SUBTYPE || _best_int_type_for( $value );
Condition66%2/3
$self->SUBTYPE || _best_int_type_for($value)
l!l&&r!l&&!r
496670
MC/DC0%0/2
$self->SUBTYPE || _best_int_type_for($value)
$self->SUBTYPE _best_int_type_for($value)
Truth table66%2/3
A || B
A= $self->SUBTYPEB= _best_int_type_for($value)
ABresult
1X1
011
000
226 716 $message->_pack_leader( DATA_NUMBER, $subtype );
227  
228 716 $message->_pack( pack( $format{$subtype}[0], $value ) );
229 14 }
230  
231 724 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=5 SCAR=16.2
232 {
233 724 my ( $type, $num ) = $message->_unpack_leader();
234  
235 724 ▶ $type == DATA_NUMBER or croak "Expected to unpack a number but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 or croak("Expected to unpack a number but did not find one") 719 5 724
236 719 ▶ exists $format{$num} or croak "Expected an integer subtype but got $num";
Branch: 1 branch
BranchTrueFalseTotal
exists $format{$num} or croak("Expected an integer subtype but got $num") 719 0 719
237  
238 719 ▶ if( my $subtype = $self->SUBTYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subtype = $self->SUBTYPE) 51 668 719
239 51 ▶ $subtype == $num or croak "Expected integer subtype $subtype, got $num";
Branch: 1 branch
BranchTrueFalseTotal
$subtype == $num or croak("Expected integer subtype $subtype, got $num") 51 0 51
240 }
241  
242 719 my ( $n ) = unpack( $format{$num}[0], $message->_unpack( $format{$num}[1] ) );
243  
244 719 return $n;
245 14 }
246 }
247  
248 14 class Tangence::Type::Primitive::u8 :isa(Tangence::Type::Primitive::_integral)
249 {
250 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_UINT8;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 }
252  
253 14 class Tangence::Type::Primitive::s8 :isa(Tangence::Type::Primitive::_integral)
254 {
255 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_SINT8;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 }
257  
258 14 class Tangence::Type::Primitive::u16 :isa(Tangence::Type::Primitive::_integral)
259 {
260 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_UINT16;
Subroutine BEGIN: called CC=1 SCAR=0.0
261 }
262  
263 14 class Tangence::Type::Primitive::s16 :isa(Tangence::Type::Primitive::_integral)
264 {
265 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_SINT16;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 }
267  
268 14 class Tangence::Type::Primitive::u32 :isa(Tangence::Type::Primitive::_integral)
269 {
270 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_UINT32;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 }
272  
273 14 class Tangence::Type::Primitive::s32 :isa(Tangence::Type::Primitive::_integral)
274 {
275 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_SINT32;
Subroutine BEGIN: called CC=1 SCAR=0.0
276 }
277  
278 14 class Tangence::Type::Primitive::u64 :isa(Tangence::Type::Primitive::_integral)
279 {
280 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_UINT64;
Subroutine BEGIN: called CC=1 SCAR=0.0
281 }
282  
283 14 class Tangence::Type::Primitive::s64 :isa(Tangence::Type::Primitive::_integral)
284 {
285 14 ▶ use constant SUBTYPE => Tangence::Constants::DATANUM_SINT64;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 }
287  
288 14 class Tangence::Type::Primitive::int :isa(Tangence::Type::Primitive::_integral)
289 {
290 # empty
291 }
292  
293 14 class Tangence::Type::Primitive::float :isa(Tangence::Type)
294 {
295 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
296 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
297  
298 14 my $TYPE_FLOAT16 = Tangence::Type->make( 'float16' );
299  
300 14 ▶ use constant SUBTYPE => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
301  
302 0 ▶ method default_value { 0.0 }
Subroutine default_value: not called CC=1 SCAR=1.2
303  
304 14 my %format = (
305 # pack, bytes, NaN
306 DATANUM_FLOAT32, [ "f>", 4, "\x7f\xc0\x00\x00" ],
307 DATANUM_FLOAT64, [ "d>", 8, "\x7f\xf8\x00\x00\x00\x00\x00\x00" ],
308 );
309  
310 7 ▶ sub _best_type_for ( $value )
Subroutine _best_type_for: called CC=7 SCAR=19.5
311 {
312 # Unpack as 64bit float and see if it's within limits
313 7 my $float64BIN = pack "d>", $value;
314  
315 # float64 == 1 / 11 / 52
316 7 my $exp64 = ( unpack "L>", $float64BIN & "\x7f\xf0\x00\x00" ) >> (52-32);
317  
318 # Zero is smallest
319 7 ▶ return DATANUM_FLOAT16 if $exp64 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exp64 == 0 0 7 7
320  
321 # De-bias
322 7 $exp64 -= 1023;
323  
324 # Special values might as well be float16
325 7 ▶ return DATANUM_FLOAT16 if $exp64 == 1024;
Branch: 1 branch
BranchTrueFalseTotal
if $exp64 == 1024 2 5 7
326  
327 # Smaller types are OK if the exponent will fit and there's no loss of
328 # mantissa precision
329  
330 5 ▶ return DATANUM_FLOAT16 if abs($exp64) < 15 &&
Branch: 1 branch
BranchTrueFalseTotal
if abs $exp64 < 15 and ($float64BIN & "\0\0\3\377\377\377\377\377") eq "\0\0\0\0\0\0\0\0" 2 3 5
Condition100%3/3
abs $exp64 < 15 and ($float64BIN & "\0\0\3\377\377\377\377\377") eq "\0\0\0\0\0\0\0\0"
!ll&&!rl&&r
212
MC/DC100%2/2
abs $exp64 < 15 and ($float64BIN & "\0\0\3\377\377\377\377\377") eq "\0\0\0\0\0\0\0\0"
abs $exp64 < 15 ($float64BIN & "\0\0\3\377\377\377\377\377") eq "\0\0\0\0\0\0\0\0"
Truth table100%3/3
A and B
A= abs $exp64 < 15B= ($float64BIN & "\0\0\3\377\377\377\377\377") eq "\0\0\0\0\0\0\0\0"
ABresult
0X0
100
111
331 ($float64BIN & "\x00\x00\x03\xff\xff\xff\xff\xff") eq "\x00"x8;
332  
333 3 ▶ return DATANUM_FLOAT32 if abs($exp64) < 127 &&
Branch: 1 branch
BranchTrueFalseTotal
if abs $exp64 < 127 and ($float64BIN & "\0\0\0\0\37\377\377\377") eq "\0\0\0\0\0\0\0\0" 1 2 3
Condition66%2/3
abs $exp64 < 127 and ($float64BIN & "\0\0\0\0\37\377\377\377") eq "\0\0\0\0\0\0\0\0"
!ll&&!rl&&r
021
MC/DC50%1/2
abs $exp64 < 127 and ($float64BIN & "\0\0\0\0\37\377\377\377") eq "\0\0\0\0\0\0\0\0"
abs $exp64 < 127 ($float64BIN & "\0\0\0\0\37\377\377\377") eq "\0\0\0\0\0\0\0\0"
Truth table66%2/3
A and B
A= abs $exp64 < 127B= ($float64BIN & "\0\0\0\0\37\377\377\377") eq "\0\0\0\0\0\0\0\0"
ABresult
0X0
100
111
334 ($float64BIN & "\x00\x00\x00\x00\x1f\xff\xff\xff") eq "\x00"x8;
335  
336 2 return DATANUM_FLOAT64;
337 }
338  
339 15 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=6 SCAR=18.1
340 {
341 15 ▶ defined $value or croak "cannot pack undef as float";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or croak("cannot pack undef as float") 15 0 15
342 15 ▶ ref $value and croak "$value is not a number";
Branch: 1 branch
BranchTrueFalseTotal
ref $value and croak("$value is not a number") 0 15 15
343  
344 15 ▶ my $subtype = $self->SUBTYPE || _best_type_for( $value );
Condition66%2/3
$self->SUBTYPE || _best_type_for($value)
l!l&&r!l&&!r
870
MC/DC0%0/2
$self->SUBTYPE || _best_type_for($value)
$self->SUBTYPE _best_type_for($value)
Truth table66%2/3
A || B
A= $self->SUBTYPEB= _best_type_for($value)
ABresult
1X1
011
000
345  
346 15 ▶ return $TYPE_FLOAT16->pack_value( $message, $value ) if $subtype == DATANUM_FLOAT16;
Branch: 1 branch
BranchTrueFalseTotal
if $subtype == 16 4 11 15
347  
348 11 $message->_pack_leader( DATA_NUMBER, $subtype );
349 $message->_pack( $value == $value ?
350 11 ▶ pack( $format{$subtype}[0], $value ) : $format{$subtype}[2]
Branch: 1 branch
BranchTrueFalseTotal
$value == $value ? : 9 2 11
351 );
352 14 }
353  
354 15 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=7 SCAR=19.7
355 {
356 15 my ( $type, $num ) = $message->_unpack_leader( "peek" );
357  
358 15 ▶ $type == DATA_NUMBER or croak "Expected to unpack a number but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 or croak("Expected to unpack a number but did not find one") 15 0 15
359 15 ▶ exists $format{$num} or $num == DATANUM_FLOAT16 or
Branch: 1 branch
BranchTrueFalseTotal
exists $format{$num} or $num == 16 or croak("Expected a float subtype but got $num") 15 0 15
Condition66%2/3
exists $format{$num} or $num == 16
l!l&&r!l&&!r
1140
MC/DC0%0/2
exists $format{$num} or $num == 16
exists $format{$num} $num == 16
Truth table66%2/3
A or B
A= exists $format{$num}B= $num == 16
ABresult
1X1
011
000
360 croak "Expected a float subtype but got $num";
361  
362 15 ▶ if( my $subtype = $self->SUBTYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subtype = $self->SUBTYPE) 8 7 15
363 8 ▶ $subtype == $num or croak "Expected float subtype $subtype, got $num";
Branch: 1 branch
BranchTrueFalseTotal
$subtype == $num or croak("Expected float subtype $subtype, got $num") 8 0 8
364 }
365  
366 15 ▶ return $TYPE_FLOAT16->unpack_value( $message ) if $num == DATANUM_FLOAT16;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 16 4 11 15
367  
368 11 $message->_unpack_leader; # no-peek
369  
370 11 my ( $n ) = unpack( $format{$num}[0], $message->_unpack( $format{$num}[1] ) );
371  
372 11 return $n;
373 14 }
374 }
375  
376 14 class Tangence::Type::Primitive::float16 :isa(Tangence::Type::Primitive::float)
377 {
378 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
379 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
380  
381 14 ▶ use constant SUBTYPE => DATANUM_FLOAT16;
Subroutine BEGIN: called CC=1 SCAR=0.0
382  
383 # TODO: This code doesn't correctly cope with Inf, -Inf or NaN
384  
385 10 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=8 SCAR=20.8
386 {
387 10 ▶ defined $value or croak "cannot pack undef as float";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or croak("cannot pack undef as float") 10 0 10
388 10 ▶ ref $value and croak "$value is not a number";
Branch: 1 branch
BranchTrueFalseTotal
ref $value and croak("$value is not a number") 0 10 10
389  
390 10 my $float32 = unpack( "N", pack "f>", $value );
391  
392 # float32 == 1 / 8 / 23
393 10 my $sign = ( $float32 & 0x80000000 ) >> 31;
394 10 my $exp = ( ( $float32 & 0x7f800000 ) >> 23 ) - 127;
395 10 my $mant32 = ( $float32 & 0x007fffff );
396  
397 # float16 == 1 / 5 / 10
398 10 my $mant16;
399  
400 10 ▶ if( $exp == 128 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($exp == 128) { } 4 6 10
elsif ($exp > 15) { } 1 5 6
elsif ($exp > -15) { } 3 2 5
401 # special value - Inf or NaN
402 4 $exp = 16;
403 4 ▶ $mant16 = $mant32 ? (1 << 9) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mant32 ? : 2 2 4
404 4 ▶ $sign = 0 if $mant16;
Branch: 1 branch
BranchTrueFalseTotal
if $mant16 2 2 4
405 }
406 elsif( $exp > 15 ) {
407 # Too large - become Inf
408 1 $exp = 16;
409 1 $mant16 = 0;
410 }
411 elsif( $exp > -15 ) {
412 3 $mant16 = $mant32 >> 13;
413 }
414 else {
415 # zero or subnormal - become zero
416 2 $exp = -15;
417 2 $mant16 = 0;
418 }
419  
420 10 my $float16 = $sign << 15 |
421 ( $exp + 15 ) << 10 |
422 $mant16;
423  
424 10 $message->_pack_leader( DATA_NUMBER, DATANUM_FLOAT16 );
425 10 $message->_pack( pack "n", $float16 );
426 14 }
427  
428 10 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=6 SCAR=17.9
429 {
430 10 my ( $type, $num ) = $message->_unpack_leader;
431  
432 10 ▶ $type == DATA_NUMBER or croak "Expected to unpack a number but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 or croak("Expected to unpack a number but did not find one") 10 0 10
433 10 ▶ $num == DATANUM_FLOAT16 or croak "Expected to unpack a float16 but found $num";
Branch: 1 branch
BranchTrueFalseTotal
$num == 16 or croak("Expected to unpack a float16 but found $num") 10 0 10
434  
435 10 my $float16 = unpack "n", $message->_unpack( 2 );
436  
437 # float16 == 1 / 5 / 10
438 10 my $sign = ( $float16 & 0x8000 ) >> 15;
439 10 my $exp = ( ( $float16 & 0x7c00 ) >> 10 ) - 15;
440 10 my $mant16 = ( $float16 & 0x03ff );
441  
442 # float32 == 1 / 8 / 23
443 10 my $mant32;
444  
445 10 ▶ if( $exp == 16 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp == 16) { } 5 5 10
elsif ($exp > -15) { } 3 2 5
446 # special value - Inf or NaN
447 5 $exp = 128;
448 5 ▶ $mant32 = $mant16 ? (1 << 22) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mant16 ? : 2 3 5
449 }
450 elsif( $exp > -15 ) {
451 3 $mant32 = $mant16 << 13;
452 }
453 else {
454 # zero
455 2 $exp = -127;
456 2 $mant32 = 0;
457 }
458  
459 10 my $float32 = $sign << 31 |
460 ( $exp + 127 ) << 23 |
461 $mant32;
462  
463 10 return unpack( "f>", pack "N", $float32 );
464 14 }
465 }
466  
467 14 class Tangence::Type::Primitive::float32 :isa(Tangence::Type::Primitive::float)
468 {
469 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
470  
471 14 ▶ use constant SUBTYPE => DATANUM_FLOAT32;
Subroutine BEGIN: called CC=1 SCAR=0.0
472 }
473  
474 14 class Tangence::Type::Primitive::float64 :isa(Tangence::Type::Primitive::float)
475 {
476 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
477  
478 14 ▶ use constant SUBTYPE => DATANUM_FLOAT64;
Subroutine BEGIN: called CC=1 SCAR=0.0
479 }
480  
481 14 class Tangence::Type::Primitive::str :isa(Tangence::Type)
482 {
483 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
484 14 ▶ use Encode qw( encode_utf8 decode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
485 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
486  
487 1 ▶ method default_value { "" }
Subroutine default_value: called CC=1 SCAR=0.0
488  
489 532 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=3 SCAR=11.0
490 {
491 532 ▶ defined $value or croak "cannot pack_str(undef)";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or croak("cannot pack_str(undef)") 531 1 532
492 531 ▶ ref $value and croak "$value is not a string";
Branch: 1 branch
BranchTrueFalseTotal
ref $value and croak("$value is not a string") 4 527 531
493 527 my $octets = encode_utf8( $value );
494 527 $message->_pack_leader( DATA_STRING, length($octets) );
495 527 $message->_pack( $octets );
496 14 }
497  
498 530 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=2 SCAR=6.9
499 {
500 530 my ( $type, $num ) = $message->_unpack_leader();
501  
502 530 ▶ $type == DATA_STRING or croak "Expected to unpack a string but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 1 or croak("Expected to unpack a string but did not find one") 525 5 530
503 525 my $octets = $message->_unpack( $num );
504 525 return decode_utf8( $octets );
505 14 }
506 }
507  
508 14 class Tangence::Type::Primitive::obj :isa(Tangence::Type)
509 {
510 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
511 14 ▶ use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
512 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
513  
514 0 ▶ method default_value { undef }
Subroutine default_value: not called CC=1 SCAR=1.2
515  
516 61 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=8 SCAR=21.3
517 {
518 61 my $stream = $message->stream;
519  
520 61 ▶ if( !defined $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 38 23 61
elsif (builtin::blessed($value) and $value->isa('Tangence::Object')) { } 21 2 23
elsif (builtin::blessed($value) and $value->isa('Tangence::ObjectProxy')) { } 2 0 2
521 38 $message->_pack_leader( DATA_OBJECT, 0 );
522 }
523 ▶ elsif( blessed $value and $value->isa( "Tangence::Object" ) ) {
Condition66%2/3
builtin::blessed($value) and $value->isa("Tangence::Object")
!ll&&!rl&&r
0221
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Tangence::Object")
builtin::blessed($value) $value->isa("Tangence::Object")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Tangence::Object")
ABresult
0X0
100
111
524 21 my $id = $value->id;
525 21 my $preamble = "";
526  
527 21 ▶ $value->{destroyed} and croak "Cannot pack destroyed object $value";
Branch: 1 branch
BranchTrueFalseTotal
$value->{"destroyed"} and croak("Cannot pack destroyed object $value") 0 21 21
528  
529 21 ▶ $message->packmeta_construct( $value ) unless $stream->peer_hasobj->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $stream->peer_hasobj->{$id} 20 1 21
530  
531 21 $message->_pack_leader( DATA_OBJECT, 4 );
532 21 $message->_pack( pack( "N", $id ) );
533 }
534 ▶ elsif( blessed $value and $value->isa( "Tangence::ObjectProxy" ) ) {
Condition33%1/3
builtin::blessed($value) and $value->isa("Tangence::ObjectProxy")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Tangence::ObjectProxy")
builtin::blessed($value) $value->isa("Tangence::ObjectProxy")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Tangence::ObjectProxy")
ABresult
0X0
100
111
535 2 $message->_pack_leader( DATA_OBJECT, 4 );
536 2 $message->_pack( pack( "N", $value->id ) );
537 }
538 else {
539 0 croak "Do not know how to pack a " . ref($value);
540 }
541 14 }
542  
543 61 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=4 SCAR=14.0
544 {
545 61 my ( $type, $num ) = $message->_unpack_leader();
546  
547 61 my $stream = $message->stream;
548  
549 61 ▶ $type == DATA_OBJECT or croak "Expected to unpack an object but did not find one";
Branch: 1 branch
BranchTrueFalseTotal
$type == 4 or croak("Expected to unpack an object but did not find one") 61 0 61
550 61 ▶ return undef unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 38 23 61
551 23 ▶ if( $num == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 4) { } 23 0 23
552 23 my ( $id ) = unpack( "N", $message->_unpack( 4 ) );
553 23 return $stream->get_by_id( $id );
554 }
555 else {
556 0 croak "Unexpected number of bits to encode an OBJECT";
557 }
558 14 }
559 }
560  
561 14 class Tangence::Type::Primitive::any :isa(Tangence::Type)
562 {
563 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
564 14 ▶ use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
565 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
566  
567 14 ▶ use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
568  
569 14 ▶ no if $] >= 5.035008, warnings => "experimental::builtin";
Subroutine BEGIN: called CC=1 SCAR=0.0
570 14 ▶ use constant HAVE_IS_BOOL => defined &builtin::is_bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
571  
572 14 my $TYPE_BOOL = Tangence::Type->make( 'bool' );
573 14 my $TYPE_INT = Tangence::Type->make( 'int' );
574 14 my $TYPE_FLOAT = Tangence::Type->make( 'float' );
575 14 my $TYPE_STR = Tangence::Type->make( 'str' );
576 14 my $TYPE_OBJ = Tangence::Type->make( 'obj' );
577 14 my $TYPE_ANY = Tangence::Type->make( 'any' );
578  
579 14 my $TYPE_LIST_ANY = Tangence::Type->make( list => $TYPE_ANY );
580 14 my $TYPE_DICT_ANY = Tangence::Type->make( dict => $TYPE_ANY );
581  
582 0 ▶ method default_value { undef }
Subroutine default_value: not called CC=1 SCAR=1.2
583  
584 209 ▶ method pack_value ( $message, $value )
Subroutine pack_value: called CC=15 SCAR=27.5
585 {
586 209 ▶ if( !defined $value ) {
Branch: 6 branches
BranchTrueFalseTotal
if (not defined $value) { } 38 171 209
elsif (not ref $value) { } 21 150 171
elsif (builtin::blessed($value) and $value->isa('Tangence::Object') || $value->isa('Tangence::ObjectProxy')) { } 0 150 150
elsif (my $struct = eval { do { 'Tangence::Struct'->for_perlname(ref $value) } }) { } 140 10 150
elsif (ref $value eq 'ARRAY') { } 5 5 10
elsif (ref $value eq 'HASH') { } 5 0 5
587 38 $TYPE_OBJ->pack_value( $message, undef );
588 }
589 elsif( !ref $value ) {
590 14 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
591  
592 21 my $is_numeric = do {
593 21 my $tmp = $value;
594  
595 # use X^X operator to distinguish actual numbers from strings
596 # If $tmp contains any non-ASCII bytes the it's definitely not a
597 # decimal representation of a number
598 21 ▶ $tmp =~ m/^[[:ascii:]]+$/ and ( $value ^ $value ) eq "0"
Condition100%3/3
$tmp =~ /^[[:ascii:]]+$/u && ($value ^ $value) eq '0'
!ll&&!rl&&r
2163
MC/DC100%2/2
$tmp =~ /^[[:ascii:]]+$/u && ($value ^ $value) eq '0'
$tmp =~ /^[[:ascii:]]+$/u ($value ^ $value) eq '0'
Truth table100%3/3
A && B
A= $tmp =~ /^[[:ascii:]]+$/uB= ($value ^ $value) eq '0'
ABresult
0X0
100
111
599 };
600  
601 21 ▶ if( HAVE_IS_BOOL && builtin::is_bool($value) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::is_bool($value)) { } 1 20 21
elsif (int $value eq $value and $value == $value) { } 1 19 20
elsif ($message->stream->_ver_can_num_float and $is_numeric) { } 2 17 19
602 1 $TYPE_BOOL->pack_value( $message, $value );
603 }
604 # test for integers, but exclude NaN
605 ▶ elsif( int($value) eq $value and $value == $value ) {
Condition100%3/3
int $value eq $value and $value == $value
!ll&&!rl&&r
1811
MC/DC100%2/2
int $value eq $value and $value == $value
int $value eq $value $value == $value
Truth table100%3/3
A and B
A= int $value eq $valueB= $value == $value
ABresult
0X0
100
111
606 1 $TYPE_INT->pack_value( $message, $value );
607 }
608 ▶ elsif( $message->stream->_ver_can_num_float and $is_numeric ) {
Condition66%2/3
$message->stream->_ver_can_num_float and $is_numeric
!ll&&!rl&&r
0172
MC/DC50%1/2
$message->stream->_ver_can_num_float and $is_numeric
$message->stream->_ver_can_num_float $is_numeric
Truth table66%2/3
A and B
A= $message->stream->_ver_can_num_floatB= $is_numeric
ABresult
0X0
100
111
609 2 $TYPE_FLOAT->pack_value( $message, $value );
610 }
611 else {
612 17 $TYPE_STR->pack_value( $message, $value );
613 }
614 }
615 ▶ elsif( blessed $value and $value->isa( "Tangence::Object" ) || $value->isa( "Tangence::ObjectProxy" ) ) {
Condition50%3/6
builtin::blessed($value) and $value->isa("Tangence::Object") || $value->isa("Tangence::ObjectProxy")
!ll&&!rl&&r
101400
$value->isa("Tangence::Object") or $value->isa("Tangence::ObjectProxy")
l!l&&r!l&&!r
00140
MC/DC0%0/3
builtin::blessed($value) and $value->isa("Tangence::Object") || $value->isa("Tangence::ObjectProxy")
builtin::blessed($value) $value->isa("Tangence::Object") $value->isa("Tangence::ObjectProxy")
Truth table50%2/4
A and B or C
A= builtin::blessed($value)B= $value->isa("Tangence::Object")C= $value->isa("Tangence::ObjectProxy")
ABCresult
0XX0
1000
11X1
1011
616 0 $TYPE_OBJ->pack_value( $message, $value );
617 }
618 150 elsif( my $struct = eval { Tangence::Struct->for_perlname( ref $value ) } ) {
619 140 $message->pack_record( $value, $struct );
620 }
621 elsif( ref $value eq "ARRAY" ) {
622 5 $TYPE_LIST_ANY->pack_value( $message, $value );
623 }
624 elsif( ref $value eq "HASH" ) {
625 5 $TYPE_DICT_ANY->pack_value( $message, $value );
626 }
627 else {
628 0 croak "Do not know how to pack a " . ref($value);
629 }
630 14 }
631  
632 209 ▶ method unpack_value ( $message )
Subroutine unpack_value: called CC=13 SCAR=26.4
633 {
634 209 my $type = $message->_peek_leader_type();
635  
636 match( $type : == ) {
637 case( DATA_NUMBER ) {
638 4 my ( undef, $num ) = $message->_unpack_leader( "peek" );
639 4 ▶ if( $num >= DATANUM_BOOLFALSE and $num <= DATANUM_BOOLTRUE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($num >= 0 and $num <= 1) { } 1 3 4
elsif ($num >= 2 and $num <= 9) { } 1 2 3
elsif ($num >= 16 and $num <= 18) { } 2 0 2
Condition66%2/3
$num >= 0 and $num <= 1
!ll&&!rl&&r
031
MC/DC50%1/2
$num >= 0 and $num <= 1
$num >= 0 $num <= 1
Truth table66%2/3
A and B
A= $num >= 0B= $num <= 1
ABresult
0X0
100
111
640 1 return $TYPE_BOOL->unpack_value( $message );
641 }
642 ▶ elsif( $num >= DATANUM_UINT8 and $num <= DATANUM_SINT64 ) {
Condition66%2/3
$num >= 2 and $num <= 9
!ll&&!rl&&r
021
MC/DC50%1/2
$num >= 2 and $num <= 9
$num >= 2 $num <= 9
Truth table66%2/3
A and B
A= $num >= 2B= $num <= 9
ABresult
0X0
100
111
643 1 return $TYPE_INT->unpack_value( $message );
644 }
645 ▶ elsif( $num >= DATANUM_FLOAT16 and $num <= DATANUM_FLOAT64 ) {
Condition33%1/3
$num >= 16 and $num <= 18
!ll&&!rl&&r
002
MC/DC0%0/2
$num >= 16 and $num <= 18
$num >= 16 $num <= 18
Truth table33%1/3
A and B
A= $num >= 16B= $num <= 18
ABresult
0X0
100
111
646 2 return $TYPE_FLOAT->unpack_value( $message );
647 }
648 else {
649 0 ▶ croak "Do not know how to unpack DATA_NUMBER subtype $num";
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 ? : 17 188 205
650 }
651 }
652 case( DATA_STRING ) {
653 17 ▶ return $TYPE_STR->unpack_value( $message );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 4 ? : 38 150 188
654 }
655 case( DATA_OBJECT ) {
656 38 ▶ return $TYPE_OBJ->unpack_value( $message );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 2 ? : 5 145 150
657 }
658 case( DATA_LIST ) {
659 5 ▶ return $TYPE_LIST_ANY->unpack_value( $message );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 3 ? : 5 140 145
660 }
661 case( DATA_DICT ) {
662 5 ▶ return $TYPE_DICT_ANY->unpack_value( $message );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 5 ? : 140 0 140
663 }
664 case( DATA_RECORD ) {
665 140 return $message->unpack_record( undef );
666 }
667 209 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 0 ? : 4 205 209
668 0 croak "Do not know how to unpack record of type $type";
669 }
670 }
671 14 }
672 }
673  
674 14 =head1 AUTHOR
675  
676 Paul Evans <leonerd@leonerd.org.uk>
677  
678 =cut
679  
680 14 0x55AA;
« Tangence/ObjectProxy.pm ↑ Summary Tangence/Meta/Type.pm »