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
18 my ( $self, $code ) = @_;
21 # set up the environment
22 my $attr = $self->associated_attribute;
23 my $attr_name = $attr->name;
25 my $type_constraint_obj = $attr->type_constraint;
26 my $type_constraint_name = $type_constraint_obj && $type_constraint_obj->name;
27 my $type_constraint = $type_constraint_obj
28 ? $type_constraint_obj->_compiled_type_constraint
31 #warn "code for $attr_name =>\n" . $code . "\n";
33 confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
38 sub generate_accessor_method_inline {
40 my $attr = $self->associated_attribute;
41 my $attr_name = $attr->name;
43 my $slot_access = $self->_inline_access($inv, $attr_name);
44 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
46 $self->_eval_code('sub { ' . "\n"
47 . $self->_inline_pre_body(@_) . "\n"
48 . 'if (scalar(@_) >= 2) {' . "\n"
49 . $self->_inline_copy_value . "\n"
50 . $self->_inline_check_required . "\n"
51 . $self->_inline_check_coercion . "\n"
52 . $self->_inline_check_constraint($value_name) . "\n"
53 . $self->_inline_store($inv, $value_name) . "\n"
54 . $self->_inline_trigger($inv, $value_name) . "\n"
56 . $self->_inline_check_lazy . "\n"
57 . $self->_inline_post_body(@_) . "\n"
58 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
62 sub generate_writer_method_inline {
64 my $attr = $self->associated_attribute;
65 my $attr_name = $attr->name;
67 my $slot_access = $self->_inline_get($inv, $attr_name);
68 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
70 $self->_eval_code('sub { '
71 . $self->_inline_pre_body(@_)
72 . $self->_inline_copy_value
73 . $self->_inline_check_required
74 . $self->_inline_check_coercion
75 . $self->_inline_check_constraint($value_name)
76 . $self->_inline_store($inv, $value_name)
77 . $self->_inline_post_body(@_)
78 . $self->_inline_trigger($inv, $value_name)
82 sub generate_reader_method_inline {
84 my $attr = $self->associated_attribute;
85 my $attr_name = $attr->name;
87 my $slot_access = $self->_inline_get($inv, $attr_name);
89 $self->_eval_code('sub {'
90 . $self->_inline_pre_body(@_)
91 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
92 . $self->_inline_check_lazy
93 . $self->_inline_post_body(@_)
94 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
98 sub _inline_copy_value {
99 return '' unless shift->_value_needs_copy;
100 return 'my $val = $_[1];'
103 sub _value_needs_copy {
104 my $attr = (shift)->associated_attribute;
105 return $attr->should_coerce;
108 sub generate_reader_method { shift->generate_reader_method_inline(@_) }
109 sub generate_writer_method { shift->generate_writer_method_inline(@_) }
110 sub generate_accessor_method { shift->generate_accessor_method_inline(@_) }
112 sub _inline_pre_body { '' }
113 sub _inline_post_body { '' }
115 sub _inline_check_constraint {
116 my ($self, $value) = @_;
118 my $attr = $self->associated_attribute;
119 my $attr_name = $attr->name;
121 return '' unless $attr->has_type_constraint;
123 my $type_constraint_name = $attr->type_constraint->name;
126 # This sprintf is insanely annoying, we should
127 # fix it someday - SL
128 return sprintf <<'EOF', $value, $attr_name, $value, $value,
129 $type_constraint->(%s)
130 || confess "Attribute (%s) does not pass the type constraint because: "
131 . $type_constraint_obj->get_message(%s);
135 sub _inline_check_coercion {
136 my $attr = (shift)->associated_attribute;
138 return '' unless $attr->should_coerce;
139 return '$val = $attr->type_constraint->coerce($_[1]);'
142 sub _inline_check_required {
143 my $attr = (shift)->associated_attribute;
145 my $attr_name = $attr->name;
147 return '' unless $attr->is_required;
148 return qq{(\@_ >= 2) || confess "Attribute ($attr_name) is required, so cannot be set to undef";} # defined $_[1] is not good enough
151 sub _inline_check_lazy {
153 my $attr = $self->associated_attribute;
155 return '' unless $attr->is_lazy;
158 my $slot_access = $self->_inline_access($inv, $attr->name);
160 my $slot_exists = $self->_inline_has($inv, $attr->name);
162 my $code = 'unless (' . $slot_exists . ') {' . "\n";
163 if ($attr->has_type_constraint) {
164 if ($attr->has_default || $attr->has_builder) {
165 if ($attr->has_default) {
166 $code .= ' my $default = $attr->default(' . $inv . ');'."\n";
168 elsif ($attr->has_builder) {
169 $code .= ' my $default;'."\n".
170 ' if(my $builder = '.$inv.'->can($attr->builder)){ '."\n".
171 ' $default = '.$inv.'->$builder; '. "\n } else {\n" .
172 ' confess(Scalar::Util::blessed('.$inv.')." does not support builder method '.
173 '\'".$attr->builder."\' for attribute \'" . $attr->name . "\'");'. "\n }";
175 $code .= ' $default = $type_constraint_obj->coerce($default);'."\n" if $attr->should_coerce;
176 $code .= ' ($type_constraint->($default))' .
177 ' || confess "Attribute (" . $attr_name . ") does not pass the type constraint ("' .
178 ' . $type_constraint_name . ") with " . (defined($default) ? overload::StrVal($default) : "undef");'
180 $code .= ' ' . $self->_inline_init_slot($attr, $inv, $slot_access, '$default') . "\n";
183 $code .= ' ' . $self->_inline_init_slot($attr, $inv, $slot_access, 'undef') . "\n";
187 if ($attr->has_default) {
188 $code .= ' ' . $self->_inline_init_slot($attr, $inv, $slot_access, ('$attr->default(' . $inv . ')')) . "\n";
190 elsif ($attr->has_builder) {
191 $code .= ' if (my $builder = '.$inv.'->can($attr->builder)) { ' . "\n"
192 . ' ' . $self->_inline_init_slot($attr, $inv, $slot_access, ($inv . '->$builder'))
194 ' confess(Scalar::Util::blessed('.$inv.')." does not support builder method '.
195 '\'".$attr->builder."\' for attribute \'" . $attr->name . "\'");'. "\n }";
198 $code .= ' ' . $self->_inline_init_slot($attr, $inv, $slot_access, 'undef') . "\n";
205 sub _inline_init_slot {
206 my ($self, $attr, $inv, $slot_access, $value) = @_;
207 if ($attr->has_initializer) {
208 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
211 return ($slot_access . ' = ' . $value . ';');
216 my ($self, $instance, $value) = @_;
217 my $attr = $self->associated_attribute;
219 my $mi = $attr->associated_class->get_meta_instance;
220 my $slot_name = sprintf "'%s'", $attr->slots;
222 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
223 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
224 if $attr->is_weak_ref;
228 sub _inline_trigger {
229 my ($self, $instance, $value) = @_;
230 my $attr = $self->associated_attribute;
231 return '' unless $attr->has_trigger;
232 return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
236 my ($self, $instance) = @_;
237 my $attr = $self->associated_attribute;
239 my $mi = $attr->associated_class->get_meta_instance;
240 my $slot_name = sprintf "'%s'", $attr->slots;
242 return $mi->inline_get_slot_value($instance, $slot_name);
246 my ($self, $instance) = @_;
247 my $attr = $self->associated_attribute;
249 my $mi = $attr->associated_class->get_meta_instance;
250 my $slot_name = sprintf "'%s'", $attr->slots;
252 return $mi->inline_slot_access($instance, $slot_name);
256 my ($self, $instance) = @_;
257 my $attr = $self->associated_attribute;
259 my $mi = $attr->associated_class->get_meta_instance;
260 my $slot_name = sprintf "'%s'", $attr->slots;
262 return $mi->inline_is_slot_initialized($instance, $slot_name);
265 sub _inline_auto_deref {
266 my ( $self, $ref_value ) = @_;
267 my $attr = $self->associated_attribute;
269 return $ref_value unless $attr->should_auto_deref;
271 my $type_constraint = $attr->type_constraint;
274 if ($type_constraint->is_a_type_of('ArrayRef')) {
277 elsif ($type_constraint->is_a_type_of('HashRef')) {
281 confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
284 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
295 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
299 This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
300 responsibility is to generate the accessor methods for attributes. It
301 can handle both closure based accessors, as well as inlined source based
304 This is a fairly new addition to the MOP, but this will play an important
305 role in the optimization strategy we are currently following.
311 =item B<generate_accessor_method>
313 =item B<generate_reader_method>
315 =item B<generate_writer_method>
317 =item B<generate_accessor_method_inline>
319 =item B<generate_reader_method_inline>
321 =item B<generate_writer_method_inline>
327 All complex software has bugs lurking in it, and this module is no
328 exception. If you find a bug please either email me, or add the bug
333 Stevan Little E<lt>stevan@iinteractive.comE<gt>
335 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
337 =head1 COPYRIGHT AND LICENSE
339 Copyright 2006-2008 by Infinity Interactive, Inc.
341 L<http://www.iinteractive.com>
343 This library is free software; you can redistribute it and/or modify
344 it under the same terms as Perl itself.