X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FInstance.pm;h=d7bb7fbaf37eea4215dedf5e03ee5c9a28ca727c;hb=c57c8b10dc66355d65b7ef2aad88edd20d8adc21;hp=fb8a276c668b392e7e45f15c9f4373530cbc6d33;hpb=2d711cc8f03b6d8cbfe53f9628883ff33582ed03;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index fb8a276..d7bb7fb 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -15,66 +15,41 @@ sub meta { } sub new { - my ( $class, $meta ) = @_; + my ($class, $meta, @attrs) = @_; + my @slots = map { $_->slots } @attrs; bless { - meta => $meta, - instance_layout => {} + # 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(), + # which is *probably* a safe + # assumption,.. but you can + # never tell <:) + meta => $meta, + slots => \@slots, } => $class; } sub create_instance { - my ( $self, $class ) = @_; - - # rely on autovivification - $self->bless_instance_structure( {}, $class ); + my $self = shift; + $self->bless_instance_structure({}); } sub bless_instance_structure { - my ( $self, $instance_structure, $class ) = @_; - $class ||= $self->{meta}->name; - bless $instance_structure, $class; -} - -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; + 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 ); - $self->{instance_layout}->{$slot_name} = undef; -} - -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; -} - -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 ); - delete $self->{instance_layout}->{$slot_name}; +sub get_all_slots { + my $self = shift; + return @{$self->{slots}}; } - # operations on created instances sub get_slot_value { @@ -82,59 +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) = @_; - $slot_name or confess "must provide slot name"; $instance->{$slot_name} = $value; } -# 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 . " = $value", -} - -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 ""; -} - -sub inline_slot_initialized { - my ($self, $instance, $slot_name) = @_; - "exists " . $self->inline_get_slot_value; + my ($self, $instance_var_name, $slot_name) = @_; + $self->inline_set_slot_value($instance_var_name, $slot_name, 'undef'); } -sub _inline_slot_lvalue { - my ($self, $instance, $slot_name) = @_; - $self->inline_slot_value; +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; @@ -157,15 +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 =back @@ -193,4 +164,4 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut