use strict;
use warnings;
-use Carp 'confess';
-use Scalar::Util 'blessed', 'reftype', 'weaken';
+use Scalar::Util 'weaken';
our $VERSION = '0.01';
sub new {
my ($class, $meta, @attrs) = @_;
- my @slots = map { $_->name } @attrs;
+ my @slots = map { $_->slots } @attrs;
bless {
# NOTE:
# I am not sure that it makes
$instance->{$slot_name} = undef;
}
+sub initialize_all_slots {
+ my ($self, $instance) = @_;
+ foreach my $slot_name ($self->get_all_slots) {
+ $self->initialize_slot($instance, $slot_name);
+ }
+}
+
sub is_slot_initialized {
my ($self, $instance, $slot_name, $value) = @_;
exists $instance->{$slot_name} ? 1 : 0;
}
-# inlinable operation snippets
-
-sub inline_get_slot_value {
- my ($self, $instance_var_name, $slot_name) = @_;
- return ($instance_var_name . '->{\'' . $slot_name . '\'}');
-}
-
-sub inline_set_slot_value {
- my ($self, $instance_var_name, $slot_name, $value_name) = @_;
- return ($self->inline_get_slot_value($instance_var_name, $slot_name) . ' = ' . $value_name);
+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 {
- my ($self, $instance_var_name, $slot_name) = @_;
- $self->inline_set_slot_value($instance_var_name, $slot_name, 'undef');
+sub weaken_slot_value {
+ my ($self, $instance, $slot_name) = @_;
+ weaken $instance->{$slot_name};
}
-sub inline_is_slot_initialized {
- my ($self, $instance_var_name, $slot_name) = @_;
- return ('exists ' . $self->inline_get_slot_value($instance_var_name, $slot_name) . ' ? 1 : 0');
+sub strengthen_slot_value {
+ my ($self, $instance, $slot_name) = @_;
+ $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name));
}
1;
=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<new ($meta, @attrs)>
+
+Creates a new instance meta-object and gathers all the slots from
+the list of C<@attrs> given.
+
+=item B<meta>
+
+This will return a B<Class::MOP::Class> instance which is related
+to this class.
+
+=back
-=item B<bless_instance_structure>
+=head2 Creation of Instances
-=item B<compute_layout_from_class>
+=over 4
=item B<create_instance>
-=item B<get_all_slots>
+This creates the appropriate structure needed for the instance and
+then calls C<bless_instance_structure> to bless it into the class.
-=item B<get_slot_value>
+=item B<bless_instance_structure ($instance_structure)>
-=item B<set_slot_value>
+This does just exactly what it says it does.
-=item B<initialize_slot>
+=back
-=item B<is_slot_initialized>
+=head2 Instrospection
-=item B<inline_get_slot_value>
+NOTE: There might be more methods added to this part of the API,
+we will add then when we need them basically.
-=item B<inline_set_slot_value>
+=over 4
-=item B<inline_initialize_slot>
+=item B<get_all_slots>
-=item B<inline_is_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
it under the same terms as Perl itself.
=cut
+