| 1 | package Thrift::Parser::Type::Number; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Thrift::Parser::Type::Number - Number base class | ||||||||||
| 6 | |||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||
| 8 | |||||||||||
| 9 | This class inherits from L<Thrift::Parser::Type>. See the docs there for all the usage details. | ||||||||||
| 10 | |||||||||||
| 11 | =cut | ||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 6 | ▶ | use base qw(Thrift::Parser::Type); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | use overload '""' => sub { $_[0]->value }, 'eq' => sub { $_[0]->value }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||
| 19 | |||||||||||
| 20 | =head1 USAGE | ||||||||||
| 21 | |||||||||||
| 22 | Firstly, you can use objects in this class in string context; the stringification overload will display the number, as you'd expect. | ||||||||||
| 23 | |||||||||||
| 24 | =head2 compose | ||||||||||
| 25 | |||||||||||
| 26 | Call with a signed number. Throws L<Thrift::Parser::InvalidTypedValue>. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub compose { | ||||||||
|
Subroutine
compose: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||
| 31 | 0 | my ($class, $value) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | Thrift::Parser::InvalidTypedValue->throw("'undef' is not valid for $class") if ! defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | if (blessed $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | ▶ | if (! $value->isa($class)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value)); | |||||||||
| 38 | } | ||||||||||
| 39 | 0 | return $value; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | if ($class eq 'Thrift::Parser::Type::double') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | ▶ | if ($value !~ m{^-?\d+\.?\d*$}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | Thrift::Parser::InvalidTypedValue->throw("Value '$value' is not a float"); | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | else { | ||||||||||
| 48 | 0 | ▶ | if ($value !~ m{^-?\d+$}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | Thrift::Parser::InvalidTypedValue->throw("Value '$value' is not a signed real number"); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | my $bit = sprintf '%d', (log($class->_max_value) / log(2)) + 1; | |||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | Thrift::Parser::InvalidTypedValue->throw("Value '$value' exceeds signed $bit-bit range") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | if abs($value * 1) > $class->_max_value; | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | return $class->SUPER::compose($value); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub values_equal { | ||||||||
|
Subroutine
values_equal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 62 | 0 | my ($class, $value_a, $value_b) = @_; | |||||||||
| 63 | 0 | return $value_a == $value_b; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub value_plain { | ||||||||
|
Subroutine
value_plain: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 67 | 0 | my $self = shift; | |||||||||
| 68 | 0 | return $self->value + 0; # ensure that it's a Perl number | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | =head1 COPYRIGHT | ||||||||||
| 72 | |||||||||||
| 73 | Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 74 | |||||||||||
| 75 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||
| 76 | |||||||||||
| 77 | =head1 AUTHOR | ||||||||||
| 78 | |||||||||||
| 79 | Eric Waters <ewaters@gmail.com> | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | 6 | 1; | |||||||||