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