microoptimize Class::MOP::Class::initialize since it's called so often
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
index 7349abc..8b70fdd 100644 (file)
@@ -11,21 +11,28 @@ our $AUTHORITY = 'cpan:STEVAN';
 
 use base 'Class::MOP::Object';
 
-sub new {
+sub BUILDARGS {
     my ($class, @args) = @_;
 
     if ( @args == 1 ) {
-        unshift @args, "metaclass";
+        unshift @args, "associated_metaclass";
     } elsif ( @args >= 2 && blessed($args[0]) && $args[0]->isa("Class::MOP::Class") ) {
         # compat mode
         my ( $meta, @attrs ) = @args;
-        @args = ( metaclass => $meta, attributes => \@attrs );
+        @args = ( associated_metaclass => $meta, attributes => \@attrs );
     }
 
     my %options = @args;
-
     # FIXME lazy_build
     $options{slots} ||= [ map { $_->slots } @{ $options{attributes} || [] } ];
+    $options{slot_hash} = { map { $_ => undef } @{ $options{slots} } }; # FIXME lazy_build
+
+    return \%options;
+}
+
+sub new {
+    my $class = shift;
+    my $options = $class->BUILDARGS(@_);
 
     # FIXME replace with a proper constructor
     my $instance = bless {
@@ -39,18 +46,19 @@ sub new {
         # which is *probably* a safe
         # assumption,.. but you can
         # never tell <:)
-        'meta'  => $options{metaclass}, # FIXME rename to associated metaclass with a compat alias?
-        'slots' => $options{slots},
-        'slot_hash' => { map { $_ => undef } @{ $options{slots} } }, # FIXME lazy_build
+        'associated_metaclass' => $options->{associated_metaclass},
+        'attributes' => $options->{attributes},
+        'slots'      => $options->{slots},
+        'slot_hash'  => $options->{slot_hash},
     } => $class;
 
     # FIXME weak_ref => 1,
-    weaken($instance->{'meta'});
+    weaken($instance->{'associated_metaclass'});
 
     return $instance;
 }
 
-sub associated_metaclass { (shift)->{'meta'} }
+sub associated_metaclass { (shift)->{'associated_metaclass'} }
 
 sub create_instance {
     my $self = shift;
@@ -135,6 +143,10 @@ sub rebless_instance_structure {
     bless $instance, $metaclass->name;
 }
 
+sub is_dependent_on_superclasses {
+    return; # for meta instances that require updates on inherited slot changes
+}
+
 # inlinable operation snippets
 
 sub is_inlinable { 1 }
@@ -215,15 +227,18 @@ F<examples/InsideOutClass.pod> for details).
 
 =over 4
 
-=item B<new ($meta, @attrs)>
+=item B<new %args>
 
 Creates a new instance meta-object and gathers all the slots from
 the list of C<@attrs> given.
 
+=item B<BUILDARGS>
+
+Processes arguments for compatibility.
+
 =item B<meta>
 
-This will return a B<Class::MOP::Class> instance which is related
-to this class.
+Returns the metaclass of L<Class::MOP::Instance>.
 
 =back
 
@@ -266,6 +281,13 @@ given to this object in C<new>.
 
 This will return true if C<$slot_name> is a valid slot name.
 
+=item B<is_dependent_on_superclasses>
+
+This method returns true when the meta instance must be recreated on any
+superclass changes.
+
+Defaults to false.
+
 =back
 
 =head2 Operations on Instance Structures