2 package Moose::Meta::Method::Accessor;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 use base 'Moose::Meta::Method',
12 'Class::MOP::Method::Accessor';
14 ## Inline method generators
17 my ( $self, $code ) = @_;
20 # set up the environment
21 my $attr = $self->associated_attribute;
22 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 $self->throw_error("Could not create writer for '$attr_name' because $@ \n code: $code", error => $@, data => $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($inv) . "\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 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
92 . $self->_inline_check_lazy($inv)
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;
125 qq{\$type_constraint->($value) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) does not pass the type constraint because: " . \$type_constraint_obj->get_message($value)}, "data => $value") . ";";
128 sub _inline_check_coercion {
129 my $attr = (shift)->associated_attribute;
131 return '' unless $attr->should_coerce;
132 return '$val = $attr->type_constraint->coerce($_[1]);'
135 sub _inline_check_required {
137 my $attr = $self->associated_attribute;
139 my $attr_name = $attr->name;
141 return '' unless $attr->is_required;
142 return qq{(\@_ >= 2) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) is required, so cannot be set to undef"}) . ';' # defined $_[1] is not good enough
145 sub _inline_check_lazy {
146 my ($self, $instance) = @_;
148 my $attr = $self->associated_attribute;
150 return '' unless $attr->is_lazy;
152 my $slot_access = $self->_inline_access($instance, $attr->name);
154 my $slot_exists = $self->_inline_has($instance, $attr->name);
156 my $code = 'unless (' . $slot_exists . ') {' . "\n";
157 if ($attr->has_type_constraint) {
158 if ($attr->has_default || $attr->has_builder) {
159 if ($attr->has_default) {
160 $code .= ' my $default = $attr->default(' . $instance . ');'."\n";
162 elsif ($attr->has_builder) {
163 $code .= ' my $default;'."\n".
164 ' if(my $builder = '.$instance.'->can($attr->builder)){ '."\n".
165 ' $default = '.$instance.'->$builder; '. "\n } else {\n" .
166 ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') .
169 $code .= ' $default = $type_constraint_obj->coerce($default);'."\n" if $attr->should_coerce;
170 $code .= ' ($type_constraint->($default))' .
171 ' || ' . $self->_inline_throw_error('"Attribute (" . $attr_name . ") does not pass the type constraint ("' .
172 ' . $type_constraint_name . ") with " . (defined($default) ? overload::StrVal($default) : "undef")' ) . ';'
174 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, '$default') . "\n";
177 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
181 if ($attr->has_default) {
182 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ('$attr->default(' . $instance . ')')) . "\n";
184 elsif ($attr->has_builder) {
185 $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
186 . ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ($instance . '->$builder'))
188 . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name')
192 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
199 sub _inline_init_slot {
200 my ($self, $attr, $inv, $slot_access, $value) = @_;
201 if ($attr->has_initializer) {
202 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
205 return ($slot_access . ' = ' . $value . ';');
210 my ($self, $instance, $value) = @_;
211 my $attr = $self->associated_attribute;
213 my $mi = $attr->associated_class->get_meta_instance;
214 my $slot_name = sprintf "'%s'", $attr->slots;
216 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
217 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
218 if $attr->is_weak_ref;
222 sub _inline_trigger {
223 my ($self, $instance, $value) = @_;
224 my $attr = $self->associated_attribute;
225 return '' unless $attr->has_trigger;
226 return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
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_get_slot_value($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_slot_access($instance, $slot_name);
250 my ($self, $instance) = @_;
251 my $attr = $self->associated_attribute;
253 my $mi = $attr->associated_class->get_meta_instance;
254 my $slot_name = sprintf "'%s'", $attr->slots;
256 return $mi->inline_is_slot_initialized($instance, $slot_name);
259 sub _inline_auto_deref {
260 my ( $self, $ref_value ) = @_;
261 my $attr = $self->associated_attribute;
263 return $ref_value unless $attr->should_auto_deref;
265 my $type_constraint = $attr->type_constraint;
268 if ($type_constraint->is_a_type_of('ArrayRef')) {
271 elsif ($type_constraint->is_a_type_of('HashRef')) {
275 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
278 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
289 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
293 This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
294 responsibility is to generate the accessor methods for attributes. It
295 can handle both closure based accessors, as well as inlined source based
298 This is a fairly new addition to the MOP, but this will play an important
299 role in the optimization strategy we are currently following.
305 =item B<generate_accessor_method>
307 =item B<generate_reader_method>
309 =item B<generate_writer_method>
311 =item B<generate_accessor_method_inline>
313 =item B<generate_reader_method_inline>
315 =item B<generate_writer_method_inline>
321 All complex software has bugs lurking in it, and this module is no
322 exception. If you find a bug please either email me, or add the bug
327 Stevan Little E<lt>stevan@iinteractive.comE<gt>
329 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
331 =head1 COPYRIGHT AND LICENSE
333 Copyright 2006-2008 by Infinity Interactive, Inc.
335 L<http://www.iinteractive.com>
337 This library is free software; you can redistribute it and/or modify
338 it under the same terms as Perl itself.