just a simple XSLoader for now
[gitmo/Mouse.git] / lib / Mouse / TypeRegistry.pm
1 #!/usr/bin/env perl
2 package Mouse::TypeRegistry;
3 use strict;
4 use warnings;
5
6 use Carp ();
7 use Mouse::Util qw/blessed looks_like_number openhandle/;
8
9 my $SUBTYPE = +{};
10 my $COERCE = +{};
11
12 sub import {
13     my $class  = shift;
14     my %args   = @_;
15     my $caller = caller(0);
16
17     if (defined $args{'-export'} && ref($args{'-export'}) eq 'ARRAY') {
18         no strict 'refs';
19         *{"$caller\::import"} = sub { _import(@_) };
20     }
21
22     no strict 'refs';
23     *{"$caller\::as"}          = \&_as;
24     *{"$caller\::where"}       = \&_where;
25     *{"$caller\::message"}     = \&_message;
26     *{"$caller\::from"}        = \&_from;
27     *{"$caller\::via"}         = \&_via;
28     *{"$caller\::subtype"}     = \&_subtype;
29     *{"$caller\::coerce"}      = \&_coerce;
30     *{"$caller\::class_type"}  = \&_class_type;
31     *{"$caller\::role_type"}   = \&_role_type;
32 }
33
34
35 sub _as ($) {
36     as => $_[0]
37 }
38 sub _where (&) {
39     where => $_[0]
40 }
41 sub _message ($) {
42     message => $_[0]
43 }
44
45 sub _from { @_ }
46 sub _via (&) {
47     $_[0]
48 }
49
50 sub _import {
51     my($class, @types) = @_;
52     return unless exists $SUBTYPE->{$class} && exists $COERCE->{$class};
53     my $pkg = caller(1);
54     return unless @types;
55     copy_types($class, $pkg, @types);
56 }
57
58 sub _subtype {
59     my $pkg = caller(0);
60     my($name, %conf) = @_;
61     if (my $type = $SUBTYPE->{$name}) {
62         Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
63     };
64     my $as = $conf{as};
65     my $stuff = $conf{where} || optimized_constraints()->{$as};
66
67     $SUBTYPE->{$name} = $stuff;
68 }
69
70 sub _coerce {
71     my($name, %conf) = @_;
72
73     Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
74         unless optimized_constraints()->{$name};
75
76     my $subtypes = optimized_constraints();
77     $COERCE->{$name} ||= {};
78     while (my($type, $code) = each %conf) {
79         Carp::croak "A coercion action already exists for '$type'"
80             if $COERCE->{$name}->{$type};
81
82         Carp::croak "Could not find the type constraint ($type) to coerce from"
83             unless $subtypes->{$type};
84
85         $COERCE->{$name}->{$type} = $code;
86     }
87 }
88
89 sub _class_type {
90     my $pkg = caller(0);
91     my($name, $conf) = @_;
92     my $class = $conf->{class};
93     _subtype(
94         $name => where => sub {
95             defined $_ && ref($_) eq $class;
96         }
97     );
98 }
99
100 sub _role_type {
101     my($name, $conf) = @_;
102     my $role = $conf->{role};
103     _subtype(
104         $name => where => sub {
105             return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
106             $_->meta->does_role($role);
107         }
108     );
109 }
110
111 sub typecast_constraints {
112     my($class, $pkg, $type, $value) = @_;
113     return $value unless $COERCE->{$type};
114
115     my $optimized_constraints = optimized_constraints();
116     for my $coerce_type (keys %{ $COERCE->{$type} }) {
117         local $_ = $value;
118         if ($optimized_constraints->{$coerce_type}->()) {
119             local $_ = $value;
120             return $COERCE->{$type}->{$coerce_type}->();
121         }
122     }
123
124     return $value;
125 }
126
127 {
128     no warnings 'uninitialized';
129     my $optimized_constraints = {
130         Any        => sub { 1 },
131         Item       => sub { 1 },
132         Bool       => sub {
133             !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
134         },
135         Undef      => sub { !defined($_) },
136         Defined    => sub { defined($_) },
137         Value      => sub { defined($_) && !ref($_) },
138         Num        => sub { !ref($_) && looks_like_number($_) },
139         Int        => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
140         Str        => sub { defined($_) && !ref($_) },
141         ClassName  => sub { Mouse::is_class_loaded($_) },
142         Ref        => sub { ref($_) },
143
144         ScalarRef  => sub { ref($_) eq 'SCALAR' },
145         ArrayRef   => sub { ref($_) eq 'ARRAY'  },
146         HashRef    => sub { ref($_) eq 'HASH'   },
147         CodeRef    => sub { ref($_) eq 'CODE'   },
148         RegexpRef  => sub { ref($_) eq 'Regexp' },
149         GlobRef    => sub { ref($_) eq 'GLOB'   },
150
151         FileHandle => sub {
152                 ref($_) eq 'GLOB'
153                 && openhandle($_)
154             or
155                 blessed($_)
156                 && $_->isa("IO::Handle")
157         },
158
159         Object     => sub { blessed($_) && blessed($_) ne 'Regexp' },
160     };
161     sub optimized_constraints {
162         return { %{ $SUBTYPE }, %{ $optimized_constraints } };
163     }
164 }
165
166 1;
167
168 __END__
169
170 =head1 NAME
171
172 Mouse::TypeRegistry - simple type constraints
173
174 =head1 METHODS
175
176 =head2 optimized_constraints -> HashRef[CODE]
177
178 Returns the simple type constraints that Mouse understands.
179
180 =cut
181
182