X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FInstance.pm;h=77c5d536b5373cb9d579e1fefc08e2aa3d00096e;hb=7202116b012d65b1c71d42819a0d2aa2ec5d3bd1;hp=b2e406a8f4f60a1ff482e821d8892f91a47ae93b;hpb=f0480c45f215203bc40abc794ac0c03622f02f1d;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index b2e406a..77c5d53 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -6,33 +6,36 @@ use warnings; use Scalar::Util 'weaken', 'blessed'; -our $VERSION = '0.03'; +our $VERSION = '0.64'; our $AUTHORITY = 'cpan:STEVAN'; -sub meta { - require Class::MOP::Class; - Class::MOP::Class->initialize(blessed($_[0]) || $_[0]); -} +use base 'Class::MOP::Object'; -sub new { +sub new { my ($class, $meta, @attrs) = @_; my @slots = map { $_->slots } @attrs; - bless { + my $instance = bless { # NOTE: # I am not sure that it makes # sense to pass in the meta - # The ideal would be to just - # pass in the class name, but - # that is placing too much of - # an assumption on bless(), + # The ideal would be to just + # pass in the class name, but + # that is placing too much of + # an assumption on bless(), # which is *probably* a safe - # assumption,.. but you can + # assumption,.. but you can # never tell <:) - meta => $meta, - slots => { map { $_ => undef } @slots }, - } => $class; + '$!meta' => $meta, + '@!slots' => { map { $_ => undef } @slots }, + } => $class; + + weaken($instance->{'$!meta'}); + + return $instance; } +sub associated_metaclass { (shift)->{'$!meta'} } + sub create_instance { my $self = shift; $self->bless_instance_structure({}); @@ -40,7 +43,7 @@ sub create_instance { sub bless_instance_structure { my ($self, $instance_structure) = @_; - bless $instance_structure, $self->{meta}->name; + bless $instance_structure, $self->associated_metaclass->name; } sub clone_instance { @@ -52,19 +55,19 @@ sub clone_instance { sub get_all_slots { my $self = shift; - return keys %{$self->{slots}}; + return keys %{$self->{'@!slots'}}; } sub is_valid_slot { my ($self, $slot_name) = @_; - exists $self->{slots}->{$slot_name} ? 1 : 0; + exists $self->{'@!slots'}->{$slot_name}; } # operations on created instances sub get_slot_value { my ($self, $instance, $slot_name) = @_; - return $instance->{$slot_name}; + $instance->{$slot_name}; } sub set_slot_value { @@ -74,7 +77,7 @@ sub set_slot_value { sub initialize_slot { my ($self, $instance, $slot_name) = @_; - $self->set_slot_value($instance, $slot_name, undef); + return; } sub deinitialize_slot { @@ -98,17 +101,22 @@ sub deinitialize_all_slots { sub is_slot_initialized { my ($self, $instance, $slot_name, $value) = @_; - exists $instance->{$slot_name} ? 1 : 0; + exists $instance->{$slot_name}; } sub weaken_slot_value { - my ($self, $instance, $slot_name) = @_; - weaken $instance->{$slot_name}; + my ($self, $instance, $slot_name) = @_; + weaken $instance->{$slot_name}; } sub strengthen_slot_value { - my ($self, $instance, $slot_name) = @_; - $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name)); + my ($self, $instance, $slot_name) = @_; + $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name)); +} + +sub rebless_instance_structure { + my ($self, $instance, $metaclass) = @_; + bless $instance, $metaclass->name; } # inlinable operation snippets @@ -132,12 +140,12 @@ sub inline_get_slot_value { sub inline_set_slot_value { my ($self, $instance, $slot_name, $value) = @_; - $self->inline_slot_access($instance, $slot_name) . " = $value", + $self->inline_slot_access($instance, $slot_name) . " = $value", } sub inline_initialize_slot { my ($self, $instance, $slot_name) = @_; - $self->inline_set_slot_value($instance, $slot_name, 'undef'), + return ''; } sub inline_deinitialize_slot { @@ -146,7 +154,7 @@ sub inline_deinitialize_slot { } sub inline_is_slot_initialized { my ($self, $instance, $slot_name) = @_; - "exists " . $self->inline_slot_access($instance, $slot_name) . " ? 1 : 0"; + "exists " . $self->inline_slot_access($instance, $slot_name); } sub inline_weaken_slot_value { @@ -165,37 +173,26 @@ __END__ =pod -=head1 NAME +=head1 NAME Class::MOP::Instance - Instance Meta Object -=head1 SYNOPSIS - - # for the most part, this protocol is internal - # and not for public usage, but this how one - # might use it - - package Foo; - - use strict; - use warnings; - use metaclass ( - ':instance_metaclass' => 'ArrayBasedStorage::Instance', - ); - - # now Foo->new produces blessed ARRAY ref based objects - =head1 DESCRIPTION -This is a sub-protocol which governs instance creation -and access to the slots of the instance structure. +The meta instance is used by attributes for low level storage. -This may seem like over-abstraction, but by abstracting -this process into a sub-protocol we make it possible to -easily switch the details of how an object's instance is -stored with minimal impact. In most cases just subclassing -this class will be all you need to do (see the examples; -F and +Using this API generally violates attribute encapsulation and is not +recommended, instead look at L, +L for the recommended way to fiddle with +attribute values in a generic way, independent of how/whether accessors have +been defined. Accessors can be found using L. + +This may seem like over-abstraction, but by abstracting +this process into a sub-protocol we make it possible to +easily switch the details of how an object's instance is +stored with minimal impact. In most cases just subclassing +this class will be all you need to do (see the examples; +F and F for details). =head1 METHODS @@ -204,12 +201,12 @@ F for details). =item B -Creates a new instance meta-object and gathers all the slots from +Creates a new instance meta-object and gathers all the slots from the list of C<@attrs> given. =item B -This will return a B instance which is related +This will return a B instance which is related to this class. =back @@ -220,7 +217,7 @@ to this class. =item B -This creates the appropriate structure needed for the instance and +This creates the appropriate structure needed for the instance and then calls C to bless it into the class. =item B @@ -229,31 +226,43 @@ This does just exactly what it says it does. =item B +This too does just exactly what it says it does. + =back -=head2 Instrospection +=head2 Introspection -NOTE: There might be more methods added to this part of the API, +NOTE: There might be more methods added to this part of the API, we will add then when we need them basically. =over 4 +=item B + +This returns the metaclass associated with this instance. + =item B -This will return the current list of slots based on what was +This will return the current list of slots based on what was given to this object in C. =item B +This will return true if C<$slot_name> is a valid slot name. + =back =head2 Operations on Instance Structures -An important distinction of this sub-protocol is that the -instance meta-object is a different entity from the actual -instance it creates. For this reason, any actions on slots +An important distinction of this sub-protocol is that the +instance meta-object is a different entity from the actual +instance it creates. For this reason, any actions on slots require that the C<$instance_structure> is passed into them. +The names of these methods pretty much explain exactly +what they do, if that is not enough then I suggest reading +the source, it is very straightfoward. + =over 4 =item B @@ -274,23 +283,19 @@ require that the C<$instance_structure> is passed into them. =item B +=item B + =back =head2 Inlineable Instance Operations -This part of the API is currently un-used. It is there for use -in future experiments in class finailization mostly. Best to -ignore this for now. - =over 4 =item B -Each meta-instance should override this method to tell Class::MOP if it's -possible to inline the slot access. - -This is currently only used by Class::MOP::Class::Immutable when performing -optimizations. +Each meta-instance should override this method to tell Class::MOP if it's +possible to inline the slot access. This is currently only used by +L when performing optimizations. =item B @@ -320,12 +325,12 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. +it under the same terms as Perl itself. =cut