From: Stevan Little Date: Thu, 27 Apr 2006 02:37:02 +0000 (+0000) Subject: new_instances X-Git-Tag: 0_29_02~47 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=839ea97307cde7e936bff72d3f76d6213b9883a9;p=gitmo%2FClass-MOP.git new_instances --- diff --git a/examples/ClassEncapsulatedAttributes.pod b/examples/ClassEncapsulatedAttributes.pod index e7688b7..e14a17a 100644 --- a/examples/ClassEncapsulatedAttributes.pod +++ b/examples/ClassEncapsulatedAttributes.pod @@ -18,17 +18,17 @@ sub initialize { sub construct_instance { my ($class, %params) = @_; - my $instance = {}; + my $meta_instance = Class::MOP::Instance->new($class); foreach my $current_class ($class->class_precedence_list()) { - $instance->{$current_class} = {} - unless exists $instance->{$current_class}; + $meta_instance->add_slot($current_class => {}) + unless $meta_instance->has_slot($current_class); my $meta = $current_class->meta; foreach my $attr_name ($meta->get_attribute_list()) { my $attr = $meta->get_attribute($attr_name); - $attr->initialize_instance_slot($meta, $instance, \%params); + $attr->initialize_instance_slot($meta, $meta_instance, \%params); } } - return $instance; + return $meta_instance->get_instance; } package # hide the package from PAUSE @@ -42,7 +42,7 @@ our $VERSION = '0.03'; use base 'Class::MOP::Attribute'; sub initialize_instance_slot { - my ($self, $class, $instance, $params) = @_; + my ($self, $class, $meta_instance, $params) = @_; # if the attr has an init_arg, use that, otherwise, # use the attributes name itself as the init_arg my $init_arg = $self->init_arg(); @@ -54,10 +54,10 @@ sub initialize_instance_slot { # if nothing was in the %params, we can use the # attribute's default value (if it has one) if (!defined $val && $self->has_default) { - $val = $self->default($instance); + $val = $self->default($meta_instance->get_instance); } # now add this to the instance structure - $instance->{$class->name}->{$self->name} = $val; + $meta_instance->get_slot_value($class->name)->{$self->name} = $val; } sub generate_accessor_method { diff --git a/examples/InsideOutClass.pod b/examples/InsideOutClass.pod index 5242275..779cc77 100644 --- a/examples/InsideOutClass.pod +++ b/examples/InsideOutClass.pod @@ -14,7 +14,7 @@ use Scalar::Util 'refaddr'; use base 'Class::MOP::Attribute'; sub initialize_instance_slot { - my ($self, $class, $instance, $params) = @_; + my ($self, $class, $meta_instance, $params) = @_; # if the attr has an init_arg, use that, otherwise, # use the attributes name itself as the init_arg my $init_arg = $self->init_arg(); @@ -24,10 +24,10 @@ sub initialize_instance_slot { # if nothing was in the %params, we can use the # attribute's default value (if it has one) if (!defined $val && $self->has_default) { - $val = $self->default($instance); + $val = $self->default($meta_instance->get_instance); } # now add this to the instance structure - $class->get_package_variable('%' . $self->name)->{ refaddr($instance) } = $val; + $class->get_package_variable('%' . $self->name)->{ refaddr($meta_instance->get_instance) } = $val; } sub generate_accessor_method { diff --git a/examples/LazyClass.pod b/examples/LazyClass.pod index fc5ee70..1847939 100644 --- a/examples/LazyClass.pod +++ b/examples/LazyClass.pod @@ -12,7 +12,7 @@ our $VERSION = '0.03'; use base 'Class::MOP::Attribute'; sub initialize_instance_slot { - my ($self, $class, $instance, $params) = @_; + my ($self, $class, $meta_instance, $params) = @_; # if the attr has an init_arg, use that, otherwise, # use the attributes name itself as the init_arg my $init_arg = $self->init_arg(); @@ -21,7 +21,7 @@ sub initialize_instance_slot { $val = $params->{$init_arg} if exists $params->{$init_arg}; # now add this to the instance structure # only if we have found a value at all - $instance->{$self->name} = $val if defined $val; + $meta_instance->add_slot($self->name, $val) if defined $val; } diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 4ea50b1..b32e02a 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -11,6 +11,8 @@ use B 'svref_2object'; our $VERSION = '0.13'; +use Class::MOP::Instance; + # Self-introspection sub meta { Class::MOP::Class->initialize(blessed($_[0]) || $_[0]) } @@ -175,7 +177,6 @@ sub new_object { sub construct_instance { my ($class, %params) = @_; - require Class::MOP::Instance; my $meta_instance = Class::MOP::Instance->new($class); foreach my $attr ($class->compute_all_applicable_attributes()) { $attr->initialize_instance_slot($class, $meta_instance, \%params); diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index 7d1e573..1bd598c 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -18,7 +18,7 @@ sub new { my $class = shift; my $meta = shift; bless { - instance => bless {} => $meta->name + instance => (bless {} => $meta->name) } => $class; } @@ -27,6 +27,18 @@ sub add_slot { return $self->{instance}->{$slot_name} = $value; } +sub has_slot { + my ($self, $slot_name) = @_; + exists $self->{instance}->{$slot_name} ? 1 : 0; +} + +sub get_slot_value { + my ($self, $slot_name) = @_; + return $self->{instance}->{$slot_name}; +} + +*set_slot_value = \&add_slot; + sub get_instance { (shift)->{instance} } 1; @@ -51,6 +63,12 @@ Class::MOP::Instance - Instance Meta Object =item B +=item B + +=item B + +=item B + =item B =back