62eaab858cd0ccd28e9fdb444f41e2540b68c7a2
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
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
10 our $VERSION   = '0.02';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Method::Generated';
14
15 sub new {
16     my $class   = shift;
17     my %options = @_;
18
19     (exists $options{attribute})
20         || confess "You must supply an attribute to construct with";
21
22     (exists $options{accessor_type})
23         || confess "You must supply an accessor_type to construct with";
24
25     (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
26         || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
27
28     my $self = bless {
29         # from our superclass
30         '&!body'          => undef,
31         # specific to this subclass
32         '$!attribute'     => $options{attribute},
33         '$!is_inline'     => ($options{is_inline} || 0),
34         '$!accessor_type' => $options{accessor_type},
35     } => $class;
36
37     # we don't want this creating
38     # a cycle in the code, if not
39     # needed
40     weaken($self->{'$!attribute'});
41
42     $self->initialize_body;
43
44     return $self;
45 }
46
47 ## accessors
48
49 sub associated_attribute { (shift)->{'$!attribute'}     }
50 sub accessor_type        { (shift)->{'$!accessor_type'} }
51
52 ## factory
53
54 sub initialize_body {
55     my $self = shift;
56
57     my $method_name = join "_" => (
58         'generate',
59         $self->accessor_type,
60         'method',
61         ($self->is_inline ? 'inline' : ())
62     );
63
64     eval { $self->{'&!body'} = $self->$method_name() };
65     die $@ if $@;
66 }
67
68 ## generators
69
70 sub generate_accessor_method {
71     my $attr = (shift)->associated_attribute;
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 {
79     my $attr = (shift)->associated_attribute;
80     return sub {
81         confess "Cannot assign a value to a read-only accessor" if @_ > 1;
82         $attr->get_value($_[0]);
83     };
84 }
85
86 sub generate_writer_method {
87     my $attr = (shift)->associated_attribute;
88     return sub {
89         $attr->set_value($_[0], $_[1]);
90     };
91 }
92
93 sub generate_predicate_method {
94     my $attr = (shift)->associated_attribute;
95     return sub {
96         $attr->has_value($_[0])
97     };
98 }
99
100 sub generate_clearer_method {
101     my $attr = (shift)->associated_attribute;
102     return sub {
103         $attr->clear_value($_[0])
104     };
105 }
106
107 ## Inline methods
108
109
110 sub generate_accessor_method_inline {
111     my $attr          = (shift)->associated_attribute;
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 {
125     my $attr          = (shift)->associated_attribute;
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 {
139     my $attr          = (shift)->associated_attribute;
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 {
153     my $attr          = (shift)->associated_attribute;
154     my $attr_name     = $attr->name;
155     my $meta_instance = $attr->associated_class->instance_metaclass;
156
157     my $code = eval 'sub {' .
158        $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
159     . '}';
160     confess "Could not generate inline predicate because : $@" if $@;
161
162     return $code;
163 }
164
165 sub generate_clearer_method_inline {
166     my $attr          = (shift)->associated_attribute;
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
184 =head1 NAME
185
186 Class::MOP::Method::Accessor - Method Meta Object for accessors
187
188 =head1 SYNOPSIS
189
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     );
197
198     $reader->body->($instance); # call the reader method
199
200 =head1 DESCRIPTION
201
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
205 methods, both as closures and as more optimized inline methods.
206
207 =head1 METHODS
208
209 =over 4
210
211 =item B<new (%options)>
212
213 This creates the method based on the criteria in C<%options>,
214 these options are:
215
216 =over 4
217
218 =item I<attribute>
219
220 This must be an instance of C<Class::MOP::Attribute> which this
221 accessor is being generated for. This paramter is B<required>.
222
223 =item I<accessor_type>
224
225 This is a string from the following set; reader, writer, accessor,
226 predicate or clearer. This is used to determine which type of
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
235
236 =item B<accessor_type>
237
238 This returns the accessor type which was passed into C<new>.
239
240 =item B<is_inline>
241
242 This returns the boolean which was passed into C<new>.
243
244 =item B<associated_attribute>
245
246 This returns the attribute instance which was passed into C<new>.
247
248 =item B<initialize_body>
249
250 This will actually generate the method based on the specified
251 criteria passed to the constructor.
252
253 =back
254
255 =head2 Method Generators
256
257 These methods will generate appropriate code references for
258 the various types of accessors which are supported by
259 C<Class::MOP::Attribute>. The names pretty much explain it all.
260
261 =over 4
262
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
289 =head1 COPYRIGHT AND LICENSE
290
291 Copyright 2006-2008 by Infinity Interactive, Inc.
292
293 L<http://www.iinteractive.com>
294
295 This library is free software; you can redistribute it and/or modify
296 it under the same terms as Perl itself.
297
298 =cut
299