working without XS
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
CommitLineData
ba38bf08 1
2package Class::MOP::Method::Accessor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken';
9
565f0cbb 10our $VERSION = '0.02';
ba38bf08 11our $AUTHORITY = 'cpan:STEVAN';
12
565f0cbb 13use base 'Class::MOP::Method::Generated';
ba38bf08 14
ba38bf08 15sub 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 51sub associated_attribute { (shift)->{'$!attribute'} }
52sub accessor_type { (shift)->{'$!accessor_type'} }
ba38bf08 53
8d2d4c67 54## factory
ba38bf08 55
565f0cbb 56sub 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
72sub 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
80sub 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
88sub generate_writer_method {
8d2d4c67 89 my $attr = (shift)->associated_attribute;
ba38bf08 90 return sub {
91 $attr->set_value($_[0], $_[1]);
92 };
93}
94
95sub generate_predicate_method {
8d2d4c67 96 my $attr = (shift)->associated_attribute;
97 return sub {
3545c727 98 $attr->has_value($_[0])
ba38bf08 99 };
100}
101
102sub 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
112sub 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
126sub 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
140sub 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
154sub 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
167sub 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
1801;
181
182__END__
183
184=pod
185
8d2d4c67 186=head1 NAME
ba38bf08 187
188Class::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 204This is a C<Class::MOP::Method> subclass which is used interally
205by C<Class::MOP::Attribute> to generate accessor code. It can
206handle generation of readers, writers, predicate and clearer
96e38ba6 207methods, 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 215This creates the method based on the criteria in C<%options>,
96e38ba6 216these options are:
217
218=over 4
219
220=item I<attribute>
221
8d2d4c67 222This must be an instance of C<Class::MOP::Attribute> which this
96e38ba6 223accessor is being generated for. This paramter is B<required>.
224
225=item I<accessor_type>
226
8d2d4c67 227This is a string from the following set; reader, writer, accessor,
228predicate or clearer. This is used to determine which type of
96e38ba6 229method is to be generated.
230
231=item I<is_inline>
232
233This is a boolean to indicate if the method should be generated
234as a closure, or as a more optimized inline version.
235
236=back
ba38bf08 237
238=item B<accessor_type>
239
96e38ba6 240This returns the accessor type which was passed into C<new>.
241
d90b42a6 242=item B<is_inline>
ba38bf08 243
96e38ba6 244This returns the boolean which was passed into C<new>.
245
ba38bf08 246=item B<associated_attribute>
247
96e38ba6 248This returns the attribute instance which was passed into C<new>.
249
565f0cbb 250=item B<initialize_body>
96e38ba6 251
8d2d4c67 252This will actually generate the method based on the specified
96e38ba6 253criteria passed to the constructor.
254
255=back
256
257=head2 Method Generators
258
8d2d4c67 259These methods will generate appropriate code references for
260the various types of accessors which are supported by
96e38ba6 261C<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
289Stevan Little E<lt>stevan@iinteractive.comE<gt>
290
ba38bf08 291=head1 COPYRIGHT AND LICENSE
292
69e3ab0a 293Copyright 2006-2008 by Infinity Interactive, Inc.
ba38bf08 294
295L<http://www.iinteractive.com>
296
297This library is free software; you can redistribute it and/or modify
8d2d4c67 298it under the same terms as Perl itself.
ba38bf08 299
300=cut
301