copyright date changes on Class::MOP
[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,
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 49sub associated_attribute { (shift)->{'$!attribute'} }
50sub accessor_type { (shift)->{'$!accessor_type'} }
ba38bf08 51
8d2d4c67 52## factory
ba38bf08 53
565f0cbb 54sub 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
70sub 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
78sub 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
86sub generate_writer_method {
8d2d4c67 87 my $attr = (shift)->associated_attribute;
ba38bf08 88 return sub {
89 $attr->set_value($_[0], $_[1]);
90 };
91}
92
93sub generate_predicate_method {
8d2d4c67 94 my $attr = (shift)->associated_attribute;
95 return sub {
3545c727 96 $attr->has_value($_[0])
ba38bf08 97 };
98}
99
100sub 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
110sub 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
124sub 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
138sub 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
152sub 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
165sub 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
1781;
179
180__END__
181
182=pod
183
8d2d4c67 184=head1 NAME
ba38bf08 185
186Class::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 202This is a C<Class::MOP::Method> subclass which is used interally
203by C<Class::MOP::Attribute> to generate accessor code. It can
204handle generation of readers, writers, predicate and clearer
96e38ba6 205methods, 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 213This creates the method based on the criteria in C<%options>,
96e38ba6 214these options are:
215
216=over 4
217
218=item I<attribute>
219
8d2d4c67 220This must be an instance of C<Class::MOP::Attribute> which this
96e38ba6 221accessor is being generated for. This paramter is B<required>.
222
223=item I<accessor_type>
224
8d2d4c67 225This is a string from the following set; reader, writer, accessor,
226predicate or clearer. This is used to determine which type of
96e38ba6 227method is to be generated.
228
229=item I<is_inline>
230
231This is a boolean to indicate if the method should be generated
232as a closure, or as a more optimized inline version.
233
234=back
ba38bf08 235
236=item B<accessor_type>
237
96e38ba6 238This returns the accessor type which was passed into C<new>.
239
d90b42a6 240=item B<is_inline>
ba38bf08 241
96e38ba6 242This returns the boolean which was passed into C<new>.
243
ba38bf08 244=item B<associated_attribute>
245
96e38ba6 246This returns the attribute instance which was passed into C<new>.
247
565f0cbb 248=item B<initialize_body>
96e38ba6 249
8d2d4c67 250This will actually generate the method based on the specified
96e38ba6 251criteria passed to the constructor.
252
253=back
254
255=head2 Method Generators
256
8d2d4c67 257These methods will generate appropriate code references for
258the various types of accessors which are supported by
96e38ba6 259C<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
287Stevan Little E<lt>stevan@iinteractive.comE<gt>
288
ba38bf08 289=head1 COPYRIGHT AND LICENSE
290
69e3ab0a 291Copyright 2006-2008 by Infinity Interactive, Inc.
ba38bf08 292
293L<http://www.iinteractive.com>
294
295This library is free software; you can redistribute it and/or modify
8d2d4c67 296it under the same terms as Perl itself.
ba38bf08 297
298=cut
299