2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.73';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Generated';
20 (exists $options{attribute})
21 || confess "You must supply an attribute to construct with";
23 (exists $options{accessor_type})
24 || confess "You must supply an accessor_type to construct with";
26 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
27 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
29 ($options{package_name} && $options{name})
30 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
32 my $self = $class->_new(\%options);
34 # we don't want this creating
35 # a cycle in the code, if not
37 weaken($self->{'attribute'});
39 $self->initialize_body;
46 my $options = @_ == 1 ? $_[0] : {@_};
48 $options->{is_inline} ||= 0;
50 return bless $options, $class;
55 sub associated_attribute { (shift)->{'attribute'} }
56 sub accessor_type { (shift)->{'accessor_type'} }
63 my $method_name = join "_" => (
67 ($self->is_inline ? 'inline' : ())
70 eval { $self->{'body'} = $self->$method_name() };
76 sub generate_accessor_method {
77 my $attr = (shift)->associated_attribute;
79 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
80 $attr->get_value($_[0]);
84 sub generate_reader_method {
85 my $attr = (shift)->associated_attribute;
87 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
88 $attr->get_value($_[0]);
92 sub generate_writer_method {
93 my $attr = (shift)->associated_attribute;
95 $attr->set_value($_[0], $_[1]);
99 sub generate_predicate_method {
100 my $attr = (shift)->associated_attribute;
102 $attr->has_value($_[0])
106 sub generate_clearer_method {
107 my $attr = (shift)->associated_attribute;
109 $attr->clear_value($_[0])
116 sub generate_accessor_method_inline {
117 my $attr = (shift)->associated_attribute;
118 my $attr_name = $attr->name;
119 my $meta_instance = $attr->associated_class->instance_metaclass;
121 my $code = eval 'sub {'
122 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
123 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
125 confess "Could not generate inline accessor because : $@" if $@;
130 sub generate_reader_method_inline {
131 my $attr = (shift)->associated_attribute;
132 my $attr_name = $attr->name;
133 my $meta_instance = $attr->associated_class->instance_metaclass;
135 my $code = eval 'sub {'
136 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
137 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
139 confess "Could not generate inline accessor because : $@" if $@;
144 sub generate_writer_method_inline {
145 my $attr = (shift)->associated_attribute;
146 my $attr_name = $attr->name;
147 my $meta_instance = $attr->associated_class->instance_metaclass;
149 my $code = eval 'sub {'
150 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
152 confess "Could not generate inline accessor because : $@" if $@;
158 sub generate_predicate_method_inline {
159 my $attr = (shift)->associated_attribute;
160 my $attr_name = $attr->name;
161 my $meta_instance = $attr->associated_class->instance_metaclass;
163 my $code = eval 'sub {' .
164 $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
166 confess "Could not generate inline predicate because : $@" if $@;
171 sub generate_clearer_method_inline {
172 my $attr = (shift)->associated_attribute;
173 my $attr_name = $attr->name;
174 my $meta_instance = $attr->associated_class->instance_metaclass;
176 my $code = eval 'sub {'
177 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
179 confess "Could not generate inline clearer because : $@" if $@;
192 Class::MOP::Method::Accessor - Method Meta Object for accessors
196 use Class::MOP::Method::Accessor;
198 my $reader = Class::MOP::Method::Accessor->new(
199 attribute => $attribute,
201 accessor_type => 'reader',
204 $reader->body->execute($instance); # call the reader method
208 This is a C<Class::MOP::Method> subclass which is used interally
209 by C<Class::MOP::Attribute> to generate accessor code. It can
210 handle generation of readers, writers, predicate and clearer
211 methods, both as closures and as more optimized inline methods.
217 =item B<new (%options)>
219 This creates the method based on the criteria in C<%options>,
226 This must be an instance of C<Class::MOP::Attribute> which this
227 accessor is being generated for. This paramter is B<required>.
229 =item I<accessor_type>
231 This is a string from the following set; reader, writer, accessor,
232 predicate or clearer. This is used to determine which type of
233 method is to be generated.
237 This is a boolean to indicate if the method should be generated
238 as a closure, or as a more optimized inline version.
242 =item B<accessor_type>
244 This returns the accessor type which was passed into C<new>.
248 This returns the boolean which was passed into C<new>.
250 =item B<associated_attribute>
252 This returns the attribute instance which was passed into C<new>.
254 =item B<initialize_body>
256 This will actually generate the method based on the specified
257 criteria passed to the constructor.
261 =head2 Method Generators
263 These methods will generate appropriate code references for
264 the various types of accessors which are supported by
265 C<Class::MOP::Attribute>. The names pretty much explain it all.
269 =item B<generate_accessor_method>
271 =item B<generate_accessor_method_inline>
273 =item B<generate_clearer_method>
275 =item B<generate_clearer_method_inline>
277 =item B<generate_predicate_method>
279 =item B<generate_predicate_method_inline>
281 =item B<generate_reader_method>
283 =item B<generate_reader_method_inline>
285 =item B<generate_writer_method>
287 =item B<generate_writer_method_inline>
293 Stevan Little E<lt>stevan@iinteractive.comE<gt>
295 =head1 COPYRIGHT AND LICENSE
297 Copyright 2006-2008 by Infinity Interactive, Inc.
299 L<http://www.iinteractive.com>
301 This library is free software; you can redistribute it and/or modify
302 it under the same terms as Perl itself.