1 package Moose::Meta::Method::Accessor::Native::Writer;
6 use List::MoreUtils qw( any );
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
14 with 'Moose::Meta::Method::Accessor::Native';
16 requires '_potential_value';
18 sub _generate_method {
22 my $slot_access = $self->_get_value($inv);
26 'my ' . $inv . ' = shift;',
27 $self->_inline_curried_arguments,
28 $self->_inline_writer_core($inv, $slot_access),
33 sub _inline_writer_core {
35 my ($inv, $slot_access) = @_;
37 my $potential = $self->_potential_value($slot_access);
42 $self->_inline_check_argument_count,
43 $self->_inline_process_arguments($inv, $slot_access),
44 $self->_inline_check_arguments('for writer'),
45 $self->_inline_check_lazy($inv, '$type_constraint', '$type_constraint_obj'),
48 if ($self->_return_value($slot_access)) {
49 # some writers will save the return value in this variable when they
50 # generate the potential value.
51 push @code, 'my @return;'
55 $self->_inline_coerce_new_values,
56 $self->_inline_copy_native_value(\$potential),
57 $self->_inline_tc_code($potential, '$type_constraint', '$type_constraint_obj'),
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'),
68 sub _inline_process_arguments { return }
70 sub _inline_check_arguments { return }
72 sub _inline_coerce_new_values { return }
74 sub _writer_value_needs_copy {
77 return $self->_constraint_must_be_checked;
80 sub _constraint_must_be_checked {
83 my $attr = $self->associated_attribute;
85 return $attr->has_type_constraint
86 && (!$self->_is_root_type( $attr->type_constraint )
87 || ( $attr->should_coerce && $attr->type_constraint->has_coercion)
95 my $name = $type->name;
97 return any { $name eq $_ } @{ $self->root_types };
100 sub _inline_copy_native_value {
102 my ($potential_ref) = @_;
104 return unless $self->_writer_value_needs_copy;
106 my $code = 'my $potential = ' . ${$potential_ref} . ';';
108 ${$potential_ref} = '$potential';
113 around _inline_tc_code => sub {
116 my ($value, $tc, $tc_obj, $for_lazy) = @_;
118 return unless $for_lazy || $self->_constraint_must_be_checked;
120 return $self->$orig(@_);
123 sub _inline_check_coercion {
125 my ($value, $tc, $tc_obj) = @_;
127 my $attr = $self->associated_attribute;
128 return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
130 # We want to break the aliasing in @_ in case the coercion tries to make a
131 # destructive change to an array member.
132 return $value . ' = ' . $tc_obj . '->coerce(' . $value . ');';
135 around _inline_check_constraint => sub {
138 my ($value, $tc, $tc_obj, $for_lazy) = @_;
140 return unless $for_lazy || $self->_constraint_must_be_checked;
142 return $self->$orig(@_);
145 sub _inline_capture_return_value { return }
147 sub _inline_set_new_value {
150 return $self->_inline_store_value(@_)
151 if $self->_writer_value_needs_copy
152 || !$self->_slot_access_can_be_inlined
153 || !$self->_get_is_lvalue;
155 return $self->_inline_optimized_set_new_value(@_);
161 return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
164 sub _inline_optimized_set_new_value {
167 return $self->_inline_store_value(@_);
172 my ($slot_access) = @_;