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 = $attr->should_coerce ? '$val' : '$_[1]';
25 my $code = 'sub { ' . "\n"
26 . $self->_inline_pre_body(@_) . "\n"
27 . 'if (scalar(@_) == 2) {' . "\n"
28 . $self->_inline_check_required . "\n"
29 . $self->_inline_check_coercion . "\n"
30 . $self->_inline_check_constraint($value_name) . "\n"
31 . $self->_inline_store($inv, $value_name) . "\n"
32 . $self->_inline_trigger($inv, $value_name) . "\n"
34 . $self->_inline_check_lazy . "\n"
35 . $self->_inline_post_body(@_) . "\n"
36 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
40 # set up the environment
41 my $type_constraint = $attr->type_constraint
42 ? $attr->type_constraint->_compiled_type_constraint
46 confess "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@;
50 sub generate_writer_method_inline {
52 my $attr = $self->associated_attribute;
53 my $attr_name = $attr->name;
55 my $slot_access = $self->_inline_get($inv, $attr_name);
56 my $value_name = $attr->should_coerce ? '$val' : '$_[1]';
59 . $self->_inline_pre_body(@_)
60 . $self->_inline_check_required
61 . $self->_inline_check_coercion
62 . $self->_inline_check_constraint($value_name)
63 . $self->_inline_store($inv, $value_name)
64 . $self->_inline_post_body(@_)
65 . $self->_inline_trigger($inv, $value_name)
69 # set up the environment
70 my $type_constraint = $attr->type_constraint
71 ? $attr->type_constraint->_compiled_type_constraint
75 confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
79 sub generate_reader_method_inline {
81 my $attr = $self->associated_attribute;
82 my $attr_name = $attr->name;
84 my $slot_access = $self->_inline_get($inv, $attr_name);
87 . $self->_inline_pre_body(@_)
88 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
89 . $self->_inline_check_lazy
90 . $self->_inline_post_body(@_)
91 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
95 # set up the environment
96 my $type_constraint = $attr->type_constraint
97 ? $attr->type_constraint->_compiled_type_constraint
100 my $sub = eval $code;
101 confess "Could not create reader for '$attr_name' because $@ \n code: $code" if $@;
105 sub generate_reader_method { shift->generate_reader_method_inline(@_) }
106 sub generate_writer_method { shift->generate_writer_method_inline(@_) }
107 sub generate_accessor_method { shift->generate_accessor_method_inline(@_) }
109 sub _inline_pre_body { '' }
110 sub _inline_post_body { '' }
112 sub _inline_check_constraint {
113 my ($self, $value) = @_;
115 my $attr = $self->associated_attribute;
117 return '' unless $attr->has_type_constraint;
120 # This sprintf is insanely annoying, we should
121 # fix it someday - SL
122 return sprintf <<'EOF', $value, $value, $value, $value, $value, $value, $value
123 defined($type_constraint->(%s))
124 || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("
125 . $attr->type_constraint->name . ") with "
126 . (defined(%s) ? (Scalar::Util::blessed(%s) && overload::Overloaded(%s) ? overload::StrVal(%s) : %s) : "undef")
131 sub _inline_check_coercion {
132 my $attr = (shift)->associated_attribute;
134 return '' unless $attr->should_coerce;
135 return 'my $val = $attr->type_constraint->coerce($_[1]);'
138 sub _inline_check_required {
139 my $attr = (shift)->associated_attribute;
141 return '' unless $attr->is_required;
142 return 'defined($_[1]) || confess "Attribute ($attr_name) is required, so cannot be set to undef";'
145 sub _inline_check_lazy {
147 my $attr = $self->associated_attribute;
149 return '' unless $attr->is_lazy;
152 my $slot_access = $self->_inline_access($inv, $attr->name);
153 my $slot_exists = $self->_inline_has($inv, $attr->name);
154 if ($attr->has_type_constraint) {
156 # this could probably be cleaned
157 # up and streamlined a little more
158 return 'unless (' . $slot_exists . ') {' .
159 ' if ($attr->has_default || $attr->has_builder ) {' .
161 ' $default = $attr->default(' . $inv . ') if $attr->has_default;' .
162 ' if ( $attr->has_builder ) { '.
163 ' my $builder = $self->builder;'.
164 ' if($builder = '.$inv.'->can($builder)){ '.
165 ' $default = '.$inv.'->$builder; '.
167 ' confess(blessed('.$inv.')." does not support builder method \'$builder\' for attribute \'" . $attr->name . "\'");'.
170 ($attr->should_coerce
171 ? '$default = $attr->type_constraint->coerce($default);'
173 ' (defined($type_constraint->($default)))' .
174 ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' .
175 ' . $attr->type_constraint->name . ") with " . (defined($default) ? (Scalar::Util::blessed($default) && overload::Overloaded($default) ? overload::StrVal($default) : $default) : "undef")' .
176 ' if defined($default);' .
177 ' ' . $slot_access . ' = $default; ' .
180 ' ' . $slot_access . ' = undef;' .
185 return 'unless ( ' . $slot_exists . ') {' .
186 ' if ($attr->has_default) { ' . $slot_access . ' = $attr->default(' . $inv . '); }' .
187 ' elsif ($attr->has_builder) { '.
188 ' my $builder = $attr->builder; ' .
189 ' if($builder = '.$inv.'->can($builder)){ '.
190 ' ' . $slot_access . ' = ' . $inv . '->$builder; '.
192 ' confess(blessed('.$inv.')." does not support builder method \'$builder\' for attribute \'" . $attr->name . "\'");'.
194 ' } else { ' .$slot_access . ' = undef; } '.
200 my ($self, $instance, $value) = @_;
201 my $attr = $self->associated_attribute;
203 my $mi = $attr->associated_class->get_meta_instance;
204 my $slot_name = sprintf "'%s'", $attr->slots;
206 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
207 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
208 if $attr->is_weak_ref;
212 sub _inline_trigger {
213 my ($self, $instance, $value) = @_;
214 my $attr = $self->associated_attribute;
215 return '' unless $attr->has_trigger;
216 return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
220 my ($self, $instance) = @_;
221 my $attr = $self->associated_attribute;
223 my $mi = $attr->associated_class->get_meta_instance;
224 my $slot_name = sprintf "'%s'", $attr->slots;
226 return $mi->inline_get_slot_value($instance, $slot_name);
230 my ($self, $instance) = @_;
231 my $attr = $self->associated_attribute;
233 my $mi = $attr->associated_class->get_meta_instance;
234 my $slot_name = sprintf "'%s'", $attr->slots;
236 return $mi->inline_slot_access($instance, $slot_name);
240 my ($self, $instance) = @_;
241 my $attr = $self->associated_attribute;
243 my $mi = $attr->associated_class->get_meta_instance;
244 my $slot_name = sprintf "'%s'", $attr->slots;
246 return $mi->inline_is_slot_initialized($instance, $slot_name);
249 sub _inline_auto_deref {
250 my ( $self, $ref_value ) = @_;
251 my $attr = $self->associated_attribute;
253 return $ref_value unless $attr->should_auto_deref;
255 my $type_constraint = $attr->type_constraint;
258 if ($type_constraint->is_a_type_of('ArrayRef')) {
261 elsif ($type_constraint->is_a_type_of('HashRef')) {
265 confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
268 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
279 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
283 This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
284 responsibility is to generate the accessor methods for attributes. It
285 can handle both closure based accessors, as well as inlined source based
288 This is a fairly new addition to the MOP, but this will play an important
289 role in the optimization strategy we are currently following.
295 =item B<generate_accessor_method>
297 =item B<generate_reader_method>
299 =item B<generate_writer_method>
301 =item B<generate_accessor_method_inline>
303 =item B<generate_reader_method_inline>
305 =item B<generate_writer_method_inline>
311 All complex software has bugs lurking in it, and this module is no
312 exception. If you find a bug please either email me, or add the bug
317 Stevan Little E<lt>stevan@iinteractive.comE<gt>
319 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
321 =head1 COPYRIGHT AND LICENSE
323 Copyright 2006, 2007 by Infinity Interactive, Inc.
325 L<http://www.iinteractive.com>
327 This library is free software; you can redistribute it and/or modify
328 it under the same terms as Perl itself.