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 | |
efa728b4 |
8 | our $VERSION = '1.15'; |
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 | |
6ff86bed |
55 | $code .= "\n" . $self->_inline_copy_native_value( \$potential_value ); |
5df54980 |
56 | $code .= "\n" |
57 | . $self->_inline_tc_code( |
5df54980 |
58 | $potential_value |
59 | ); |
60 | |
61 | $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv); |
e32b7489 |
62 | $code .= "\n" . $self->_inline_capture_return_value($slot_access); |
5df54980 |
63 | $code .= "\n" |
64 | . $self->_inline_set_new_value( |
65 | $inv, |
e32b7489 |
66 | $potential_value, |
67 | $slot_access, |
584540d9 |
68 | ) . ';'; |
5df54980 |
69 | $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' ); |
e32b7489 |
70 | $code .= "\n" . $self->_return_value( $slot_access, 'for writer' ); |
5df54980 |
71 | |
72 | return $code; |
73 | } |
74 | |
75 | sub _inline_process_arguments {q{}} |
76 | |
77 | sub _inline_check_arguments {q{}} |
78 | |
c302c35a |
79 | sub _value_needs_copy { |
80 | my $self = shift; |
81 | |
82 | return $self->_constraint_must_be_checked; |
83 | } |
5df54980 |
84 | |
a6ae7438 |
85 | sub _constraint_must_be_checked { |
86 | my $self = shift; |
87 | |
88 | my $attr = $self->associated_attribute; |
89 | |
90 | return $attr->has_type_constraint |
91 | && ( !$self->_is_root_type( $attr->type_constraint ) |
92 | || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) ); |
93 | } |
94 | |
95 | sub _is_root_type { |
96 | my ($self, $type) = @_; |
97 | |
98 | my $name = $type->name(); |
99 | |
100 | return any { $name eq $_ } @{ $self->root_types }; |
101 | } |
102 | |
6ff86bed |
103 | sub _inline_copy_native_value { |
fa072458 |
104 | my ( $self, $potential_ref ) = @_; |
105 | |
106 | return q{} unless $self->_value_needs_copy; |
107 | |
108 | my $code = "my \$potential = ${$potential_ref};"; |
109 | |
110 | ${$potential_ref} = '$potential'; |
111 | |
112 | return $code; |
113 | } |
114 | |
e7724627 |
115 | sub _inline_tc_code { |
44babf1f |
116 | my ( $self, $potential_value ) = @_; |
8044d617 |
117 | |
118 | return q{} unless $self->_constraint_must_be_checked; |
119 | |
120 | return $self->_inline_check_coercion($potential_value) . "\n" |
121 | . $self->_inline_check_constraint($potential_value); |
e7724627 |
122 | } |
5df54980 |
123 | |
e7724627 |
124 | sub _inline_check_coercion { |
a6ae7438 |
125 | my ( $self, $value ) = @_; |
126 | |
127 | my $attr = $self->associated_attribute; |
128 | |
129 | return '' |
130 | unless $attr->should_coerce && $attr->type_constraint->has_coercion; |
131 | |
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);"; |
e7724627 |
135 | } |
5df54980 |
136 | |
8b9641b8 |
137 | override _inline_check_constraint => sub { |
5df54980 |
138 | my $self = shift; |
139 | |
140 | return q{} unless $self->_constraint_must_be_checked; |
141 | |
8b9641b8 |
142 | return super(); |
143 | }; |
5df54980 |
144 | |
e32b7489 |
145 | sub _inline_capture_return_value { return q{} } |
5df54980 |
146 | |
147 | sub _inline_set_new_value { |
148 | my $self = shift; |
149 | |
8b9641b8 |
150 | return $self->_inline_store(@_) |
54e259f6 |
151 | if $self->_value_needs_copy || !$self->_slot_access_can_be_inlined; |
e32b7489 |
152 | |
153 | return $self->_inline_optimized_set_new_value(@_); |
8b9641b8 |
154 | }; |
e32b7489 |
155 | |
156 | sub _inline_optimized_set_new_value { |
157 | my $self = shift; |
158 | |
8b9641b8 |
159 | return $self->_inline_store(@_); |
5df54980 |
160 | } |
161 | |
e7724627 |
162 | sub _return_value { return q{} } |
5df54980 |
163 | |
8b9641b8 |
164 | no Moose::Role; |
165 | |
5df54980 |
166 | 1; |