make inlining a bit more easily extensible
[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';
15961c86 9use Try::Tiny;
ba38bf08 10
a9f48b4b 11our $VERSION = '1.11';
d519662a 12$VERSION = eval $VERSION;
ba38bf08 13our $AUTHORITY = 'cpan:STEVAN';
14
565f0cbb 15use base 'Class::MOP::Method::Generated';
ba38bf08 16
ba38bf08 17sub new {
18 my $class = shift;
19 my %options = @_;
8d2d4c67 20
ba38bf08 21 (exists $options{attribute})
22 || confess "You must supply an attribute to construct with";
8d2d4c67 23
ba38bf08 24 (exists $options{accessor_type})
8d2d4c67 25 || confess "You must supply an accessor_type to construct with";
26
ba38bf08 27 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
8d2d4c67 28 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
29
b38f3848 30 ($options{package_name} && $options{name})
32202ce2 31 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 32
0bfc85b8 33 my $self = $class->_new(\%options);
8d2d4c67 34
35 # we don't want this creating
36 # a cycle in the code, if not
ba38bf08 37 # needed
8683db0e 38 weaken($self->{'attribute'});
8d2d4c67 39
e9497117 40 $self->_initialize_body;
8d2d4c67 41
ba38bf08 42 return $self;
43}
44
a9e38dc7 45sub _new {
0bfc85b8 46 my $class = shift;
a9e38dc7 47
ec9e38e5 48 return Class::MOP::Class->initialize($class)->new_object(@_)
812d58f9 49 if $class ne __PACKAGE__;
a9e38dc7 50
ec9e38e5 51 my $params = @_ == 1 ? $_[0] : {@_};
52
53 return bless {
54 # inherited from Class::MOP::Method
55 body => $params->{body},
56 associated_metaclass => $params->{associated_metaclass},
57 package_name => $params->{package_name},
58 name => $params->{name},
59 original_method => $params->{original_method},
60
61 # inherit from Class::MOP::Generated
62 is_inline => $params->{is_inline} || 0,
63 definition_context => $params->{definition_context},
64
65 # defined in this class
66 attribute => $params->{attribute},
67 accessor_type => $params->{accessor_type},
68 } => $class;
a9e38dc7 69}
70
ba38bf08 71## accessors
72
8683db0e 73sub associated_attribute { (shift)->{'attribute'} }
74sub accessor_type { (shift)->{'accessor_type'} }
ba38bf08 75
8d2d4c67 76## factory
ba38bf08 77
e9497117 78sub _initialize_body {
ba38bf08 79 my $self = shift;
8d2d4c67 80
ba38bf08 81 my $method_name = join "_" => (
afc92ac6 82 '_generate',
8d2d4c67 83 $self->accessor_type,
ba38bf08 84 'method',
d90b42a6 85 ($self->is_inline ? 'inline' : ())
ba38bf08 86 );
8d2d4c67 87
1be5f78f 88 $self->{'body'} = $self->$method_name();
ba38bf08 89}
90
91## generators
92
afc92ac6 93sub _generate_accessor_method {
5e5102f1 94 my $self = shift;
95 my $attr = $self->associated_attribute;
ba38bf08 96
8d2d4c67 97 return sub {
5e5102f1 98 if (@_ >= 2) {
99 $attr->set_value($_[0], $_[1]);
100 }
ba38bf08 101 $attr->get_value($_[0]);
8d2d4c67 102 };
ba38bf08 103}
104
afc92ac6 105sub _generate_accessor_method_inline {
03a3092d 106 my $self = shift;
107 my $attr = $self->associated_attribute;
ba38bf08 108
5e5102f1 109 return try {
5efa6a46 110 $self->_compile_code([
111 'sub {',
5e5102f1 112 'if (@_ >= 2) {',
113 $attr->_inline_set_value('$_[0]', '$_[1]'),
114 '}',
115 $attr->_inline_get_value('$_[0]'),
5efa6a46 116 '}',
117 ]);
15961c86 118 }
119 catch {
120 confess "Could not generate inline accessor because : $_";
121 };
5e5102f1 122}
123
124sub _generate_reader_method {
125 my $self = shift;
126 my $attr = $self->associated_attribute;
a6eef5a3 127
5e5102f1 128 return sub {
129 confess "Cannot assign a value to a read-only accessor"
130 if @_ > 1;
131 $attr->get_value($_[0]);
132 };
ba38bf08 133}
134
afc92ac6 135sub _generate_reader_method_inline {
03a3092d 136 my $self = shift;
137 my $attr = $self->associated_attribute;
ba38bf08 138
5e5102f1 139 return try {
5efa6a46 140 $self->_compile_code([
141 'sub {',
142 'confess "Cannot assign a value to a read-only accessor"',
143 'if @_ > 1;',
5e5102f1 144 $attr->_inline_get_value('$_[0]'),
5efa6a46 145 '}',
146 ]);
15961c86 147 }
148 catch {
149 confess "Could not generate inline reader because : $_";
150 };
5e5102f1 151}
a6eef5a3 152
5e5102f1 153sub _generate_writer_method {
154 my $self = shift;
155 my $attr = $self->associated_attribute;
156
157 return sub {
158 $attr->set_value($_[0], $_[1]);
159 };
ba38bf08 160}
161
afc92ac6 162sub _generate_writer_method_inline {
03a3092d 163 my $self = shift;
164 my $attr = $self->associated_attribute;
ba38bf08 165
5e5102f1 166 return try {
5efa6a46 167 $self->_compile_code([
168 'sub {',
5e5102f1 169 $attr->_inline_set_value('$_[0]', '$_[1]'),
5efa6a46 170 '}',
171 ]);
15961c86 172 }
173 catch {
174 confess "Could not generate inline writer because : $_";
175 };
5e5102f1 176}
177
178sub _generate_predicate_method {
179 my $self = shift;
180 my $attr = $self->associated_attribute;
a6eef5a3 181
5e5102f1 182 return sub {
183 $attr->has_value($_[0])
184 };
ba38bf08 185}
186
afc92ac6 187sub _generate_predicate_method_inline {
03a3092d 188 my $self = shift;
189 my $attr = $self->associated_attribute;
ba38bf08 190
5e5102f1 191 return try {
5efa6a46 192 $self->_compile_code([
193 'sub {',
5e5102f1 194 $attr->_inline_has_value('$_[0]'),
5efa6a46 195 '}',
196 ]);
15961c86 197 }
198 catch {
199 confess "Could not generate inline predicate because : $_";
200 };
5e5102f1 201}
a6eef5a3 202
5e5102f1 203sub _generate_clearer_method {
204 my $self = shift;
205 my $attr = $self->associated_attribute;
206
207 return sub {
208 $attr->clear_value($_[0])
209 };
ba38bf08 210}
211
afc92ac6 212sub _generate_clearer_method_inline {
03a3092d 213 my $self = shift;
214 my $attr = $self->associated_attribute;
ba38bf08 215
5e5102f1 216 return try {
5efa6a46 217 $self->_compile_code([
218 'sub {',
5e5102f1 219 $attr->_inline_clear_value('$_[0]'),
5efa6a46 220 '}',
221 ]);
15961c86 222 }
223 catch {
224 confess "Could not generate inline clearer because : $_";
225 };
ba38bf08 226}
227
2281;
229
230__END__
231
232=pod
233
8d2d4c67 234=head1 NAME
ba38bf08 235
236Class::MOP::Method::Accessor - Method Meta Object for accessors
237
238=head1 SYNOPSIS
239
96e38ba6 240 use Class::MOP::Method::Accessor;
241
242 my $reader = Class::MOP::Method::Accessor->new(
243 attribute => $attribute,
244 is_inline => 1,
245 accessor_type => 'reader',
246 );
8d2d4c67 247
b7045e66 248 $reader->body->execute($instance); # call the reader method
ba38bf08 249
250=head1 DESCRIPTION
251
6f241a63 252This is a subclass of C<Class::MOP::Method> which is used by
1385ad9d 253C<Class::MOP::Attribute> to generate accessor code. It handles
254generation of readers, writers, predicates and clearers. For each type
255of method, it can either create a subroutine reference, or actually
256inline code by generating a string and C<eval>'ing it.
96e38ba6 257
ba38bf08 258=head1 METHODS
259
260=over 4
261
1385ad9d 262=item B<< Class::MOP::Method::Accessor->new(%options) >>
ba38bf08 263
1385ad9d 264This returns a new C<Class::MOP::Method::Accessor> based on the
265C<%options> provided.
96e38ba6 266
267=over 4
268
9258c369 269=item * attribute
96e38ba6 270
1385ad9d 271This is the C<Class::MOP::Attribute> for which accessors are being
272generated. This option is required.
96e38ba6 273
9258c369 274=item * accessor_type
96e38ba6 275
1385ad9d 276This is a string which should be one of "reader", "writer",
277"accessor", "predicate", or "clearer". This is the type of method
278being generated. This option is required.
96e38ba6 279
9258c369 280=item * is_inline
96e38ba6 281
1385ad9d 282This indicates whether or not the accessor should be inlined. This
cb8d08c6 283defaults to false.
96e38ba6 284
9258c369 285=item * name
286
287The method name (without a package name). This is required.
288
289=item * package_name
290
291The package name for the method. This is required.
292
96e38ba6 293=back
ba38bf08 294
1385ad9d 295=item B<< $metamethod->accessor_type >>
ba38bf08 296
1385ad9d 297Returns the accessor type which was passed to C<new>.
96e38ba6 298
1385ad9d 299=item B<< $metamethod->is_inline >>
ba38bf08 300
1385ad9d 301Returns a boolean indicating whether or not the accessor is inlined.
96e38ba6 302
1385ad9d 303=item B<< $metamethod->associated_attribute >>
ba38bf08 304
1385ad9d 305This returns the L<Class::MOP::Attribute> object which was passed to
306C<new>.
96e38ba6 307
1385ad9d 308=item B<< $metamethod->body >>
96e38ba6 309
1385ad9d 310The method itself is I<generated> when the accessor object is
311constructed.
ba38bf08 312
313=back
314
315=head1 AUTHORS
316
317Stevan Little E<lt>stevan@iinteractive.comE<gt>
318
ba38bf08 319=head1 COPYRIGHT AND LICENSE
320
3e2c8600 321Copyright 2006-2010 by Infinity Interactive, Inc.
ba38bf08 322
323L<http://www.iinteractive.com>
324
325This library is free software; you can redistribute it and/or modify
8d2d4c67 326it under the same terms as Perl itself.
ba38bf08 327
328=cut
329