DEMOLISH to XS
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
306290e8 1package Mouse::Meta::Class;
bc69ee88 2use Mouse::Util qw/:meta get_linear_isa not_supported/; # enables strict and warnings
c3398f5b 3
cecfb973 4use Scalar::Util qw/blessed weaken/;
6d28c5cf 5
6d28c5cf 6use Mouse::Meta::Method::Destructor;
7use Mouse::Meta::Module;
f3bb863f 8our @ISA = qw(Mouse::Meta::Module);
3a63a2e7 9
deb9a0f3 10sub method_metaclass() { 'Mouse::Meta::Method' }
11sub attribute_metaclass() { 'Mouse::Meta::Attribute' }
3a63a2e7 12
a5c683f6 13sub constructor_class();
14sub destructor_class();
380e1cd7 15
8e64d0fa 16sub _construct_meta {
88ed7189 17 my($class, %args) = @_;
c3398f5b 18
5132ec42 19 $args{attributes} = {};
20 $args{methods} = {};
21 $args{roles} = [];
8536d351 22
c3398f5b 23 $args{superclasses} = do {
24 no strict 'refs';
88ed7189 25 \@{ $args{package} . '::ISA' };
c3398f5b 26 };
27
8d40c3b8 28 my $self = bless \%args, ref($class) || $class;
7eb3a8d5 29 if(ref($self) ne __PACKAGE__){
9009aca1 30 $self->meta->_initialize_object($self, \%args);
8d40c3b8 31 }
32 return $self;
7a50b450 33}
34
35sub create_anon_class{
36 my $self = shift;
37 return $self->create(undef, @_);
38}
39
43165725 40sub is_anon_class;
c3398f5b 41
43165725 42sub roles;
c3398f5b 43
e7264861 44sub calculate_all_roles {
45 my $self = shift;
46 my %seen;
47 return grep { !$seen{ $_->name }++ }
48 map { $_->calculate_all_roles } @{ $self->roles };
49}
50
c3398f5b 51sub superclasses {
52 my $self = shift;
53
54 if (@_) {
9d0686b2 55 foreach my $super(@_){
56 Mouse::Util::load_class($super);
57 my $meta = Mouse::Util::get_metaclass_by_name($super);
f48920c1 58 if(Mouse::Util::is_a_metarole($meta)){
9d0686b2 59 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
60 }
61 }
c3398f5b 62 @{ $self->{superclasses} } = @_;
63 }
64
8e64d0fa 65 return @{ $self->{superclasses} };
66}
67
68sub find_method_by_name{
69 my($self, $method_name) = @_;
70 defined($method_name)
71 or $self->throw_error('You must define a method name to find');
0126c27c 72
8e64d0fa 73 foreach my $class( $self->linearized_isa ){
74 my $method = $self->initialize($class)->get_method($method_name);
75 return $method if defined $method;
76 }
77 return undef;
78}
79
80sub get_all_methods {
81 my($self) = @_;
612d3e1a 82 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
c3398f5b 83}
84
60cfc6ad 85sub get_all_method_names {
86 my $self = shift;
87 my %uniq;
88 return grep { $uniq{$_}++ == 0 }
3a63a2e7 89 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
60cfc6ad 90 $self->linearized_isa;
91}
92
2b908b79 93sub find_attribute_by_name{
94 my($self, $name) = @_;
95 my $attr;
96 foreach my $class($self->linearized_isa){
97 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
98 $attr = $meta->get_attribute($name) and last;
99 }
100 return $attr;
101}
102
87ca293b 103sub add_attribute {
c3398f5b 104 my $self = shift;
60f6eba9 105
87ca293b 106 my($attr, $name);
1b9e472d 107
87ca293b 108 if(blessed $_[0]){
109 $attr = $_[0];
1b9e472d 110
87ca293b 111 $attr->isa('Mouse::Meta::Attribute')
112 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
1b9e472d 113
87ca293b 114 $name = $attr->name;
1b9e472d 115 }
116 else{
87ca293b 117 # _process_attribute
118 $name = shift;
1b9e472d 119
87ca293b 120 my %args = (@_ == 1) ? %{$_[0]} : @_;
1b9e472d 121
87ca293b 122 defined($name)
123 or $self->throw_error('You must provide a name for the attribute');
1b9e472d 124
87ca293b 125 if ($name =~ s/^\+//) { # inherited attributes
2b908b79 126 my $inherited_attr = $self->find_attribute_by_name($name)
87ca293b 127 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
128
8cf51b82 129 $attr = $inherited_attr->clone_and_inherit_options(%args);
87ca293b 130 }
131 else{
8cf51b82 132 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
87ca293b 133 $args{traits} = \@traits if @traits;
134
2a464664 135 $attr = $attribute_class->new($name, %args);
87ca293b 136 }
137 }
1b9e472d 138
139 weaken( $attr->{associated_class} = $self );
140
141 $self->{attributes}{$attr->name} = $attr;
142 $attr->install_accessors();
143
04493075 144 if(_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
1b9e472d 145 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
60f6eba9 146 }
1b9e472d 147 return $attr;
c3398f5b 148}
149
3a683350 150sub compute_all_applicable_attributes {
0126c27c 151 Carp::cluck('compute_all_applicable_attributes() has been deprecated')
152 if _MOUSE_VERBOSE;
3a683350 153 return shift->get_all_attributes(@_)
154}
155
cccb83de 156sub linearized_isa;
8536d351 157
158sub new_object {
c68b4110 159 my $self = shift;
7a50b450 160 my %args = (@_ == 1 ? %{$_[0]} : @_);
c3398f5b 161
926290ac 162 my $object = bless {}, $self->name;
fce211ae 163
926290ac 164 $self->_initialize_object($object, \%args);
165 return $object;
1b9e472d 166}
167
7a59f4e8 168sub clone_object {
926290ac 169 my $class = shift;
170 my $object = shift;
171 my %params = (@_ == 1) ? %{$_[0]} : @_;
7a59f4e8 172
926290ac 173 (blessed($object) && $object->isa($class->name))
174 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
7a59f4e8 175
926290ac 176 my $cloned = bless { %$object }, ref $object;
177 $class->_initialize_object($cloned, \%params);
7a59f4e8 178
926290ac 179 return $cloned;
2cea7a5f 180}
7a59f4e8 181
2cea7a5f 182sub clone_instance {
183 my ($class, $instance, %params) = @_;
184
04493075 185 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
186 if _MOUSE_VERBOSE;
2cea7a5f 187 return $class->clone_object($instance, %params);
7a59f4e8 188}
189
fc1d8369 190sub make_immutable {
191 my $self = shift;
6a1d1835 192 my %args = (
7efbc77d 193 inline_constructor => 1,
e578d610 194 inline_destructor => 1,
2a464664 195 constructor_name => 'new',
6a1d1835 196 @_,
197 );
198
fc1d8369 199 $self->{is_immutable}++;
c7a6403f 200
6a1d1835 201 if ($args{inline_constructor}) {
a5c683f6 202 my $c = $self->constructor_class;
203 Mouse::Util::load_class($c);
380e1cd7 204 $self->add_method($args{constructor_name} =>
a5c683f6 205 $c->_generate_constructor($self, \%args));
c7a6403f 206 }
207
8632b6fe 208 if ($args{inline_destructor}) {
a5c683f6 209 my $c = $self->destructor_class;
210 Mouse::Util::load_class($c);
380e1cd7 211 $self->add_method(DESTROY =>
a5c683f6 212 $c->_generate_destructor($self, \%args));
8632b6fe 213 }
2276cb14 214
215 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
216 # at the end of a source file.
217 return 1;
fc1d8369 218}
ad958001 219
fce211ae 220sub make_mutable { not_supported }
ad958001 221
6cfa1e5e 222sub is_immutable { $_[0]->{is_immutable} }
223sub is_mutable { !$_[0]->{is_immutable} }
84ef660f 224
3fbade18 225sub _install_modifier_pp{
8d59c723 226 my( $self, $type, $name, $code ) = @_;
227 my $into = $self->name;
3fbade18 228
229 my $original = $into->can($name)
230 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
231
232 my $modifier_table = $self->{modifiers}{$name};
233
234 if(!$modifier_table){
235 my(@before, @after, @around, $cache, $modified);
236
237 $cache = $original;
238
239 $modified = sub {
240 for my $c (@before) { $c->(@_) }
241
242 if(wantarray){ # list context
243 my @rval = $cache->(@_);
244
245 for my $c(@after){ $c->(@_) }
246 return @rval;
247 }
248 elsif(defined wantarray){ # scalar context
249 my $rval = $cache->(@_);
250
251 for my $c(@after){ $c->(@_) }
252 return $rval;
253 }
254 else{ # void context
255 $cache->(@_);
256
257 for my $c(@after){ $c->(@_) }
258 return;
259 }
260 };
261
262 $self->{modifiers}{$name} = $modifier_table = {
263 original => $original,
264
265 before => \@before,
266 after => \@after,
267 around => \@around,
268
269 cache => \$cache, # cache for around modifiers
270 };
271
272 $self->add_method($name => $modified);
273 }
274
275 if($type eq 'before'){
276 unshift @{$modifier_table->{before}}, $code;
277 }
278 elsif($type eq 'after'){
279 push @{$modifier_table->{after}}, $code;
280 }
281 else{ # around
282 push @{$modifier_table->{around}}, $code;
283
284 my $next = ${ $modifier_table->{cache} };
285 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
286 }
287
288 return;
289}
290
4859d490 291sub _install_modifier {
8d59c723 292 my ( $self, $type, $name, $code ) = @_;
4f5b44a0 293
3fbade18 294 # load Class::Method::Modifiers first
7ca5c5fb 295 my $no_cmm_fast = do{
3fbade18 296 local $@;
297 eval q{ require Class::Method::Modifiers::Fast };
298 $@;
4f5b44a0 299 };
4f5b44a0 300
3fbade18 301 my $impl;
302 if($no_cmm_fast){
303 $impl = \&_install_modifier_pp;
304 }
305 else{
306 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
307 $impl = sub {
8d59c723 308 my ( $self, $type, $name, $code ) = @_;
309 my $into = $self->name;
95ecd6f1 310 $install_modifier->($into, $type, $name, $code);
311
8d59c723 312 $self->add_method($name => do{
313 no strict 'refs';
314 \&{ $into . '::' . $name };
315 });
6cfa1e5e 316 return;
4f5b44a0 317 };
1b79a118 318 }
4f5b44a0 319
3fbade18 320 # replace this method itself :)
321 {
322 no warnings 'redefine';
323 *_install_modifier = $impl;
324 }
325
8d59c723 326 $self->$impl( $type, $name, $code );
4859d490 327}
328
50dc6ee5 329sub add_before_method_modifier {
4859d490 330 my ( $self, $name, $code ) = @_;
8d59c723 331 $self->_install_modifier( 'before', $name, $code );
50dc6ee5 332}
333
334sub add_around_method_modifier {
4859d490 335 my ( $self, $name, $code ) = @_;
8d59c723 336 $self->_install_modifier( 'around', $name, $code );
50dc6ee5 337}
338
339sub add_after_method_modifier {
4859d490 340 my ( $self, $name, $code ) = @_;
8d59c723 341 $self->_install_modifier( 'after', $name, $code );
50dc6ee5 342}
343
67199842 344sub add_override_method_modifier {
345 my ($self, $name, $code) = @_;
346
768804c0 347 if($self->has_method($name)){
348 $self->throw_error("Cannot add an override method if a local method is already present");
85bd3f44 349 }
350
6cfa1e5e 351 my $package = $self->name;
67199842 352
85bd3f44 353 my $super_body = $package->can($name)
6cfa1e5e 354 or $self->throw_error("You cannot override '$name' because it has no super method");
67199842 355
85bd3f44 356 $self->add_method($name => sub {
357 local $Mouse::SUPER_PACKAGE = $package;
358 local $Mouse::SUPER_BODY = $super_body;
359 local @Mouse::SUPER_ARGS = @_;
360
361 $code->(@_);
362 });
363 return;
67199842 364}
365
768804c0 366sub add_augment_method_modifier {
367 my ($self, $name, $code) = @_;
368 if($self->has_method($name)){
369 $self->throw_error("Cannot add an augment method if a local method is already present");
370 }
371
372 my $super = $self->find_method_by_name($name)
373 or $self->throw_error("You cannot augment '$name' because it has no super method");
374
375 my $super_package = $super->package_name;
376 my $super_body = $super->body;
377
378 $self->add_method($name => sub{
379 local $Mouse::INNER_BODY{$super_package} = $code;
380 local $Mouse::INNER_ARGS{$super_package} = [@_];
381 $super_body->(@_);
382 });
383 return;
384}
385
47f36c05 386sub does_role {
387 my ($self, $role_name) = @_;
ad958001 388
47f36c05 389 (defined $role_name)
fce211ae 390 || $self->throw_error("You must supply a role name to look for");
ad958001 391
f7fec86c 392 for my $class ($self->linearized_isa) {
95ecd6f1 393 my $meta = Mouse::Util::get_metaclass_by_name($class)
394 or next;
3a63a2e7 395
396 for my $role (@{ $meta->roles }) {
ff687069 397
3a63a2e7 398 return 1 if $role->does_role($role_name);
f7fec86c 399 }
47f36c05 400 }
ad958001 401
47f36c05 402 return 0;
403}
404
c3398f5b 4051;
c3398f5b 406__END__
407
408=head1 NAME
409
1820fffe 410Mouse::Meta::Class - The Mouse class metaclass
c3398f5b 411
a25ca8d6 412=head1 VERSION
413
6e168432 414This document describes Mouse version 0.40_05
a25ca8d6 415
c3398f5b 416=head1 METHODS
417
612d3e1a 418=head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
c3398f5b 419
31c5194b 420Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
306290e8 421one instance should exist for a given class.
c3398f5b 422
612d3e1a 423=head2 C<< name -> ClassName >>
c3398f5b 424
425Returns the name of the owner class.
426
612d3e1a 427=head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
c3398f5b 428
429Gets (or sets) the list of superclasses of the owner class.
430
31c5194b 431=head2 C<< add_method(name => CodeRef) >>
c3398f5b 432
31c5194b 433Adds a method to the owner class.
c3398f5b 434
31c5194b 435=head2 C<< has_method(name) -> Bool >>
72b88a88 436
31c5194b 437Returns whether we have a method with the given name.
72b88a88 438
31c5194b 439=head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
c68b4110 440
31c5194b 441Returns a L<Mouse::Meta::Method> with the given name.
442
443Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
444
445=head2 C<< get_method_list -> Names >>
446
447Returns a list of method names which are defined in the local class.
448If you want a list of all applicable methods for a class, use the
449C<get_all_methods> method.
450
451=head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
452
453Return the list of all L<Mouse::Meta::Method> instances associated with
454the class and its superclasses.
455
456=head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
457
458Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
459class.
c68b4110 460
612d3e1a 461=head2 C<< has_attribute(Name) -> Bool >>
66eea168 462
463Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
464
31c5194b 465=head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
c3398f5b 466
306290e8 467Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 468
31c5194b 469=head2 C<< get_attribute_list -> Names >>
470
471Returns a list of attribute names which are defined in the local
472class. If you want a list of all applicable attributes for a class,
473use the C<get_all_attributes> method.
474
475=head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
476
477Returns the list of all L<Mouse::Meta::Attribute> instances associated with
478this class and its superclasses.
479
612d3e1a 480=head2 C<< linearized_isa -> [ClassNames] >>
c3398f5b 481
482Returns the list of classes in method dispatch order, with duplicates removed.
483
612d3e1a 484=head2 C<< new_object(Parameters) -> Instance >>
2cea7a5f 485
612d3e1a 486Creates a new instance.
2cea7a5f 487
612d3e1a 488=head2 C<< clone_object(Instance, Parameters) -> Instance >>
f7b11a21 489
31c5194b 490Clones the given instance which must be an instance governed by this
f7b11a21 491metaclass.
492
31c5194b 493=head2 C<< throw_error(Message, Parameters) >>
494
495Throws an error with the given message.
496
1820fffe 497=head1 SEE ALSO
f7b11a21 498
1820fffe 499L<Moose::Meta::Class>
f7b11a21 500
31c5194b 501L<Class::MOP::Class>
502
c3398f5b 503=cut
504