2 package Class::MOP::Instance;
7 use Scalar::Util 'weaken', 'blessed';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Object';
15 # make this not a valid method name, to avoid (most) attribute conflicts
16 my $RESERVED_MOP_SLOT = '<<MOP>>';
19 my ($class, @args) = @_;
22 unshift @args, "associated_metaclass";
23 } elsif ( @args >= 2 && blessed($args[0]) && $args[0]->isa("Class::MOP::Class") ) {
25 my ( $meta, @attrs ) = @args;
26 @args = ( associated_metaclass => $meta, attributes => \@attrs );
31 $options{slots} ||= [ map { $_->slots } @{ $options{attributes} || [] } ];
32 $options{slot_hash} = { map { $_ => undef } @{ $options{slots} } }; # FIXME lazy_build
39 my $options = $class->BUILDARGS(@_);
41 # FIXME replace with a proper constructor
42 my $instance = $class->_new(%$options);
44 # FIXME weak_ref => 1,
45 weaken($instance->{'associated_metaclass'});
52 return Class::MOP::Class->initialize($class)->new_object(@_)
53 if $class ne __PACKAGE__;
55 my $params = @_ == 1 ? $_[0] : {@_};
58 # I am not sure that it makes
59 # sense to pass in the meta
60 # The ideal would be to just
61 # pass in the class name, but
62 # that is placing too much of
63 # an assumption on bless(),
64 # which is *probably* a safe
65 # assumption,.. but you can
67 'associated_metaclass' => $params->{associated_metaclass},
68 'attributes' => $params->{attributes},
69 'slots' => $params->{slots},
70 'slot_hash' => $params->{slot_hash},
74 sub _class_name { $_[0]->{_class_name} ||= $_[0]->associated_metaclass->name }
76 sub associated_metaclass { $_[0]{'associated_metaclass'} }
80 bless {}, $self->_class_name;
84 my ($self, $instance) = @_;
85 bless { %$instance }, $self->_class_name;
88 # operations on meta instance
92 return @{$self->{'slots'}};
95 sub get_all_attributes {
97 return @{$self->{attributes}};
101 my ($self, $slot_name) = @_;
102 exists $self->{'slot_hash'}->{$slot_name};
105 # operations on created instances
108 my ($self, $instance, $slot_name) = @_;
109 $instance->{$slot_name};
113 my ($self, $instance, $slot_name, $value) = @_;
114 $instance->{$slot_name} = $value;
117 sub initialize_slot {
118 my ($self, $instance, $slot_name) = @_;
122 sub deinitialize_slot {
123 my ( $self, $instance, $slot_name ) = @_;
124 delete $instance->{$slot_name};
127 sub initialize_all_slots {
128 my ($self, $instance) = @_;
129 foreach my $slot_name ($self->get_all_slots) {
130 $self->initialize_slot($instance, $slot_name);
134 sub deinitialize_all_slots {
135 my ($self, $instance) = @_;
136 foreach my $slot_name ($self->get_all_slots) {
137 $self->deinitialize_slot($instance, $slot_name);
141 sub is_slot_initialized {
142 my ($self, $instance, $slot_name, $value) = @_;
143 exists $instance->{$slot_name};
146 sub weaken_slot_value {
147 my ($self, $instance, $slot_name) = @_;
148 weaken $instance->{$slot_name};
151 sub strengthen_slot_value {
152 my ($self, $instance, $slot_name) = @_;
153 $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name));
156 sub rebless_instance_structure {
157 my ($self, $instance, $metaclass) = @_;
159 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
160 bless $_[1], $metaclass->name;
163 sub is_dependent_on_superclasses {
164 return; # for meta instances that require updates on inherited slot changes
168 my ($self, $instance) = @_;
169 $self->get_slot_value($instance, $RESERVED_MOP_SLOT);
173 my ($self, $instance, $value) = @_;
174 $self->set_slot_value($instance, $RESERVED_MOP_SLOT, $value);
177 sub _clear_mop_slot {
178 my ($self, $instance) = @_;
179 $self->deinitialize_slot($instance, $RESERVED_MOP_SLOT);
182 # inlinable operation snippets
184 sub is_inlinable { 1 }
186 sub inline_create_instance {
187 my ($self, $class_variable) = @_;
188 'bless {} => ' . $class_variable;
191 sub inline_slot_access {
192 my ($self, $instance, $slot_name) = @_;
193 sprintf q[%s->{"%s"}], $instance, quotemeta($slot_name);
196 sub inline_get_is_lvalue { 1 }
198 sub inline_get_slot_value {
199 my ($self, $instance, $slot_name) = @_;
200 $self->inline_slot_access($instance, $slot_name);
203 sub inline_set_slot_value {
204 my ($self, $instance, $slot_name, $value) = @_;
205 $self->inline_slot_access($instance, $slot_name) . " = $value",
208 sub inline_initialize_slot {
209 my ($self, $instance, $slot_name) = @_;
213 sub inline_deinitialize_slot {
214 my ($self, $instance, $slot_name) = @_;
215 "delete " . $self->inline_slot_access($instance, $slot_name);
217 sub inline_is_slot_initialized {
218 my ($self, $instance, $slot_name) = @_;
219 "exists " . $self->inline_slot_access($instance, $slot_name);
222 sub inline_weaken_slot_value {
223 my ($self, $instance, $slot_name) = @_;
224 sprintf "Scalar::Util::weaken( %s )", $self->inline_slot_access($instance, $slot_name);
227 sub inline_strengthen_slot_value {
228 my ($self, $instance, $slot_name) = @_;
229 $self->inline_set_slot_value($instance, $slot_name, $self->inline_slot_access($instance, $slot_name));
232 sub inline_rebless_instance_structure {
233 my ($self, $instance, $class_variable) = @_;
234 "bless $instance => $class_variable";
237 sub _inline_get_mop_slot {
238 my ($self, $instance) = @_;
239 $self->inline_get_slot_value($instance, $RESERVED_MOP_SLOT);
242 sub _inline_set_mop_slot {
243 my ($self, $instance, $value) = @_;
244 $self->inline_set_slot_value($instance, $RESERVED_MOP_SLOT, $value);
247 sub _inline_clear_mop_slot {
248 my ($self, $instance) = @_;
249 $self->inline_deinitialize_slot($instance, $RESERVED_MOP_SLOT);
260 Class::MOP::Instance - Instance Meta Object
264 The Instance Protocol controls the creation of object instances, and
265 the storage of attribute values in those instances.
267 Using this API directly in your own code violates encapsulation, and
268 we recommend that you use the appropriate APIs in L<Class::MOP::Class>
269 and L<Class::MOP::Attribute> instead. Those APIs in turn call the
270 methods in this class as appropriate.
272 This class also participates in generating inlined code by providing
273 snippets of code to access an object instance.
277 =head2 Object construction
281 =item B<< Class::MOP::Instance->new(%options) >>
283 This method creates a new meta-instance object.
285 It accepts the following keys in C<%options>:
289 =item * associated_metaclass
291 The L<Class::MOP::Class> object for which instances will be created.
295 An array reference of L<Class::MOP::Attribute> objects. These are the
296 attributes which can be stored in each instance.
302 =head2 Creating and altering instances
306 =item B<< $metainstance->create_instance >>
308 This method returns a reference blessed into the associated
311 The default is to use a hash reference. Subclasses can override this.
313 =item B<< $metainstance->clone_instance($instance) >>
315 Given an instance, this method creates a new object by making
316 I<shallow> clone of the original.
324 =item B<< $metainstance->associated_metaclass >>
326 This returns the L<Class::MOP::Class> object associated with the
327 meta-instance object.
329 =item B<< $metainstance->get_all_slots >>
331 This returns a list of slot names stored in object instances. In
332 almost all cases, slot names correspond directly attribute names.
334 =item B<< $metainstance->is_valid_slot($slot_name) >>
336 This will return true if C<$slot_name> is a valid slot name.
338 =item B<< $metainstance->get_all_attributes >>
340 This returns a list of attributes corresponding to the attributes
341 passed to the constructor.
345 =head2 Operations on Instance Structures
347 It's important to understand that the meta-instance object is a
348 different entity from the actual instances it creates. For this
349 reason, any operations on the C<$instance_structure> always require
350 that the object instance be passed to the method.
354 =item B<< $metainstance->get_slot_value($instance_structure, $slot_name) >>
356 =item B<< $metainstance->set_slot_value($instance_structure, $slot_name, $value) >>
358 =item B<< $metainstance->initialize_slot($instance_structure, $slot_name) >>
360 =item B<< $metainstance->deinitialize_slot($instance_structure, $slot_name) >>
362 =item B<< $metainstance->initialize_all_slots($instance_structure) >>
364 =item B<< $metainstance->deinitialize_all_slots($instance_structure) >>
366 =item B<< $metainstance->is_slot_initialized($instance_structure, $slot_name) >>
368 =item B<< $metainstance->weaken_slot_value($instance_structure, $slot_name) >>
370 =item B<< $metainstance->strengthen_slot_value($instance_structure, $slot_name) >>
372 =item B<< $metainstance->rebless_instance_structure($instance_structure, $new_metaclass) >>
374 The exact details of what each method does should be fairly obvious
375 from the method name.
379 =head2 Inlinable Instance Operations
383 =item B<< $metainstance->is_inlinable >>
385 This is a boolean that indicates whether or not slot access operations
386 can be inlined. By default it is true, but subclasses can override
389 =item B<< $metainstance->inline_create_instance($class_variable) >>
391 This method expects a string that, I<when inlined>, will become a
392 class name. This would literally be something like C<'$class'>, not an
395 It returns a snippet of code that creates a new object for the
396 class. This is something like C< bless {}, $class_name >.
398 =item B<< $metainstance->inline_get_is_lvalue >>
400 Returns whether or not C<inline_get_slot_value> is a valid lvalue. This can be
401 used to do extra optimizations when generating inlined methods.
403 =item B<< $metainstance->inline_slot_access($instance_variable, $slot_name) >>
405 =item B<< $metainstance->inline_get_slot_value($instance_variable, $slot_name) >>
407 =item B<< $metainstance->inline_set_slot_value($instance_variable, $slot_name, $value) >>
409 =item B<< $metainstance->inline_initialize_slot($instance_variable, $slot_name) >>
411 =item B<< $metainstance->inline_deinitialize_slot($instance_variable, $slot_name) >>
413 =item B<< $metainstance->inline_is_slot_initialized($instance_variable, $slot_name) >>
415 =item B<< $metainstance->inline_weaken_slot_value($instance_variable, $slot_name) >>
417 =item B<< $metainstance->inline_strengthen_slot_value($instance_variable, $slot_name) >>
419 These methods all expect two arguments. The first is the name of a
420 variable, than when inlined, will represent the object
421 instance. Typically this will be a literal string like C<'$_[0]'>.
423 The second argument is a slot name.
425 The method returns a snippet of code that, when inlined, performs some
426 operation on the instance.
428 =item B<< $metainstance->inline_rebless_instance_structure($instance_variable, $class_variable) >>
430 This takes the name of a variable that will, when inlined, represent the object
431 instance, and the name of a variable that will represent the class to rebless
432 into, and returns code to rebless an instance into a class.
440 =item B<< Class::MOP::Instance->meta >>
442 This will return a L<Class::MOP::Class> instance for this class.
444 It should also be noted that L<Class::MOP> will actually bootstrap
445 this module by installing a number of attribute meta-objects into its
452 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
454 Stevan Little E<lt>stevan@iinteractive.comE<gt>
456 =head1 COPYRIGHT AND LICENSE
458 Copyright 2006-2010 by Infinity Interactive, Inc.
460 L<http://www.iinteractive.com>
462 This library is free software; you can redistribute it and/or modify
463 it under the same terms as Perl itself.