Commit | Line | Data |
5df54980 |
1 | package Moose::Meta::Method::Accessor::Native::Writer; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a6ae7438 |
6 | use List::MoreUtils qw( any ); |
7 | |
10bd99ec |
8 | our $VERSION = '1.14'; |
5df54980 |
9 | $VERSION = eval $VERSION; |
10 | our $AUTHORITY = 'cpan:STEVAN'; |
11 | |
12 | use base 'Moose::Meta::Method::Accessor::Native'; |
13 | |
14 | sub _generate_method { |
15 | my $self = shift; |
16 | |
17 | my $inv = '$self'; |
18 | |
19 | my $slot_access = $self->_inline_get($inv); |
20 | |
21 | my $code = 'sub {'; |
e7724627 |
22 | |
5df54980 |
23 | $code .= "\n" . $self->_inline_pre_body(@_); |
24 | |
25 | $code .= "\n" . 'my $self = shift;'; |
26 | |
5df54980 |
27 | $code .= "\n" . $self->_inline_curried_arguments; |
28 | |
e7724627 |
29 | $code .= $self->_writer_core( $inv, $slot_access ); |
5df54980 |
30 | |
e7724627 |
31 | $code .= "\n" . $self->_inline_post_body(@_); |
32 | |
33 | $code .= "\n}"; |
5df54980 |
34 | |
e7724627 |
35 | return $code; |
36 | } |
37 | |
38 | sub _writer_core { |
39 | my ( $self, $inv, $slot_access ) = @_; |
40 | |
41 | my $code = q{}; |
42 | |
43 | $code .= "\n" . $self->_inline_check_argument_count; |
44 | $code .= "\n" . $self->_inline_process_arguments( $inv, $slot_access ); |
45 | $code .= "\n" . $self->_inline_check_arguments('for writer'); |
46 | |
47 | $code .= "\n" . $self->_inline_check_lazy($inv); |
5df54980 |
48 | |
5df54980 |
49 | my $potential_value = $self->_potential_value($slot_access); |
50 | |
6ff86bed |
51 | $code .= "\n" . $self->_inline_copy_native_value( \$potential_value ); |
5df54980 |
52 | $code .= "\n" |
53 | . $self->_inline_tc_code( |
5df54980 |
54 | $potential_value |
55 | ); |
56 | |
57 | $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv); |
e32b7489 |
58 | $code .= "\n" . $self->_inline_capture_return_value($slot_access); |
5df54980 |
59 | $code .= "\n" |
60 | . $self->_inline_set_new_value( |
61 | $inv, |
e32b7489 |
62 | $potential_value, |
63 | $slot_access, |
584540d9 |
64 | ) . ';'; |
5df54980 |
65 | $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' ); |
e32b7489 |
66 | $code .= "\n" . $self->_return_value( $slot_access, 'for writer' ); |
5df54980 |
67 | |
68 | return $code; |
69 | } |
70 | |
71 | sub _inline_process_arguments {q{}} |
72 | |
73 | sub _inline_check_arguments {q{}} |
74 | |
c302c35a |
75 | sub _value_needs_copy { |
76 | my $self = shift; |
77 | |
78 | return $self->_constraint_must_be_checked; |
79 | } |
5df54980 |
80 | |
a6ae7438 |
81 | sub _constraint_must_be_checked { |
82 | my $self = shift; |
83 | |
84 | my $attr = $self->associated_attribute; |
85 | |
86 | return $attr->has_type_constraint |
87 | && ( !$self->_is_root_type( $attr->type_constraint ) |
88 | || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) ); |
89 | } |
90 | |
91 | sub _is_root_type { |
92 | my ($self, $type) = @_; |
93 | |
94 | my $name = $type->name(); |
95 | |
96 | return any { $name eq $_ } @{ $self->root_types }; |
97 | } |
98 | |
6ff86bed |
99 | sub _inline_copy_native_value { |
fa072458 |
100 | my ( $self, $potential_ref ) = @_; |
101 | |
102 | return q{} unless $self->_value_needs_copy; |
103 | |
104 | my $code = "my \$potential = ${$potential_ref};"; |
105 | |
106 | ${$potential_ref} = '$potential'; |
107 | |
108 | return $code; |
109 | } |
110 | |
e7724627 |
111 | sub _inline_tc_code { |
44babf1f |
112 | my ( $self, $potential_value ) = @_; |
8044d617 |
113 | |
114 | return q{} unless $self->_constraint_must_be_checked; |
115 | |
116 | return $self->_inline_check_coercion($potential_value) . "\n" |
117 | . $self->_inline_check_constraint($potential_value); |
e7724627 |
118 | } |
5df54980 |
119 | |
e7724627 |
120 | sub _inline_check_coercion { |
a6ae7438 |
121 | my ( $self, $value ) = @_; |
122 | |
123 | my $attr = $self->associated_attribute; |
124 | |
125 | return '' |
126 | unless $attr->should_coerce && $attr->type_constraint->has_coercion; |
127 | |
128 | # We want to break the aliasing in @_ in case the coercion tries to make a |
129 | # destructive change to an array member. |
130 | return "$value = \$type_constraint_obj->coerce($value);"; |
e7724627 |
131 | } |
5df54980 |
132 | |
133 | sub _inline_check_constraint { |
134 | my $self = shift; |
135 | |
136 | return q{} unless $self->_constraint_must_be_checked; |
137 | |
138 | return $self->SUPER::_inline_check_constraint( $_[0] ); |
139 | } |
140 | |
e32b7489 |
141 | sub _inline_capture_return_value { return q{} } |
5df54980 |
142 | |
143 | sub _inline_set_new_value { |
144 | my $self = shift; |
145 | |
e32b7489 |
146 | return $self->SUPER::_inline_store(@_) |
54e259f6 |
147 | if $self->_value_needs_copy || !$self->_slot_access_can_be_inlined; |
e32b7489 |
148 | |
149 | return $self->_inline_optimized_set_new_value(@_); |
150 | } |
151 | |
152 | sub _inline_optimized_set_new_value { |
153 | my $self = shift; |
154 | |
155 | return $self->SUPER::_inline_store(@_) |
5df54980 |
156 | } |
157 | |
e7724627 |
158 | sub _return_value { return q{} } |
5df54980 |
159 | |
160 | 1; |