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