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