From: Yuval Kogman Date: Sun, 10 Aug 2008 19:31:27 +0000 (+0000) Subject: small fixes & optimizations in Meta::Instance X-Git-Tag: 0_64_01~48 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=748906876440d84c45993e1040750564d084ec37;p=gitmo%2FClass-MOP.git small fixes & optimizations in Meta::Instance --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 1d0ea46..6c82116 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -699,19 +699,36 @@ Class::MOP::Method::Constructor->meta->add_method('new' => sub { # included for completeness Class::MOP::Instance->meta->add_attribute( - Class::MOP::Attribute->new('associated_metaclass') + Class::MOP::Attribute->new('associated_metaclass', + reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass }, + ), ); Class::MOP::Instance->meta->add_attribute( - Class::MOP::Attribute->new('attributes') + Class::MOP::Attribute->new('_class_name', + init_arg => undef, + reader => { _class_name => \&Class::MOP::Instance::_class_name }, + #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway + #default => sub { $_[0]->associated_metaclass->name }, + ), +); + +Class::MOP::Instance->meta->add_attribute( + Class::MOP::Attribute->new('attributes', + reader => { attributes => \&Class::MOP::Instance::attributes }, + ), ); Class::MOP::Instance->meta->add_attribute( - Class::MOP::Attribute->new('slots') + Class::MOP::Attribute->new('slots', + reader => { slots => \&Class::MOP::Instance::slots }, + ), ); Class::MOP::Instance->meta->add_attribute( - Class::MOP::Attribute->new('slot_hash') + Class::MOP::Attribute->new('slot_hash', + reader => { slot_hash => \&Class::MOP::Instance::slot_hash }, + ), ); diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index 8b70fdd..1875668 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -47,9 +47,9 @@ sub new { # assumption,.. but you can # never tell <:) 'associated_metaclass' => $options->{associated_metaclass}, - 'attributes' => $options->{attributes}, - 'slots' => $options->{slots}, - 'slot_hash' => $options->{slot_hash}, + 'attributes' => $options->{attributes}, + 'slots' => $options->{slots}, + 'slot_hash' => $options->{slot_hash}, } => $class; # FIXME weak_ref => 1, @@ -58,7 +58,9 @@ sub new { return $instance; } -sub associated_metaclass { (shift)->{'associated_metaclass'} } +sub _class_name { $_[0]->{_class_name} ||= $_[0]->associated_metaclass->name } + +sub associated_metaclass { $_[0]{'associated_metaclass'} } sub create_instance { my $self = shift; @@ -67,7 +69,7 @@ sub create_instance { sub bless_instance_structure { my ($self, $instance_structure) = @_; - bless $instance_structure, $self->associated_metaclass->name; + bless $instance_structure, $self->_class_name; } sub clone_instance {