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 $self->_inline_pre_body(@_),
27 'my ' . $inv . ' = shift;',
28 $self->_inline_curried_arguments,
29 $self->_inline_writer_core($inv, $slot_access),
30 $self->_inline_post_body(@_),
35 sub _inline_writer_core {
37 my ($inv, $slot_access) = @_;
39 my $potential = $self->_potential_value($slot_access);
44 $self->_inline_check_argument_count,
45 $self->_inline_process_arguments($inv, $slot_access),
46 $self->_inline_check_arguments('for writer'),
47 $self->_inline_check_lazy($inv),
50 if ($self->_return_value($slot_access)) {
51 # some writers will save the return value in this variable when they
52 # generate the potential value.
53 push @code, 'my @return;'
57 $self->_inline_coerce_new_values,
58 $self->_inline_copy_native_value(\$potential),
59 $self->_inline_tc_code($potential),
60 $self->_inline_get_old_value_for_trigger($inv, $old),
61 $self->_inline_capture_return_value($slot_access),
62 $self->_inline_set_new_value($inv, $potential, $slot_access),
63 $self->_inline_trigger($inv, $slot_access, $old),
64 $self->_inline_return_value($slot_access, 'for writer'),
70 sub _inline_process_arguments { return }
72 sub _inline_check_arguments { return }
74 sub _inline_coerce_new_values { return }
76 sub _value_needs_copy {
79 return $self->_constraint_must_be_checked;
82 sub _constraint_must_be_checked {
85 my $attr = $self->associated_attribute;
87 return $attr->has_type_constraint
88 && (!$self->_is_root_type( $attr->type_constraint )
89 || ( $attr->should_coerce && $attr->type_constraint->has_coercion)
97 my $name = $type->name;
99 return any { $name eq $_ } @{ $self->root_types };
102 sub _inline_copy_native_value {
104 my ($potential_ref) = @_;
106 return unless $self->_value_needs_copy;
108 my $code = 'my $potential = ' . ${$potential_ref} . ';';
110 ${$potential_ref} = '$potential';
115 around _inline_tc_code => sub {
118 my ($value, $for_lazy) = @_;
120 return unless $for_lazy || $self->_constraint_must_be_checked;
122 return $self->$orig(@_);
125 sub _inline_check_coercion {
129 my $attr = $self->associated_attribute;
130 return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
132 # We want to break the aliasing in @_ in case the coercion tries to make a
133 # destructive change to an array member.
134 return $value . ' = $type_constraint_obj->coerce(' . $value . ');';
137 around _inline_check_constraint => sub {
140 my ($value, $for_lazy) = @_;
142 return unless $for_lazy || $self->_constraint_must_be_checked;
144 return $self->$orig(@_);
147 sub _inline_capture_return_value { return }
149 sub _inline_set_new_value {
152 return $self->_inline_store_value(@_)
153 if $self->_value_needs_copy
154 || !$self->_slot_access_can_be_inlined
155 || !$self->_get_is_lvalue;
157 return $self->_inline_optimized_set_new_value(@_);
163 return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
166 sub _inline_optimized_set_new_value {
169 return $self->_inline_store_value(@_);
174 my ($slot_access) = @_;