merging the immutable branch into trunk
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
index 4f56213..89ea9c8 100644 (file)
@@ -6,7 +6,8 @@ use warnings;
 
 use Scalar::Util 'weaken', 'blessed';
 
-our $VERSION = '0.01';
+our $VERSION   = '0.03';
+our $AUTHORITY = 'cpan:STEVAN';
 
 sub meta { 
     require Class::MOP::Class;
@@ -16,7 +17,7 @@ sub meta {
 sub new { 
     my ($class, $meta, @attrs) = @_;
     my @slots = map { $_->slots } @attrs;
-    bless {
+    my $instance = bless {
         # NOTE:
         # I am not sure that it makes
         # sense to pass in the meta
@@ -27,11 +28,17 @@ sub new {
         # which is *probably* a safe
         # assumption,.. but you can 
         # never tell <:)
-        meta  => $meta,
-        slots => { map { $_ => undef } @slots },
+        '$!meta'  => $meta,
+        '@!slots' => { map { $_ => undef } @slots },
     } => $class; 
+    
+    weaken($instance->{'$!meta'});
+    
+    return $instance;
 }
 
+sub associated_metaclass { (shift)->{'$!meta'} }
+
 sub create_instance {
     my $self = shift;
     $self->bless_instance_structure({});
@@ -39,7 +46,7 @@ sub create_instance {
 
 sub bless_instance_structure {
     my ($self, $instance_structure) = @_;
-    bless $instance_structure, $self->{meta}->name;
+    bless $instance_structure, $self->associated_metaclass->name;
 }
 
 sub clone_instance {
@@ -51,12 +58,12 @@ sub clone_instance {
 
 sub get_all_slots {
     my $self = shift;
-    return keys %{$self->{slots}};
+    return keys %{$self->{'@!slots'}};
 }
 
 sub is_valid_slot {
     my ($self, $slot_name) = @_;
-    exists $self->{slots}->{$slot_name} ? 1 : 0;
+    exists $self->{'@!slots'}->{$slot_name} ? 1 : 0;
 }
 
 # operations on created instances
@@ -76,6 +83,11 @@ sub initialize_slot {
     $self->set_slot_value($instance, $slot_name, undef);
 }
 
+sub deinitialize_slot {
+    my ( $self, $instance, $slot_name ) = @_;
+    delete $instance->{$slot_name};
+}
+
 sub initialize_all_slots {
     my ($self, $instance) = @_;
     foreach my $slot_name ($self->get_all_slots) {
@@ -83,6 +95,13 @@ sub initialize_all_slots {
     }
 }
 
+sub deinitialize_all_slots {
+    my ($self, $instance) = @_;
+    foreach my $slot_name ($self->get_all_slots) {
+        $self->deinitialize_slot($instance, $slot_name);
+    }
+}
+
 sub is_slot_initialized {
     my ($self, $instance, $slot_name, $value) = @_;
     exists $instance->{$slot_name} ? 1 : 0;
@@ -127,6 +146,10 @@ sub inline_initialize_slot {
     $self->inline_set_slot_value($instance, $slot_name, 'undef'),
 }
 
+sub inline_deinitialize_slot {
+    my ($self, $instance, $slot_name) = @_;
+    "delete " . $self->inline_slot_access($instance, $slot_name);
+}
 sub inline_is_slot_initialized {
     my ($self, $instance, $slot_name) = @_;
     "exists " . $self->inline_slot_access($instance, $slot_name) . " ? 1 : 0";
@@ -221,6 +244,8 @@ we will add then when we need them basically.
 
 =over 4
 
+=item B<associated_metaclass>
+
 =item B<get_all_slots>
 
 This will return the current list of slots based on what was 
@@ -245,8 +270,12 @@ require that the C<$instance_structure> is passed into them.
 
 =item B<initialize_slot ($instance_structure, $slot_name)>
 
+=item B<deinitialize_slot ($instance_structure, $slot_name)>
+
 =item B<initialize_all_slots ($instance_structure)>
 
+=item B<deinitialize_all_slots ($instance_structure)>
+
 =item B<is_slot_initialized ($instance_structure, $slot_name)>
 
 =item B<weaken_slot_value ($instance_structure, $slot_name)>
@@ -281,6 +310,8 @@ optimizations.
 
 =item B<inline_initialize_slot ($instance_structure, $slot_name)>
 
+=item B<inline_deinitialize_slot ($instance_structure, $slot_name)>
+
 =item B<inline_is_slot_initialized ($instance_structure, $slot_name)>
 
 =item B<inline_weaken_slot_value ($instance_structure, $slot_name)>
@@ -289,7 +320,7 @@ optimizations.
 
 =back
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>