inject empty DESTRUCT method for performance improvement.
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
306290e8 2package Mouse::Meta::Class;
c3398f5b 3use strict;
4use warnings;
5
fc1d8369 6use Mouse::Meta::Method::Constructor;
bbf64e76 7use Mouse::Meta::Method::Destructor;
8fcbe7fb 8use Mouse::Util qw/get_linear_isa blessed/;
7a59f4e8 9use Carp 'confess';
72b88a88 10
c3398f5b 11do {
12 my %METACLASS_CACHE;
72b88a88 13
14 # because Mouse doesn't introspect existing classes, we're forced to
15 # only pay attention to other Mouse classes
16 sub _metaclass_cache {
17 my $class = shift;
18 my $name = shift;
19 return $METACLASS_CACHE{$name};
20 }
21
c3398f5b 22 sub initialize {
23 my $class = shift;
24 my $name = shift;
25 $METACLASS_CACHE{$name} = $class->new(name => $name)
26 if !exists($METACLASS_CACHE{$name});
27 return $METACLASS_CACHE{$name};
28 }
29};
30
31sub new {
32 my $class = shift;
6cbacbf6 33 my %args = @_;
c3398f5b 34
35 $args{attributes} = {};
36 $args{superclasses} = do {
37 no strict 'refs';
38 \@{ $args{name} . '::ISA' };
39 };
47f36c05 40 $args{roles} ||= [];
c3398f5b 41
42 bless \%args, $class;
43}
44
45sub name { $_[0]->{name} }
46
47sub superclasses {
48 my $self = shift;
49
50 if (@_) {
51 Mouse::load_class($_) for @_;
52 @{ $self->{superclasses} } = @_;
53 }
54
55 @{ $self->{superclasses} };
56}
57
d16fe7d7 58sub add_method {
59 my $self = shift;
60 my $name = shift;
61 my $code = shift;
62
63 my $pkg = $self->name;
64
65 no strict 'refs';
66 *{ $pkg . '::' . $name } = $code;
67}
68
2e92bb89 69# copied from Class::Inspector
70sub get_method_list {
71 my $self = shift;
72 my $name = $self->name;
73
74 no strict 'refs';
75 # Get all the CODE symbol table entries
76 my @functions = grep !/^meta$/,
77 grep { /\A[^\W\d]\w*\z/o }
78 grep { defined &{"${name}::$_"} }
79 keys %{"${name}::"};
80 wantarray ? @functions : \@functions;
81}
82
c3398f5b 83sub add_attribute {
84 my $self = shift;
85 my $attr = shift;
86
87 $self->{'attributes'}{$attr->name} = $attr;
88}
89
72b88a88 90sub compute_all_applicable_attributes {
91 my $self = shift;
92 my (@attr, %seen);
93
94 for my $class ($self->linearized_isa) {
95 my $meta = $self->_metaclass_cache($class)
96 or next;
97
98 for my $name (keys %{ $meta->get_attribute_map }) {
99 next if $seen{$name}++;
100 push @attr, $meta->get_attribute($name);
101 }
102 }
103
104 return @attr;
105}
106
c3398f5b 107sub get_attribute_map { $_[0]->{attributes} }
66eea168 108sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
c3398f5b 109sub get_attribute { $_[0]->{attributes}->{$_[1]} }
110
00ca1c62 111sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
c3398f5b 112
7a59f4e8 113sub clone_object {
114 my $class = shift;
115 my $instance = shift;
116
117 (blessed($instance) && $instance->isa($class->name))
1a0f0802 118 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
7a59f4e8 119
120 $class->clone_instance($instance, @_);
121}
122
123sub clone_instance {
124 my ($class, $instance, %params) = @_;
125
126 (blessed($instance))
e42bee44 127 || confess "You can only clone instances, ($instance) is not a blessed instance";
7a59f4e8 128
129 my $clone = bless { %$instance }, ref $instance;
130
131 foreach my $attr ($class->compute_all_applicable_attributes()) {
132 if ( defined( my $init_arg = $attr->init_arg ) ) {
133 if (exists $params{$init_arg}) {
134 $clone->{ $attr->name } = $params{$init_arg};
135 }
136 }
137 }
138
139 return $clone;
140
141}
142
fc1d8369 143sub make_immutable {
144 my $self = shift;
145 my $name = $self->name;
146 $self->{is_immutable}++;
147 no strict 'refs';
bbf64e76 148 *{"$name\::new"} = Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self );
8da68341 149
150 my $destructor = Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self );
151 if ($destructor) {
152 *{"$name\::DESTROY"} = $destructor;
153 }
fc1d8369 154}
155sub make_mutable {
156 Carp::croak "Mouse::Meta::Class->make_mutable does not supported by Mouse";
157}
158sub is_immutable { $_[0]->{is_immutable} }
84ef660f 159
160sub attribute_metaclass { "Mouse::Meta::Class" }
7a59f4e8 161
50dc6ee5 162sub add_before_method_modifier {
163 my ($self, $name, $code) = @_;
4e31595c 164 require Class::Method::Modifiers;
50dc6ee5 165 Class::Method::Modifiers::_install_modifier(
166 $self->name,
167 'before',
168 $name,
169 $code,
170 );
171}
172
173sub add_around_method_modifier {
174 my ($self, $name, $code) = @_;
4e31595c 175 require Class::Method::Modifiers;
50dc6ee5 176 Class::Method::Modifiers::_install_modifier(
177 $self->name,
178 'around',
179 $name,
180 $code,
181 );
182}
183
184sub add_after_method_modifier {
185 my ($self, $name, $code) = @_;
4e31595c 186 require Class::Method::Modifiers;
50dc6ee5 187 Class::Method::Modifiers::_install_modifier(
188 $self->name,
189 'after',
190 $name,
191 $code,
192 );
193}
194
47f36c05 195sub roles { $_[0]->{roles} }
196
197sub does_role {
198 my ($self, $role_name) = @_;
199 (defined $role_name)
200 || confess "You must supply a role name to look for";
201 for my $role (@{ $self->{roles} }) {
202 return 1 if $role->name eq $role_name;
203 }
204 return 0;
205}
206
ae3edb8a 207sub create {
208 my ( $class, @args ) = @_;
209
210 unshift @args, 'package' if @args % 2 == 1;
211
212 my (%options) = @args;
213 my $package_name = $options{package};
214
215 (ref $options{superclasses} eq 'ARRAY')
216 || confess "You must pass an ARRAY ref of superclasses"
217 if exists $options{superclasses};
218
219 (ref $options{attributes} eq 'ARRAY')
220 || confess "You must pass an ARRAY ref of attributes"
221 if exists $options{attributes};
222
223 (ref $options{methods} eq 'HASH')
224 || confess "You must pass a HASH ref of methods"
225 if exists $options{methods};
226
227 do {
228 # XXX should I implement Mouse::Meta::Module?
229 my $package_name = $options{package};
230
231 ( defined $package_name && $package_name )
232 || confess "You must pass a package name";
233
234 my $code = "package $package_name;";
235 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
236 if exists $options{version};
237 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
238 if exists $options{authority};
239
240 eval $code;
241 confess "creation of $package_name failed : $@" if $@;
242 };
243
244 my (%initialize_options) = @args;
245 delete @initialize_options{qw(
246 package
247 superclasses
248 attributes
249 methods
250 version
251 authority
252 )};
253 my $meta = $class->initialize( $package_name => %initialize_options );
254
255 # FIXME totally lame
256 $meta->add_method('meta' => sub {
257 $class->initialize(ref($_[0]) || $_[0]);
258 });
259
260 $meta->superclasses(@{$options{superclasses}})
261 if exists $options{superclasses};
262 # NOTE:
263 # process attributes first, so that they can
264 # install accessors, but locally defined methods
265 # can then overwrite them. It is maybe a little odd, but
266 # I think this should be the order of things.
267 if (exists $options{attributes}) {
268 foreach my $attr (@{$options{attributes}}) {
269 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
270 }
271 }
272 if (exists $options{methods}) {
273 foreach my $method_name (keys %{$options{methods}}) {
274 $meta->add_method($method_name, $options{methods}->{$method_name});
275 }
276 }
277 return $meta;
278}
279
280{
281 my $ANON_CLASS_SERIAL = 0;
282 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
283 sub create_anon_class {
284 my ( $class, %options ) = @_;
285 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
286 return $class->create( $package_name, %options );
287 }
288}
289
c3398f5b 2901;
291
292__END__
293
294=head1 NAME
295
306290e8 296Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 297
298=head1 METHODS
299
306290e8 300=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 301
306290e8 302Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
303one instance should exist for a given class.
c3398f5b 304
306290e8 305=head2 new %args -> Mouse::Meta::Class
c3398f5b 306
306290e8 307Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 308
309=head2 name -> ClassName
310
311Returns the name of the owner class.
312
313=head2 superclasses -> [ClassName]
314
315Gets (or sets) the list of superclasses of the owner class.
316
306290e8 317=head2 add_attribute Mouse::Meta::Attribute
c3398f5b 318
306290e8 319Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
320class.
c3398f5b 321
72b88a88 322=head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
323
324Returns the list of all L<Mouse::Meta::Attribute> instances associated with
325this class and its superclasses.
326
306290e8 327=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 328
329Returns a mapping of attribute names to their corresponding
306290e8 330L<Mouse::Meta::Attribute> objects.
c3398f5b 331
66eea168 332=head2 has_attribute Name -> Boool
333
334Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
335
306290e8 336=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 337
306290e8 338Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 339
340=head2 linearized_isa -> [ClassNames]
341
342Returns the list of classes in method dispatch order, with duplicates removed.
343
f7b11a21 344=head2 clone_object Instance -> Instance
345
346Clones the given C<Instance> which must be an instance governed by this
347metaclass.
348
349=head2 clone_instance Instance, Parameters -> Instance
350
351Clones the given C<Instance> and sets any additional parameters.
352
c3398f5b 353=cut
354