Move accessors into XS
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
CommitLineData
3a63a2e7 1package Mouse::Meta::Module;
bc69ee88 2use Mouse::Util qw/:meta get_code_package load_class not_supported/; # enables strict and warnings
3a63a2e7 3
6d28c5cf 4use Carp ();
ad022aac 5use Scalar::Util qw/blessed weaken/;
fce211ae 6
fe1221ad 7my %METAS;
8536d351 8
deb9a0f3 9sub _metaclass_cache { # DEPRECATED
739525d0 10 my($class, $name) = @_;
fe1221ad 11 return $METAS{$name};
739525d0 12}
8536d351 13
739525d0 14sub initialize {
15 my($class, $package_name, @args) = @_;
8536d351 16
739525d0 17 ($package_name && !ref($package_name))
18 || $class->throw_error("You must pass a package name and it cannot be blessed");
8536d351 19
fe1221ad 20 return $METAS{$package_name}
739525d0 21 ||= $class->_construct_meta(package => $package_name, @args);
22}
8536d351 23
739525d0 24sub class_of{
25 my($class_or_instance) = @_;
26 return undef unless defined $class_or_instance;
23274b88 27 return $METAS{ ref($class_or_instance) || $class_or_instance };
8536d351 28}
29
739525d0 30# Means of accessing all the metaclasses that have
31# been initialized thus far
fe1221ad 32#sub get_all_metaclasses { %METAS }
33sub get_all_metaclass_instances { values %METAS }
34sub get_all_metaclass_names { keys %METAS }
35sub get_metaclass_by_name { $METAS{$_[0]} }
36#sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
37#sub weaken_metaclass { weaken($METAS{$_[0]}) }
38#sub does_metaclass_exist { defined $METAS{$_[0]} }
39#sub remove_metaclass_by_name { delete $METAS{$_[0]} }
739525d0 40
41
43165725 42sub name;
3a63a2e7 43
739525d0 44# The followings are Class::MOP specific methods
45
46#sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
47#sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
48#sub identifier {
49# my $self = shift;
50# return join '-' => (
51# $self->name,
52# ($self->version || ()),
53# ($self->authority || ()),
54# );
55#}
23264b5b 56
8536d351 57# add_attribute is an abstract method
58
deb9a0f3 59sub get_attribute_map { # DEPRECATED
3a683350 60 Carp::cluck('get_attribute_map() has been deprecated');
61 return $_[0]->{attributes};
62}
63
8536d351 64sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
65sub get_attribute { $_[0]->{attributes}->{$_[1]} }
66sub get_attribute_list{ keys %{$_[0]->{attributes}} }
6cfa1e5e 67sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
23264b5b 68
3a63a2e7 69sub namespace{
70 my $name = $_[0]->{package};
71 no strict 'refs';
72 return \%{ $name . '::' };
73}
74
75sub add_method {
76 my($self, $name, $code) = @_;
77
78 if(!defined $name){
1b9e472d 79 $self->throw_error('You must pass a defined name');
3a63a2e7 80 }
1b9e472d 81 if(!defined $code){
82 $self->throw_error('You must pass a defined code');
83 }
84
3a63a2e7 85 if(ref($code) ne 'CODE'){
78dc5ed1 86 $code = \&{$code}; # coerce
3a63a2e7 87 }
88
8d59c723 89 $self->{methods}->{$name} = $code; # Moose stores meta object here.
3a63a2e7 90
91 my $pkg = $self->name;
92 no strict 'refs';
c6e5eee1 93 no warnings 'redefine', 'once';
3a63a2e7 94 *{ $pkg . '::' . $name } = $code;
95}
96
01afd8ff 97# XXX: for backward compatibility
98my %foreign = map{ $_ => undef } qw(
99 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
100 Carp Scalar::Util
101);
102sub _code_is_mine{
103 my($self, $code) = @_;
8e64d0fa 104
01afd8ff 105 my $package = get_code_package($code);
8e64d0fa 106
01afd8ff 107 return !exists $foreign{$package};
3a63a2e7 108}
109
110sub has_method {
111 my($self, $method_name) = @_;
112
78dc5ed1 113 defined($method_name)
114 or $self->throw_error('You must define a method name');
115
2d2e77f9 116 return 1 if $self->{methods}{$method_name};
5fd0bdcf 117
2d2e77f9 118 my $code = do{
119 no strict 'refs';
c6e5eee1 120 no warnings 'once';
2d2e77f9 121 *{ $self->{package} . '::' . $method_name }{CODE};
122 };
3a63a2e7 123
124 return $code && $self->_code_is_mine($code);
125}
126
71e7b544 127sub get_method_body{
128 my($self, $method_name) = @_;
129
130 defined($method_name)
131 or $self->throw_error('You must define a method name');
132
133 return $self->{methods}{$method_name} ||= do{
c6e5eee1 134 my $code = do{
135 no strict 'refs';
136 no warnings 'once';
137 *{$self->{package} . '::' . $method_name}{CODE};
138 };
71e7b544 139
2d2e77f9 140 ($code && $self->_code_is_mine($code)) ? $code : undef;
71e7b544 141 };
142}
143
8536d351 144sub get_method{
6cfa1e5e 145 my($self, $method_name) = @_;
146
147 if($self->has_method($method_name)){
148 my $method_metaclass = $self->method_metaclass;
149 load_class($method_metaclass);
150
9010458d 151 return $method_metaclass->wrap(
60e446e8 152 body => $self->get_method_body($method_name),
9010458d 153 name => $method_name,
60e446e8 154 package => $self->name,
9010458d 155 associated_metaclass => $self,
6cfa1e5e 156 );
157 }
158
159 return undef;
8536d351 160}
3a63a2e7 161
8e64d0fa 162sub get_method_list {
3a63a2e7 163 my($self) = @_;
8e64d0fa 164
165 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 166}
167
7a50b450 168{
169 my $ANON_SERIAL = 0;
7a50b450 170
171 my %IMMORTALS;
172
173 sub create {
7eb3a8d5 174 my($self, $package_name, %options) = @_;
7a50b450 175
7eb3a8d5 176 my $class = ref($self) || $self;
177 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 178
8f40866c 179 my $superclasses;
7a50b450 180 if(exists $options{superclasses}){
7eb3a8d5 181 if($self->isa('Mouse::Meta::Role')){
7a50b450 182 delete $options{superclasses};
183 }
8f40866c 184 else{
185 $superclasses = delete $options{superclasses};
186 (ref $superclasses eq 'ARRAY')
7eb3a8d5 187 || $self->throw_error("You must pass an ARRAY ref of superclasses");
8f40866c 188 }
7a50b450 189 }
190
8f40866c 191 my $attributes = delete $options{attributes};
192 if(defined $attributes){
193 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
7eb3a8d5 194 || $self->throw_error("You must pass an ARRAY ref of attributes");
8f40866c 195 }
196 my $methods = delete $options{methods};
197 if(defined $methods){
198 (ref $methods eq 'HASH')
7eb3a8d5 199 || $self->throw_error("You must pass a HASH ref of methods");
8f40866c 200 }
201 my $roles = delete $options{roles};
202 if(defined $roles){
203 (ref $roles eq 'ARRAY')
7eb3a8d5 204 || $self->throw_error("You must pass an ARRAY ref of roles");
8f40866c 205 }
7a50b450 206 my $mortal;
207 my $cache_key;
208
209 if(!defined $package_name){ # anonymous
210 $mortal = !$options{cache};
211
212 # anonymous but immortal
213 if(!$mortal){
8e64d0fa 214 # something like Super::Class|Super::Class::2=Role|Role::1
215 $cache_key = join '=' => (
8f40866c 216 join('|', @{$superclasses || []}),
217 join('|', sort @{$roles || []}),
7a50b450 218 );
219 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
220 }
8f40866c 221 $options{anon_serial_id} = ++$ANON_SERIAL;
222 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
7a50b450 223 }
224
225 # instantiate a module
226 {
227 no strict 'refs';
228 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
229 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
230 }
231
7eb3a8d5 232 my $meta = $self->initialize( $package_name, %options);
7a50b450 233
fe1221ad 234 weaken $METAS{$package_name}
7a50b450 235 if $mortal;
236
7eb3a8d5 237 $meta->add_method(meta => sub{
238 $self->initialize(ref($_[0]) || $_[0]);
7a50b450 239 });
240
8f40866c 241 $meta->superclasses(@{$superclasses})
242 if defined $superclasses;
7a50b450 243
244 # NOTE:
245 # process attributes first, so that they can
246 # install accessors, but locally defined methods
247 # can then overwrite them. It is maybe a little odd, but
248 # I think this should be the order of things.
249 if (defined $attributes) {
250 if(ref($attributes) eq 'ARRAY'){
8f40866c 251 # array of Mouse::Meta::Attribute
7a50b450 252 foreach my $attr (@{$attributes}) {
8f40866c 253 $meta->add_attribute($attr);
7a50b450 254 }
255 }
256 else{
8f40866c 257 # hash map of name and attribute spec pairs
7a50b450 258 while(my($name, $attr) = each %{$attributes}){
259 $meta->add_attribute($name => $attr);
260 }
261 }
262 }
8f40866c 263 if (defined $methods) {
264 while(my($method_name, $method_body) = each %{$methods}){
265 $meta->add_method($method_name, $method_body);
7a50b450 266 }
267 }
8f40866c 268 if (defined $roles){
269 Mouse::Util::apply_all_roles($package_name, @{$roles});
7a50b450 270 }
271
8f40866c 272 if($cache_key){
7a50b450 273 $IMMORTALS{$cache_key} = $meta;
274 }
275
276 return $meta;
277 }
278
279 sub DESTROY{
280 my($self) = @_;
281
282 my $serial_id = $self->{anon_serial_id};
283
284 return if !$serial_id;
285
e75f21db 286 # @ISA is a magical variable, so we clear it manually.
287 @{$self->{superclasses}} = () if exists $self->{superclasses};
288
289 # Then, clear the symbol table hash
290 %{$self->namespace} = ();
7a50b450 291
8f40866c 292 my $name = $self->name;
293 delete $METAS{$name};
294
e75f21db 295 $name =~ s/ $serial_id \z//xms;
7a50b450 296
297 no strict 'refs';
8f40866c 298 delete ${$name}{ $serial_id . '::' };
7a50b450 299
300 return;
301 }
302}
303
8536d351 304sub throw_error{
305 my($class, $message, %args) = @_;
306
fce211ae 307 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
8e64d0fa 308 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
3a63a2e7 309
8536d351 310 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
311 Carp::croak($message);
312 }
313 else{
314 Carp::confess($message);
315 }
316}
3a63a2e7 317
3181;
319
320__END__
321
322=head1 NAME
323
1820fffe 324Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 325
a25ca8d6 326=head1 VERSION
327
034587d8 328This document describes Mouse version 0.40
a25ca8d6 329
b995be6a 330=head1 SEE ALSO
331
31c5194b 332L<Class::MOP::Class>
333
334L<Class::MOP::Module>
335
336L<Class::MOP::Package>
b995be6a 337
3a63a2e7 338=cut
2cea7a5f 339