bump all the versions to 0.76
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Constructor.pm
CommitLineData
d90b42a6 1
2package Class::MOP::Method::Constructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
9
5d10c516 10our $VERSION = '0.76';
d519662a 11$VERSION = eval $VERSION;
d90b42a6 12our $AUTHORITY = 'cpan:STEVAN';
13
565f0cbb 14use base 'Class::MOP::Method::Generated';
d90b42a6 15
16sub new {
17 my $class = shift;
18 my %options = @_;
8d2d4c67 19
ad315b75 20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
8d2d4c67 22 if $options{is_inline};
23
b38f3848 24 ($options{package_name} && $options{name})
32202ce2 25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 26
0bfc85b8 27 my $self = $class->_new(\%options);
d90b42a6 28
8d2d4c67 29 # we don't want this creating
30 # a cycle in the code, if not
d90b42a6 31 # needed
8683db0e 32 weaken($self->{'associated_metaclass'});
d90b42a6 33
565f0cbb 34 $self->initialize_body;
d90b42a6 35
8d2d4c67 36 return $self;
d90b42a6 37}
38
28b97bef 39sub _new {
0bfc85b8 40 my $class = shift;
41 my $options = @_ == 1 ? $_[0] : {@_};
28b97bef 42
43 bless {
44 # from our superclass
45 'body' => undef,
0bfc85b8 46 'package_name' => $options->{package_name},
47 'name' => $options->{name},
28b97bef 48 # specific to this subclass
0bfc85b8 49 'options' => $options->{options} || {},
50 'associated_metaclass' => $options->{metaclass},
51 'is_inline' => ($options->{is_inline} || 0),
28b97bef 52 }, $class;
53}
54
f0de47d9 55sub can_be_inlined { 1 }
56
8d2d4c67 57## accessors
c23184fc 58
8683db0e 59sub options { (shift)->{'options'} }
60sub associated_metaclass { (shift)->{'associated_metaclass'} }
c23184fc 61
565f0cbb 62## cached values ...
d90b42a6 63
8d2d4c67 64sub meta_instance {
565f0cbb 65 my $self = shift;
8683db0e 66 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
565f0cbb 67}
c23184fc 68
8d2d4c67 69sub attributes {
565f0cbb 70 my $self = shift;
8683db0e 71 $self->{'attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
565f0cbb 72}
d90b42a6 73
74## method
75
565f0cbb 76sub initialize_body {
77 my $self = shift;
78 my $method_name = 'generate_constructor_method';
8d2d4c67 79
565f0cbb 80 $method_name .= '_inline' if $self->is_inline;
8d2d4c67 81
8683db0e 82 $self->{'body'} = $self->$method_name;
565f0cbb 83}
84
85sub generate_constructor_method {
2a2b8458 86 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
565f0cbb 87}
88
89sub generate_constructor_method_inline {
d90b42a6 90 my $self = shift;
565f0cbb 91
0c6f3280 92 my $close_over = {};
93
d90b42a6 94 my $source = 'sub {';
26ffbb36 95 $source .= "\n" . 'my $class = shift;';
8d2d4c67 96
26ffbb36 97 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
8d2d4c67 98 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
99
26ffbb36 100 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
101
d90b42a6 102 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
8d2d4c67 103 $source .= ";\n" . (join ";\n" => map {
82cff1ae 104 $self->_generate_slot_initializer($_, $close_over)
d90b42a6 105 } 0 .. (@{$self->attributes} - 1));
106 $source .= ";\n" . 'return $instance';
8d2d4c67 107 $source .= ";\n" . '}';
108 warn $source if $self->options->{debug};
109
d90b42a6 110 my $code;
111 {
112 # NOTE:
113 # create the nessecary lexicals
8d2d4c67 114 # to be picked up in the eval
8d2d4c67 115
7f8de9b4 116 $code = $self->_eval_closure(
82cff1ae 117 $close_over,
7f8de9b4 118 $source
119 );
d90b42a6 120 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
121 }
565f0cbb 122 return $code;
d90b42a6 123}
124
125sub _generate_slot_initializer {
126 my $self = shift;
127 my $index = shift;
82cff1ae 128 my $close = shift;
8d2d4c67 129
d90b42a6 130 my $attr = $self->attributes->[$index];
8d2d4c67 131
d90b42a6 132 my $default;
133 if ($attr->has_default) {
134 # NOTE:
135 # default values can either be CODE refs
8d2d4c67 136 # in which case we need to call them. Or
d90b42a6 137 # they can be scalars (strings/numbers)
138 # in which case we can just deal with them
139 # in the code we eval.
140 if ($attr->is_default_a_coderef) {
82cff1ae 141 my $idx = @{$close->{'@defaults'}||=[]};
142 push(@{$close->{'@defaults'}}, $attr->default);
143 $default = '$defaults[' . $idx . ']->($instance)';
d90b42a6 144 }
145 else {
146 $default = $attr->default;
147 # make sure to quote strings ...
148 unless (looks_like_number($default)) {
149 $default = "'$default'";
150 }
151 }
8d2d4c67 152 } elsif( $attr->has_builder ) {
153 $default = '$instance->'.$attr->builder;
d90b42a6 154 }
8d2d4c67 155
c16c9c1b 156 if ( defined $attr->init_arg ) {
157 return (
26ffbb36 158 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" .
c16c9c1b 159 $self->meta_instance->inline_set_slot_value(
160 '$instance',
161 ("'" . $attr->name . "'"),
26ffbb36 162 '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" .
c16c9c1b 163 '} ' . (!defined $default ? '' : 'else {' . "\n" .
164 $self->meta_instance->inline_set_slot_value(
165 '$instance',
166 ("'" . $attr->name . "'"),
167 $default ) . "\n" .
168 '}')
169 );
170 } elsif ( defined $default ) {
171 return (
172 $self->meta_instance->inline_set_slot_value(
173 '$instance',
174 ("'" . $attr->name . "'"),
175 $default ) . "\n"
176 );
177 } else { return '' }
d90b42a6 178}
179
1801;
181
d90b42a6 182__END__
183
184=pod
185
8d2d4c67 186=head1 NAME
d90b42a6 187
188Class::MOP::Method::Constructor - Method Meta Object for constructors
189
190=head1 SYNOPSIS
191
96e38ba6 192 use Class::MOP::Method::Constructor;
8d2d4c67 193
96e38ba6 194 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 195 metaclass => $metaclass,
96e38ba6 196 options => {
197 debug => 1, # this is all for now
8d2d4c67 198 },
96e38ba6 199 );
8d2d4c67 200
96e38ba6 201 # calling the constructor ...
b7045e66 202 $constructor->body->execute($metaclass->name, %params);
8d2d4c67 203
d90b42a6 204=head1 DESCRIPTION
205
8d2d4c67 206This is a subclass of C<Class::MOP::Method> which deals with
127d39a7 207class constructors. This is used when making a class immutable
208to generate an optimized constructor.
96e38ba6 209
d90b42a6 210=head1 METHODS
211
212=over 4
213
96e38ba6 214=item B<new (metaclass => $meta, options => \%options)>
d90b42a6 215
96e38ba6 216=item B<options>
217
218This returns the options HASH which is passed into C<new>.
219
220=item B<associated_metaclass>
221
222This returns the metaclass which is passed into C<new>.
c23184fc 223
d90b42a6 224=item B<attributes>
225
8d2d4c67 226This returns the list of attributes which are associated with the
96e38ba6 227metaclass which is passed into C<new>.
228
d90b42a6 229=item B<meta_instance>
230
8d2d4c67 231This returns the meta instance which is associated with the
96e38ba6 232metaclass which is passed into C<new>.
c23184fc 233
96e38ba6 234=item B<is_inline>
235
8d2d4c67 236This returns a boolean, but since constructors are very rarely
96e38ba6 237not inlined, this always returns true for now.
d90b42a6 238
f0de47d9 239=item B<can_be_inlined>
240
241This method always returns true in this class. It exists so that
242subclasses (like in Moose) can override and do some sort of checking
243to determine whether or not inlining the constructor is safe.
244
565f0cbb 245=item B<initialize_body>
d90b42a6 246
8d2d4c67 247This creates the code reference for the constructor itself.
96e38ba6 248
d90b42a6 249=back
250
127d39a7 251=head2 Method Generators
565f0cbb 252
253=over 4
254
255=item B<generate_constructor_method>
256
257=item B<generate_constructor_method_inline>
258
259=back
260
d90b42a6 261=head1 AUTHORS
262
263Stevan Little E<lt>stevan@iinteractive.comE<gt>
264
265=head1 COPYRIGHT AND LICENSE
266
69e3ab0a 267Copyright 2006-2008 by Infinity Interactive, Inc.
d90b42a6 268
269L<http://www.iinteractive.com>
270
271This library is free software; you can redistribute it and/or modify
8d2d4c67 272it under the same terms as Perl itself.
d90b42a6 273
274=cut
275