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