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