foo
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor.pm
CommitLineData
8ee73eeb 1
2package Moose::Meta::Method::Accessor;
3
4use strict;
5use warnings;
6
39b3bc94 7use Carp 'confess';
8
c8cf9aaa 9our $VERSION = '0.02';
8ee73eeb 10
39b3bc94 11use base 'Moose::Meta::Method',
d617b644 12 'Class::MOP::Method::Accessor';
13
946289d1 14## Inline method generators
d617b644 15
946289d1 16sub generate_accessor_method_inline {
d617b644 17 my $self = shift;
18 my $attr = $self->associated_attribute;
19 my $attr_name = $attr->name;
20
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;
24 my $inv = '$_[0]';
25 my $code = 'sub { '
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)
32 . ' }'
33 . $self->_inline_check_lazy
34 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv))
35 . ' }';
36
37 # NOTE:
38 # set up the environment
39 my $type_constraint = $attr->type_constraint
40 ? $attr->type_constraint->_compiled_type_constraint
41 : undef;
42
43 my $sub = eval $code;
44 confess "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@;
45 return $sub;
46}
47
946289d1 48sub generate_writer_method_inline {
d617b644 49 my $self = shift;
50 my $attr = $self->associated_attribute;
51 my $attr_name = $attr->name;
52
53 my $value_name = $attr->should_coerce ? '$val' : '$_[1]';
54 my $inv = '$_[0]';
55 my $code = 'sub { '
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)
61 . ' }';
62
63 # NOTE:
64 # set up the environment
65 my $type_constraint = $attr->type_constraint
66 ? $attr->type_constraint->_compiled_type_constraint
67 : undef;
68
69 my $sub = eval $code;
70 confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
71 return $sub;
72}
73
946289d1 74sub generate_reader_method_inline {
d617b644 75 my $self = shift;
76 my $attr = $self->associated_attribute;
77 my $attr_name = $attr->name;
78
d617b644 79 my $code = 'sub {'
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}' ) . ';'
83 . '}';
84 my $sub = eval $code;
85 confess "Could not create reader for '$attr_name' because $@ \n code: $code" if $@;
86 return $sub;
87}
88
c8cf9aaa 89## normal method generators
90
91*generate_reader_method = \&generate_reader_method_inline;
92*generate_writer_method = \&generate_writer_method_inline;
93*generate_accessor_method = \&generate_accessor_method_inline;
94
39b3bc94 95## ... private helpers
d617b644 96
97sub _inline_check_constraint {
98 my ($self, $value) = @_;
39b3bc94 99
100 my $attr = $self->associated_attribute;
101
102 return '' unless $attr->has_type_constraint;
d617b644 103
104 # FIXME - remove 'unless defined($value) - constraint Undef
105 return sprintf <<'EOF', $value, $value, $value, $value
106defined($type_constraint->(%s))
107 || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("
108 . $attr->type_constraint->name . ") with " . (defined(%s) ? "'%s'" : "undef")
109 if defined(%s);
110EOF
111}
112
113sub _inline_check_coercion {
39b3bc94 114 my $attr = (shift)->associated_attribute;
115
116 return '' unless $attr->should_coerce;
d617b644 117 return 'my $val = $attr->type_constraint->coerce($_[1]);'
118}
119
120sub _inline_check_required {
39b3bc94 121 my $attr = (shift)->associated_attribute;
122
123 return '' unless $attr->is_required;
d617b644 124 return 'defined($_[1]) || confess "Attribute ($attr_name) is required, so cannot be set to undef";'
125}
126
127sub _inline_check_lazy {
39b3bc94 128 my $attr = (shift)->associated_attribute;
129
130 return '' unless $attr->is_lazy;
131
132 if ($attr->has_type_constraint) {
d617b644 133 # NOTE:
134 # this could probably be cleaned
135 # up and streamlined a little more
136 return 'unless (exists $_[0]->{$attr_name}) {' .
137 ' if ($attr->has_default) {' .
138 ' my $default = $attr->default($_[0]);' .
139 ' (defined($type_constraint->($default)))' .
140 ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' .
141 ' . $attr->type_constraint->name . ") with " . (defined($default) ? "\'$default\'" : "undef")' .
142 ' if defined($default);' .
143 ' $_[0]->{$attr_name} = $default; ' .
144 ' }' .
145 ' else {' .
146 ' $_[0]->{$attr_name} = undef;' .
147 ' }' .
148 '}';
149 }
150 return '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)'
151 . 'unless exists $_[0]->{$attr_name};';
152}
153
154
155sub _inline_store {
156 my ($self, $instance, $value) = @_;
39b3bc94 157 my $attr = $self->associated_attribute;
d617b644 158
39b3bc94 159 my $mi = $attr->associated_class->get_meta_instance;
160 my $slot_name = sprintf "'%s'", $attr->slots;
d617b644 161
162 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
163 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
39b3bc94 164 if $attr->is_weak_ref;
d617b644 165 return $code;
166}
167
168sub _inline_trigger {
169 my ($self, $instance, $value) = @_;
39b3bc94 170 my $attr = $self->associated_attribute;
171 return '' unless $attr->has_trigger;
d617b644 172 return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
173}
174
175sub _inline_get {
176 my ($self, $instance) = @_;
39b3bc94 177 my $attr = $self->associated_attribute;
d617b644 178
39b3bc94 179 my $mi = $attr->associated_class->get_meta_instance;
180 my $slot_name = sprintf "'%s'", $attr->slots;
d617b644 181
182 return $mi->inline_get_slot_value($instance, $slot_name);
183}
184
185sub _inline_auto_deref {
186 my ( $self, $ref_value ) = @_;
39b3bc94 187 my $attr = $self->associated_attribute;
d617b644 188
39b3bc94 189 return $ref_value unless $attr->should_auto_deref;
d617b644 190
39b3bc94 191 my $type_constraint = $attr->type_constraint;
d617b644 192
193 my $sigil;
194 if ($type_constraint->is_a_type_of('ArrayRef')) {
195 $sigil = '@';
196 }
197 elsif ($type_constraint->is_a_type_of('HashRef')) {
198 $sigil = '%';
199 }
200 else {
201 confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
202 }
203
204 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
205}
8ee73eeb 206
2071;
208
209__END__
210
211=pod
212
39b3bc94 213=head1 NAME
214
ecb59493 215Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
39b3bc94 216
217=head1 DESCRIPTION
218
ecb59493 219This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
220responsibility is to generate the accessor methods for attributes. It
221can handle both closure based accessors, as well as inlined source based
222accessors.
223
224This is a fairly new addition to the MOP, but this will play an important
225role in the optimization strategy we are currently following.
226
39b3bc94 227=head1 METHODS
228
229=over 4
230
c8cf9aaa 231=item B<generate_accessor_method>
232
233=item B<generate_reader_method>
234
235=item B<generate_writer_method>
236
39b3bc94 237=item B<generate_accessor_method_inline>
238
39b3bc94 239=item B<generate_reader_method_inline>
240
39b3bc94 241=item B<generate_writer_method_inline>
242
243=back
244
245=head1 BUGS
246
247All complex software has bugs lurking in it, and this module is no
248exception. If you find a bug please either email me, or add the bug
249to cpan-RT.
250
251=head1 AUTHOR
252
253Stevan Little E<lt>stevan@iinteractive.comE<gt>
254
255Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
256
257=head1 COPYRIGHT AND LICENSE
258
259Copyright 2006 by Infinity Interactive, Inc.
260
261L<http://www.iinteractive.com>
262
263This library is free software; you can redistribute it and/or modify
264it under the same terms as Perl itself.
265
8ee73eeb 266=cut