refactor constructor inlining to reuse attribute code
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Writer.pm
CommitLineData
5df54980 1package Moose::Meta::Method::Accessor::Native::Writer;
2
3use strict;
4use warnings;
5
a6ae7438 6use List::MoreUtils qw( any );
7
245478d5 8our $VERSION = '1.19';
5df54980 9$VERSION = eval $VERSION;
10our $AUTHORITY = 'cpan:STEVAN';
11
8b9641b8 12use Moose::Role;
13
14with 'Moose::Meta::Method::Accessor::Native';
15
16requires '_potential_value';
5df54980 17
18sub _generate_method {
19 my $self = shift;
20
53a4677c 21 my $inv = '$self';
1e2c801e 22 my $slot_access = $self->_get_value($inv);
5df54980 23
53a4677c 24 return (
25 'sub {',
53a4677c 26 'my ' . $inv . ' = shift;',
27 $self->_inline_curried_arguments,
1e2c801e 28 $self->_inline_writer_core($inv, $slot_access),
53a4677c 29 '}',
30 );
e7724627 31}
32
1e2c801e 33sub _inline_writer_core {
53a4677c 34 my $self = shift;
35 my ($inv, $slot_access) = @_;
e7724627 36
53a4677c 37 my $potential = $self->_potential_value($slot_access);
38 my $old = '@old';
5df54980 39
53a4677c 40 my @code;
41 push @code, (
42 $self->_inline_check_argument_count,
43 $self->_inline_process_arguments($inv, $slot_access),
44 $self->_inline_check_arguments('for writer'),
ec86bdff 45 $self->_inline_check_lazy($inv, '$type_constraint', '$type_constraint_obj'),
53a4677c 46 );
5df54980 47
53a4677c 48 if ($self->_return_value($slot_access)) {
7f5ec80d 49 # some writers will save the return value in this variable when they
50 # generate the potential value.
53a4677c 51 push @code, 'my @return;'
7f5ec80d 52 }
53
53a4677c 54 push @code, (
55 $self->_inline_coerce_new_values,
56 $self->_inline_copy_native_value(\$potential),
ec86bdff 57 $self->_inline_tc_code($potential, '$type_constraint', '$type_constraint_obj'),
53a4677c 58 $self->_inline_get_old_value_for_trigger($inv, $old),
59 $self->_inline_capture_return_value($slot_access),
60 $self->_inline_set_new_value($inv, $potential, $slot_access),
61 $self->_inline_trigger($inv, $slot_access, $old),
62 $self->_inline_return_value($slot_access, 'for writer'),
63 );
64
65 return @code;
5df54980 66}
67
53a4677c 68sub _inline_process_arguments { return }
5df54980 69
53a4677c 70sub _inline_check_arguments { return }
5df54980 71
53a4677c 72sub _inline_coerce_new_values { return }
7bf5e58d 73
6e50f7e9 74sub _writer_value_needs_copy {
c302c35a 75 my $self = shift;
76
77 return $self->_constraint_must_be_checked;
78}
5df54980 79
a6ae7438 80sub _constraint_must_be_checked {
81 my $self = shift;
82
83 my $attr = $self->associated_attribute;
84
85 return $attr->has_type_constraint
1e2c801e 86 && (!$self->_is_root_type( $attr->type_constraint )
87 || ( $attr->should_coerce && $attr->type_constraint->has_coercion)
88 );
a6ae7438 89}
90
91sub _is_root_type {
53a4677c 92 my $self = shift;
93 my ($type) = @_;
a6ae7438 94
53a4677c 95 my $name = $type->name;
a6ae7438 96
97 return any { $name eq $_ } @{ $self->root_types };
98}
99
6ff86bed 100sub _inline_copy_native_value {
53a4677c 101 my $self = shift;
102 my ($potential_ref) = @_;
fa072458 103
6e50f7e9 104 return unless $self->_writer_value_needs_copy;
fa072458 105
53a4677c 106 my $code = 'my $potential = ' . ${$potential_ref} . ';';
fa072458 107
108 ${$potential_ref} = '$potential';
109
1e2c801e 110 return $code;
fa072458 111}
112
53a4677c 113around _inline_tc_code => sub {
114 my $orig = shift;
115 my $self = shift;
ec86bdff 116 my ($value, $tc, $tc_obj, $for_lazy) = @_;
8044d617 117
53a4677c 118 return unless $for_lazy || $self->_constraint_must_be_checked;
8044d617 119
53a4677c 120 return $self->$orig(@_);
121};
5df54980 122
e7724627 123sub _inline_check_coercion {
53a4677c 124 my $self = shift;
ec86bdff 125 my ($value, $tc, $tc_obj) = @_;
a6ae7438 126
127 my $attr = $self->associated_attribute;
53a4677c 128 return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
a6ae7438 129
130 # We want to break the aliasing in @_ in case the coercion tries to make a
131 # destructive change to an array member.
ec86bdff 132 return $value . ' = ' . $tc_obj . '->coerce(' . $value . ');';
e7724627 133}
5df54980 134
53a4677c 135around _inline_check_constraint => sub {
136 my $orig = shift;
137 my $self = shift;
ec86bdff 138 my ($value, $tc, $tc_obj, $for_lazy) = @_;
5df54980 139
53a4677c 140 return unless $for_lazy || $self->_constraint_must_be_checked;
5df54980 141
53a4677c 142 return $self->$orig(@_);
8b9641b8 143};
5df54980 144
53a4677c 145sub _inline_capture_return_value { return }
5df54980 146
a486d5ad 147sub _inline_set_new_value {
5df54980 148 my $self = shift;
149
a486d5ad 150 return $self->_inline_store_value(@_)
6e50f7e9 151 if $self->_writer_value_needs_copy
f878e6cf 152 || !$self->_slot_access_can_be_inlined
1e2c801e 153 || !$self->_get_is_lvalue;
e32b7489 154
a486d5ad 155 return $self->_inline_optimized_set_new_value(@_);
f878e6cf 156}
157
1e2c801e 158sub _get_is_lvalue {
f878e6cf 159 my $self = shift;
160
161 return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
162}
e32b7489 163
a486d5ad 164sub _inline_optimized_set_new_value {
e32b7489 165 my $self = shift;
166
a486d5ad 167 return $self->_inline_store_value(@_);
5df54980 168}
169
7f5ec80d 170sub _return_value {
53a4677c 171 my $self = shift;
172 my ($slot_access) = @_;
7f5ec80d 173
174 return $slot_access;
175}
5df54980 176
8b9641b8 177no Moose::Role;
178
5df54980 1791;