2 package Moose::Meta::Method::Accessor;
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::Method',
13 'Class::MOP::Method::Accessor';
15 ## Inline method generators
17 sub generate_accessor_method_inline {
19 my $attr = $self->associated_attribute;
20 my $attr_name = $attr->name;
22 my $slot_access = $self->_inline_access($inv, $attr_name);
23 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
25 my $code = 'sub { ' . "\n"
26 . $self->_inline_pre_body(@_) . "\n"
27 . 'if (scalar(@_) == 2) {' . "\n"
28 . $self->_inline_copy_value . "\n"
29 . $self->_inline_check_required . "\n"
30 . $self->_inline_check_coercion . "\n"
31 . $self->_inline_check_constraint($value_name) . "\n"
32 . $self->_inline_store($inv, $value_name) . "\n"
33 . $self->_inline_trigger($inv, $value_name) . "\n"
35 . $self->_inline_check_lazy . "\n"
36 . $self->_inline_post_body(@_) . "\n"
37 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
41 # set up the environment
42 my $type_constraint = $attr->type_constraint
43 ? $attr->type_constraint->_compiled_type_constraint
47 confess "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@;
51 sub generate_writer_method_inline {
53 my $attr = $self->associated_attribute;
54 my $attr_name = $attr->name;
56 my $slot_access = $self->_inline_get($inv, $attr_name);
57 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
60 . $self->_inline_pre_body(@_)
61 . $self->_inline_copy_value
62 . $self->_inline_check_required
63 . $self->_inline_check_coercion
64 . $self->_inline_check_constraint($value_name)
65 . $self->_inline_store($inv, $value_name)
66 . $self->_inline_post_body(@_)
67 . $self->_inline_trigger($inv, $value_name)
71 # set up the environment
72 my $type_constraint = $attr->type_constraint
73 ? $attr->type_constraint->_compiled_type_constraint
77 confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
81 sub generate_reader_method_inline {
83 my $attr = $self->associated_attribute;
84 my $attr_name = $attr->name;
86 my $slot_access = $self->_inline_get($inv, $attr_name);
89 . $self->_inline_pre_body(@_)
90 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
91 . $self->_inline_check_lazy
92 . $self->_inline_post_body(@_)
93 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
97 # set up the environment
98 my $type_constraint = $attr->type_constraint
99 ? $attr->type_constraint->_compiled_type_constraint
102 my $sub = eval $code;
103 confess "Could not create reader for '$attr_name' because $@ \n code: $code" if $@;
107 sub _inline_copy_value {
108 return '' unless shift->_value_needs_copy;
109 return 'my $val = $_[1];'
112 sub _value_needs_copy {
113 my $attr = (shift)->associated_attribute;
114 return $attr->should_coerce;
117 sub generate_reader_method { shift->generate_reader_method_inline(@_) }
118 sub generate_writer_method { shift->generate_writer_method_inline(@_) }
119 sub generate_accessor_method { shift->generate_accessor_method_inline(@_) }
121 sub _inline_pre_body { '' }
122 sub _inline_post_body { '' }
124 sub _inline_check_constraint {
125 my ($self, $value) = @_;
127 my $attr = $self->associated_attribute;
129 return '' unless $attr->has_type_constraint;
132 # This sprintf is insanely annoying, we should
133 # fix it someday - SL
134 return sprintf <<'EOF', $value, $value, $value, $value, $value, $value, $value
135 defined($type_constraint->(%s))
136 || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("
137 . $attr->type_constraint->name . ") with "
138 . (defined(%s) ? (Scalar::Util::blessed(%s) && overload::Overloaded(%s) ? overload::StrVal(%s) : %s) : "undef")
143 sub _inline_check_coercion {
144 my $attr = (shift)->associated_attribute;
146 return '' unless $attr->should_coerce;
147 return '$val = $attr->type_constraint->coerce($_[1]);'
150 sub _inline_check_required {
151 my $attr = (shift)->associated_attribute;
153 return '' unless $attr->is_required;
154 return 'defined($_[1]) || confess "Attribute ($attr_name) is required, so cannot be set to undef";'
157 sub _inline_check_lazy {
159 my $attr = $self->associated_attribute;
161 return '' unless $attr->is_lazy;
164 my $slot_access = $self->_inline_access($inv, $attr->name);
166 my $slot_exists = $self->_inline_has($inv, $attr->name);
168 my $code = 'unless (' . $slot_exists . ') {' . "\n";
169 if ($attr->has_type_constraint) {
170 if($attr->has_default || $attr->has_builder){
171 if($attr->has_default){
172 $code .= ' my $default = $attr->default(' . $inv . ');'."\n";
173 } elsif($attr->has_builder){
174 $code .= ' my $default;'."\n".
175 ' if(my $builder = '.$inv.'->can($attr->builder)){ '."\n".
176 ' $default = '.$inv.'->$builder; '. "\n } else {\n" .
177 ' confess(Scalar::Util::blessed('.$inv.')." does not support builder method '.
178 '\'".$attr->builder."\' for attribute \'" . $attr->name . "\'");'. "\n }";
180 $code .= ' $default = $attr->type_constraint->coerce($default);'."\n" if $attr->should_coerce;
181 $code .= ' (defined($type_constraint->($default)))' .
182 ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' .
183 ' . $attr->type_constraint->name . ") with " . (defined($default) ? (Scalar::Util::blessed($default) && overload::Overloaded($default) ? overload::StrVal($default) : $default) : "undef")' .
184 ' if defined($default);' . "\n" .
185 ' ' . $slot_access . ' = $default; ' . "\n";
187 $code .= ' ' . $slot_access . " = undef; \n";
191 if($attr->has_default){
192 $code .= ' '.$slot_access.' = $attr->default(' . $inv . ');'."\n";
193 } elsif($attr->has_builder){
194 $code .= ' if(my $builder = '.$inv.'->can($attr->builder)){ '."\n".
195 ' '.$slot_access.' = '.$inv.'->$builder; '. "\n } else {\n" .
196 ' confess(Scalar::Util::blessed('.$inv.')." does not support builder method '.
197 '\'".$attr->builder."\' for attribute \'" . $attr->name . "\'");'. "\n }";
199 $code .= ' ' . $slot_access . " = undef; \n";
208 my ($self, $instance, $value) = @_;
209 my $attr = $self->associated_attribute;
211 my $mi = $attr->associated_class->get_meta_instance;
212 my $slot_name = sprintf "'%s'", $attr->slots;
214 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
215 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
216 if $attr->is_weak_ref;
220 sub _inline_trigger {
221 my ($self, $instance, $value) = @_;
222 my $attr = $self->associated_attribute;
223 return '' unless $attr->has_trigger;
224 return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
228 my ($self, $instance) = @_;
229 my $attr = $self->associated_attribute;
231 my $mi = $attr->associated_class->get_meta_instance;
232 my $slot_name = sprintf "'%s'", $attr->slots;
234 return $mi->inline_get_slot_value($instance, $slot_name);
238 my ($self, $instance) = @_;
239 my $attr = $self->associated_attribute;
241 my $mi = $attr->associated_class->get_meta_instance;
242 my $slot_name = sprintf "'%s'", $attr->slots;
244 return $mi->inline_slot_access($instance, $slot_name);
248 my ($self, $instance) = @_;
249 my $attr = $self->associated_attribute;
251 my $mi = $attr->associated_class->get_meta_instance;
252 my $slot_name = sprintf "'%s'", $attr->slots;
254 return $mi->inline_is_slot_initialized($instance, $slot_name);
257 sub _inline_auto_deref {
258 my ( $self, $ref_value ) = @_;
259 my $attr = $self->associated_attribute;
261 return $ref_value unless $attr->should_auto_deref;
263 my $type_constraint = $attr->type_constraint;
266 if ($type_constraint->is_a_type_of('ArrayRef')) {
269 elsif ($type_constraint->is_a_type_of('HashRef')) {
273 confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
276 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
287 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
291 This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
292 responsibility is to generate the accessor methods for attributes. It
293 can handle both closure based accessors, as well as inlined source based
296 This is a fairly new addition to the MOP, but this will play an important
297 role in the optimization strategy we are currently following.
303 =item B<generate_accessor_method>
305 =item B<generate_reader_method>
307 =item B<generate_writer_method>
309 =item B<generate_accessor_method_inline>
311 =item B<generate_reader_method_inline>
313 =item B<generate_writer_method_inline>
319 All complex software has bugs lurking in it, and this module is no
320 exception. If you find a bug please either email me, or add the bug
325 Stevan Little E<lt>stevan@iinteractive.comE<gt>
327 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
329 =head1 COPYRIGHT AND LICENSE
331 Copyright 2006, 2007 by Infinity Interactive, Inc.
333 L<http://www.iinteractive.com>
335 This library is free software; you can redistribute it and/or modify
336 it under the same terms as Perl itself.