Implement type parameterization in XS
[gitmo/Mouse.git] / lib / Mouse / PurePerl.pm
CommitLineData
ccb38d0b 1package Mouse::PurePerl;
2
df6dd016 3package
4 Mouse::Util;
5
6use strict;
7use warnings;
8
9use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
10
11use B ();
12
13sub is_class_loaded {
14 my $class = shift;
15
16 return 0 if ref($class) || !defined($class) || !length($class);
17
18 # walk the symbol table tree to avoid autovififying
19 # \*{${main::}{"Foo::"}} == \*main::Foo::
20
21 my $pack = \%::;
22 foreach my $part (split('::', $class)) {
23 my $entry = \$pack->{$part . '::'};
24 return 0 if ref($entry) ne 'GLOB';
25 $pack = *{$entry}{HASH} or return 0;
26 }
27
28 # check for $VERSION or @ISA
29 return 1 if exists $pack->{VERSION}
30 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
31 return 1 if exists $pack->{ISA}
32 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
33
34 # check for any method
35 foreach my $name( keys %{$pack} ) {
36 my $entry = \$pack->{$name};
37 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
38 }
39
40 # fail
41 return 0;
42}
43
44
45# taken from Sub::Identify
46sub get_code_info {
47 my ($coderef) = @_;
48 ref($coderef) or return;
49
50 my $cv = B::svref_2object($coderef);
51 $cv->isa('B::CV') or return;
52
53 my $gv = $cv->GV;
54 $gv->isa('B::GV') or return;
55
56 return ($gv->STASH->NAME, $gv->NAME);
57}
58
59sub get_code_package{
60 my($coderef) = @_;
61
62 my $cv = B::svref_2object($coderef);
63 $cv->isa('B::CV') or return '';
64
65 my $gv = $cv->GV;
66 $gv->isa('B::GV') or return '';
67
68 return $gv->STASH->NAME;
69}
70
7d96ae4d 71sub get_code_ref{
72 my($package, $name) = @_;
73 no strict 'refs';
74 no warnings 'once';
75 use warnings FATAL => 'uninitialized';
76 return *{$package . '::' . $name}{CODE};
77}
78
e3540312 79sub generate_isa_predicate_for {
1d5ecd5f 80 my($for_class, $name) = @_;
81
f48920c1 82 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
1d5ecd5f 83
84 if(defined $name){
85 no strict 'refs';
86 *{ caller() . '::' . $name } = $predicate;
87 return;
88 }
89
90 return $predicate;
91}
92
93
f48920c1 94package
95 Mouse::Util::TypeConstraints;
96
97use Scalar::Util qw(blessed looks_like_number openhandle);
98
7d96ae4d 99sub Any { 1 }
100sub Item { 1 }
7d96ae4d 101
102sub Bool { $_[0] ? $_[0] eq '1' : 1 }
103sub Undef { !defined($_[0]) }
104sub Defined { defined($_[0]) }
105sub Value { defined($_[0]) && !ref($_[0]) }
106sub Num { !ref($_[0]) && looks_like_number($_[0]) }
107sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
108sub Str { defined($_[0]) && !ref($_[0]) }
109
110sub Ref { ref($_[0]) }
111sub ScalarRef { ref($_[0]) eq 'SCALAR' }
112sub ArrayRef { ref($_[0]) eq 'ARRAY' }
113sub HashRef { ref($_[0]) eq 'HASH' }
114sub CodeRef { ref($_[0]) eq 'CODE' }
115sub RegexpRef { ref($_[0]) eq 'Regexp' }
116sub GlobRef { ref($_[0]) eq 'GLOB' }
117
118sub FileHandle {
119 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
120}
121
122sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
123
124sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
125sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
126
619338ac 127sub _parameterize_ArrayRef_for {
128 my($type_parameter) = @_;
129 my $check = $type_parameter->_compiled_type_constraint;
130
131 return sub {
132 foreach my $value (@{$_}) {
133 return undef unless $check->($value);
134 }
135 return 1;
136 }
137}
138
139sub _parameterize_HashRef_for {
140 my($type_parameter) = @_;
141 my $check = $type_parameter->_compiled_type_constraint;
142
143 return sub {
144 foreach my $value(values %{$_}){
145 return undef unless $check->($value);
146 }
147 return 1;
148 };
149}
150
151# 'Maybe' type accepts 'Any', so it requires parameters
152sub _parameterize_Maybe_for {
153 my($type_parameter) = @_;
154 my $check = $type_parameter->_compiled_type_constraint;
155
156 return sub{
157 return !defined($_) || $check->($_);
158 };
159};
160
161
7d96ae4d 162
43165725 163package
164 Mouse::Meta::Module;
165
926404f2 166sub name { $_[0]->{package} }
167
168sub _method_map { $_[0]->{methods} }
169sub _attribute_map{ $_[0]->{attribute_map} }
43165725 170
2591e962 171sub namespace{
172 my $name = $_[0]->{package};
173 no strict 'refs';
174 return \%{ $name . '::' };
175}
176
3e44140b 177sub add_method {
178 my($self, $name, $code) = @_;
179
180 if(!defined $name){
181 $self->throw_error('You must pass a defined name');
182 }
183 if(!defined $code){
184 $self->throw_error('You must pass a defined code');
185 }
186
187 if(ref($code) ne 'CODE'){
188 $code = \&{$code}; # coerce
189 }
190
191 $self->{methods}->{$name} = $code; # Moose stores meta object here.
192
193 my $pkg = $self->name;
194 no strict 'refs';
195 no warnings 'redefine', 'once';
196 *{ $pkg . '::' . $name } = $code;
197 return;
198}
199
200
43165725 201package
202 Mouse::Meta::Class;
203
204sub is_anon_class{
205 return exists $_[0]->{anon_serial_id};
206}
207
208sub roles { $_[0]->{roles} }
209
cccb83de 210sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
211
43165725 212package
213 Mouse::Meta::Role;
214
215sub is_anon_role{
216 return exists $_[0]->{anon_serial_id};
217}
218
219sub get_roles { $_[0]->{roles} }
220
221package
222 Mouse::Meta::Attribute;
223
d7d8d49b 224use Mouse::Meta::Method::Accessor;
43165725 225
226# readers
227
228sub name { $_[0]->{name} }
229sub associated_class { $_[0]->{associated_class} }
230
231sub accessor { $_[0]->{accessor} }
232sub reader { $_[0]->{reader} }
233sub writer { $_[0]->{writer} }
234sub predicate { $_[0]->{predicate} }
235sub clearer { $_[0]->{clearer} }
236sub handles { $_[0]->{handles} }
237
238sub _is_metadata { $_[0]->{is} }
239sub is_required { $_[0]->{required} }
240sub default { $_[0]->{default} }
241sub is_lazy { $_[0]->{lazy} }
242sub is_lazy_build { $_[0]->{lazy_build} }
243sub is_weak_ref { $_[0]->{weak_ref} }
244sub init_arg { $_[0]->{init_arg} }
245sub type_constraint { $_[0]->{type_constraint} }
246
247sub trigger { $_[0]->{trigger} }
248sub builder { $_[0]->{builder} }
249sub should_auto_deref { $_[0]->{auto_deref} }
250sub should_coerce { $_[0]->{coerce} }
251
d899d3e7 252sub documentation { $_[0]->{documentation} }
253
43165725 254# predicates
255
256sub has_accessor { exists $_[0]->{accessor} }
257sub has_reader { exists $_[0]->{reader} }
258sub has_writer { exists $_[0]->{writer} }
259sub has_predicate { exists $_[0]->{predicate} }
260sub has_clearer { exists $_[0]->{clearer} }
261sub has_handles { exists $_[0]->{handles} }
262
263sub has_default { exists $_[0]->{default} }
264sub has_type_constraint { exists $_[0]->{type_constraint} }
265sub has_trigger { exists $_[0]->{trigger} }
266sub has_builder { exists $_[0]->{builder} }
267
d899d3e7 268sub has_documentation { exists $_[0]->{documentation} }
269
93540011 270sub accessor_metaclass(){ 'Mouse::Meta::Method::Accessor' }
271
43165725 272package
273 Mouse::Meta::TypeConstraint;
274
275sub name { $_[0]->{name} }
276sub parent { $_[0]->{parent} }
277sub message { $_[0]->{message} }
278
279sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
280
93540011 281sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
df6dd016 282
93540011 283sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
df6dd016 284
2851;
286__END__
ccb38d0b 287
288=head1 NAME
289
290Mouse::PurePerl - A Mouse guts in pure Perl
291
292=head1 VERSION
293
2b68f76d 294This document describes Mouse version 0.40_03
ccb38d0b 295
296=head1 SEE ALSO
297
298L<Mouse::XS>
299
300=cut