# Creation
-#{
+{
# Metaclasses are singletons, so we cache them here.
# there is no need to worry about destruction though
# because they should die only when the program dies.
|| confess $self->name . "->meta => (" . (blessed($self)) . ")" .
" is not compatible with the " .
$class_name . "->meta => (" . (blessed($meta)) . ")";
+ # NOTE:
+ # we also need to check that instance metaclasses
+ # are compatabile in the same the class.
+ ($self->instance_metaclass->isa($meta->instance_metaclass))
+ || confess $self->name . "->meta => (" . ($self->instance_metaclass) . ")" .
+ " is not compatible with the " .
+ $class_name . "->meta => (" . ($meta->instance_metaclass) . ")";
}
}
-#}
+}
sub create {
my ($class, $package_name, $package_version, %options) = @_;
# Class::MOP::Class singletons here, they
# should not be cloned.
return $instance if $instance->isa('Class::MOP::Class');
- bless $class->clone_instance($instance, @_) => blessed($instance);
+ $class->clone_instance($instance, @_);
}
sub clone_instance {
my ($class, $instance, %params) = @_;
(blessed($instance))
|| confess "You can only clone instances, \$self is not a blessed instance";
- my $clone = { %$instance, %params };
+ my $meta_instance = $class->get_meta_instance();
+ my $clone = $meta_instance->clone_instance($instance);
+ foreach my $key (%params) {
+ next unless $meta_instance->is_valid_slot($key);
+ $meta_instance->set_slot_value($clone, $key, $params{$key});
+ }
return $clone;
}
(
$self->name,
map {
- # OPTIMIZATION NOTE:
- # we grab the metaclass from the %METAS
- # hash here to save the initialize() call
- # if we can, but it is not always possible
- ($METAS{$_} || $self->initialize($_))->class_precedence_list()
+ $self->initialize($_)->class_precedence_list()
} $self->superclasses()
);
}
my ($self, $attribute_name) = @_;
(defined $attribute_name && $attribute_name)
|| confess "You must define an attribute name";
- # OPTIMIZATION NOTE:
- # we used to say `if $self->has_attribute($attribute_name)`
- # here, but since get_attribute is called so often, we
- # eliminate the function call here
- return $self->{'%:attributes'}->{$attribute_name}
- if exists $self->{'%:attributes'}->{$attribute_name};
+ return $self->get_attribute_map->{$attribute_name}
+ if $self->has_attribute($attribute_name);
return;
}
sub get_attribute_list {
my $self = shift;
- # OPTIMIZATION NOTE:
- # We don't use get_attribute_map here because
- # we ask for the attribute list quite often
- # in compute_all_applicable_attributes, so
- # eliminating the function call helps
- keys %{$self->{'%:attributes'}};
+ keys %{$self->get_attribute_map};
}
sub compute_all_applicable_attributes {
next if $seen_class{$class};
$seen_class{$class}++;
# fetch the meta-class ...
- # OPTIMIZATION NOTE:
- # we grab the metaclass from the %METAS
- # hash here to save the initialize() call
- my $meta = $METAS{$class};
+ my $meta = $self->initialize($class);
foreach my $attr_name ($meta->get_attribute_list()) {
next if exists $seen_attr{$attr_name};
$seen_attr{$attr_name}++;
# assumption,.. but you can
# never tell <:)
meta => $meta,
- slots => \@slots,
+ slots => { map { $_ => undef } @slots },
} => $class;
}
bless $instance_structure, $self->{meta}->name;
}
+sub clone_instance {
+ my ($self, $instance) = @_;
+ $self->bless_instance_structure({ %$instance });
+}
+
# operations on meta instance
sub get_all_slots {
my $self = shift;
- return @{$self->{slots}};
+ return keys %{$self->{slots}};
+}
+
+sub is_valid_slot {
+ my ($self, $slot_name) = @_;
+ exists $self->{slots}->{$slot_name} ? 1 : 0;
}
# operations on created instances
This does just exactly what it says it does.
+=item B<clone_instance ($instance_structure)>
+
=back
=head2 Instrospection
This will return the current list of slots based on what was
given to this object in C<new>.
+=item B<is_valid_slot ($slot_name)>
+
=back
=head2 Operations on Instance Structures
=head2 Inlineable Instance Operations
+This part of the API is currently un-used. It is there for use
+in future experiments in class finailization mostly. Best to
+ignore this for now.
+
=over 4
=item B<inline_slot_access ($instance_structure, $slot_name)>