X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FInstance.pm;h=b6d6435890319581aa98becce6001fdcab42a7ec;hb=49c93440bd912ed231b8ab8e93a8e9ac7328fdc7;hp=dcc13c76d3bea66251c7f7041619277ba7b2d0c4;hpb=84ef30d18bb35c6ac09d1e3f7d49a27d275c88e1;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index dcc13c7..b6d6435 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -15,79 +15,41 @@ sub meta { } sub new { - my ( $class, $meta ) = @_; + my ($class, $meta) = @_; + my $slots = $class->_compute_slot_list_from_class($meta); bless { - meta => $meta, - instance_layout => {} + meta => $meta, + slots => $slots, } => $class; } -sub create_instance { - my ( $self, $class ) = @_; - - # rely on autovivification - $self->bless_instance_structure( {}, $class ); +# private for now ... +sub _compute_slot_list_from_class { + my ($self, $meta) = @_; + return [ + map { + $_->name + } $meta->compute_all_applicable_attributes() + ]; } -sub bless_instance_structure { - my ( $self, $instance_structure, $class ) = @_; - $class ||= $self->{meta}->name; - bless $instance_structure, $class; +sub create_instance { + my $self = shift; + $self->bless_instance_structure({}); } -sub get_all_parents { - my $self = shift; - my @parents = $self->{meta}->class_precedence_list; - shift @parents; # shift off ourselves - return map { $_->get_meta_instance } map { $_->meta || () } @parents; +sub bless_instance_structure { + my ($self, $instance_structure) = @_; + bless $instance_structure, $self->{meta}->name; } # operations on meta instance -sub add_slot { - my ($self, $slot_name ) = @_; - confess "The slot '$slot_name' already exists" - if 0 && $self->has_slot_recursively( $slot_name ); # FIXME - $self->{instance_layout}->{$slot_name} = undef; -} - sub get_all_slots { my $self = shift; - keys %{ $self->{instance_layout} }; -} - -sub get_all_slots_recursively { - my $self = shift; - return ( - $self->get_all_slots, - map { $_->get_all_slots } $self->get_all_parents, - ), -} - -sub has_slot { - my ($self, $slot_name) = @_; - exists $self->{instance_layout}->{$slot_name} ? 1 : 0; -} - -sub has_slot_recursively { - my ( $self, $slot_name ) = @_; - return 1 if $self->has_slot($slot_name); - $_->has_slot_recursively($slot_name) && return 1 for $self->get_all_parents; - return 0; + return @{$self->{slots}}; } -sub remove_slot { - my ( $self, $slot_name ) = @_; - # NOTE: - # this does not search recursively cause - # that is not the domain of this meta-instance - # it is specific to this class ... - confess "The slot '$slot_name' does not exist (maybe it's inherited?)" - if 0 && $self->has_slot( $slot_name ); # FIXME - delete $self->{instance_layout}->{$slot_name}; -} - - # operations on created instances sub get_slot_value { @@ -95,82 +57,41 @@ sub get_slot_value { return $instance->{$slot_name}; } -# can be called only after initialize_slot_value sub set_slot_value { my ($self, $instance, $slot_name, $value) = @_; $instance->{$slot_name} = $value; } -sub set_weak_slot_value { - my ( $self, $instance, $slot_name, $value) = @_; - $self->set_slot_value( $instance, $slot_name, $value ); - $self->weeaken_slot_value( $instance, $slot_name ); -} - -sub weaken_slot_value { - my ( $self, $instance, $slot_name ) = @_; - weaken( $instance->{$slot_name} ); -} - -# convenience method -# non autovivifying stores will have this as { initialize_slot unless slot_initlized; set_slot_value } -sub set_slot_value_with_init { - my ( $self, $instance, $slot_name, $value ) = @_; - $self->set_slot_value( $instance, $slot_name, $value ); -} - sub initialize_slot { - my ( $self, $instance, $slot_name ) = @_; + my ($self, $instance, $slot_name) = @_; + $instance->{$slot_name} = undef; } -sub slot_initialized { - my ($self, $instance, $slot_name) = @_; +sub is_slot_initialized { + my ($self, $instance, $slot_name, $value) = @_; exists $instance->{$slot_name} ? 1 : 0; } - # inlinable operation snippets sub inline_get_slot_value { - my ($self, $instance, $slot_name) = @_; - sprintf "%s->{%s}", $instance, $slot_name; + my ($self, $instance_var_name, $slot_name) = @_; + return ($instance_var_name . '->{\'' . $slot_name . '\'}'); } sub inline_set_slot_value { - my ($self, $instance, $slot_name, $value) = @_; - $self->_inline_slot_lvalue( $instance, $slot_name ) . " = $value", -} - -sub inline_set_weak_slot_value { - my ( $self, $instance, $slot_name, $value ) = @_; - return "" - . $self->inline_set_slot_value( $instance, $slot_name, $value ) - . "; " - . $self->inline_weaken_slot_value( $instance, $slot_name ); -} - -sub inline_weaken_slot_value { - my ( $self, $instance, $slot_name ) = @_; - return 'Scalar::Util::weaken( ' . $self->_inline_slot_lvalue( $instance, $slot_name ) . ')'; -} - -sub inline_set_slot_value_with_init { - my ( $self, $instance, $slot_name, $value) = @_; - $self->inline_set_slot_value( $instance, $slot_name, $value ) . ";"; + my ($self, $instance_var_name, $slot_name, $value_name) = @_; + return ($self->inline_get_slot_value($instance_var_name, $slot_name) . ' = ' . $value_name); } sub inline_initialize_slot { - return ""; + my ($self, $instance_var_name, $slot_name) = @_; + $self->inline_set_slot_value($instance_var_name, $slot_name, 'undef'); } -sub inline_slot_initialized { - my ($self, $instance, $slot_name) = @_; - "exists " . $self->inline_get_slot_value; -} - -sub _inline_slot_lvalue { - my ($self, $instance, $slot_name) = @_; - $self->inline_get_slot_value( $instance, $slot_name ); +sub inline_is_slot_initialized { + my ($self, $instance_var_name, $slot_name) = @_; + return ('exists ' . $self->inline_get_slot_value($instance_var_name, $slot_name) . ' ? 1 : 0'); } 1; @@ -193,43 +114,29 @@ Class::MOP::Instance - Instance Meta Object =item B -=item B - =item B +=item B + =item B -=item B +=item B =item B -=item B - -=item B +=item B =item B -=item B +=item B -=item B +=item B =item B -=item B - -=item B - -=item B - -=item B - -=item B - -=item B - -=item B +=item B -=item B +=item B =back