2 package # hide the package from PAUSE
3 ArrayBasedStorage::Instance;
10 our $VERSION = '0.01';
12 use base 'Class::MOP::Instance';
15 my ($class, $meta, @attrs) = @_;
16 my $self = $class->SUPER::new($meta, @attrs);
18 $self->{slot_index_map} = { map { $_ => $index++ } $self->get_all_slots };
24 $self->bless_instance_structure([]);
28 my ($self, $instance) = shift;
29 $self->bless_instance_structure([ @$instance ]);
32 # operations on meta instance
34 sub get_slot_index_map { (shift)->{slot_index_map} }
38 return sort $self->SUPER::get_all_slots;
42 my ($self, $instance, $slot_name) = @_;
43 return $instance->[ $self->{slot_index_map}->{$slot_name} ];
47 my ($self, $instance, $slot_name, $value) = @_;
48 $instance->[ $self->{slot_index_map}->{$slot_name} ] = $value;
52 my ($self, $instance, $slot_name) = @_;
53 $instance->[ $self->{slot_index_map}->{$slot_name} ] = undef;
56 sub is_slot_initialized {
58 # maybe use CLOS's *special-unbound-value*
60 confess "Cannot really tell this for sure";
71 ArrayBasedStorage - An example of an Array based instance storage
78 ':instance_metaclass' => 'ArrayBasedStorage::Instance'
81 __PACKAGE__->meta->add_attribute('foo' => (
88 $class->meta->new_object(@_);
91 # now you can just use the class as normal
95 This is a proof of concept using the Instance sub-protocol
96 which uses ARRAY refs to store the instance data.
98 This is very similar now to the InsideOutClass example, and
99 in fact, they both share the exact same test suite, with
100 the only difference being the Instance metaclass they use.
104 Stevan Little E<lt>stevan@iinteractive.comE<gt>
108 =head1 COPYRIGHT AND LICENSE
110 Copyright 2006 by Infinity Interactive, Inc.
112 L<http://www.iinteractive.com>
114 This library is free software; you can redistribute it and/or modify
115 it under the same terms as Perl itself.