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