/
Bitfield.pm
146 lines (118 loc) · 3.29 KB
/
Bitfield.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package Type::Tiny::Bitfield;
use 5.008001;
use strict;
use warnings;
BEGIN {
$Type::Tiny::Bitfield::AUTHORITY = 'cpan:TOBYINK';
$Type::Tiny::Bitfield::VERSION = '2.002001';
}
$Type::Tiny::Bitfield::VERSION =~ tr/_//d;
sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
use Exporter::Tiny 1.004001 ();
use Type::Tiny ();
our @ISA = qw( Type::Tiny Exporter::Tiny );
sub _is_power_of_two { not $_[0] & $_[0]-1 }
sub _exporter_fail {
my ( $class, $type_name, $values, $globals ) = @_;
my $caller = $globals->{into};
my $type = $class->new(
name => $type_name,
values => { %$values },
#coercion => 1,
);
$INC{'Type/Registry.pm'}
? 'Type::Registry'->for_class( $caller )->add_type( $type, $type_name )
: ( $Type::Registry::DELAYED{$caller}{$type_name} = $type )
unless( ref($caller) or $caller eq '-lexical' or $globals->{'lexical'} );
return map +( $_->{name} => $_->{code} ), @{ $type->exportables };
}
sub new {
my $proto = shift;
my %opts = ( @_ == 1 ) ? %{ $_[0] } : @_;
_croak
"Bitfield type constraints cannot have a parent constraint passed to the constructor"
if exists $opts{parent};
_croak
"Bitfield type constraints cannot have a constraint coderef passed to the constructor"
if exists $opts{constraint};
_croak
"Bitfield type constraints cannot have a inlining coderef passed to the constructor"
if exists $opts{inlined};
_croak "Need to supply hashref of values"
unless exists $opts{values};
require Types::Common::Numeric;
$opts{parent} = Types::Common::Numeric::PositiveOrZeroInt();
my $ALL = 0;
my %already = ();
for my $value ( values %{ $opts{values} } ) {
_croak "Not a positive power of 2 in a bitfield: $value"
unless _is_power_of_two $value;
_croak "Duplicate value in a bitfield: $value"
if $already{$value}++;
$ALL |= ( 0 + $value );
}
$opts{ALL} = $ALL;
# TODO: ensure all keys in $opt{values} are caps
$opts{constraint} = sub {
not shift() & ~$ALL;
};
# TODO: coercion
return $proto->SUPER::new( %opts );
} #/ sub new
sub values {
$_[0]{values}
}
sub _lockdown {
my ( $self, $callback ) = @_;
$callback->( $self->{values} );
}
sub exportables {
my ( $self, $base_name ) = @_;
if ( not $self->is_anon ) {
$base_name ||= $self->name;
}
my $exportables = $self->SUPER::exportables( $base_name );
require Eval::TypeTiny;
require B;
for my $key ( keys %{ $self->values } ) {
my $value = $self->values->{$key};
push @$exportables, {
name => uc( sprintf '%s_%s', $base_name, $key ),
tags => [ 'constants' ],
code => Eval::TypeTiny::eval_closure(
source => sprintf( 'sub () { %d }', $value ),
environment => {},
),
};
}
return $exportables;
}
sub can_be_inlined {
!!1;
}
sub inline_check {
my ( $self, $var ) = @_;
return sprintf(
'( %s and not %s & ~%d )',
Types::Common::Numeric::PositiveOrZeroInt()->inline_check( $var ),
$var,
$self->{ALL},
);
}
sub AUTOLOAD {
my $self = shift;
my ( $m ) = ( our $AUTOLOAD =~ /::(\w+)$/ );
return if $m eq 'DESTROY';
if ( ref $self and exists $self->{values}{$m} ) {
return $self->{values}{$m};
}
return $self->SUPER::AUTOLOAD( @_ );
}
sub can {
my ( $self, $m ) = ( shift, @_ );
if ( ref $self and exists $self->{values}{$m} ) {
return sub () { $self->{values}{$m} };
}
return $self->SUPER::can( @_ );
}
1;