2 package Moose::Meta::Method::Accessor;
11 use base 'Moose::Meta::Method',
12 'Class::MOP::Method::Accessor';
14 ## Inline method generators
16 sub generate_accessor_method_inline {
18 my $attr = $self->associated_attribute;
19 my $attr_name = $attr->name;
21 my $value_name = $attr->should_coerce ? '$val' : '$_[1]';
22 my $mi = $attr->associated_class->get_meta_instance;
23 my $slot_name = sprintf "'%s'", $attr->slots;
26 . 'if (scalar(@_) == 2) {'
27 . $self->_inline_check_required
28 . $self->_inline_check_coercion
29 . $self->_inline_check_constraint($value_name)
30 . $self->_inline_store($inv, $value_name)
31 . $self->_inline_trigger($inv, $value_name)
33 . $self->_inline_check_lazy
34 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv))
38 # set up the environment
39 my $type_constraint = $attr->type_constraint
40 ? $attr->type_constraint->_compiled_type_constraint
44 confess "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@;
48 sub generate_writer_method_inline {
50 my $attr = $self->associated_attribute;
51 my $attr_name = $attr->name;
53 my $value_name = $attr->should_coerce ? '$val' : '$_[1]';
56 . $self->_inline_check_required
57 . $self->_inline_check_coercion
58 . $self->_inline_check_constraint($value_name)
59 . $self->_inline_store($inv, $value_name)
60 . $self->_inline_trigger($inv, $value_name)
64 # set up the environment
65 my $type_constraint = $attr->type_constraint
66 ? $attr->type_constraint->_compiled_type_constraint
70 confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
74 sub generate_reader_method_inline {
76 my $attr = $self->associated_attribute;
77 my $attr_name = $attr->name;
80 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
81 . $self->_inline_check_lazy
82 . 'return ' . $self->_inline_auto_deref( '$_[0]->{$attr_name}' ) . ';'
86 # set up the environment
87 my $type_constraint = $attr->type_constraint
88 ? $attr->type_constraint->_compiled_type_constraint
92 confess "Could not create reader for '$attr_name' because $@ \n code: $code" if $@;
96 ## normal method generators
98 *generate_reader_method = \&generate_reader_method_inline;
99 *generate_writer_method = \&generate_writer_method_inline;
100 *generate_accessor_method = \&generate_accessor_method_inline;
102 ## ... private helpers
104 sub _inline_check_constraint {
105 my ($self, $value) = @_;
107 my $attr = $self->associated_attribute;
109 return '' unless $attr->has_type_constraint;
111 # FIXME - remove 'unless defined($value) - constraint Undef
112 return sprintf <<'EOF', $value, $value, $value, $value
113 defined($type_constraint->(%s))
114 || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("
115 . $attr->type_constraint->name . ") with " . (defined(%s) ? "'%s'" : "undef")
120 sub _inline_check_coercion {
121 my $attr = (shift)->associated_attribute;
123 return '' unless $attr->should_coerce;
124 return 'my $val = $attr->type_constraint->coerce($_[1]);'
127 sub _inline_check_required {
128 my $attr = (shift)->associated_attribute;
130 return '' unless $attr->is_required;
131 return 'defined($_[1]) || confess "Attribute ($attr_name) is required, so cannot be set to undef";'
134 sub _inline_check_lazy {
135 my $attr = (shift)->associated_attribute;
137 return '' unless $attr->is_lazy;
139 if ($attr->has_type_constraint) {
141 # this could probably be cleaned
142 # up and streamlined a little more
143 return 'unless (exists $_[0]->{$attr_name}) {' .
144 ' if ($attr->has_default) {' .
145 ' my $default = $attr->default($_[0]);' .
146 ' (defined($type_constraint->($default)))' .
147 ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' .
148 ' . $attr->type_constraint->name . ") with " . (defined($default) ? "\'$default\'" : "undef")' .
149 ' if defined($default);' .
150 ' $_[0]->{$attr_name} = $default; ' .
153 ' $_[0]->{$attr_name} = undef;' .
157 return '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)'
158 . 'unless exists $_[0]->{$attr_name};';
163 my ($self, $instance, $value) = @_;
164 my $attr = $self->associated_attribute;
166 my $mi = $attr->associated_class->get_meta_instance;
167 my $slot_name = sprintf "'%s'", $attr->slots;
169 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
170 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
171 if $attr->is_weak_ref;
175 sub _inline_trigger {
176 my ($self, $instance, $value) = @_;
177 my $attr = $self->associated_attribute;
178 return '' unless $attr->has_trigger;
179 return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
183 my ($self, $instance) = @_;
184 my $attr = $self->associated_attribute;
186 my $mi = $attr->associated_class->get_meta_instance;
187 my $slot_name = sprintf "'%s'", $attr->slots;
189 return $mi->inline_get_slot_value($instance, $slot_name);
192 sub _inline_auto_deref {
193 my ( $self, $ref_value ) = @_;
194 my $attr = $self->associated_attribute;
196 return $ref_value unless $attr->should_auto_deref;
198 my $type_constraint = $attr->type_constraint;
201 if ($type_constraint->is_a_type_of('ArrayRef')) {
204 elsif ($type_constraint->is_a_type_of('HashRef')) {
208 confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
211 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
222 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
226 This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
227 responsibility is to generate the accessor methods for attributes. It
228 can handle both closure based accessors, as well as inlined source based
231 This is a fairly new addition to the MOP, but this will play an important
232 role in the optimization strategy we are currently following.
238 =item B<generate_accessor_method>
240 =item B<generate_reader_method>
242 =item B<generate_writer_method>
244 =item B<generate_accessor_method_inline>
246 =item B<generate_reader_method_inline>
248 =item B<generate_writer_method_inline>
254 All complex software has bugs lurking in it, and this module is no
255 exception. If you find a bug please either email me, or add the bug
260 Stevan Little E<lt>stevan@iinteractive.comE<gt>
262 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
264 =head1 COPYRIGHT AND LICENSE
266 Copyright 2006 by Infinity Interactive, Inc.
268 L<http://www.iinteractive.com>
270 This library is free software; you can redistribute it and/or modify
271 it under the same terms as Perl itself.