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