Mouse:::Meta::Method::Constructor is used only in pure perl mode
[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
7efbc77d 13sub constructor_class(); # XS
380e1cd7 14sub destructor_class() { 'Mouse::Meta::Method::Destructor' }
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}) {
380e1cd7 202 $self->add_method($args{constructor_name} =>
203 $self->constructor_class->_generate_constructor($self, \%args));
c7a6403f 204 }
205
8632b6fe 206 if ($args{inline_destructor}) {
380e1cd7 207 $self->add_method(DESTROY =>
208 $self->destructor_class->_generate_destructor($self, \%args));
8632b6fe 209 }
2276cb14 210
211 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
212 # at the end of a source file.
213 return 1;
fc1d8369 214}
ad958001 215
fce211ae 216sub make_mutable { not_supported }
ad958001 217
6cfa1e5e 218sub is_immutable { $_[0]->{is_immutable} }
219sub is_mutable { !$_[0]->{is_immutable} }
84ef660f 220
3fbade18 221sub _install_modifier_pp{
8d59c723 222 my( $self, $type, $name, $code ) = @_;
223 my $into = $self->name;
3fbade18 224
225 my $original = $into->can($name)
226 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
227
228 my $modifier_table = $self->{modifiers}{$name};
229
230 if(!$modifier_table){
231 my(@before, @after, @around, $cache, $modified);
232
233 $cache = $original;
234
235 $modified = sub {
236 for my $c (@before) { $c->(@_) }
237
238 if(wantarray){ # list context
239 my @rval = $cache->(@_);
240
241 for my $c(@after){ $c->(@_) }
242 return @rval;
243 }
244 elsif(defined wantarray){ # scalar context
245 my $rval = $cache->(@_);
246
247 for my $c(@after){ $c->(@_) }
248 return $rval;
249 }
250 else{ # void context
251 $cache->(@_);
252
253 for my $c(@after){ $c->(@_) }
254 return;
255 }
256 };
257
258 $self->{modifiers}{$name} = $modifier_table = {
259 original => $original,
260
261 before => \@before,
262 after => \@after,
263 around => \@around,
264
265 cache => \$cache, # cache for around modifiers
266 };
267
268 $self->add_method($name => $modified);
269 }
270
271 if($type eq 'before'){
272 unshift @{$modifier_table->{before}}, $code;
273 }
274 elsif($type eq 'after'){
275 push @{$modifier_table->{after}}, $code;
276 }
277 else{ # around
278 push @{$modifier_table->{around}}, $code;
279
280 my $next = ${ $modifier_table->{cache} };
281 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
282 }
283
284 return;
285}
286
4859d490 287sub _install_modifier {
8d59c723 288 my ( $self, $type, $name, $code ) = @_;
4f5b44a0 289
3fbade18 290 # load Class::Method::Modifiers first
7ca5c5fb 291 my $no_cmm_fast = do{
3fbade18 292 local $@;
293 eval q{ require Class::Method::Modifiers::Fast };
294 $@;
4f5b44a0 295 };
4f5b44a0 296
3fbade18 297 my $impl;
298 if($no_cmm_fast){
299 $impl = \&_install_modifier_pp;
300 }
301 else{
302 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
303 $impl = sub {
8d59c723 304 my ( $self, $type, $name, $code ) = @_;
305 my $into = $self->name;
95ecd6f1 306 $install_modifier->($into, $type, $name, $code);
307
8d59c723 308 $self->add_method($name => do{
309 no strict 'refs';
310 \&{ $into . '::' . $name };
311 });
6cfa1e5e 312 return;
4f5b44a0 313 };
1b79a118 314 }
4f5b44a0 315
3fbade18 316 # replace this method itself :)
317 {
318 no warnings 'redefine';
319 *_install_modifier = $impl;
320 }
321
8d59c723 322 $self->$impl( $type, $name, $code );
4859d490 323}
324
50dc6ee5 325sub add_before_method_modifier {
4859d490 326 my ( $self, $name, $code ) = @_;
8d59c723 327 $self->_install_modifier( 'before', $name, $code );
50dc6ee5 328}
329
330sub add_around_method_modifier {
4859d490 331 my ( $self, $name, $code ) = @_;
8d59c723 332 $self->_install_modifier( 'around', $name, $code );
50dc6ee5 333}
334
335sub add_after_method_modifier {
4859d490 336 my ( $self, $name, $code ) = @_;
8d59c723 337 $self->_install_modifier( 'after', $name, $code );
50dc6ee5 338}
339
67199842 340sub add_override_method_modifier {
341 my ($self, $name, $code) = @_;
342
768804c0 343 if($self->has_method($name)){
344 $self->throw_error("Cannot add an override method if a local method is already present");
85bd3f44 345 }
346
6cfa1e5e 347 my $package = $self->name;
67199842 348
85bd3f44 349 my $super_body = $package->can($name)
6cfa1e5e 350 or $self->throw_error("You cannot override '$name' because it has no super method");
67199842 351
85bd3f44 352 $self->add_method($name => sub {
353 local $Mouse::SUPER_PACKAGE = $package;
354 local $Mouse::SUPER_BODY = $super_body;
355 local @Mouse::SUPER_ARGS = @_;
356
357 $code->(@_);
358 });
359 return;
67199842 360}
361
768804c0 362sub add_augment_method_modifier {
363 my ($self, $name, $code) = @_;
364 if($self->has_method($name)){
365 $self->throw_error("Cannot add an augment method if a local method is already present");
366 }
367
368 my $super = $self->find_method_by_name($name)
369 or $self->throw_error("You cannot augment '$name' because it has no super method");
370
371 my $super_package = $super->package_name;
372 my $super_body = $super->body;
373
374 $self->add_method($name => sub{
375 local $Mouse::INNER_BODY{$super_package} = $code;
376 local $Mouse::INNER_ARGS{$super_package} = [@_];
377 $super_body->(@_);
378 });
379 return;
380}
381
47f36c05 382sub does_role {
383 my ($self, $role_name) = @_;
ad958001 384
47f36c05 385 (defined $role_name)
fce211ae 386 || $self->throw_error("You must supply a role name to look for");
ad958001 387
f7fec86c 388 for my $class ($self->linearized_isa) {
95ecd6f1 389 my $meta = Mouse::Util::get_metaclass_by_name($class)
390 or next;
3a63a2e7 391
392 for my $role (@{ $meta->roles }) {
ff687069 393
3a63a2e7 394 return 1 if $role->does_role($role_name);
f7fec86c 395 }
47f36c05 396 }
ad958001 397
47f36c05 398 return 0;
399}
400
c3398f5b 4011;
c3398f5b 402__END__
403
404=head1 NAME
405
1820fffe 406Mouse::Meta::Class - The Mouse class metaclass
c3398f5b 407
a25ca8d6 408=head1 VERSION
409
6e168432 410This document describes Mouse version 0.40_05
a25ca8d6 411
c3398f5b 412=head1 METHODS
413
612d3e1a 414=head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
c3398f5b 415
31c5194b 416Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
306290e8 417one instance should exist for a given class.
c3398f5b 418
612d3e1a 419=head2 C<< name -> ClassName >>
c3398f5b 420
421Returns the name of the owner class.
422
612d3e1a 423=head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
c3398f5b 424
425Gets (or sets) the list of superclasses of the owner class.
426
31c5194b 427=head2 C<< add_method(name => CodeRef) >>
c3398f5b 428
31c5194b 429Adds a method to the owner class.
c3398f5b 430
31c5194b 431=head2 C<< has_method(name) -> Bool >>
72b88a88 432
31c5194b 433Returns whether we have a method with the given name.
72b88a88 434
31c5194b 435=head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
c68b4110 436
31c5194b 437Returns a L<Mouse::Meta::Method> with the given name.
438
439Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
440
441=head2 C<< get_method_list -> Names >>
442
443Returns a list of method names which are defined in the local class.
444If you want a list of all applicable methods for a class, use the
445C<get_all_methods> method.
446
447=head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
448
449Return the list of all L<Mouse::Meta::Method> instances associated with
450the class and its superclasses.
451
452=head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
453
454Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
455class.
c68b4110 456
612d3e1a 457=head2 C<< has_attribute(Name) -> Bool >>
66eea168 458
459Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
460
31c5194b 461=head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
c3398f5b 462
306290e8 463Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 464
31c5194b 465=head2 C<< get_attribute_list -> Names >>
466
467Returns a list of attribute names which are defined in the local
468class. If you want a list of all applicable attributes for a class,
469use the C<get_all_attributes> method.
470
471=head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
472
473Returns the list of all L<Mouse::Meta::Attribute> instances associated with
474this class and its superclasses.
475
612d3e1a 476=head2 C<< linearized_isa -> [ClassNames] >>
c3398f5b 477
478Returns the list of classes in method dispatch order, with duplicates removed.
479
612d3e1a 480=head2 C<< new_object(Parameters) -> Instance >>
2cea7a5f 481
612d3e1a 482Creates a new instance.
2cea7a5f 483
612d3e1a 484=head2 C<< clone_object(Instance, Parameters) -> Instance >>
f7b11a21 485
31c5194b 486Clones the given instance which must be an instance governed by this
f7b11a21 487metaclass.
488
31c5194b 489=head2 C<< throw_error(Message, Parameters) >>
490
491Throws an error with the given message.
492
1820fffe 493=head1 SEE ALSO
f7b11a21 494
1820fffe 495L<Moose::Meta::Class>
f7b11a21 496
31c5194b 497L<Class::MOP::Class>
498
c3398f5b 499=cut
500