Add support for weak references to Class::MOP::Instance
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
index 13010c2..f79c063 100644 (file)
@@ -4,8 +4,7 @@ package Class::MOP::Instance;
 use strict;
 use warnings;
 
-use Carp         'confess';
-use Scalar::Util 'blessed', 'reftype', 'weaken';
+use Scalar::Util 'weaken';
 
 our $VERSION = '0.01';
 
@@ -15,79 +14,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 ); # 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;
-}
-
-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};
+    return @{$self->{slots}};
 }
 
-
 # operations on created instances
 
 sub get_slot_value {
@@ -95,58 +56,42 @@ 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;
 }
 
-# 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 ) = @_;
-}
-
-sub slot_initialized {
     my ($self, $instance, $slot_name) = @_;
-    exists $instance->{$slot_name} ? 1 : 0;
+    $instance->{$slot_name} = undef;
 }
 
-
-# inlinable operation snippets
-
-sub inline_get_slot_value {
-    my ($self, $instance, $slot_name) = @_;
-    sprintf "%s->{%s}", $instance, $slot_name;
+sub initialize_all_slots {
+    my ($self, $instance) = @_;
+    foreach my $slot_name ($self->get_all_slots) {
+        $self->initialize_slot($instance, $slot_name);
+    }
 }
 
-sub inline_set_slot_value {
+sub is_slot_initialized {
     my ($self, $instance, $slot_name, $value) = @_;
-    $self->_inline_slot_lvalue . " = $value", 
+    exists $instance->{$slot_name} ? 1 : 0;
 }
 
-sub inline_set_slot_value_with_init { 
-    my ( $self, $instance, $slot_name, $value) = @_;
-    $self->inline_set_slot_value( $instance, $slot_name, $value ) . ";";
+sub set_slot_value_weak {
+    my ($self, $instance, $slot_name, $value) = @_;
+       $self->set_slot_value($instance, $slot_name, $value);
+       $self->weaken_slot_value($instance, $slot_name);
 }
 
-sub inline_initialize_slot {
-    return "";
+sub weaken_slot_value {
+       my ($self, $instance, $slot_name) = @_;
+       weaken $instance->{$slot_name};
 }
 
-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_slot_value;
+sub strengthen_slot_value {
+       my ($self, $instance, $slot_name) = @_;
+       $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name));
 }
 
 1;
@@ -161,63 +106,109 @@ Class::MOP::Instance - Instance Meta Object
 
 =head1 SYNOPSIS
 
+  # for the most part, this protocol is internal 
+  # and not for public usage, but this how one 
+  # might use it
+  
+  package Foo;
+  
+  use strict;
+  use warnings;
+  use metaclass 'Class::MOP::Class' => (
+      ':instance_metaclass'  => 'ArrayBasedStorage::Instance',
+  );
+  
+  # now Foo->new produces blessed ARRAY ref based objects
+
 =head1 DESCRIPTION
 
+This is a sub-protocol which governs instance creation 
+and access to the slots of the instance structure.
+
+This may seem like over-abstraction, but by abstracting 
+this process into a sub-protocol we make it possible to 
+easily switch the details of how an object's instance is 
+stored with minimal impact. In most cases just subclassing 
+this class will be all you need to do (occasionally it  
+requires that you also subclass Class::MOP::Attribute if 
+you require some kind of specific attribute initializations).
+
 =head1 METHODS
 
 =over 4
 
-=item B<new>
-
-=item B<add_slot>
+=item B<new ($meta, @attrs)>
 
-=item B<bless_instance_structure>
+Creates a new instance meta-object and gathers all the slots from 
+the list of C<@attrs> given.
 
-=item B<create_instance>
+=item B<meta>
 
-=item B<get_all_parents>
+This will return a B<Class::MOP::Class> instance which is related 
+to this class.
 
-=item B<get_slot_value>
+=back
 
-=item B<has_slot>
+=head2 Creation of Instances
 
-=item B<has_slot_recursively>
+=over 4
 
-=item B<initialize_slot>
+=item B<create_instance>
 
-=item B<inline_get_slot_value>
+This creates the appropriate structure needed for the instance and 
+then calls C<bless_instance_structure> to bless it into the class.
 
-=item B<inline_initialize_slot>
+=item B<bless_instance_structure ($instance_structure)>
 
-=item B<inline_set_slot_value>
+This does just exactly what it says it does.
 
-=item B<inline_set_slot_value_with_init>
+=back
 
-=item B<inline_slot_initialized>
+=head2 Instrospection
 
-=item B<remove_slot>
+NOTE: There might be more methods added to this part of the API, 
+we will add then when we need them basically.
 
-=item B<set_slot_value>
+=over 4
 
-=item B<set_slot_value_with_init>
+=item B<get_all_slots>
 
-=item B<slot_initialized>
+This will return the current list of slots based on what was 
+given to this object in C<new>.
 
 =back
 
-=head2 Introspection
+=head2 Operations on Instance Structures
+
+An important distinction of this sub-protocol is that the 
+instance meta-object is a different entity from the actual 
+instance it creates. For this reason, any actions on slots 
+require that the C<$instance_structure> is passed into them.
 
 =over 4
 
-=item B<meta>
+=item B<get_slot_value ($instance_structure, $slot_name)>
 
-This will return a B<Class::MOP::Class> instance which is related 
-to this class.
+=item B<set_slot_value ($instance_structure, $slot_name, $value)>
+
+=item B<initialize_slot ($instance_structure, $slot_name)>
+
+=item B<initialize_all_slots ($instance_structure)>
+
+=item B<is_slot_initialized ($instance_structure, $slot_name)>
+
+=item B<set_slot_value_weak ($instance_structure, $slot_name, $ref_value)>
+
+=item B<weaken_slot_value>
+
+=item B<strengthen_slot_value>
 
 =back
 
 =head1 AUTHOR
 
+Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
+
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
@@ -229,4 +220,5 @@ L<http://www.iinteractive.com>
 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
+