2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.64';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method::Generated';
19 (exists $options{attribute})
20 || confess "You must supply an attribute to construct with";
22 (exists $options{accessor_type})
23 || confess "You must supply an accessor_type to construct with";
25 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
26 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
28 ($options{package_name} && $options{name})
29 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
34 '$!package_name' => $options{package_name},
35 '$!name' => $options{name},
36 # specific to this subclass
37 '$!attribute' => $options{attribute},
38 '$!is_inline' => ($options{is_inline} || 0),
39 '$!accessor_type' => $options{accessor_type},
42 # we don't want this creating
43 # a cycle in the code, if not
45 weaken($self->{'$!attribute'});
47 $self->initialize_body;
54 sub associated_attribute { (shift)->{'$!attribute'} }
55 sub accessor_type { (shift)->{'$!accessor_type'} }
62 my $method_name = join "_" => (
66 ($self->is_inline ? 'inline' : ())
69 eval { $self->{'&!body'} = $self->$method_name() };
75 sub generate_accessor_method {
76 my $attr = (shift)->associated_attribute;
78 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
79 $attr->get_value($_[0]);
83 sub generate_reader_method {
84 my $attr = (shift)->associated_attribute;
86 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
87 $attr->get_value($_[0]);
91 sub generate_writer_method {
92 my $attr = (shift)->associated_attribute;
94 $attr->set_value($_[0], $_[1]);
98 sub generate_predicate_method {
99 my $attr = (shift)->associated_attribute;
101 $attr->has_value($_[0])
105 sub generate_clearer_method {
106 my $attr = (shift)->associated_attribute;
108 $attr->clear_value($_[0])
115 sub generate_accessor_method_inline {
116 my $attr = (shift)->associated_attribute;
117 my $attr_name = $attr->name;
118 my $meta_instance = $attr->associated_class->instance_metaclass;
120 my $code = eval 'sub {'
121 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
122 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
124 confess "Could not generate inline accessor because : $@" if $@;
129 sub generate_reader_method_inline {
130 my $attr = (shift)->associated_attribute;
131 my $attr_name = $attr->name;
132 my $meta_instance = $attr->associated_class->instance_metaclass;
134 my $code = eval 'sub {'
135 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
136 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
138 confess "Could not generate inline accessor because : $@" if $@;
143 sub generate_writer_method_inline {
144 my $attr = (shift)->associated_attribute;
145 my $attr_name = $attr->name;
146 my $meta_instance = $attr->associated_class->instance_metaclass;
148 my $code = eval 'sub {'
149 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
151 confess "Could not generate inline accessor because : $@" if $@;
157 sub generate_predicate_method_inline {
158 my $attr = (shift)->associated_attribute;
159 my $attr_name = $attr->name;
160 my $meta_instance = $attr->associated_class->instance_metaclass;
162 my $code = eval 'sub {' .
163 $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
165 confess "Could not generate inline predicate because : $@" if $@;
170 sub generate_clearer_method_inline {
171 my $attr = (shift)->associated_attribute;
172 my $attr_name = $attr->name;
173 my $meta_instance = $attr->associated_class->instance_metaclass;
175 my $code = eval 'sub {'
176 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
178 confess "Could not generate inline clearer because : $@" if $@;
191 Class::MOP::Method::Accessor - Method Meta Object for accessors
195 use Class::MOP::Method::Accessor;
197 my $reader = Class::MOP::Method::Accessor->new(
198 attribute => $attribute,
200 accessor_type => 'reader',
203 $reader->body->($instance); # call the reader method
207 This is a C<Class::MOP::Method> subclass which is used interally
208 by C<Class::MOP::Attribute> to generate accessor code. It can
209 handle generation of readers, writers, predicate and clearer
210 methods, both as closures and as more optimized inline methods.
216 =item B<new (%options)>
218 This creates the method based on the criteria in C<%options>,
225 This must be an instance of C<Class::MOP::Attribute> which this
226 accessor is being generated for. This paramter is B<required>.
228 =item I<accessor_type>
230 This is a string from the following set; reader, writer, accessor,
231 predicate or clearer. This is used to determine which type of
232 method is to be generated.
236 This is a boolean to indicate if the method should be generated
237 as a closure, or as a more optimized inline version.
241 =item B<accessor_type>
243 This returns the accessor type which was passed into C<new>.
247 This returns the boolean which was passed into C<new>.
249 =item B<associated_attribute>
251 This returns the attribute instance which was passed into C<new>.
253 =item B<initialize_body>
255 This will actually generate the method based on the specified
256 criteria passed to the constructor.
260 =head2 Method Generators
262 These methods will generate appropriate code references for
263 the various types of accessors which are supported by
264 C<Class::MOP::Attribute>. The names pretty much explain it all.
268 =item B<generate_accessor_method>
270 =item B<generate_accessor_method_inline>
272 =item B<generate_clearer_method>
274 =item B<generate_clearer_method_inline>
276 =item B<generate_predicate_method>
278 =item B<generate_predicate_method_inline>
280 =item B<generate_reader_method>
282 =item B<generate_reader_method_inline>
284 =item B<generate_writer_method>
286 =item B<generate_writer_method_inline>
292 Stevan Little E<lt>stevan@iinteractive.comE<gt>
294 =head1 COPYRIGHT AND LICENSE
296 Copyright 2006-2008 by Infinity Interactive, Inc.
298 L<http://www.iinteractive.com>
300 This library is free software; you can redistribute it and/or modify
301 it under the same terms as Perl itself.