X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FInstance.pm;h=89ea9c8129272e378ada59390062850552a80fee;hb=c23184fc39306590f9e481d76c199020a638bb28;hp=6cb6f0adba190dc39c2027b167f6dd48c6b388fd;hpb=ee7c04677234b6bfa7eddc0896ef4255e713d1b2;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index 6cb6f0a..89ea9c8 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -4,9 +4,10 @@ package Class::MOP::Instance; use strict; use warnings; -use Scalar::Util 'weaken'; +use Scalar::Util 'weaken', 'blessed'; -our $VERSION = '0.01'; +our $VERSION = '0.03'; +our $AUTHORITY = 'cpan:STEVAN'; sub meta { require Class::MOP::Class; @@ -16,7 +17,7 @@ sub meta { 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 @@ -27,11 +28,17 @@ sub new { # which is *probably* a safe # assumption,.. but you can # never tell <:) - meta => $meta, - slots => \@slots, + '$!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({}); @@ -39,14 +46,24 @@ 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 { + my ($self, $instance) = @_; + $self->bless_instance_structure({ %$instance }); } # operations on meta instance sub get_all_slots { my $self = shift; - return @{$self->{slots}}; + return keys %{$self->{'@!slots'}}; +} + +sub is_valid_slot { + my ($self, $slot_name) = @_; + exists $self->{'@!slots'}->{$slot_name} ? 1 : 0; } # operations on created instances @@ -63,7 +80,12 @@ sub set_slot_value { sub initialize_slot { my ($self, $instance, $slot_name) = @_; - $instance->{$slot_name} = undef; + $self->set_slot_value($instance, $slot_name, undef); +} + +sub deinitialize_slot { + my ( $self, $instance, $slot_name ) = @_; + delete $instance->{$slot_name}; } sub initialize_all_slots { @@ -73,6 +95,13 @@ sub initialize_all_slots { } } +sub deinitialize_all_slots { + my ($self, $instance) = @_; + foreach my $slot_name ($self->get_all_slots) { + $self->deinitialize_slot($instance, $slot_name); + } +} + sub is_slot_initialized { my ($self, $instance, $slot_name, $value) = @_; exists $instance->{$slot_name} ? 1 : 0; @@ -90,6 +119,13 @@ sub strengthen_slot_value { # inlinable operation snippets +sub is_inlinable { 1 } + +sub inline_create_instance { + my ($self, $class_variable) = @_; + 'bless {} => ' . $class_variable; +} + sub inline_slot_access { my ($self, $instance, $slot_name) = @_; sprintf "%s->{%s}", $instance, $slot_name; @@ -110,6 +146,10 @@ sub inline_initialize_slot { $self->inline_set_slot_value($instance, $slot_name, 'undef'), } +sub inline_deinitialize_slot { + my ($self, $instance, $slot_name) = @_; + "delete " . $self->inline_slot_access($instance, $slot_name); +} sub inline_is_slot_initialized { my ($self, $instance, $slot_name) = @_; "exists " . $self->inline_slot_access($instance, $slot_name) . " ? 1 : 0"; @@ -193,6 +233,8 @@ then calls C to bless it into the class. This does just exactly what it says it does. +=item B + =back =head2 Instrospection @@ -202,11 +244,15 @@ we will add then when we need them basically. =over 4 +=item B + =item B This will return the current list of slots based on what was given to this object in C. +=item B + =back =head2 Operations on Instance Structures @@ -224,8 +270,12 @@ require that the C<$instance_structure> is passed into them. =item B +=item B + =item B +=item B + =item B =item B @@ -236,8 +286,22 @@ require that the C<$instance_structure> is passed into them. =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. + +=item B + =item B =item B @@ -246,6 +310,8 @@ require that the C<$instance_structure> is passed into them. =item B +=item B + =item B =item B @@ -254,7 +320,7 @@ require that the C<$instance_structure> is passed into them. =back -=head1 AUTHOR +=head1 AUTHORS Yuval Kogman Enothingmuch@woobling.comE