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