Version 1.12
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
index 885e17f..e64a4c2 100644 (file)
@@ -6,12 +6,15 @@ use warnings;
 
 use Scalar::Util 'weaken', 'blessed';
 
-our $VERSION   = '0.80';
+our $VERSION   = '1.12';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
 use base 'Class::MOP::Object';
 
+# make this not a valid method name, to avoid (most) attribute conflicts
+my $RESERVED_MOP_SLOT = '<<MOP>>';
+
 sub BUILDARGS {
     my ($class, @args) = @_;
 
@@ -45,8 +48,12 @@ sub new {
 }
 
 sub _new {
-    my ( $class, %options ) = @_;
-    bless {
+    my $class = shift;
+    return Class::MOP::Class->initialize($class)->new_object(@_)
+      if $class ne __PACKAGE__;
+
+    my $params = @_ == 1 ? $_[0] : {@_};
+    return bless {
         # NOTE:
         # I am not sure that it makes
         # sense to pass in the meta
@@ -57,10 +64,10 @@ sub _new {
         # which is *probably* a safe
         # assumption,.. but you can
         # never tell <:)
-        'associated_metaclass' => $options{associated_metaclass},
-        'attributes'           => $options{attributes},
-        'slots'                => $options{slots},
-        'slot_hash'            => $options{slot_hash},
+        'associated_metaclass' => $params->{associated_metaclass},
+        'attributes'           => $params->{attributes},
+        'slots'                => $params->{slots},
+        'slot_hash'            => $params->{slot_hash},
     } => $class;
 }
 
@@ -73,12 +80,6 @@ sub create_instance {
     bless {}, $self->_class_name;
 }
 
-# for compatibility
-sub bless_instance_structure {
-    my ($self, $instance_structure) = @_;
-    bless $instance_structure, $self->_class_name;
-}
-
 sub clone_instance {
     my ($self, $instance) = @_;
     bless { %$instance }, $self->_class_name;
@@ -163,6 +164,21 @@ sub is_dependent_on_superclasses {
     return; # for meta instances that require updates on inherited slot changes
 }
 
+sub _get_mop_slot {
+    my ($self, $instance) = @_;
+    $self->get_slot_value($instance, $RESERVED_MOP_SLOT);
+}
+
+sub _set_mop_slot {
+    my ($self, $instance, $value) = @_;
+    $self->set_slot_value($instance, $RESERVED_MOP_SLOT, $value);
+}
+
+sub _clear_mop_slot {
+    my ($self, $instance) = @_;
+    $self->deinitialize_slot($instance, $RESERVED_MOP_SLOT);
+}
+
 # inlinable operation snippets
 
 sub is_inlinable { 1 }
@@ -177,6 +193,8 @@ sub inline_slot_access {
     sprintf q[%s->{"%s"}], $instance, quotemeta($slot_name);
 }
 
+sub inline_get_is_lvalue { 1 }
+
 sub inline_get_slot_value {
     my ($self, $instance, $slot_name) = @_;
     $self->inline_slot_access($instance, $slot_name);
@@ -211,6 +229,26 @@ sub inline_strengthen_slot_value {
     $self->inline_set_slot_value($instance, $slot_name, $self->inline_slot_access($instance, $slot_name));
 }
 
+sub inline_rebless_instance_structure {
+    my ($self, $instance, $class_variable) = @_;
+    "bless $instance => $class_variable";
+}
+
+sub _inline_get_mop_slot {
+    my ($self, $instance) = @_;
+    $self->inline_get_slot_value($instance, $RESERVED_MOP_SLOT);
+}
+
+sub _inline_set_mop_slot {
+    my ($self, $instance, $value) = @_;
+    $self->inline_set_slot_value($instance, $RESERVED_MOP_SLOT, $value);
+}
+
+sub _inline_clear_mop_slot {
+    my ($self, $instance) = @_;
+    $self->inline_deinitialize_slot($instance, $RESERVED_MOP_SLOT);
+}
+
 1;
 
 __END__
@@ -357,6 +395,11 @@ actual class name.
 It returns a snippet of code that creates a new object for the
 class. This is something like C< bless {}, $class_name >.
 
+=item B<< $metainstance->inline_get_is_lvalue >>
+
+Returns whether or not C<inline_get_slot_value> is a valid lvalue. This can be
+used to do extra optimizations when generating inlined methods.
+
 =item B<< $metainstance->inline_slot_access($instance_variable, $slot_name) >>
 
 =item B<< $metainstance->inline_get_slot_value($instance_variable, $slot_name) >>
@@ -382,6 +425,12 @@ The second argument is a slot name.
 The method returns a snippet of code that, when inlined, performs some
 operation on the instance.
 
+=item B<< $metainstance->inline_rebless_instance_structure($instance_variable, $class_variable) >>
+
+This takes the name of a variable that will, when inlined, represent the object
+instance, and the name of a variable that will represent the class to rebless
+into, and returns code to rebless an instance into a class.
+
 =back
 
 =head2 Introspection
@@ -406,7 +455,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>