Commit | Line | Data |
ba38bf08 |
1 | |
2 | package Class::MOP::Method::Accessor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | use Scalar::Util 'blessed', 'weaken'; |
9 | |
b4bd10ec |
10 | our $VERSION = '0.77'; |
d519662a |
11 | $VERSION = eval $VERSION; |
ba38bf08 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
13 | |
565f0cbb |
14 | use base 'Class::MOP::Method::Generated'; |
ba38bf08 |
15 | |
ba38bf08 |
16 | sub new { |
17 | my $class = shift; |
18 | my %options = @_; |
8d2d4c67 |
19 | |
ba38bf08 |
20 | (exists $options{attribute}) |
21 | || confess "You must supply an attribute to construct with"; |
8d2d4c67 |
22 | |
ba38bf08 |
23 | (exists $options{accessor_type}) |
8d2d4c67 |
24 | || confess "You must supply an accessor_type to construct with"; |
25 | |
ba38bf08 |
26 | (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute')) |
8d2d4c67 |
27 | || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance"; |
28 | |
b38f3848 |
29 | ($options{package_name} && $options{name}) |
32202ce2 |
30 | || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; |
b38f3848 |
31 | |
0bfc85b8 |
32 | my $self = $class->_new(\%options); |
8d2d4c67 |
33 | |
34 | # we don't want this creating |
35 | # a cycle in the code, if not |
ba38bf08 |
36 | # needed |
8683db0e |
37 | weaken($self->{'attribute'}); |
8d2d4c67 |
38 | |
565f0cbb |
39 | $self->initialize_body; |
8d2d4c67 |
40 | |
ba38bf08 |
41 | return $self; |
42 | } |
43 | |
a9e38dc7 |
44 | sub _new { |
0bfc85b8 |
45 | my $class = shift; |
46 | my $options = @_ == 1 ? $_[0] : {@_}; |
a9e38dc7 |
47 | |
0bfc85b8 |
48 | $options->{is_inline} ||= 0; |
a9e38dc7 |
49 | |
0bfc85b8 |
50 | return bless $options, $class; |
a9e38dc7 |
51 | } |
52 | |
ba38bf08 |
53 | ## accessors |
54 | |
8683db0e |
55 | sub associated_attribute { (shift)->{'attribute'} } |
56 | sub accessor_type { (shift)->{'accessor_type'} } |
ba38bf08 |
57 | |
8d2d4c67 |
58 | ## factory |
ba38bf08 |
59 | |
565f0cbb |
60 | sub initialize_body { |
ba38bf08 |
61 | my $self = shift; |
8d2d4c67 |
62 | |
ba38bf08 |
63 | my $method_name = join "_" => ( |
8d2d4c67 |
64 | 'generate', |
65 | $self->accessor_type, |
ba38bf08 |
66 | 'method', |
d90b42a6 |
67 | ($self->is_inline ? 'inline' : ()) |
ba38bf08 |
68 | ); |
8d2d4c67 |
69 | |
ffe92c8b |
70 | $self->{'body'} = $self->$method_name(); |
ba38bf08 |
71 | } |
72 | |
73 | ## generators |
74 | |
75 | sub generate_accessor_method { |
8d2d4c67 |
76 | my $attr = (shift)->associated_attribute; |
ba38bf08 |
77 | return sub { |
78 | $attr->set_value($_[0], $_[1]) if scalar(@_) == 2; |
79 | $attr->get_value($_[0]); |
80 | }; |
81 | } |
82 | |
83 | sub generate_reader_method { |
8d2d4c67 |
84 | my $attr = (shift)->associated_attribute; |
85 | return sub { |
ba38bf08 |
86 | confess "Cannot assign a value to a read-only accessor" if @_ > 1; |
87 | $attr->get_value($_[0]); |
8d2d4c67 |
88 | }; |
ba38bf08 |
89 | } |
90 | |
91 | sub generate_writer_method { |
8d2d4c67 |
92 | my $attr = (shift)->associated_attribute; |
ba38bf08 |
93 | return sub { |
94 | $attr->set_value($_[0], $_[1]); |
95 | }; |
96 | } |
97 | |
98 | sub generate_predicate_method { |
8d2d4c67 |
99 | my $attr = (shift)->associated_attribute; |
100 | return sub { |
3545c727 |
101 | $attr->has_value($_[0]) |
ba38bf08 |
102 | }; |
103 | } |
104 | |
105 | sub generate_clearer_method { |
8d2d4c67 |
106 | my $attr = (shift)->associated_attribute; |
107 | return sub { |
3545c727 |
108 | $attr->clear_value($_[0]) |
ba38bf08 |
109 | }; |
110 | } |
111 | |
112 | ## Inline methods |
113 | |
114 | |
115 | sub generate_accessor_method_inline { |
7f8de9b4 |
116 | my $self = shift; |
117 | my $attr = $self->associated_attribute; |
ba38bf08 |
118 | my $attr_name = $attr->name; |
119 | my $meta_instance = $attr->associated_class->instance_metaclass; |
120 | |
ffe92c8b |
121 | return $self->_eval_closure( |
0c6f3280 |
122 | {}, |
7f8de9b4 |
123 | 'sub {' |
ffe92c8b |
124 | . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]') |
7f8de9b4 |
125 | . ' if scalar(@_) == 2; ' |
ffe92c8b |
126 | . $meta_instance->inline_get_slot_value('$_[0]', $attr_name) |
7f8de9b4 |
127 | . '}' |
128 | ); |
ba38bf08 |
129 | } |
130 | |
131 | sub generate_reader_method_inline { |
7f8de9b4 |
132 | my $self = shift; |
133 | my $attr = $self->associated_attribute; |
ba38bf08 |
134 | my $attr_name = $attr->name; |
135 | my $meta_instance = $attr->associated_class->instance_metaclass; |
136 | |
ffe92c8b |
137 | return $self->_eval_closure( |
0c6f3280 |
138 | {}, |
7f8de9b4 |
139 | 'sub {' |
ba38bf08 |
140 | . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;' |
e9a19694 |
141 | . $meta_instance->inline_get_slot_value('$_[0]', $attr_name) |
7f8de9b4 |
142 | . '}' |
143 | ); |
ba38bf08 |
144 | } |
145 | |
146 | sub generate_writer_method_inline { |
7f8de9b4 |
147 | my $self = shift; |
148 | my $attr = $self->associated_attribute; |
ba38bf08 |
149 | my $attr_name = $attr->name; |
150 | my $meta_instance = $attr->associated_class->instance_metaclass; |
151 | |
ffe92c8b |
152 | return $self->_eval_closure( |
0c6f3280 |
153 | {}, |
7f8de9b4 |
154 | 'sub {' |
e9a19694 |
155 | . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]') |
7f8de9b4 |
156 | . '}' |
157 | ); |
ba38bf08 |
158 | } |
159 | |
160 | |
161 | sub generate_predicate_method_inline { |
7f8de9b4 |
162 | my $self = shift; |
163 | my $attr = $self->associated_attribute; |
ba38bf08 |
164 | my $attr_name = $attr->name; |
165 | my $meta_instance = $attr->associated_class->instance_metaclass; |
166 | |
ffe92c8b |
167 | return $self->_eval_closure( |
0c6f3280 |
168 | {}, |
7f8de9b4 |
169 | 'sub {' |
e9a19694 |
170 | . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name) |
7f8de9b4 |
171 | . '}' |
172 | ); |
ba38bf08 |
173 | } |
174 | |
175 | sub generate_clearer_method_inline { |
7f8de9b4 |
176 | my $self = shift; |
177 | my $attr = $self->associated_attribute; |
ba38bf08 |
178 | my $attr_name = $attr->name; |
179 | my $meta_instance = $attr->associated_class->instance_metaclass; |
180 | |
ffe92c8b |
181 | return $self->_eval_closure( |
0c6f3280 |
182 | {}, |
7f8de9b4 |
183 | 'sub {' |
e9a19694 |
184 | . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name) |
7f8de9b4 |
185 | . '}' |
186 | ); |
ba38bf08 |
187 | } |
188 | |
189 | 1; |
190 | |
191 | __END__ |
192 | |
193 | =pod |
194 | |
8d2d4c67 |
195 | =head1 NAME |
ba38bf08 |
196 | |
197 | Class::MOP::Method::Accessor - Method Meta Object for accessors |
198 | |
199 | =head1 SYNOPSIS |
200 | |
96e38ba6 |
201 | use Class::MOP::Method::Accessor; |
202 | |
203 | my $reader = Class::MOP::Method::Accessor->new( |
204 | attribute => $attribute, |
205 | is_inline => 1, |
206 | accessor_type => 'reader', |
207 | ); |
8d2d4c67 |
208 | |
b7045e66 |
209 | $reader->body->execute($instance); # call the reader method |
ba38bf08 |
210 | |
211 | =head1 DESCRIPTION |
212 | |
8d2d4c67 |
213 | This is a C<Class::MOP::Method> subclass which is used interally |
214 | by C<Class::MOP::Attribute> to generate accessor code. It can |
215 | handle generation of readers, writers, predicate and clearer |
96e38ba6 |
216 | methods, both as closures and as more optimized inline methods. |
217 | |
ba38bf08 |
218 | =head1 METHODS |
219 | |
220 | =over 4 |
221 | |
96e38ba6 |
222 | =item B<new (%options)> |
ba38bf08 |
223 | |
8d2d4c67 |
224 | This creates the method based on the criteria in C<%options>, |
96e38ba6 |
225 | these options are: |
226 | |
227 | =over 4 |
228 | |
229 | =item I<attribute> |
230 | |
8d2d4c67 |
231 | This must be an instance of C<Class::MOP::Attribute> which this |
96e38ba6 |
232 | accessor is being generated for. This paramter is B<required>. |
233 | |
234 | =item I<accessor_type> |
235 | |
8d2d4c67 |
236 | This is a string from the following set; reader, writer, accessor, |
237 | predicate or clearer. This is used to determine which type of |
96e38ba6 |
238 | method is to be generated. |
239 | |
240 | =item I<is_inline> |
241 | |
242 | This is a boolean to indicate if the method should be generated |
243 | as a closure, or as a more optimized inline version. |
244 | |
245 | =back |
ba38bf08 |
246 | |
247 | =item B<accessor_type> |
248 | |
96e38ba6 |
249 | This returns the accessor type which was passed into C<new>. |
250 | |
d90b42a6 |
251 | =item B<is_inline> |
ba38bf08 |
252 | |
96e38ba6 |
253 | This returns the boolean which was passed into C<new>. |
254 | |
ba38bf08 |
255 | =item B<associated_attribute> |
256 | |
96e38ba6 |
257 | This returns the attribute instance which was passed into C<new>. |
258 | |
565f0cbb |
259 | =item B<initialize_body> |
96e38ba6 |
260 | |
8d2d4c67 |
261 | This will actually generate the method based on the specified |
96e38ba6 |
262 | criteria passed to the constructor. |
263 | |
264 | =back |
265 | |
266 | =head2 Method Generators |
267 | |
8d2d4c67 |
268 | These methods will generate appropriate code references for |
269 | the various types of accessors which are supported by |
96e38ba6 |
270 | C<Class::MOP::Attribute>. The names pretty much explain it all. |
271 | |
272 | =over 4 |
273 | |
ba38bf08 |
274 | =item B<generate_accessor_method> |
275 | |
276 | =item B<generate_accessor_method_inline> |
277 | |
278 | =item B<generate_clearer_method> |
279 | |
280 | =item B<generate_clearer_method_inline> |
281 | |
282 | =item B<generate_predicate_method> |
283 | |
284 | =item B<generate_predicate_method_inline> |
285 | |
286 | =item B<generate_reader_method> |
287 | |
288 | =item B<generate_reader_method_inline> |
289 | |
290 | =item B<generate_writer_method> |
291 | |
292 | =item B<generate_writer_method_inline> |
293 | |
294 | =back |
295 | |
296 | =head1 AUTHORS |
297 | |
298 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
299 | |
ba38bf08 |
300 | =head1 COPYRIGHT AND LICENSE |
301 | |
69e3ab0a |
302 | Copyright 2006-2008 by Infinity Interactive, Inc. |
ba38bf08 |
303 | |
304 | L<http://www.iinteractive.com> |
305 | |
306 | This library is free software; you can redistribute it and/or modify |
8d2d4c67 |
307 | it under the same terms as Perl itself. |
ba38bf08 |
308 | |
309 | =cut |
310 | |