File Coverage

blib/lib/Types/CLike.pm
Criterion Covered Total %
statement 27 35 77.1
branch 0 4 0.0
condition n/a
subroutine 11 12 91.6
pod n/a
total 38 51 74.5


line stmt bran cond sub pod time code
1             package Types::CLike;
2              
3 1     1   155331 use v5.8.8;
  1         4  
  1         55  
4 1     1   7 use strict;
  1         2  
  1         36  
5 1     1   6 use warnings;
  1         2  
  1         74  
6              
7             our $VERSION = '0.91'; # VERSION
8             # ABSTRACT: C-like data types for Moo(se)
9              
10             our @EXPORT_OK = ();
11              
12 1     1   3443 use Type::Library -base;
  1         51429  
  1         10  
13 1     1   4505 use Types::Numbers;
  1         357249  
  1         17  
14              
15             my $meta = __PACKAGE__->meta;
16              
17             # All of these types are just aliases of parameterized parents
18             sub __alias_subtype {
19 91     91   743615 $meta->add_type(
20             name => $_[0],
21             parent => $_[1],
22             library => __PACKAGE__,
23             );
24             };
25              
26             sub __integer_builder {
27 7     7   3306 my ($bits, $signed_names, $unsigned_names) = @_;
28              
29 7         51 __alias_subtype($_, Types::Numbers::SignedInt[$bits]) for @$signed_names;
30 7         3825 __alias_subtype($_, Types::Numbers::UnsignedInt[$bits]) for @$unsigned_names;
31             };
32              
33             sub __money_builder {
34 3     3   1881 my ($bits, $scale, $names) = @_;
35 3         21 __alias_subtype($_, Types::Numbers::FixedBinary[$bits, $scale]) for @$names;
36             };
37              
38             sub __float_builder {
39 8     8   4562 my ($bits, $ebits, $names) = @_;
40 8         46 __alias_subtype($_, Types::Numbers::FloatBinary[$bits, $ebits]) for @$names;
41             };
42              
43             sub __decimal_builder {
44 3     3   1876 my ($digits, $emax, $names) = @_;
45 3         21 __alias_subtype($_, Types::Numbers::FloatDecimal[$digits, $emax]) for @$names;
46             };
47              
48             sub __char_builder {
49 5     5   2729 my ($bits, $names) = @_;
50 5         24 __alias_subtype($_, Types::Numbers::Char[$bits]) for @$names;
51             }
52              
53             ### Integer definitions ###
54             # being careful with char here...
55             __integer_builder( 4, [qw(SNibble SSemiOctet Int4 Signed4)], [qw(Nibble SemiOctet UInt4 Unsigned4)]),
56             __integer_builder( 8, [qw(SByte SOctet TinyInt Int8 Signed8)], [qw(Byte Octet UnsignedTinyInt UInt8 Unsigned8)]),
57             __integer_builder( 16, [qw(Short SmallInt Int16 Signed16)], [qw(UShort UnsignedSmallInt UInt16 Unsigned16)]),
58             __integer_builder( 24, [qw(MediumInt Int24 Signed24)], [qw(UnsignedMediumInt UInt24 Unsigned24)]),
59             __integer_builder( 32, [qw(Int Int32 Signed32)], [qw(UInt UnsignedInt UInt32 Unsigned32)]),
60             __integer_builder( 64, [qw(Long LongLong BigInt Int64 Signed64)], [qw(ULong ULongLong UnsignedBigInt UInt64 Unsigned64)]),
61             __integer_builder(128, [qw(SOctaWord SDoubleQuadWord Int128 Signed128)], [qw(OctaWord DoubleQuadWord UInt128 Unsigned128)]),
62              
63             ### "Money" definitions ###
64             __money_builder( 32, 4, [qw(SmallMoney)]),
65             __money_builder( 64, 4, [qw(Money Currency)]),
66             __money_builder(128, 6, [qw(BigMoney)]),
67              
68             ### Float definitions ###
69             __float_builder( 16, 4, [qw(ShortFloat)]),
70             __float_builder( 16, 5, [qw(Half Float16 Binary16)]),
71             __float_builder( 32, 8, [qw(Single Real Float Float32 Binary32)]),
72             __float_builder( 40, 8, [qw(ExtendedSingle Float40)]),
73             __float_builder( 64, 11, [qw(Double Float64 Binary64)]),
74             __float_builder( 80, 15, [qw(ExtendedDouble Float80)]),
75             __float_builder(104, 8, [qw(Decimal)]),
76             __float_builder(128, 15, [qw(Quadruple Quad Float128 Binary128)]),
77              
78             ### Decimal definitions ###
79             __decimal_builder( 7, 96, [ 'Decimal32']),
80             __decimal_builder(16, 384, [ 'Decimal64']),
81             __decimal_builder(34, 6144, ['Decimal128']),
82              
83             ### Char definitions ###
84             __char_builder( 8, [qw(Char Char8)]),
85             __char_builder(16, [qw(Char16)]),
86             __char_builder(32, [qw(Char32)]),
87             __char_builder(48, [qw(Char48)]),
88             __char_builder(64, [qw(Char64)]),
89              
90             my %base_tags = (
91             'c' => [qw(Char Byte Short UShort Int UInt Long ULong Float Double ExtendedDouble)],
92             'stdint' => [ map { ('Int'.$_, 'UInt'.$_) } (4,8,16,32,64,128) ],
93             'c#' => [qw(SByte Byte Char16 Short UShort Int UInt Long ULong Float Double Decimal)],
94             'ieee754' => ['Binary16', map { ('Binary'.$_, 'Decimal'.$_) } (32,64,128) ],
95             'tsql' => [qw(TinyInt SmallInt Int BigInt SmallMoney Money Float64 Real)],
96             'mysql' => [ (map { ($_, 'Unsigned'.$_) } qw(TinyInt SmallInt MediumInt Int BigInt)), qw(Float Double)],
97             'ansisql' => [qw(SmallInt Int Float Real Double)],
98             );
99              
100             sub _exporter_expand_tag {
101 0     0     my $class = shift;
102 0           my ($name, $value, $globals) = @_;
103              
104 0           my $p = '';
105 0           my $base_name = $name;
106 0 0         $base_name =~ s/^((?:is|assert|to)_|\+)// and $p = $1;
107              
108 0 0         $base_tags{$base_name} and return map [ "$p$_" => $value ], @{ $base_tags{$base_name} };
  0            
109              
110 0           return $class->SUPER::_exporter_expand_tag(@_);
111             }
112              
113             42;
114              
115             __END__